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

import com.madeu.constants.Constants;
import com.madeu.dao.web.webauthmenurelation.WebAuthMenuRelationSqlMapDAO;
import com.madeu.dao.web.webkakaotemplate.WebKakaoTemplateSqlMapDAO;
import com.madeu.dao.web.webmember.WebMemberSqlMapDAO;
import com.madeu.service.common.file.FileService;
import com.madeu.service.web.webkakaotemplate.WebKakaoTemplateService;
import com.madeu.util.ValidationCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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

@Slf4j
@Service("WebKakaoTemplateService")
public class WebKakaoTemplateServiceImpl implements WebKakaoTemplateService{
    
    @Autowired
    private WebKakaoTemplateSqlMapDAO webKakaoTemplateSqlMapDAO;

    @Autowired
    private WebMemberSqlMapDAO webMemberSqlMapDAO;

    /**
     * 알림톡 템플릿 리스트 조회 (option)
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectListKakaoTemplateOption(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebKakaoTemplateServiceImpl selectListKakaoTemplateOption 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;

            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){
                List<Map<String, Object>> hospitalListMap = webKakaoTemplateSqlMapDAO.selectListKakaoTemplateOption(paramMap);

                map.put("msgCode", Constants.OK);
                map.put("success","true");
                map.put("rows",hospitalListMap);
            }

        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("WebKakaoTemplateServiceImpl selectListKakaoTemplateOption END");
        return map;
    }
    
    /**
     * 알림톡 템플릿 상세 조회
     *
     *
     * @param paramMap
     * @return
     * @throws Exception
     */
    @Override
    public HashMap<String, Object> selectKakaoTemplate(
            HashMap<String, Object> paramMap) throws Exception {
        log.debug("WebKakaoTemplateServiceImpl selectKakaoTemplate 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;

            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){
                List<Map<String, Object>> kakaotemplateItems = webKakaoTemplateSqlMapDAO.selectKakaoTemplate(paramMap);

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

            }

        }
        catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        log.debug("WebKakaoTemplateServiceImpl selectKakaoTemplate END");
        return map;
    }
}
