package sgc.portal.cd.cd0304.service;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sgc.portal.cd.cd0301.model.CD03010001P;
import sgc.portal.cd.cd0304.dto.CD03040001PSearch;
import sgc.portal.cd.cd0304.model.CD03040001P;
import sgc.portal.cd.cd0304.persistence.CD03040001PMapper;

@Service
public class CD03040001PService {

	private final CD03040001PMapper mapper;
	
    public CD03040001PService(CD03040001PMapper mapper) {
        this.mapper = mapper;
    }
	
	public CD03040001P selectConWkiConf(CD03040001PSearch search) {
		CD03040001P cd03040001p = mapper.selectConWkiConf(search);
		String[] wkiNoArr = cd03040001p.getWkiNo().split("-");
		cd03040001p.setWkiNo1(wkiNoArr[1]);
		cd03040001p.setWkiNo2(wkiNoArr[2]);
		cd03040001p.setWkiNo3(wkiNoArr[3]);
		return cd03040001p;
	}
    
    @Transactional
	public Map<String, String> saveConWkiConf(CD03040001P cD03040001P) {
    	Map<String,String> result = new HashMap<>();
    	
    	if(StringUtils.isNotBlank(cD03040001P.getWkiSeq())) {
    		//mapper.updateConWkiConf(cD03040001P);    		
    	}else {
        	cD03040001P.setWkiSeq(mapper.selectWkiSeq());
    	}
    	mapper.saveConWkiConf(cD03040001P);
    	
		result.put("wkiSeq", cD03040001P.getWkiSeq());
		return result;
	}
    
    @Transactional
    public Map<String, String> requestConWkiConf(CD03040001P cD03040001P) {
    	Map<String,String> result = new HashMap<>();
    	
    	mapper.saveConWkiConf(cD03040001P);
    	mapper.requestConWkiConf(cD03040001P);
    	
    	return result;
    }

	public Map<String, String> approval(CD03010001P cD03010001P) {
    	Map<String,String> result = new HashMap<>();
    	
    	// SM_COM_APRV.APRV_STUS_CD의 결재상태 조회
    	String aprvStusCd = mapper.selectAprvStusCd(cD03010001P);
    	cD03010001P.setAprvStusCd(aprvStusCd);
    	
    	mapper.approval(cD03010001P);
		return result;
	}

	public Map<String, String> coApprove(CD03010001P cD03010001P) {
    	Map<String,String> result = new HashMap<>();
    	
    	mapper.coApprove(cD03010001P);
		return result;
	}

}