package sgc.portal.cw.cw1604.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.com.sym.mail.service.MailService;
import sgc.portal.cw.cw1604.model.CW16040001U;
import sgc.portal.cw.cw1604.dto.CW16040001USearch;
import sgc.portal.cw.cw1604.persistence.CW16040001UMapper;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CW16040001UService {
    private final CW16040001UMapper mapper;
    private final MailService mailService;

    public CW16040001UService(CW16040001UMapper mapper, MailService mailService) {
        this.mapper = mapper;
        this.mailService = mailService;
    }

    public List<EasyHashMap<String, Object>> getList(CW16040001USearch search) {
    	return mapper.getList(search);
    }

    @Transactional
    public Map<String, String> save(List<CW16040001U> CW16040001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (CW16040001U cW16040001U : CW16040001U) {
            switch (cW16040001U.getRowStatus()) {
                case GridRowStatus.CREATED:
                    insert(cW16040001U);
                    break;
                case GridRowStatus.UPDATED:
                    update(cW16040001U);
                    break;
                case GridRowStatus.DELETED:
                	int count1 = mapper.selectAtchfileIdCount(cW16040001U);
                	if(count1 != 0) {
                		mapper.deleteAtchfileId(cW16040001U);	
                	}
                    mapper.delete(cW16040001U);
                    break;
                default:
                    break;
            }
        }
    	return result;
    }

    private void insert(CW16040001U cW16040001U) {
        mapper.insert(cW16040001U);
    }

    private void update(CW16040001U cW16040001U) {
        mapper.update(cW16040001U);
    }
    
    @Transactional
    public Map<String, String> updateStatus(List<CW16040001U> CW16040001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
        for (CW16040001U cW16040001U : CW16040001U) {

    		String cmpnCd = cW16040001U.getCmpnCd();
    		String sSender = cW16040001U.getLoginUserNm();
        	String ssendAddress 	= mapper.selectSenderEmail(cW16040001U);
        	String srecvAddress 	= mapper.selectRecvEmail(cW16040001U);
        	String workNm 			= mapper.selectWorkNm(cW16040001U);
    		
        	String sSubject = "";
        	String str1 = "";
        	String str2 = "";
    		String str3 = "";
    		String str4 = "";
    		String str5 = "";

        	if(cW16040001U.getStatusCd().equals("30")) {
            	sSubject 	= "[안내] 신청업무 승인";
        		str1 = workNm +" 신청이 승인 처리되었습니다.";
        	} else if(cW16040001U.getStatusCd().equals("40")) {
            	sSubject 	= "[안내] 신청업무 반려";
        		str1 = workNm +" 신청이 반려 처리되었습니다.";
        		str2 = "●반려사유 : "+ cW16040001U.getRemark();
        	}
        	
        	updateStatus(cW16040001U);
			mailService.mailSend8(cmpnCd, sSubject, srecvAddress, str1, str2, str3, str4, str5, ssendAddress, sSender);
			
        }
        return result;
    }
    
    private void updateStatus(CW16040001U cW16040001U) {
        mapper.updateStatus(cW16040001U);
    }
    
    @Transactional
    public Map<String, String> saveSendCnt(List<CW16040001U> CW16040001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
        for (CW16040001U cW16040001U : CW16040001U) {
        	saveSendCnt(cW16040001U);
        }
        return result;
    }
    
    private void saveSendCnt(CW16040001U cW16040001U) {
        mapper.saveSendCnt(cW16040001U);
    }
}
