package com.madeu.crm.feedbackReception.service.impl;

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

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 com.madeu.common.dao.CommonDao;
import com.madeu.common.service.MenuAuthService;
import com.madeu.constants.Constants;
import com.madeu.crm.feedbackReception.service.FeedbackReceptionService;
import com.madeu.dao.web.webauthmenurelation.WebAuthMenuRelationSqlMapDAO;
import com.madeu.dao.web.webmember.WebMemberSqlMapDAO;
import com.madeu.service.web.websms.WebSmsService;
import com.madeu.util.ValidationCheckUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class FeedbackReceptionServiceImpl implements FeedbackReceptionService {
    
    @Autowired
    private CommonDao cmmnDAO;
    
    @Autowired
    private MenuAuthService menuAuthService;
    
    @Autowired
    private WebMemberSqlMapDAO webMemberSqlMapDAO;

    @Autowired
    private WebAuthMenuRelationSqlMapDAO webAuthMenuRelationSqlMapDAO;
   
    @Autowired
    private WebSmsService smsSvc; 

    /**
     * 칭찬불만접수 게시판으로 이동
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> moveFeedbackReceptionList(HashMap<String, Object> paramMap) throws Exception {
        log.debug("moveFeedbackReceptionList START");

        HashMap<String, Object> map = menuAuthService.getMenuAuthority(paramMap);

        log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));
        log.debug("menuClass           :"+paramMap.get("menuClass"));
        log.debug("moveFeedbackReceptionList END");
        return map;
    }
    
    /**
     * 홈페이지 칭찬불만접수 리스트 조회
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> getFeedbackReceptionList(HashMap<String, Object> paramMap) throws Exception {

        HashMap<String, Object> map = menuAuthService.getMenuAuthority(paramMap);

        List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();
        
        int totalCount = cmmnDAO.selectCount("FeedbackReceptionSql.getFeedbackReceptionCnt", paramMap);

        if(0 < totalCount) {
            listMap = cmmnDAO.selectList("FeedbackReceptionSql.getFeedbackReceptionList", paramMap);
        }

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

        return map;
    }
    /**
     * 홈페이지 칭찬불만접수정보 상세 조회 (one)
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> getFeedbackReception(
            HashMap<String, Object> paramMap) throws Exception {

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

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

            // 정렬
            if(true != ValidationCheckUtil.emptyCheck(postNo)){
                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);

                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){
                
                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("detail", cmmnDAO.select("FeedbackReceptionSql.getFeedbackReception", paramMap));                
            }
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("FeedbackReceptionServiceImpl selectFeedbackReception END");
        return map;
    }

    /**
     * 홈페이지 칭찬불만접수 콘텐츠 등록 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> insertFeedbackReceptionIntro(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("FeedbackReceptionServiceImpl insertFeedbackReceptionIntro 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", "권한 정보가 없습니다.");
                }
            }
            //List<Map<String, Object>> listMap = FeedbackReceptionSqlMapDAO.selectFeedbackReceptionCategory(paramMap);                        
        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("FeedbackReceptionServiceImpl insertFeedbackReceptionIntro END");
        return map;
    }

    /**
     * 홈페이지 칭찬불만접수 정보 등록
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> putFeedbackReception(
            HashMap<String, Object> paramMap) throws Exception{
    	 log.debug("FeedbackReceptionServiceImpl putFeedbackReception START");
    	
	    HashMap<String, Object> map = new HashMap<>();

	    try {
	        boolean check = true;

	        String branchOfficeCd = String.valueOf(paramMap.get("branchOfficeCd"));
	        String mbName = String.valueOf(paramMap.get("mbName"));
	        String mbHp = String.valueOf(paramMap.get("mbHp"));
	        String opinionClassificationCd = String.valueOf(paramMap.get("opinionClassificationCd"));
	        String content = String.valueOf(paramMap.get("content"));

	        // Validation
	        if (!ValidationCheckUtil.emptyCheck(branchOfficeCd)) {
	            check = false;
	            map.put("msgCode", Constants.FAIL);
	            map.put("msgDesc", "지점 정보가 없습니다.");
	        }
	        if (!ValidationCheckUtil.emptyCheck(mbName)) {
	            check = false;
	            map.put("msgCode", Constants.FAIL);
	            map.put("msgDesc", "이름 정보가 없습니다.");
	        }
	        if (!ValidationCheckUtil.emptyCheck(mbHp)) {
	            check = false;
	            map.put("msgCode", Constants.FAIL);
	            map.put("msgDesc", "전화번호 정보가 없습니다.");
	        }
	        if (!ValidationCheckUtil.emptyCheck(opinionClassificationCd)) {
	            check = false;
	            map.put("msgCode", Constants.FAIL);
	            map.put("msgDesc", "구분 정보가 없습니다.");
	        }
	        if (!ValidationCheckUtil.emptyCheck(content)) {
	            check = false;
	            map.put("msgCode", Constants.FAIL);
	            map.put("msgDesc", "내용 정보가 없습니다.");
	        }

	        // 권한 및 사용자 확인
	        if (check) {
	            List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
	            if (userListMap.size() == 1) {
	                paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
	            } else {
	                check = false;
	                map.put("msgCode", Constants.FAIL);
	                map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
	            }
	        }

	        if (check) {
	            HashMap<String, Object> authCheckParamMap = new HashMap<>();
	            authCheckParamMap.put("menuClass", paramMap.get("menuClass"));
	            authCheckParamMap.put("muAuthId", paramMap.get("menuClassAuthId"));
	            List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);

	            if (authMenuRelationlistMap.size() == 1 && "Y".equals(authMenuRelationlistMap.get(0).get("insertUseYn"))) {
	                // 권한 있음
	            } else {
	                check = false;
	                map.put("msgCode", Constants.FAIL);
	                map.put("msgDesc", "등록 권한 정보가 없습니다.");
	            }
	        }

	        if (check) {
	            // 등록 처리
	            paramMap.put("muMemberId", paramMap.get("loginMemberId"));
	            cmmnDAO.insert("FeedbackReceptionSql.putFeedbackReception", paramMap);

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

    /**
     * 홈페이지 칭찬불만접수 수정 화면으로 이동.
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> moveFeedbackReception(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("FeedbackReceptionServiceImpl updateFeedbackReceptionIntro 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("FeedbackReceptionServiceImpl updateFeedbackReceptionIntro END");
        return map;
    }

    /**
     * 홈페이지 칭찬불만접수 정보 수정
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> modFeedbackReception(
            HashMap<String, Object> paramMap) throws Exception{
    	log.debug("FeedbackReceptionServiceImpl modFeedbackReception START");
    	
    	HashMap<String, Object> map = new HashMap<>();

        try {
            boolean check = true;

            String branchOfficeCd = String.valueOf(paramMap.get("branchOfficeCd"));
            String mbName = String.valueOf(paramMap.get("mbName"));
            String mbHp = String.valueOf(paramMap.get("mbHp"));
            String opinionClassificationCd = String.valueOf(paramMap.get("opinionClassificationCd"));
            String content = String.valueOf(paramMap.get("content"));

            // Validation
            if (!ValidationCheckUtil.emptyCheck(branchOfficeCd)) {
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc", "지점 정보가 없습니다.");
            }
            if (!ValidationCheckUtil.emptyCheck(mbName)) {
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc", "이름 정보가 없습니다.");
            }
            if (!ValidationCheckUtil.emptyCheck(mbHp)) {
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc", "전화번호 정보가 없습니다.");
            }
            if (!ValidationCheckUtil.emptyCheck(opinionClassificationCd)) {
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc", "구분 정보가 없습니다.");
            }
            if (!ValidationCheckUtil.emptyCheck(content)) {
                check = false;
                map.put("msgCode", Constants.FAIL);
                map.put("msgDesc", "내용 정보가 없습니다.");
            }

            // 권한 및 사용자 확인
            if (check) {
                List<Map<String, Object>> userListMap = webMemberSqlMapDAO.checkMember(paramMap);
                if (userListMap.size() == 1) {
                    paramMap.put("menuClassAuthId", userListMap.get(0).get("muAuthId"));
                } else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "사용자 정보가 올바르지 않습니다.");
                }
            }

            if (check) {
                HashMap<String, Object> authCheckParamMap = new HashMap<>();
                authCheckParamMap.put("menuClass", paramMap.get("menuClass"));
                authCheckParamMap.put("muAuthId", paramMap.get("menuClassAuthId"));
                List<Map<String, Object>> authMenuRelationlistMap = webAuthMenuRelationSqlMapDAO.selectAuthMenuRelation(authCheckParamMap);

                if (authMenuRelationlistMap.size() == 1 && "Y".equals(authMenuRelationlistMap.get(0).get("updateUseYn"))) {
                    // 권한 있음
                } else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "수정 권한 정보가 없습니다.");
                }
            }

            if (check) {
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                //본문 UPDATE
                cmmnDAO.update("FeedbackReceptionSql.modFeedbackReception", paramMap);
                
                smsSvc.insertSms(paramMap);
                
                paramMap.put("msgSendYn", "Y");
                //답변 INSERT
                cmmnDAO.insert("FeedbackReceptionSql.putFeedbackReceptionReply", paramMap);
                
                map.put("msgCode", Constants.OK);
                map.put("msgDesc", "수정되었습니다.");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        log.debug("FeedbackReceptionServiceImpl modFeedbackReception END");
        return map;
    }

    /**
     * 홈페이지 칭찬불만접수 정보 삭제
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> delFeedbackReception(
            HashMap<String, Object> paramMap) throws Exception{

    	HashMap<String, Object> map = new HashMap<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);
                int authMenuRelationlistMapSize = authMenuRelationlistMap.size();

                if(1==authMenuRelationlistMapSize){

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

                    }
                    else {
                        check = false;
                        map.put("msgCode", Constants.FAIL);
                        map.put("msgDesc", "삭제 권한 정보가 없습니다.");
                    }
                }
                else {
                    check = false;
                    map.put("msgCode", Constants.FAIL);
                    map.put("msgDesc", "권한 정보가 없습니다.");
                }
            }
            int delCnt = 0;
            if(true == check) {
            	
            	
            	delCnt = cmmnDAO.update("FeedbackReceptionSql.delFeedbackReception", paramMap);
            	map.put("msgCode", Constants.OK);
                map.put("msgDesc", delCnt + "건 삭제 되었습니다.");
            }

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

        log.debug("FeedbackReceptionServiceImpl deleteFeedbackReception END");
        return map;
    }
}
