package sgc.portal.cw.cw0303.service;

import org.springframework.beans.factory.annotation.Autowired;
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.auth.model.SmComHdlgAuth;
import sgc.portal.com.auth.persistence.ComBusiHandleAuthMngMapper;
import sgc.portal.com.sym.mail.service.MailService;
import sgc.portal.cw.cw0303.dto.CW03030001USearch;
import sgc.portal.cw.cw0303.model.CW03030001U;
import sgc.portal.cw.cw0303.persistence.CW03030001UMapper;


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

@Service
public class CW03030001UService {

	private final CW03030001UMapper mapper;
	private final MailService mailService;

    public CW03030001UService(CW03030001UMapper mapper, MailService mailService) {
        this.mapper = mapper;
        this.mailService = mailService;
    }
    
	//위임고충 처리 조회
    public List<EasyHashMap<String, Object>> selectProblemMandateList(CW03030001USearch search) {
        return mapper.selectProblemMandateList(search);
    }
    
    //위임고충 처리
    @Transactional
    public void saveProblemMandateList(CW03030001U cW03030001U) {
    	
    	String sSubject = null;
    	String ssubSubject = null;
    	String srecvAddress = null;
    	String sSender = null;
    	StringBuffer sContent = new StringBuffer();
    	StringBuffer sqlstr = new StringBuffer();
    	String cmpnCd = null;
    	
    	if(cW03030001U.getStatusCd().equals("20")){
    		
    		for(CW03030001U vo1 : cW03030001U.getDsList()) {
    			
    			vo1.setCmpnCd(cW03030001U.getCmpnCd());
    			vo1.setMAnsSabun(cW03030001U.getUserId());
    			vo1.setMAnsNm(cW03030001U.getUserNm());
    			vo1.setMAnsDeptCd(cW03030001U.getUserDeptCd());
    			vo1.setMAnsDeptNm(cW03030001U.getUserDeptNm());
    			vo1.setAnsSabun(cW03030001U.getUserId());
        		mapper.updateProblemMandate3(vo1);
        		
        		vo1.setAnsSabun(cW03030001U.getUserId());
        		vo1.setAnsNm(cW03030001U.getUserNm());
        		vo1.setAnsDeptCd(cW03030001U.getUserDeptCd());
        		vo1.setAnsDeptNm(cW03030001U.getUserDeptNm());
        		mapper.updateProblemMandate4(vo1);
        		
        		cmpnCd = vo1.getCmpnCd();
        		srecvAddress = mapper.selectUserEmailList(vo1);
        		sSender = vo1.getAnsSabun();
        		sSubject = "[고충번호:"+ vo1.getProbNo() +"] 신청하신 고충이 반송되었습니다. 확인 바랍니다."; 
        		ssubSubject = "고충제목 : \""+ vo1.getSubject() + "\"";
        		sContent.append("● 고충 내용 : <br>");
        		sContent.append(vo1.getModContent());
        		sContent.append("<br><br><br><br>● 고충 답변 : <br>");
        		sContent.append(vo1.getAnsContent());
        		
        		mailService.mailSend(cmpnCd, sSubject, ssubSubject, sContent, sSender, srecvAddress);
    		}
    		
    	}else if(cW03030001U.getStatusCd().equals("99")) {
    		
    		for(CW03030001U vo1 : cW03030001U.getDsList()) {
    			
    			vo1.setCmpnCd(cW03030001U.getCmpnCd());
    			vo1.setMAnsSabun(cW03030001U.getUserId());
    			vo1.setMAnsNm(cW03030001U.getUserNm());
    			vo1.setMAnsDeptCd(cW03030001U.getUserDeptCd());
    			vo1.setMAnsDeptNm(cW03030001U.getUserDeptNm());
        		mapper.updateProblemMandate1(vo1);
        		
        		vo1.setAnsSabun(cW03030001U.getUserId());
        		vo1.setAnsNm(cW03030001U.getUserNm());
        		vo1.setAnsDeptCd(cW03030001U.getUserDeptCd());
        		vo1.setAnsDeptNm(cW03030001U.getUserDeptNm());
        		mapper.updateProblemMandate2(vo1);
        		
        		cmpnCd = vo1.getCmpnCd();
        		srecvAddress = mapper.selectUserEmailList(vo1);
        		sSender = vo1.getAnsSabun();
        		sSubject = "[고충번호:"+ vo1.getProbNo() +"] 신청하신 고충 내용에 대한 처리가 완료되었습니다."; 
        		ssubSubject = "고충관리 위임 : \""+ vo1.getSubject() + "\"";
        		sContent.append("● 고충 내용 : <br>");
        		sContent.append(vo1.getModContent());
        		sContent.append("<br><br><br><br>● 고충 답변 : <br>");
        		sContent.append(vo1.getAnsContent());
        		
        		mailService.mailSend(cmpnCd, sSubject, ssubSubject, sContent, sSender, srecvAddress);
    		}
    	}
    }
    
    //접수
    public void updateProblemMandateReceipt(CW03030001U cW03030001U) {
    	mapper.updateProblemMandateReceipt(cW03030001U);	
    }
    
}
