package com.madeu.service.web.webuserinquiry.impl;

import com.madeu.constants.Constants;
import com.madeu.dao.web.webauthmenurelation.WebAuthMenuRelationSqlMapDAO;
import com.madeu.dao.web.webmember.WebMemberSqlMapDAO;
import com.madeu.dao.web.webuserconsultingchart.WebUserConsultingChartSqlMapDAO;
import com.madeu.dao.web.webuserinquiry.WebUserInquirySqlMapDAO;
import com.madeu.service.web.webuserconsultingchart.WebUserConsultingChartService;
import com.madeu.service.web.webuserinquiry.WebUserInquiryService;
import com.madeu.util.ValidationCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service("WebUserInquiryService")
public class WebUserInquiryServiceImpl implements WebUserInquiryService {

    @Autowired
    private WebUserInquirySqlMapDAO webUserInquirySqlMapDAO;

    @Autowired
    private WebMemberSqlMapDAO webMemberSqlMapDAO;

    @Autowired
    private WebAuthMenuRelationSqlMapDAO webAuthMenuRelationSqlMapDAO;

    /**
     * 고객 1:1 문의 리스트 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListUserInquiryIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebUserInquiryServiceImpl selectListUserInquiryIntro START");

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

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

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

        try{
            boolean check  = true;

            String menuClass    = String.valueOf(paramMap.get("menuClass"));

            // 정렬
            if(true != ValidationCheckUtil.emptyCheck(menuClass)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","메뉴 정보가 없습니다.");
            }

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(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){
                // 권한 조회
                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);
                int authMenuRelationlistMapSize = authMenuRelationlistMap.size();

                if(1==authMenuRelationlistMapSize){
                    map.put("msgCode", Constants.OK);
                    map.put("success","true");

                    map.put("selectUseYn", authMenuRelationlistMap.get(0).get("selectUseYn"));
                    map.put("insertUseYn", authMenuRelationlistMap.get(0).get("insertUseYn"));
                    map.put("updateUseYn", authMenuRelationlistMap.get(0).get("updateUseYn"));
                    map.put("deleteUseYn", authMenuRelationlistMap.get(0).get("deleteUseYn"));
                    map.put("downloadUseYn", authMenuRelationlistMap.get(0).get("downloadUseYn"));
                }
                else {
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("WebUserInquiryServiceImpl selectListUserInquiryIntro END");
        return map;
    }

    /**
     * 고객 1:1 문의 리스트 조회
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListAppUserInquiry(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebAppUserInquiryServiceImpl selectListAppUserInquiry START");

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

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

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

        log.debug("appUserInquirySearchKeywordParam  :"+paramMap.get("appUserInquirySearchKeywordParam"));
        log.debug("appUserInquirySort                :"+paramMap.get("appUserInquirySort"));
        log.debug("appUserInquiryStart               :"+paramMap.get("appUserInquiryStart"));
        log.debug("appUserInquiryLimit               :"+paramMap.get("appUserInquiryLimit"));
        log.debug("appUserInquirySort                :"+paramMap.get("appUserInquirySort"));
        log.debug("appUserInquiryDir                 :"+paramMap.get("appUserInquiryDir"));
        log.debug("appUserInquiryCurrentPage         :"+paramMap.get("appUserInquiryCurrentPage"));

        log.debug("appUserInquirySearchStartDate     :"+paramMap.get("appUserInquirySearchStartDate"));
        log.debug("appUserInquirySearchEndDate       :"+paramMap.get("appUserInquirySearchEndDate"));

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

        try{
            boolean check  = true;

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(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){
                // 권한 조회
                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);

                if(1==authMenuRelationlistMap.size()){

                    if(("Y").equals(authMenuRelationlistMap.get(0).get("selectUseYn"))) {

                    }
                    else {
                        check = false;
                        map.put("msgCode", Constants.FAIL);
                        map.put("msgDesc", "조회 권한 정보가 없습니다.");
                    }
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }

            if(true == check){
                // 정렬 
                if(null==paramMap.get("appUserInquiryDir")||("").equals(paramMap.get("appUserInquiryDir"))){

                }
                else{
                    String dir = String.valueOf(paramMap.get("appUserInquiryDir"));
                    if(("A").equals(dir)){
                        paramMap.put("appUserInquiryDir", "DESC");
                    }
                    else if(("B").equals(dir)){
                        paramMap.put("appUserInquiryDir", "ASC");
                    }
                    else {
                        paramMap.put("appUserInquiryDir", "DESC");
                    }
                }

                paramMap.put("useYn", "Y");

                List<Map<String, Object>> totalCountListMap = webUserInquirySqlMapDAO.selectTotalAppUserInquiryCount(paramMap);
                int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));

                if(0 < totalCount) {
                    // 리스트 조회
                    listMap = webUserInquirySqlMapDAO.selectListAppUserInquiry(paramMap);
                }

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

    /**
     * 고객 1:1 문의 정보 상세 조회 (one)
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectAppUserInquiry(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebAppUserInquiryServiceImpl selectAppUserInquiry START");

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

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

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

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

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

            // 정렬
            if(true != ValidationCheckUtil.emptyCheck(muAppUserInquiryId)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","고객 1:1 문의 정보가 없습니다.");
            }

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(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){
                // 권한 조회
                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);

                if(1==authMenuRelationlistMap.size()){

                    if(("Y").equals(authMenuRelationlistMap.get(0).get("selectUseYn"))) {

                    }
                    else {
                        check = false;
                        map.put("msgCode", Constants.FAIL);
                        map.put("msgDesc", "조회 권한 정보가 없습니다.");
                    }
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }

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

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

    /**
     * 고객 1:1 문의 정보 등록
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> insertAppUserInquiry(
            HashMap<String, Object> paramMap) throws Exception{
        log.debug("WebAppUserInquiryServiceImpl insertNotice START");

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

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

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

        log.debug("title                :"+paramMap.get("title"));
        log.debug("content              :"+paramMap.get("content"));
        log.debug("muUserId             :"+paramMap.get("muUserId"));
        log.debug("membershipAccount    :"+paramMap.get("membershipAccount"));
        log.debug("phoneNumber          :"+paramMap.get("phoneNumber"));

        try{
            boolean check  = true;
            String tId = String.valueOf(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c1 = Calendar.getInstance();
            String tDate = sdf.format(c1.getTime());

            String title              = String.valueOf(paramMap.get("title"));
            String content            = String.valueOf(paramMap.get("content"));
            String muUserId           = String.valueOf(paramMap.get("muUserId"));
            String membershipAccount  = String.valueOf(paramMap.get("membershipAccount"));
            String phoneNumber        = String.valueOf(paramMap.get("phoneNumber"));

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

            if(true != ValidationCheckUtil.emptyCheck(membershipAccount)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","고객 계정 정보가 없습니다.");
            }

            if(true != ValidationCheckUtil.emptyCheck(phoneNumber)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","고객 연락처 정보가 없습니다.");
            }

            if(true != ValidationCheckUtil.emptyCheck(title)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","제목 정보가 없습니다.");
            }
            else{
                if(true != ValidationCheckUtil.lengthCheck(title,100)){
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc","제목 정보는 100자를 넘을 수 없습니다.");
                }
            }

            if(true != ValidationCheckUtil.emptyCheck(content)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","내용 정보가 없습니다.");
            }
//todo 고객 계정 확인 로직으로 변경
//            if(true == check){
//                // 사용자 확인
//                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(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){
//                // 권한 조회
//                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
//                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
//                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
//                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);
//                int authMenuRelationlistMapSize = authMenuRelationlistMap.size();
//
//                if(1==authMenuRelationlistMapSize){
//
//                    if(("Y").equals(authMenuRelationlistMap.get(0).get("insertUseYn"))) {
//
//                    }
//                    else {
//                        check = false;
//                        map.put("msgCode", Constants.FAIL);
//                        map.put("msgDesc", "등록 권한 정보가 없습니다.");
//                    }
//                }
//                else {
//                    check = false;
//                    map.put("msgCode", Constants.FAIL);
//                    map.put("msgDesc", "권한 정보가 없습니다.");
//                }
//            }

            if(true == check){
                paramMap.put("tDate", tDate);
                paramMap.put("tId", tId);

                String muAppUserInquiryId = ("N").concat(String.valueOf(System.currentTimeMillis()));
                paramMap.put("muAppUserInquiryId",muAppUserInquiryId);
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                webUserInquirySqlMapDAO.insertAppUserInquiry(paramMap);
                log.debug("muAppUserInquiryId :"+paramMap.get("id"));
                paramMap.put("muAppUserInquiryId", paramMap.get("id"));
                map.put("muAppUserInquiryId", paramMap.get("muAppUserInquiryId"));

                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "등록되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("WebAppUserInquiryServiceImpl insertAppUserInquiry END");
        return map;
    }

    /**
     * 고객 1:1 문의 답변 입력
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> updateAppUserInquiry(
            HashMap<String, Object> paramMap) throws Exception{
        log.debug("WebAppUserInquiryServiceImpl updateAppUserInquiry START");

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

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

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

        log.debug("muAppUserInquiryId      :"+paramMap.get("muAppUserInquiryId"));
        log.debug("muMemberId              :"+paramMap.get("muMemberId"));
        log.debug("answer                  :"+paramMap.get("answer"));

        try{
            boolean check  = true;

            String muAppUserInquiryId    = String.valueOf(paramMap.get("muAppUserInquiryId"));
            String answer               = String.valueOf(paramMap.get("answer"));
            String muMemberId               = String.valueOf(paramMap.get("muMemberId"));

            if(true != ValidationCheckUtil.emptyCheck(muAppUserInquiryId)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","고객 1:1 문의 식별자 정보가 없습니다.");
            }

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


            if(true != ValidationCheckUtil.emptyCheck(muMemberId)){
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc","담당자 정보가 없습니다.");
            }

            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(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){
                // 권한 조회
                HashMap<String, Object> authCheckParamMap = new HashMap<String, Object>();
                authCheckParamMap.put("menuClass",paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId",paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);
                int authMenuRelationlistMapSize = authMenuRelationlistMap.size();

                if(1==authMenuRelationlistMapSize){

                    if(("Y").equals(authMenuRelationlistMap.get(0).get("updateUseYn"))) {

                    }
                    else {
                        check = false;
                        map.put("msgCode", Constants.FAIL);
                        map.put("msgDesc", "수정 권한 정보가 없습니다.");
                    }
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }

            if(true == check){
                webUserInquirySqlMapDAO.updateAppUserInquiry(paramMap);
                map.put("muAppUserInquiryId", paramMap.get("muAppUserInquiryId"));

                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "수정되었습니다.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("WebAppUserInquiryServiceImpl updateAppUserInquiry END");
        return map;
    }
}
