package com.madeu.crm.settings.code.service;

import com.madeu.crm.settings.code.mapper.CodeMapper;
import com.madeu.constants.Constants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class CodeService {

    private final CodeMapper codeMapper;

    // ==========================================
    // 1. 코드 그룹 (Code Group)
    // ==========================================

    public HashMap<String, Object> getCodeGroupList(HashMap<String, Object> paramMap) {
        HashMap<String, Object> returnMap = new HashMap<>();
        List<HashMap<String, Object>> rows = codeMapper.getCodeGroupList(paramMap);

        returnMap.put("msgCode", Constants.OK);
        returnMap.put("rows", rows);
        return returnMap;
    }

    public HashMap<String, Object> getCodeGroup(HashMap<String, Object> paramMap) {
        HashMap<String, Object> returnMap = new HashMap<>();
        HashMap<String, Object> data = codeMapper.getCodeGroup(paramMap);

        returnMap.put("msgCode", Constants.OK);
        returnMap.put("data", data);
        return returnMap;
    }

    @Transactional(rollbackFor = Exception.class)
    public HashMap<String, Object> saveCodeGroup(HashMap<String, Object> paramMap) {
        HashMap<String, Object> returnMap = new HashMap<>();

        String pid = paramMap.get("pid") == null ? "" : String.valueOf(paramMap.get("pid"));

        // 중복 검사
        int dupCnt = codeMapper.checkGrpCd(paramMap);
        if (dupCnt > 0) {
            returnMap.put("msgCode", Constants.FAIL);
            returnMap.put("msgDesc", "이미 사용중인 코드그룹 ID입니다.");
            return returnMap;
        }

        if ("".equals(pid)) {
            // 신규 등록
            codeMapper.insertCodeGroup(paramMap);
            returnMap.put("msgDesc", "코드그룹이 등록되었습니다.");
        } else {
            // 수정
            codeMapper.updateCodeGroup(paramMap);
            returnMap.put("msgDesc", "코드그룹이 수정되었습니다.");
        }

        returnMap.put("msgCode", Constants.OK);
        return returnMap;
    }

    @Transactional(rollbackFor = Exception.class)
    public HashMap<String, Object> deleteCodeGroup(HashMap<String, Object> paramMap) {
        HashMap<String, Object> returnMap = new HashMap<>();

        codeMapper.deleteCodeGroup(paramMap);

        returnMap.put("msgCode", Constants.OK);
        returnMap.put("msgDesc", "코드그룹이 삭제되었습니다.");
        return returnMap;
    }

    // ==========================================
    // 2. 상세 코드 (Code List)
    // ==========================================

    public HashMap<String, Object> getCodeList(HashMap<String, Object> paramMap) {
        HashMap<String, Object> returnMap = new HashMap<>();
        List<HashMap<String, Object>> rows = codeMapper.getCodeList(paramMap);

        returnMap.put("msgCode", Constants.OK);
        returnMap.put("rows", rows);
        return returnMap;
    }

    public HashMap<String, Object> getCode(HashMap<String, Object> paramMap) {
        HashMap<String, Object> returnMap = new HashMap<>();
        HashMap<String, Object> data = codeMapper.getCode(paramMap);

        returnMap.put("msgCode", Constants.OK);
        returnMap.put("data", data);
        return returnMap;
    }

    @Transactional(rollbackFor = Exception.class)
    public HashMap<String, Object> saveCode(HashMap<String, Object> paramMap) {
        HashMap<String, Object> returnMap = new HashMap<>();

        String pid = paramMap.get("pid") == null ? "" : String.valueOf(paramMap.get("pid"));

        // 중복 검사
        int dupCnt = codeMapper.checkCodeCd(paramMap);
        if (dupCnt > 0) {
            returnMap.put("msgCode", Constants.FAIL);
            returnMap.put("msgDesc", "이미 사용중인 상세 코드 ID입니다.");
            return returnMap;
        }

        if ("".equals(pid)) {
            // 신규 등록
            codeMapper.insertCode(paramMap);
            returnMap.put("msgDesc", "상세 코드가 등록되었습니다.");
        } else {
            // 수정
            codeMapper.updateCode(paramMap);
            returnMap.put("msgDesc", "상세 코드가 수정되었습니다.");
        }

        returnMap.put("msgCode", Constants.OK);
        return returnMap;
    }

    @Transactional(rollbackFor = Exception.class)
    public HashMap<String, Object> deleteCode(HashMap<String, Object> paramMap) {
        HashMap<String, Object> returnMap = new HashMap<>();

        codeMapper.deleteCode(paramMap);

        returnMap.put("msgCode", Constants.OK);
        returnMap.put("msgDesc", "상세 코드가 삭제되었습니다.");
        return returnMap;
    }
}
