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

import com.madeu.constants.Constants;
import com.madeu.dao.mobile.mobileuser.MobileUserSqlMapDAO;
import com.madeu.dao.mobile.mobileuserinquiry.MobileUserInquirySqlMapDAO;
import com.madeu.service.mobile.mobileuserinquiry.MobileUserInquiryService;
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("MobileUserInquiryService")
public class MobileUserInquiryServiceImpl implements MobileUserInquiryService {
    
    @Autowired
    private MobileUserInquirySqlMapDAO mobileUserInquirySqlMapDAO;

    @Autowired
    private MobileUserSqlMapDAO mobileUserSqlMapDAO;

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

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

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

        try{
            boolean check  = true;

            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){
                List<Map<String, Object>> totalCountListMap = mobileUserInquirySqlMapDAO.selectTotalUserInquiryCount(paramMap);
                int totalCount = Integer.parseInt(String.valueOf(totalCountListMap.get(0).get("totalCount")));

                if(0 < totalCount) {
                    // 리스트 조회
                    listMap = mobileUserInquirySqlMapDAO.selectListUserInquiry(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("MobileUserInquiryServiceImpl selectListUserInquiry END");
        return map;
    }

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

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

        log.debug("loginUserId       :"+paramMap.get("muUserInquiryId"));

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

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

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

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

            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){
                List<Map<String, Object>> listMap = mobileUserInquirySqlMapDAO.selectUserInquiry(paramMap);

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

    /**
     * 고객 1:1 문의 정보 등록
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor={Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public HashMap<String, Object> insertUserInquiry(
            HashMap<String, Object> paramMap) throws Exception{
        log.debug("MobileUserInquiryServiceImpl 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"));


            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","내용 정보가 없습니다.");
            }
            if(true == check){
                // 사용자 확인
                List<Map<String, Object>> userListMap = mobileUserSqlMapDAO.checkUser(paramMap);
                int userListMapSize = userListMap.size();

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

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

                String muAppUserInquiryId = ("I").concat(String.valueOf(System.currentTimeMillis()));
                paramMap.put("muAppUserInquiryId",muAppUserInquiryId);
                paramMap.put("muMemberId", paramMap.get("loginMemberId"));
                mobileUserInquirySqlMapDAO.insertUserInquiry(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("MobileUserInquiryServiceImpl insertAppUserInquiry END");
        return map;
    }
}
