package sgc.portal.com.auth.service;

import sgc.portal.com.auth.model.SmComHdlgAuth;
import sgc.portal.com.auth.persistence.ComBusiHandleAuthMngMapper;
import sgc.portal.base.exception.UnsupportedGirdStatusException;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.websquare.GridRowStatus;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

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

/**
 * @author SangWoo Park
 * @version 1.0.0
 * @since  2020. 8. 21.
 */
@Transactional
@Service
public class ComBusiHandleAuthMngService {

    @Autowired
    private ComBusiHandleAuthMngMapper comBusiHandleAuthMngMapper;

    // 업무취급권한 정보 조회
    public List<EasyHashMap<String, Object>> selectComBusiHandleAuthMng(SmComHdlgAuth smComHdlgAuth) {
        return comBusiHandleAuthMngMapper.selectComBusiHandleAuthMng(smComHdlgAuth);
    }

    // 업무취급권한 정보 조회(combo)
    public List<EasyHashMap<String, Object>> selectComBusiHandleAuthMngCombo(SmComHdlgAuth smComHdlgAuth) {
        return comBusiHandleAuthMngMapper.selectComBusiHandleAuthMngCombo(smComHdlgAuth);
    }

    // 업무취급권한 정보 저장(저장/수정/삭제)
    public Map<String,String> saveComBusiHandleAuthMng(@RequestBody List<SmComHdlgAuth> smComHdlgAuthes){
        Map<String,String> result = new HashMap<>();
        if (CollectionUtils.isNotEmpty(smComHdlgAuthes)) {
            for (SmComHdlgAuth smComHdlgAuth : smComHdlgAuthes) {
                switch (smComHdlgAuth.getRowStatus()) {
                case GridRowStatus.CREATED:
                    result.put("result", smComHdlgAuth.getBzafHdlgCd()+""+smComHdlgAuth.getLginId());
                    comBusiHandleAuthMngMapper.insertComBusiHandleAuthMng(smComHdlgAuth);
                    break;
                case GridRowStatus.UPDATED:
                    result.put("result", smComHdlgAuth.getBzafHdlgCd()+""+smComHdlgAuth.getLginId());
                    comBusiHandleAuthMngMapper.updateComBusiHandleAuthMng(smComHdlgAuth);
                    break;
                case GridRowStatus.DELETED:
                    comBusiHandleAuthMngMapper.deleteComBusiHandleAuthMng(smComHdlgAuth);
                    break;
                default:
                    throw new UnsupportedGirdStatusException();
                }
            }
        }
        return result;
    }

}
