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

import com.madeu.constants.Constants;
import com.madeu.dao.mobile.mobileuser.MobileUserSqlMapDAO;
import com.madeu.dao.mobile.mobileuserphysical.MobileUserPhysicalSqlMapDAO;
import com.madeu.service.mobile.mobileuserphysical.MobileUserPhysicalService;
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;

@Slf4j
@Service("MobileUserPhysicalService")
public class MobileUserPhysicalServiceImpl implements MobileUserPhysicalService {

    @Autowired
    private MobileUserPhysicalSqlMapDAO mobileUserPhysicalSqlMapDAO;
    @Autowired
    private MobileUserSqlMapDAO mobileUserSqlMapDAO;
    
    /**
     * 신체정보 리스트 조회.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectUserPhysicalList(HashMap<String, Object> paramMap) throws Exception {
        log.debug("MobileUserPhysicalServiceImpl selectUserPhysicalList START");

        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> listMap = new ArrayList<>();
        log.debug("loginUserId                  :"+paramMap.get("loginUserId"));
        log.debug("loginUserId                  :"+paramMap.get("loginUserId"));
        log.debug("muUserId                     :"+paramMap.get("muUserId"));
        log.debug("menuClass                    :"+paramMap.get("menuClass"));
        log.debug("userStart                    :"+paramMap.get("userStart"));
        log.debug("userLimit                    :"+paramMap.get("userLimit"));
        log.debug("physicalSearchDate           :"+paramMap.get("physicalSearchDate"));

        try{
            boolean check  = true;

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = mobileUserSqlMapDAO.checkUser(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                    paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if(true == check){
                int TotalPhysicalCount = mobileUserPhysicalSqlMapDAO.selectTotalPhysicalCount(paramMap);
                if(0<TotalPhysicalCount){
                    listMap = mobileUserPhysicalSqlMapDAO.selectUserPhysicalList(paramMap);
                }


                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("rows",listMap);
                map.put("totalCount",TotalPhysicalCount);
            }

        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }

        log.debug("MobileUserPhysicalServiceImpl selectUserPhysicalList END");
        return map;
    }

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

        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> listMap = new ArrayList<>();
        log.debug("loginUserId                :"+paramMap.get("loginUserId"));
        log.debug("loginUserId                :"+paramMap.get("loginUserId"));
        log.debug("muUserId                    :"+paramMap.get("muUserId"));
        log.debug("menuClass                 :"+paramMap.get("menuClass"));
        log.debug("userStart                    :"+paramMap.get("userStart"));
        log.debug("userLimit                    :"+paramMap.get("userLimit"));

        try{
            boolean check  = true;

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = mobileUserSqlMapDAO.checkUser(paramMap);
                int userListMapSize = userListMap.size();

                if(1==userListMapSize){
                    paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if(true == check){
                int TotalPhysicalCount = mobileUserPhysicalSqlMapDAO.selectTotalPhysicalCount(paramMap);
                if(0<TotalPhysicalCount){
                    listMap = mobileUserPhysicalSqlMapDAO.selectRecentUserPhysicalList(paramMap);
                }


                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("rows",listMap);
                map.put("totalCount",TotalPhysicalCount);
            }

        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }

        log.debug("MobileUserPhysicalServiceImpl selectUserPhysicalList END");
        return map;
    }
}
