package sgc.portal.cw.cw1603.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.cw.cw1603.model.CW16030001U;
import sgc.portal.cw.cw1603.dto.CW16030001USearch;
import sgc.portal.cw.cw1603.persistence.CW16030001UMapper;
import sgc.portal.com.sym.mail.service.MailService;

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

@Service
public class CW16030001UService {
    private final CW16030001UMapper mapper;
    private final MailService mailService;

    public CW16030001UService(CW16030001UMapper mapper, MailService mailService) {
        this.mapper = mapper;
        this.mailService = mailService;
    }

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

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

    private void insert(CW16030001U cW16030001U) {
        mapper.insert(cW16030001U);
    }

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

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

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

        for (CW16030001U cW16030001U : CW16030001U) {

    		String cmpnCd = cW16030001U.getCmpnCd();
    		String sSender = cW16030001U.getLoginUserNm();
        	String ssendAddress 	= mapper.selectSenderEmail(cW16030001U);
        	String srecvAddress 	= mapper.selectRecvEmail(cW16030001U);
    		
        	String sSubject 	= "[SGC이앤씨] 항공권 발권 요청";
    		String str1 = "하단의 내역 확인 후 항공권 예약 먼저 부탁드립니다.";
    		String str2 = "●탑승자 : "+ cW16030001U.getUserNm();
    		String str3 = "●좌석 : 이코노미";
    		String str4 = "●항공권 일정1 : "+ cW16030001U.getSconts1();
    		String str5 = "●항공권 일정2 : "+ cW16030001U.getSconts2();
    		//String str4 = "●항공권 일정1 : "+ cW16030001U.getYmd1() +" "+ cW16030001U.getDeparture1() +"("+ cW16030001U.getTime1() +") -> "+
    		//			  cW16030001U.getArrival1() +"("+ cW16030001U.getTime2() +"), "+ cW16030001U.getAirline1();
    		//String str5 = "●항공권 일정2 : "+ cW16030001U.getYmd2() +" "+ cW16030001U.getDeparture2() +"("+ cW16030001U.getTime3() +") -> "+
			//		  cW16030001U.getArrival2() +"("+ cW16030001U.getTime4() +"), "+ cW16030001U.getAirline2();

			mailService.mailSend8(cmpnCd, sSubject, srecvAddress, str1, str2, str3, str4, str5, ssendAddress, sSender);
        }
        return result;
    }
    
    @Transactional
    public Map<String, String> sendResultMail(List<CW16030001U> CW16030001U) {
    	
    	Map<String,String> result = new HashMap<>();

        for (CW16030001U cW16030001U : CW16030001U) {

    		String cmpnCd = cW16030001U.getCmpnCd();
    		String sSender = cW16030001U.getLoginUserNm();
        	String ssendAddress 	= mapper.selectSenderEmail(cW16030001U);
        	String srecvAddress 	= mapper.selectRecvEmail(cW16030001U);
    		
        	String sSubject = "";
        	String str1 = "";
        	String str2 = "";
    		String str3 = "";
    		String str4 = "";
    		String str5 = "";
    		
        	if(cW16030001U.getStatusCd() == "30") {
            	sSubject 	= "[안내] 신청업무 승인";
        		str1 = "신청하신 업무가 승인완료되었습니다.";
        	} else if(cW16030001U.getStatusCd() == "40") {
            	sSubject 	= "[안내] 신청업무 반려";
        		str1 = "신청하신 업무가 반료 처리되었습니다.";
        		str2 = "●반려사유 : "+ cW16030001U.getUserNm();
        	}
        	
    		
			mailService.mailSend8(cmpnCd, sSubject, srecvAddress, str1, str2, str3, str4, str5, ssendAddress, sSender);
        }
        return result;
    }
}
