package sgc.portal.cw.cw0304.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.persistence.MailMapper;
import sgc.portal.com.sym.mail.service.MailService;
import sgc.portal.cw.cw0303.persistence.CW03030001UMapper;
import sgc.portal.cw.cw0304.dto.CW03040001USearch;
import sgc.portal.cw.cw0304.model.CW03040001U;
import sgc.portal.cw.cw0304.persistence.CW03040001UMapper;

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

@Service
public class CW03040001UService {

	private final CW03040001UMapper mapper;
	private final MailService mailService;

    public CW03040001UService(CW03040001UMapper mapper, MailService mailService) {
        this.mapper = mapper;
        this.mailService = mailService;
    }
    
	//고충접수처리 조회
    public List<EasyHashMap<String, Object>> selectProblemProcessList(CW03040001USearch search) {
        return mapper.selectProblemProcessList(search);
    }
    
    //고충접수처리
    @Transactional
    public void saveProblemProcessList(CW03040001U cW03040001U) {
    	
    	String sSubject = null;
    	String ssubSubject = null;
    	String srecvAddress = null;
    	String sSender = null;
    	StringBuffer sContent = new StringBuffer();
    	StringBuffer sqlstr = new StringBuffer();
    	String cmpnCd = null;
    	
    	if(cW03040001U.getStatusCd().equals("12")){
    		
    		for(CW03040001U vo1 : cW03040001U.getDsList()) {
    			
    			vo1.setCmpnCd(cW03040001U.getCmpnCd());
    			vo1.setMAnsSabun(cW03040001U.getUserId());
    			vo1.setMAnsNm(cW03040001U.getUserNm());
    			vo1.setMAnsDeptCd(cW03040001U.getUserDeptCd());
    			vo1.setMAnsDeptNm(cW03040001U.getUserDeptNm());
        		mapper.updateProblemProcess3(vo1);
        		
        		vo1.setAnsSabun(cW03040001U.getUserId());
        		vo1.setAnsNm(cW03040001U.getUserNm());
        		vo1.setAnsDeptCd(cW03040001U.getUserDeptCd());
        		vo1.setAnsDeptNm(cW03040001U.getUserDeptNm());
        		mapper.updateProblemProcess4(vo1);
        		
        		cmpnCd = vo1.getCmpnCd();
        		srecvAddress = mapper.selectUserEmailList(vo1);
        		sSender = cW03040001U.getUserId();		
        		sSubject = "[고충번호:"+ vo1.getProbNo() +"] 고충관리 위임건이 있습니다 확인 바랍니다.";
        		ssubSubject = "고충관리 위임 : \""+ vo1.getSubject() + "\"";
        		sContent.append("● 고충 내용 : <br>");
        		sContent.append(vo1.getModContent());
        		mailService.mailSend(cmpnCd, sSubject, ssubSubject, sContent, sSender, srecvAddress);
    		}
    		
    	}else if(cW03040001U.getStatusCd().equals("99")) {
    		
    		for(CW03040001U vo1 : cW03040001U.getDsList()) {
    			
    			vo1.setCmpnCd(cW03040001U.getCmpnCd());
    			vo1.setMAnsSabun(cW03040001U.getUserId());
    			vo1.setMAnsNm(cW03040001U.getUserNm());
    			vo1.setMAnsDeptCd(cW03040001U.getUserDeptCd());
    			vo1.setMAnsDeptNm(cW03040001U.getUserDeptNm());
        		mapper.updateProblemProcess1(vo1);
        		
        		vo1.setAnsSabun(cW03040001U.getUserId());
        		vo1.setAnsNm(cW03040001U.getUserNm());
        		vo1.setAnsDeptCd(cW03040001U.getUserDeptCd());
        		vo1.setAnsDeptNm(cW03040001U.getUserDeptNm());
        		mapper.updateProblemProcess2(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 updateProblemProcessReceipt(CW03040001U cW03040001U) {
    	mapper.updateProblemProcessReceipt(cW03040001U);	
    }
    
    //공개여부
    public void updateProblemProcessOpen(CW03040001U cW03040001U) {
    	if(cW03040001U.getOpenCd().equals("10")){
    		mapper.updateProblemProcessOpen(cW03040001U);
    	}else if(cW03040001U.getOpenCd().equals("20")){
    		mapper.updateProblemProcessOpen(cW03040001U);
    	}
    }
    
    /* 메일 테스트 확인 */
    /*public void mailTest(CW03040001U cW03040001U) {
	
    	String sSubject = null;
    	String ssubSubject = null;
    	String srecvAddress = null;
    	String sSender = null;
    	StringBuffer sContent = new StringBuffer();
    	StringBuffer sqlstr = new StringBuffer();
    	String cmpnCd = null;
    	
    	if(cW03040001U.getStatusCd().equals("12")){
    		
    		for(CW03040001U vo1 : cW03040001U.getDsList()) {
    			
    			vo1.setCmpnCd(cW03040001U.getCmpnCd());
    			vo1.setMAnsSabun(cW03040001U.getUserId());
    			vo1.setMAnsNm(cW03040001U.getUserNm());
    			vo1.setMAnsDeptCd(cW03040001U.getUserDeptCd());
    			vo1.setMAnsDeptNm(cW03040001U.getUserDeptNm());
        		//mapper.updateProblemProcess3(vo1);
        		
        		vo1.setAnsSabun(cW03040001U.getUserId());
        		vo1.setAnsNm(cW03040001U.getUserNm());
        		vo1.setAnsDeptCd(cW03040001U.getUserDeptCd());
        		vo1.setAnsDeptNm(cW03040001U.getUserDeptNm());
        		//mapper.updateProblemProcess4(vo1);
        		
        		
        		cmpnCd = vo1.getCmpnCd();
        		srecvAddress = mapper.selectUserEmailList(vo1);
        		sSender = cW03040001U.getEmlAddr();		
        		sSubject = "[고충번호:"+ vo1.getProbNo() +"] 고충관리 위임건이 있습니다 확인 바랍니다.";
        		ssubSubject = "고충관리 위임 : \""+ vo1.getSubject() + "\"";
        		sContent.append("● 고충 내용 : <br>");
        		sContent.append(vo1.getModContent());
        		mailService.mailSend(cmpnCd, sSubject, ssubSubject, sContent, sSender, srecvAddress);
    		}
    		
    	}else if(cW03040001U.getStatusCd().equals("99")) {
    		
    		for(CW03040001U vo1 : cW03040001U.getDsList()) {
    			
    			vo1.setCmpnCd(cW03040001U.getCmpnCd());
    			vo1.setMAnsSabun(cW03040001U.getUserId());
    			vo1.setMAnsNm(cW03040001U.getUserNm());
    			vo1.setMAnsDeptCd(cW03040001U.getUserDeptCd());
    			vo1.setMAnsDeptNm(cW03040001U.getUserDeptNm());
        		//mapper.updateProblemProcess1(vo1);
        		
        		vo1.setAnsSabun(cW03040001U.getUserId());
        		vo1.setAnsNm(cW03040001U.getUserNm());
        		vo1.setAnsDeptCd(cW03040001U.getUserDeptCd());
        		vo1.setAnsDeptNm(cW03040001U.getUserDeptNm());
        		//mapper.updateProblemProcess2(vo1);
        		
        		cmpnCd = vo1.getCmpnCd();
        		srecvAddress = mapper.selectUserEmailList(vo1);
        		sSender = cW03040001U.getEmlAddr();
        		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);
    		}
    	}	
    }*/
    
}