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.CS03010002PSearch;
import sgc.portal.cs.cs0301.model.CS03010001U;
import sgc.portal.cs.cs0301.model.CS03010002P;
import sgc.portal.cs.cs0301.model.TcsDeftVo;
import sgc.portal.cs.cs0301.model.TcsDeftElpsVo;
import sgc.portal.cs.cs0301.persistence.CS03010002PMapper;

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

@Service
public class CS03010002PService {
    private final CS03010002PMapper mapper;

    public CS03010002PService(CS03010002PMapper mapper) {
        this.mapper = mapper;
    }
    
    /*조회*/
    public EasyHashMap<String, Object> getList(CS03010002PSearch search) {
        return mapper.getList(search);
    }
    
    /*경과조회*/
    public List<EasyHashMap<String, Object>> getListElps(CS03010002PSearch search) {
        return mapper.getListElps(search);
    }
    
    /*경과조회*/
    public List<EasyHashMap<String, Object>> getChrgrList(CS03010002PSearch search) {
        return mapper.getChrgrList(search);
    }
    
    /*접수처리*/
    @Transactional
    public Map<String, String> saveReceipt(CS03010002P cS03010002P) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	TcsDeftVo tcsDeft = cS03010002P.getTcsDeft();
    	TcsDeftElpsVo tcsDeftElps = cS03010002P.getTcsDeftElps();
		
		mapper.insertDeftElpsReceipt(tcsDeftElps);				//TCS_DEFT_ELPS insert
		
		//tcsDeft.setWorkElpsSeq(tcsDeftElps.getPrcgElpsSeq());	//작업 경과 순번
		
		mapper.updateDeftReceipt(tcsDeft);						//TCS_DEFT update

        return result;
    }
    
    /*반려처리*/
    @Transactional
    public Map<String, String> saveReject(CS03010002P cS03010002P) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	TcsDeftVo tcsDeft = cS03010002P.getTcsDeft();
    	TcsDeftElpsVo tcsDeftElps = cS03010002P.getTcsDeftElps();
		
		mapper.insertDeftElpsReject(tcsDeftElps);				//TCS_DEFT_ELPS insert
		
		tcsDeft.setCpltElpsSeq(tcsDeftElps.getPrcgElpsSeq());	//완료 경과 순번
		
		mapper.updateDeftReject(tcsDeft);						//TCS_DEFT update

        return result;
    }
    
    /*작업완료처리*/
    @Transactional
    public Map<String, String> saveComplete(CS03010002P cS03010002P) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	TcsDeftVo tcsDeft = cS03010002P.getTcsDeft();
    	TcsDeftElpsVo tcsDeftElps = cS03010002P.getTcsDeftElps();
		
		mapper.insertDeftElpsComplete(tcsDeftElps);				//TCS_DEFT_ELPS insert
		
		tcsDeft.setPrcgElpsSeq(tcsDeftElps.getPrcgElpsSeq());	//처리 경과 순번
		
		mapper.updateDeftComplete(tcsDeft);						//TCS_DEFT update

        return result;
    }
    
    /*최종완료처리*/
    @Transactional
    public Map<String, String> saveFinComplete(CS03010002P cS03010002P) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	TcsDeftVo tcsDeft = cS03010002P.getTcsDeft();
    	TcsDeftElpsVo tcsDeftElps = cS03010002P.getTcsDeftElps();
		
		mapper.insertDeftElpsFinComplete(tcsDeftElps);				//TCS_DEFT_ELPS insert
		
		tcsDeft.setCpltElpsSeq(tcsDeftElps.getPrcgElpsSeq());		//완료 경과 순번
		
		mapper.updateDeftFinComplete(tcsDeft);						//TCS_DEFT update

        return result;
    }
    
    /*작업경과등록*/
    @Transactional
    public Map<String, String> saveElpsProcess(CS03010002P cS03010002P) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	TcsDeftVo tcsDeft = cS03010002P.getTcsDeft();
    	TcsDeftElpsVo tcsDeftElps = cS03010002P.getTcsDeftElps();
		
		mapper.insertDeftElpsProcess(tcsDeftElps);				//TCS_DEFT_ELPS insert
		
		cS03010002P.setDeftPrcgGbCd(tcsDeftElps.getDeftPrcgGbCd());
		String process = mapper.getProcess(cS03010002P);
		if("VISIT".equals(process)) {
			tcsDeft.setVisElpsSeq(tcsDeftElps.getPrcgElpsSeq());	//방문 경과 순번
			mapper.updateDeftProcess(tcsDeft);						//TCS_DEFT update
		} else if("WORK_ING".equals(process)) {
			tcsDeft.setWorkElpsSeq(tcsDeftElps.getPrcgElpsSeq());	//작업 경과 순번
			mapper.updateDeftProcess(tcsDeft);						//TCS_DEFT update
		}
        return result;
    }
    
    /*저장*/
    @Transactional
    public Map<String, Object> save(CS03010002P cS03010002P) {
    	
    	Map<String,Object> result = new HashMap<>();
    	TcsDeftVo tcsDeft = cS03010002P.getTcsDeft();
    	
    	if(tcsDeft.getWorkWkiNo().equals("")) {
    		if(!"".equals(tcsDeft.getPrcgCovendId())) {
    			tcsDeft.setWorkWkiNo(mapper.getWorkWkiNo(tcsDeft));
    		}
    	} 
    	
    	mapper.updateDeft(tcsDeft);
		result.put("tcsDeft", cS03010002P.getTcsDeft());
    	
        return result;
    }
    
    /*삭제*/
    @Transactional
    public Map<String, String> delete(CS03010002P cS03010002P) {
    	
    	Map<String,String> result = new HashMap<>();
    	TcsDeftVo tcsDeft = cS03010002P.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;
    }
    
    
    
    @Transactional
    public Map<String, String> saveDeftElpsSms(CS03010002P cS03010002P) {
    	
    	Map<String,String> result = new HashMap<>();
    	TcsDeftElpsVo tcsDeftElpsSms = cS03010002P.getTcsDeftElpsSms();
    	
    	mapper.insertDeftElpsSms(tcsDeftElpsSms);
    	
        return result;
    }
}
