package incheon.ags.ias.dept.service.impl;

import incheon.ags.ias.dept.mapper.DeptMapper;
import incheon.ags.ias.dept.service.DeptService;
import incheon.ags.ias.dept.vo.DeptSearchVO;
import incheon.ags.ias.dept.vo.DeptVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Slf4j
@Service("deptService")
@RequiredArgsConstructor
public class DeptServiceImpl extends EgovAbstractServiceImpl implements DeptService {
    private final DeptMapper deptMapper;

    @Override
    public List<Map<String, Object>> selectDeptList(DeptSearchVO deptSearchVO) throws Exception {
        return deptMapper.selectDeptList(deptSearchVO);
    }

    @Override
    public int selectDeptCnt(DeptSearchVO deptSearchVO) throws Exception {
        return deptMapper.selectDeptCnt(deptSearchVO);
    }

    @Override
    public DeptVO selectDeptDetail(DeptVO deptVO) throws Exception {
        return deptMapper.selectDeptDetail(deptVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDept(DeptVO deptVO) throws Exception {
        // 1. INSERT 실행
        int result = deptMapper.insertDept(deptVO);

        // 2. 히스토리 적재 (신규 데이터 - INSERT 후)
        Map<String, Object> historyParam = new HashMap<>();
        historyParam.put("deptCd", deptVO.getDeptCd());
        historyParam.put("cngypCd", "C");  // Create
        historyParam.put("frstRegId", deptVO.getLOGIN_USER_ID());
        historyParam.put("lastMdfcnId", deptVO.getLOGIN_USER_ID());
        deptMapper.insertDeptHistory(historyParam);
        log.debug("부서 히스토리 적재 (CREATE): {}", deptVO.getDeptCd());

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDept(DeptVO deptVO) throws Exception {
        // 1. 히스토리 적재 (UPDATE 전 데이터)
        Map<String, Object> historyParam = new HashMap<>();
        historyParam.put("deptCd", deptVO.getDeptCd());
        historyParam.put("cngypCd", "U");  // Update
        historyParam.put("frstRegId", deptVO.getLOGIN_USER_ID());
        historyParam.put("lastMdfcnId", deptVO.getLOGIN_USER_ID());
        deptMapper.insertDeptHistory(historyParam);
        log.debug("부서 히스토리 적재 (UPDATE 전): {}", deptVO.getDeptCd());

        // 2. UPDATE 실행
        return deptMapper.updateDept(deptVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteDept(DeptVO deptVO) throws Exception {
        // 1. 히스토리 적재 (DELETE 전 데이터)
        Map<String, Object> historyParam = new HashMap<>();
        historyParam.put("deptCd", deptVO.getDeptCd());
        historyParam.put("cngypCd", "D");  // Delete
        historyParam.put("frstRegId", deptVO.getLOGIN_USER_ID());
        historyParam.put("lastMdfcnId", deptVO.getLOGIN_USER_ID());
        deptMapper.insertDeptHistory(historyParam);
        log.debug("부서 히스토리 적재 (DELETE 전): {}", deptVO.getDeptCd());

        // 2. DELETE 실행
        return deptMapper.deleteDept(deptVO);
    }

    @Override
    public int checkDeptCdDuplicate(String deptCd) throws Exception {
        return deptMapper.checkDeptCdDuplicate(deptCd);
    }
}
