package com.madeu.service.mobile.mobilemedical.impl;

import com.madeu.constants.Constants;
import com.madeu.dao.mobile.mobilemedical.MobileMedicalSqlMapDAO;
import com.madeu.service.mobile.mobilemedical.MobileMedicalService;
import com.madeu.util.ValidationCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service("MobileMedicalService")
public class MobileMedicalServiceImpl implements MobileMedicalService {
    @Autowired
    private MobileMedicalSqlMapDAO mobileMedicalSqlMapDAO;

    /**
     * 시술 보드 정보 리스트 조회 (list)
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListMedicalBoard(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("MobileMedicalServiceImpl selectListMedicalBoard START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));

        log.debug("menuClass           :"+paramMap.get("menuClass"));

        log.debug("muAppBoardId          :"+paramMap.get("muAppBoardId"));

        try{
            boolean check  = true;
            String muAppBoardId    = String.valueOf(paramMap.get("muAppBoardId"));
             Map<String, List<Map<String, Object>>> listMap = new HashMap<>();

            if(true == check){
                List<Map<String, Object>> totalCountListMap = mobileMedicalSqlMapDAO.selectListMedicalBoardCount(paramMap);
                int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));

                if(0 < totalCount) {
                    // 리스트 조회
                     Map<String, List<Map<String, Object>>> groupedByMuAppBoardId =
                         mobileMedicalSqlMapDAO.selectListMedicalBoard(paramMap).stream()
                              .collect(Collectors.groupingBy(data -> (String) data.get("muAppBoardId")));

                    // muAppBoardId별로 그룹화된 데이터를 저장
                    listMap.putAll(groupedByMuAppBoardId);
                }

                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("rows",listMap);
            }
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("MobileMedicalServiceImpl selectListMedicalBoard END");
        return map;
    }

    /**
     * 시술 정보 리스트 조회
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListMedical(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("MobileMedicalServiceImpl selectListMedical START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));

        log.debug("menuClass           :"+paramMap.get("menuClass"));

        log.debug("muAppBoardId          :"+paramMap.get("muAppBoardId"));

        try{
            boolean check  = true;

            if(true == check){
                List<Map<String, Object>> listMap =  mobileMedicalSqlMapDAO.selectListMedical(paramMap);

                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("rows",listMap);
            }
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("MobileMedicalServiceImpl selectListMedicalBoard END");
        return map;
    }
    /**
     * 시술 정보 조회
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectMedical(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("MobileMedicalServiceImpl selectListMedical START");

        HashMap<String, Object> map = new HashMap<String, Object>();

        log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));

        log.debug("menuClass           :"+paramMap.get("menuClass"));

        log.debug("muAppContentId          :"+paramMap.get("muAppContentId"));

        try{
            boolean check  = true;
            String muAppContentId    = String.valueOf(paramMap.get("muAppContentId"));

            if(true == check){
                List<Map<String, Object>> listMap =  mobileMedicalSqlMapDAO.selectMedical(paramMap);

                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("rows",listMap);
            }
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("MobileMedicalServiceImpl selectListMedical END");
        return map;
    }
}
