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

import com.madeu.constants.Constants;
import com.madeu.dao.mobile.mobileuser.MobileUserSqlMapDAO;
import com.madeu.dao.mobile.mobiletreatmentprocedure.MobileTreatmentProcedureSqlMapDAO;
import com.madeu.service.mobile.mobiletreatmentProcedure.MobileTreatmentProcedureService;
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.*;

@Slf4j
@Service("MobileTreatmentProcedureService")
public class MobileTreatmentProcedureServiceImpl implements MobileTreatmentProcedureService {
	
	@Autowired
	private MobileTreatmentProcedureSqlMapDAO mobileTreatmentProcedureSqlMapDAO;
	
	@Autowired
	private MobileUserSqlMapDAO mobileUserSqlMapDAO;
	
	/**
	 * 진료 시술 리스트 조회 (List, option)
	 *
	 *
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	@Override
	public HashMap<String, Object> selectListTreatmentProcedureOption(
			HashMap<String, Object> paramMap) throws Exception {
		log.debug("MobileTreatmentProcedureServiceImpl selectListTreatmentProcedureOption START");

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

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


		List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();

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

			if(true != ValidationCheckUtil.emptyCheck(muTreatmentId)){
				check = false;
				map.put("msgCode", Constants.FAIL);
				map.put("msgDesc","직무명 정보가 없습니다.");
			}

			if(true == check){
				// 사용자 확인
				List<Map<String, Object>> userListMap = mobileUserSqlMapDAO.checkUser(paramMap);
				int userListMapSize = userListMap.size();
				
				if(1==userListMapSize){
				}
				else {
					check = false;
					map.put("msgCode", Constants.FAIL);
					map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
				}
			}

			if(true == check){
				String[] ids = muTreatmentId.split(",");
				for (String id : ids) {
					HashMap<String, Object> dataMap = new HashMap<>();
					dataMap.put("muTreatmentId",id);
					List<Map<String, Object>> totalCountListMap = mobileTreatmentProcedureSqlMapDAO.selectTotalTreatmentProcedureOptionCount(dataMap);
					int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));

					if(0 < totalCount) {
						// 리스트 조회
						List<Map<String, Object>> resultList = mobileTreatmentProcedureSqlMapDAO.selectListTreatmentProcedureOption(dataMap);
						listMap.addAll(resultList);
					}
				}
				map.put("msgCode", Constants.OK);
				map.put("success","true");
				map.put("rows",listMap);
			}
		}
		catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		log.debug("MobileTreatmentProcedureServiceImpl selectListTreatmentProcedureOption END");
		return map;
	}

}
