package sgc.portal.cs.cs0301.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.websquare.GridRowStatus;
import sgc.portal.cs.cs0301.dto.CS03010001PSearch;
import sgc.portal.cs.cs0301.model.CS03010001P;
import sgc.portal.cs.cs0301.model.TcsDeftVo;
import sgc.portal.cs.cs0301.model.TcsDeftElpsVo;
import sgc.portal.cs.cs0301.persistence.CS03010001PMapper;

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

@Service
public class CS03010001PService {
    private final CS03010001PMapper mapper;

    public CS03010001PService(CS03010001PMapper mapper) {
        this.mapper = mapper;
    }
    
    public List<EasyHashMap<String, Object>> getList(CS03010001PSearch search) {
        return mapper.getList(search);
    }
    
    public EasyHashMap<String, Object> getListModify(CS03010001PSearch search) {
        return mapper.getListModify(search);
    }

    @Transactional
    public Map<String, Object> save(CS03010001P cS03010001P) {
    	
    	Map<String,Object> result = new HashMap<>();
    	TcsDeftVo tcsDeft = cS03010001P.getTcsDeft();
    	TcsDeftElpsVo tcsDeftElps = cS03010001P.getTcsDeftElps();
    	
    	if(tcsDeft.getDeftNo().equals("")) {
    		//진행상태가 신청 이면 INSERT
        	if(tcsDeft.getDeftPrcgStusCd().equals("A")) {
        		mapper.insertDeft(tcsDeft);
        		result.put("tcsDeft", cS03010001P.getTcsDeft());
        	}
        	
        	//진행상태가 접수 이면 INSERT
        	if(tcsDeft.getDeftPrcgStusCd().equals("B")) {
        		mapper.insertDeft(tcsDeft);
        		tcsDeftElps.setDeftNo(tcsDeft.getDeftNo());
        		tcsDeftElps.setPgprNm(tcsDeft.getRcvprNm());
        		tcsDeftElps.setPrcgPrStrtDt(tcsDeft.getRcptDt());
        		mapper.insertDeftElps(tcsDeftElps);
        		//tcsDeft.setWorkElpsSeq(tcsDeftElps.getPrcgElpsSeq());
        		mapper.updateDeft(tcsDeft);
        		result.put("tcsDeft", cS03010001P.getTcsDeft());
        		result.put("tcsDeftElps", cS03010001P.getTcsDeftElps());
        	}
        	
        	//진행상태가 재하자 이면 INSERT
        	if(tcsDeft.getDeftPrcgStusCd().equals("C")) {
        		mapper.insertDeft(tcsDeft);
        		tcsDeftElps.setDeftNo(tcsDeft.getDeftNo());
        		tcsDeftElps.setPgprNm(tcsDeft.getPgprNm());
        		tcsDeftElps.setPrcgPrStrtDt(tcsDeft.getRcptDt());
        		mapper.insertDeftElps(tcsDeftElps);
        		//tcsDeft.setWorkElpsSeq(tcsDeftElps.getPrcgElpsSeq());
        		mapper.updateDeft(tcsDeft);
        		result.put("tcsDeft", cS03010001P.getTcsDeft());
        		result.put("tcsDeftElps", cS03010001P.getTcsDeftElps());
        	}
    	} else {
    		mapper.updateDeft(tcsDeft);
    		result.put("tcsDeft", cS03010001P.getTcsDeft());
    	}
    	
        return result;
    }
    
    @Transactional
    public Map<String, String> saveDeftElpsSms(CS03010001P cS03010001P) {
    	
    	Map<String,String> result = new HashMap<>();
    	TcsDeftElpsVo tcsDeftElpsSms = cS03010001P.getTcsDeftElpsSms();
    	
    	mapper.insertDeftElpsSms(tcsDeftElpsSms);
    	
        return result;
    }
    
    @Transactional
    public Map<String, String> delete(CS03010001P cS03010001P) {
    	
    	Map<String,String> result = new HashMap<>();
    	TcsDeftVo tcsDeft = cS03010001P.getTcsDeft();
    	
    	int cnt = mapper.getDeftElpsCnt(tcsDeft);
    	
    	if(cnt == 0) {
    		mapper.deleteDeftElps(tcsDeft);
    		mapper.deleteDeft(tcsDeft);
        	result.put("status", "true");
    	} else {
    		result.put("status", "false");
    	}
    	
        return result;
    }
}
