package sgc.portal.com.com.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.util.SessionUtil;
import sgc.portal.base.websquare.GridRowStatus;
import sgc.portal.cd.cd0301.model.CD03010001P;
import sgc.portal.cd.cd0302.persistence.CD03020001PMapper;
import sgc.portal.com.com.dto.ApprovalSearch;
import sgc.portal.com.com.model.Approval;
import sgc.portal.com.com.persistence.ComApprovalPopMapper;
import sgc.portal.com.sym.mail.service.MailService;
import sgc.portal.com.sym.user.model.UserDetails;

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

@Service
public class ComApprovalPopService {
    private final ComApprovalPopMapper mapper;
    private final MailService mailService;
	private final CD03020001PMapper cd0302mapper;

    public ComApprovalPopService(ComApprovalPopMapper mapper, MailService mailService, CD03020001PMapper cd0302mapper) {
        this.mapper = mapper;
        this.mailService = mailService;
        this.cd0302mapper = cd0302mapper;
    }
    
    /*
     * 조회 LIST
     */
    public List<EasyHashMap<String, Object>> getList(ApprovalSearch search) {
        return mapper.getList(search);
    }
    
    
    /*
     * 최근결재정보 조회 LIST
     */
    public List<EasyHashMap<String, Object>> getList1(ApprovalSearch search) {
        return mapper.getList1(search);
    }
    
    /*
     * draft
     */
    @Transactional
    public Map<String, String> draft(List<Approval> approval) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	String aprvNo = mapper.selectAprvNo(); //결재 번호 채번
    	String gubun = approval.get(0).getGubun();	//구분
    	String ccNm = approval.get(0).getCcNm();	//현장명
    	String lang = approval.get(0).getLang();	//다국어
    	String date = approval.get(0).getDate();	//일자
    	
    	Approval smComAprv = new Approval();
    	smComAprv.setAprvNo(aprvNo); //결재번호
    	smComAprv.setAprvStusCd(approval.get(0).getAprvStusCd());
    	smComAprv.setEmailTtl("공사일보 승인요청");
    	smComAprv.setEmailCn(approval.get(0).getAprvOpinCn());
    	
    	mapper.insertSmComAprv(smComAprv);
    	
    	for (Approval vo : approval) {
            switch (vo.getRowStatus()) {
                case GridRowStatus.CREATED:
                	vo.setAprvNo(aprvNo);
                	mapper.insertSmComAprvDtl(vo);
                    break;
                case GridRowStatus.UPDATED:
                    break;
                case GridRowStatus.DELETED:
                    break;
                default:
                    break;
            }
        }
    	
    	//-----------------------------------------------------------
		//메일전송
    	//cmpnCd : 회사코드,  recvAddress : 받는이이메일주소, sendUser : 답변자사번, mailSubject : 메일제목,  subSubject : 메일소제목, mailContent : 메일내용
		//-----------------------------------------------------------
    	String cmpnCd = smComAprv.getCmpnCd(); //회사코드
    	String recvAddress = mapper.selectRecvEmail(smComAprv); //받는이이메일주소
    	String sendUser = smComAprv.getMdftPr(); //답변자사번
    	String mailSubject = ""; //메일제목
    	String subSubject = ""; //메일소제목
    	StringBuffer sContent = new StringBuffer(); //메일내용
    	
    	if(gubun.equals("REPORT")) {	//공사일보 승인요청
    		if(lang.equals("ko")) {
    			mailSubject = "[SGC PORTAL] 승인요청 : 공사일보 (" + date + ")";
    			subSubject = "공사일보 승인요청 알림 메일 입니다.";
    			sContent.append("아래와 같이 승인 건이 대기중입니다. <br>");
    			sContent.append("해당 화면에서 검토 후 승인 바랍니다. <br>");
    			sContent.append("■ 시스템 : SGC PORTAL <br>");
    			sContent.append("■ 현장명 : " + ccNm + "<br>");
    			sContent.append("■ 결재내용  <br>");
    			sContent.append(smComAprv.getEmailCn() + "<br>");
    			sContent.append("■ 화면경로 : 공사관리 - 공사일보 - 공사일보승인 <br>");
    		} else if(lang.equals("en")) {
    			mailSubject = "[SGC PORTAL] Request approval : DAILY REPORT (" + date + ")";
    			subSubject = "This is a notification email for requesting approval of the DAILY REPORT.";
    			sContent.append("There is the document awating approval as below.  <br>");
    			sContent.append("Please review it on the screen and approve it. <br>");
    			sContent.append("■ System : SGC PORTAL <br>");
    			sContent.append("■ Project : " + ccNm + "<br>");
    			sContent.append("■ Contents  <br>");
    			sContent.append(smComAprv.getEmailCn() + "<br>");
    			sContent.append("■ Menu Path : CONSTRUCTION MANAGEMENT - DAILY REPORT - APPROVAL DAILY REPORT <br>");
    		}
    	} else if (gubun.equals("WORK ORDER")) {	//작업지시서 승인요청
    		if(lang.equals("ko")) {
    			mailSubject = "[SGC PORTAL] 승인요청 : 작업지시서 (" + ccNm + ")";
    			subSubject = "작업지시서 승인요청 알림 메일 입니다.";
    			sContent.append("아래와 같이 승인 건이 대기중입니다. <br>");
    			sContent.append("해당 화면에서 검토 후 승인 바랍니다. <br>");
    			sContent.append("■ 시스템 : SGC PORTAL <br>");
    			sContent.append("■ 현장명 : " + ccNm + "<br>");
    			sContent.append("■ 결재내용  <br>");
    			sContent.append(smComAprv.getEmailCn() + "<br>");
    			sContent.append("■ 화면경로 : 공사관리 - 작업지시 - 작업지시서승인 <br>");
    		} else if(lang.equals("en")) {
    			mailSubject = "[SGC PORTAL] Request approval : WORK ORDER (" + ccNm + ")";
    			subSubject = "This is a notification email for requesting approval of the WORK ORDER.";
    			sContent.append("There is the document awating approval as below.  <br>");
    			sContent.append("Please review it on the screen and approve it. <br>");
    			sContent.append("■ System : SGC PORTAL <br>");
    			sContent.append("■ Project : " + ccNm + "<br>");
    			sContent.append("■ Contents  <br>");
    			sContent.append(smComAprv.getEmailCn() + "<br>");
    			sContent.append("■ Menu Path : CONSTRUCTION MANAGEMENT - WORK ORDER - APPROVAL WORK ORDER <br>");
    		}
    	} else if (gubun.equals("COMPLETION")) {	//작업확인서 승인요청
    		if(lang.equals("ko")) {
    			mailSubject = "[SGC PORTAL] 승인요청 : 작업확인서 (" + ccNm + ")";
    			subSubject = "작업확인서 승인요청 알림 메일 입니다.";
    			sContent.append("아래와 같이 승인 건이 대기중입니다. <br>");
    			sContent.append("해당 화면에서 검토 후 승인 바랍니다. <br>");
    			sContent.append("■ 시스템 : SGC PORTAL <br>");
    			sContent.append("■ 현장명 : " + ccNm + "<br>");
    			sContent.append("■ 결재내용  <br>");
    			sContent.append(smComAprv.getEmailCn() + "<br>");
    			sContent.append("■ 화면경로 : 공사관리 - 작업지시 - 작업확인서승인 <br>");
    		} else if(lang.equals("en")) {
    			mailSubject = "[SGC PORTAL] Request approval : COMPLETION OF WORK ORDER (" + ccNm + ")";
    			subSubject = "This is a notification email for requesting approval of the COMPLETION OF WORK ORDER.";
    			sContent.append("There is the document awating approval as below.  <br>");
    			sContent.append("Please review it on the screen and approve it. <br>");
    			sContent.append("■ System : SGC PORTAL <br>");
    			sContent.append("■ Project : " + ccNm + "<br>");
    			sContent.append("■ Contents  <br>");
    			sContent.append(smComAprv.getEmailCn() + "<br>");
    			sContent.append("■ Menu Path : CONSTRUCTION MANAGEMENT - WORK ORDER - APPROVAL COMPLETION OF WORK ORDER <br>");
    		}
    	}

    	mailService.mailSendApproval(cmpnCd, recvAddress, sendUser, mailSubject, subSubject, sContent);
    	//-----------------------------------------------------------
    	
    	result.put("aprvNo", aprvNo);
    	
    	return result;
    }
    
    /*
     * approval
     */
    @Transactional
    public Map<String, String> approval(List<Approval> approval) {
    	
    	Map<String,String> result = new HashMap<>();
		UserDetails userDetails = SessionUtil.getUserDetails();
    	String gubun = approval.get(0).getGubun();	//구분
    	String ccNm = approval.get(0).getCcNm();	//현장명
    	String lang = approval.get(0).getLang();	//다국어
    	String date = approval.get(0).getDate();	//일자
    	String fnlAprvYn = approval.get(0).getFnlAprvYn();	//최종결재여부
    	
    	Approval smComAprv = new Approval();
    	smComAprv.setAprvNo(approval.get(0).getAprvNo());
    	smComAprv.setAprvStusCd(approval.get(0).getAprvStusCd());
    	smComAprv.setEmailTtl("공사일보 승인");
    	smComAprv.setEmailCn(approval.get(0).getEmailCn());

    	CD03010001P cd03010001P = new CD03010001P();
    	
    	mapper.updateSmComAprv(smComAprv);
    	
    	for (Approval vo : approval) {
            switch (vo.getRowStatus()) {
                case GridRowStatus.CREATED:
                    break;
                case GridRowStatus.UPDATED:
                	mapper.updateSmComAprvDtl(vo);
                    break;
                case GridRowStatus.DELETED:
                    break;
                default:
                    break;
            }
        }
    	
    	//-----------------------------------------------------------
		//메일전송
    	//cmpnCd : 회사코드,  recvAddress : 받는이이메일주소, sendUser : 답변자사번, mailSubject : 메일제목,  subSubject : 메일소제목, mailContent : 메일내용
		//-----------------------------------------------------------
    	String cmpnCd = smComAprv.getCmpnCd(); //회사코드
    	String recvAddress = null;
    	String sendUser = smComAprv.getMdftPr(); //답변자사번
    	String mailSubject = ""; //메일제목
    	String subSubject = ""; //메일소제목
    	StringBuffer sContent = new StringBuffer(); //메일내용

    	String recvAddress2 = null;
    	String recvUserNm = null;
    	String wkiSeq = null;
    	String prgsCd = null;
    	String enfcCd = null;
    	String mailSubject2 = ""; //메일제목
    	String subSubject2 = ""; //메일소제목
    	StringBuffer sContent2 = new StringBuffer(); //메일내용

    	String coMailChk = "N";
    	
    	if (fnlAprvYn.equals("N")){
    		recvAddress = mapper.selectRecvEmail(smComAprv); //받는이이메일주소
    	} else if (fnlAprvYn.equals("Y")) {
    		if (gubun.equals("WORK ORDER")) {

        		Map<String, String> details = mapper.selectWkiInfo(smComAprv);
        		
        		wkiSeq = details.get("WKI_SEQ"); //작업지시서 번호
        		prgsCd = details.get("PRGS_STUS_GB_CD"); //PRGS_STUS_GB_CD 번호
        		enfcCd = details.get("ENFC_GB_CD"); //ENFC_GB_CD 번호

        		smComAprv.setWkiSeq(wkiSeq);
        		
        		if(enfcCd.equals("BF")) {
        			if(prgsCd.equals("A23")) {
        				coMailChk = "Y";
        	    		recvAddress2 = mapper.selectRecvCoEmail(smComAprv); //업체 이메일주소
        	    		recvUserNm = mapper.selectRecvUserNm(smComAprv); //업체 담당자
        			} 
        		} else if(enfcCd.equals("AF")) {
        			if(prgsCd.equals("A11")) {
    					coMailChk = "Y";
    		    		recvAddress2 = mapper.selectRecvCoEmail(smComAprv); //업체 이메일주소
    		    		recvUserNm = mapper.selectRecvUserNm(smComAprv); //업체 담당자
        			}
        		}
    		}
    		
    		recvAddress = mapper.selectApprovedEmail(smComAprv); //받는이이메일주소
    	} 			
    	
    	if(fnlAprvYn.equals("N")) {	//중간단계승인
    		if(gubun.equals("REPORT")) {	//공사일보 승인요청
        		if(lang.equals("ko")) {
        			mailSubject = "[SGC PORTAL] 승인요청 : 공사일보 (" + date + ")";
        			subSubject = "공사일보 승인요청 알림 메일 입니다.";
        			sContent.append("아래와 같이 승인 건이 대기중입니다. <br>");
        			sContent.append("해당 화면에서 검토 후 승인 바랍니다. <br>");
        			sContent.append("■ 시스템 : SGC PORTAL <br>");
        			sContent.append("■ 현장명 : " + ccNm + "<br>");
        			sContent.append("■ 결재내용  <br>");
        			sContent.append(smComAprv.getEmailCn() + "<br>");
        			sContent.append("■ 화면경로 : 공사관리 - 공사일보 - 공사일보승인 <br>");
        		} else if(lang.equals("en")) {
        			mailSubject = "[SGC PORTAL] Request approval : DAILY REPORT (" + date + ")";
        			subSubject = "This is a notification email for requesting approval of the DAILY REPORT.";
        			sContent.append("There is the document awating approval as below.  <br>");
        			sContent.append("Please review it on the screen and approve it. <br>");
        			sContent.append("■ System : SGC PORTAL <br>");
        			sContent.append("■ Project : " + ccNm + "<br>");
        			sContent.append("■ Contents  <br>");
        			sContent.append(smComAprv.getEmailCn() + "<br>");
        			sContent.append("■ Menu Path : CONSTRUCTION MANAGEMENT - DAILY REPORT - APPROVAL DAILY REPORT <br>");
        		}
        	} else if (gubun.equals("WORK ORDER")) {	//작업지시서 승인요청
        		if(lang.equals("ko")) {
        			mailSubject = "[SGC PORTAL] 승인요청 : 작업지시서 (" + ccNm + ")";
        			subSubject = "작업지시서 승인요청 알림 메일 입니다.";
        			sContent.append("아래와 같이 승인 건이 대기중입니다. <br>");
        			sContent.append("해당 화면에서 검토 후 승인 바랍니다. <br>");
        			sContent.append("■ 시스템 : SGC PORTAL <br>");
        			sContent.append("■ 현장명 : " + ccNm + "<br>");
        			sContent.append("■ 결재내용  <br>");
        			sContent.append(smComAprv.getEmailCn() + "<br>");
        			sContent.append("■ 화면경로 : 공사관리 - 작업지시 - 작업지시서승인 <br>");
        		} else if(lang.equals("en")) {
        			mailSubject = "[SGC PORTAL] Request approval : WORK ORDER (" + ccNm + ")";
        			subSubject = "This is a notification email for requesting approval of the WORK ORDER.";
        			sContent.append("There is the document awating approval as below.  <br>");
        			sContent.append("Please review it on the screen and approve it. <br>");
        			sContent.append("■ System : SGC PORTAL <br>");
        			sContent.append("■ Project : " + ccNm + "<br>");
        			sContent.append("■ Contents  <br>");
        			sContent.append(smComAprv.getEmailCn() + "<br>");
        			sContent.append("■ Menu Path : CONSTRUCTION MANAGEMENT - WORK ORDER - APPROVAL WORK ORDER <br>");
        		}
        	} else if (gubun.equals("COMPLETION")) {	//작업확인서 승인요청
        		if(lang.equals("ko")) {
        			mailSubject = "[SGC PORTAL] 승인요청 : 작업확인서 (" + ccNm + ")";
        			subSubject = "작업확인서 승인요청 알림 메일 입니다.";
        			sContent.append("아래와 같이 승인 건이 대기중입니다. <br>");
        			sContent.append("해당 화면에서 검토 후 승인 바랍니다. <br>");
        			sContent.append("■ 시스템 : SGC PORTAL <br>");
        			sContent.append("■ 현장명 : " + ccNm + "<br>");
        			sContent.append("■ 결재내용  <br>");
        			sContent.append(smComAprv.getEmailCn() + "<br>");
        			sContent.append("■ 화면경로 : 공사관리 - 작업지시 - 작업확인서승인 <br>");
        		} else if(lang.equals("en")) {
        			mailSubject = "[SGC PORTAL] Request approval : COMPLETION OF WORK ORDER (" + ccNm + ")";
        			subSubject = "This is a notification email for requesting approval of the COMPLETION OF WORK ORDER.";
        			sContent.append("There is the document awating approval as below.  <br>");
        			sContent.append("Please review it on the screen and approve it. <br>");
        			sContent.append("■ System : SGC PORTAL <br>");
        			sContent.append("■ Project : " + ccNm + "<br>");
        			sContent.append("■ Contents  <br>");
        			sContent.append(smComAprv.getEmailCn() + "<br>");
        			sContent.append("■ Menu Path : CONSTRUCTION MANAGEMENT - WORK ORDER - APPROVAL COMPLETION OF WORK ORDER <br>");
        		}
        	}
    	} else if(fnlAprvYn.equals("Y")) {	//최종승인 (최종승인 시 상신자에세 메일 발송)
    		if(gubun.equals("REPORT")) {	//공사일보 승인완료
        		if(lang.equals("ko")) {
        			mailSubject = "[SGC PORTAL] 승인완료 : 공사일보 (" + date + ")";
        			subSubject = "공사일보 승인 완료 알림 메일 입니다.";
        			sContent.append("아래와 같이 승인이 완료되었습니다. <br>");
        			sContent.append("해당 화면에서 확인하시기 바랍니다. <br>");
        			sContent.append("■ 시스템 : SGC PORTAL <br>");
        			sContent.append("■ 현장명 : " + ccNm + "<br>");
        			sContent.append("■ 결재내용  <br>");
        			sContent.append(smComAprv.getEmailCn() + "<br>");
        			sContent.append("■ 화면경로 : 공사관리 - 공사일보 - 공사일보승인 <br>");
        		} else if(lang.equals("en")) {
        			mailSubject = "[SGC PORTAL] Approved : DAILY REPORT (" + date + ")";
        			subSubject = "This is a notification email that the DAILY REPORT has been approved.";
        			sContent.append("The document has been approved as below.  <br>");
        			sContent.append("Please check it on the screen. <br>");
        			sContent.append("■ System : SGC PORTAL <br>");
        			sContent.append("■ Project : " + ccNm + "<br>");
        			sContent.append("■ Contents  <br>");
        			sContent.append(smComAprv.getEmailCn() + "<br>");
        			sContent.append("■ Menu Path : CONSTRUCTION MANAGEMENT - DAILY REPORT - APPROVAL DAILY REPORT <br>");
        		}
        	} else if (gubun.equals("WORK ORDER")) {	//작업지시서 승인완료
        		if(lang.equals("ko")) {
        			mailSubject = "[SGC PORTAL] 승인완료 : 작업지시서 (" + ccNm + ")";
        			subSubject = "작업지시서 승인 완료 알림 메일 입니다.";
        			sContent.append("아래와 같이 승인이 완료되었습니다. <br>");
        			sContent.append("해당 화면에서 확인하시기 바랍니다. <br>");
        			sContent.append("■ 시스템 : SGC PORTAL <br>");
        			sContent.append("■ 현장명 : " + ccNm + "<br>");
        			sContent.append("■ 결재내용  <br>");
        			sContent.append(smComAprv.getEmailCn() + "<br>");
        			sContent.append("■ 화면경로 : 공사관리 - 작업지시 - 작업지시서승인 <br>");
        			
        			mailSubject2 = "[SGC이앤씨] 승인요청 : 작업지시서 (" + ccNm + ")";
        			subSubject2 = "작업지시서 승인요청 알림 메일 입니다.";
        			sContent2.append("아래와 같이 승인 건이 대기중입니다. <br>");
        			sContent2.append("자세한 사항은 SGC 협력업체 사이트(http://as.sgcenc.co.kr)로 접속하여 확인하시기 바랍니다.<br>");
        			sContent2.append("■ 시스템 : SGC AS PORTAL <br>");
        			sContent2.append("■ 현장명 : " + ccNm + "<br>");
        			sContent2.append("■ 결재내용  <br>");
        			sContent2.append(smComAprv.getEmailCn() + "<br>");
        			sContent2.append("■ 화면경로 : 공사관리 - 작업지시 - 작업확인서승인 <br>");     
        		} else if(lang.equals("en")) {
        			mailSubject = "[SGC PORTAL] Approved : WORK ORDER (" + ccNm + ")";
        			subSubject = "This is a notification email that the WORK ORDER has been approved.";
        			sContent.append("The document has been approved as below.  <br>");
        			sContent.append("Please check it on the screen. <br>");
        			sContent.append("■ System : SGC PORTAL <br>");
        			sContent.append("■ Project : " + ccNm + "<br>");
        			sContent.append("■ Contents  <br>");
        			sContent.append(smComAprv.getEmailCn() + "<br>");
        			sContent.append("■ Menu Path : CONSTRUCTION MANAGEMENT - WORK ORDER - APPROVAL WORK ORDER <br>");     

        			mailSubject2 = "[SGC E&C] Request approval : COMPLETION OF WORK ORDER (" + ccNm + ")";
        			subSubject2 = "This is a notification email for requesting approval of the WORK ORDER.";
        			sContent2.append("There is the document awating approval as below.  <br>");
        			sContent2.append("For more information, see the SGC Partner sites(http://as.sgcenc.co.kr) to check it out.<br>");
        			sContent2.append("■ System : SGC AS PORTAL <br>");
        			sContent2.append("■ Project : " + ccNm + "<br>");
        			sContent2.append("■ Contents  <br>");
        			sContent2.append(smComAprv.getEmailCn() + "<br>");
        			sContent2.append("■ Menu Path : CONSTRUCTION MANAGEMENT - WORK ORDER - APPROVAL COMPLETION OF WORK ORDER <br>");  
        		}
        	} else if (gubun.equals("COMPLETION")) {	//작업확인서 승인완료
        		if(lang.equals("ko")) {
        			mailSubject = "[SGC PORTAL] 승인완료 : 작업확인서 (" + ccNm + ")";
        			subSubject = "작업확인서 승인 완료 알림 메일 입니다.";
        			sContent.append("아래와 같이 승인이 완료되었습니다. <br>");
        			sContent.append("해당 화면에서 확인하시기 바랍니다. <br>");
        			sContent.append("■ 시스템 : SGC PORTAL <br>");
        			sContent.append("■ 현장명 : " + ccNm + "<br>");
        			sContent.append("■ 결재내용  <br>");
        			sContent.append(smComAprv.getEmailCn() + "<br>");
        			sContent.append("■ 화면경로 : 공사관리 - 작업지시 - 작업확인서승인 <br>");
        		} else if(lang.equals("en")) {
        			mailSubject = "[SGC PORTAL] Approved : COMPLETION OF WORK ORDER (" + ccNm + ")";
        			subSubject = "This is a notification email that the COMPLETION OF WORK ORDER has been approved.";
        			sContent.append("The document has been approved as below.  <br>");
        			sContent.append("Please check it on the screen. <br>");
        			sContent.append("■ System : SGC PORTAL <br>");
        			sContent.append("■ Project : " + ccNm + "<br>");
        			sContent.append("■ Contents  <br>");
        			sContent.append(smComAprv.getEmailCn() + "<br>");
        			sContent.append("■ Menu Path : CONSTRUCTION MANAGEMENT - WORK ORDER - APPROVAL COMPLETION OF WORK ORDER <br>");      
        		}
        	}
    	}
    	
    	if(!(recvAddress == null)) {
    		mailService.mailSendApproval(cmpnCd, recvAddress, sendUser, mailSubject, subSubject, sContent);
    	}
    	if(!(recvAddress2 == null)) {

			if(coMailChk.equals("Y")) {
	    		//mailService.mailSendApproval(cmpnCd, recvAddress2, sendUser, mailSubject2, subSubject2, sContent2);
	    		mailService.mailSendWorkOrder(cmpnCd, mailSubject2, subSubject2, sContent2, sendUser, recvAddress2);
	
	    		cd03010001P.setWkiSeq(smComAprv.getWkiSeq());
	    		cd03010001P.setAprvNo(approval.get(0).getAprvNo());
	    		cd03010001P.setSndLginId(userDetails.getLginId());
	    		cd03010001P.setSndEmpNo(userDetails.getEmpNo());
	    		cd03010001P.setSndEmpNm(userDetails.getUserNm());
	    		cd03010001P.setSndEmail(result.get("fromEmail"));
	    		cd03010001P.setRcvName(recvUserNm);
	    		cd03010001P.setRcvEmail(recvAddress2);
	    		cd03010001P.setMailTtl(mailSubject2);
	    		cd03010001P.setContentBase(result.get("contentBase"));
	
	    		//교부일자, 메일전송횟수 등 업데이트
	    		cd0302mapper.updateMailsend(cd03010001P);
	    		
	    		//메일전송 히스토리
	    		cd0302mapper.insertConWkiOdrMailhis(cd03010001P);
			}
    		
    	}
    	//-----------------------------------------------------------
    		
    	return result;
    }
    
    /*
     * reject
     */
    @Transactional
    public Map<String, String> reject(List<Approval> approval) {
    	
    	Map<String,String> result = new HashMap<>();
    	String gubun = approval.get(0).getGubun();	//구분
    	String ccNm = approval.get(0).getCcNm();	//현장명
    	String lang = approval.get(0).getLang();	//다국어
    	String date = approval.get(0).getDate();	//일자
    	
    	Approval smComAprv = new Approval();
    	smComAprv.setAprvNo(approval.get(0).getAprvNo());
    	smComAprv.setAprvStusCd(approval.get(0).getAprvStusCd());
    	smComAprv.setEmailTtl("공사일보 반려");
    	smComAprv.setEmailCn(approval.get(0).getEmailCn());
    	
    	mapper.updateSmComAprv(smComAprv);
    	
    	for (Approval vo : approval) {
            switch (vo.getRowStatus()) {
                case GridRowStatus.CREATED:
                    break;
                case GridRowStatus.UPDATED:
                	mapper.updateSmComAprvDtl(vo);
                    break;
                case GridRowStatus.DELETED:
                    break;
                default:
                    break;
            }
        }
    	
    	//-----------------------------------------------------------
		//메일전송
    	//cmpnCd : 회사코드,  recvAddress : 받는이이메일주소, sendUser : 답변자사번, mailSubject : 메일제목,  subSubject : 메일소제목, mailContent : 메일내용
		//-----------------------------------------------------------
    	String cmpnCd = smComAprv.getCmpnCd(); //회사코드
    	String recvAddress = mapper.selectRejectEmail(smComAprv); //받는이이메일주소
    	String sendUser = smComAprv.getMdftPr(); //답변자사번
    	String mailSubject = ""; //메일제목
    	String subSubject = ""; //메일소제목
    	StringBuffer sContent = new StringBuffer(); //메일내용
    	
    	if(gubun.equals("REPORT")) {	//공사일보 반려
    		if(lang.equals("ko")) {
    			mailSubject = "[SGC PORTAL] 반려 : 공사일보 (" + date + ")";
    			subSubject = "공사일보 결재 반려 알림 메일 입니다.";
    			sContent.append("아래와 같이 반려 되었습니다. <br>");
    			sContent.append("해당 화면에서 확인하시기 바랍니다. <br>");
    			sContent.append("■ 시스템 : SGC PORTAL <br>");
    			sContent.append("■ 현장명 : " + ccNm + "<br>");
    			sContent.append("■ 반려의견 : <br>");
    			sContent.append(smComAprv.getEmailCn() + "<br>");
    			sContent.append("■ 화면경로 : 공사관리 - 공사일보 - 공사일보승인 <br>");
    		} else if(lang.equals("en")) {
    			mailSubject = "[SGC PORTAL] Returned : DAILY REPORT (" + date + ")";
    			subSubject = "This is a notification email that the DAILY REPORT has been returned.";
    			sContent.append("The document has been returned as below.  <br>");
    			sContent.append("Please check it on the screen. <br>");
    			sContent.append("■ System : SGC PORTAL <br>");
    			sContent.append("■ Project : " + ccNm + "<br>");
    			sContent.append("■ Return opinion : <br>");
    			sContent.append(smComAprv.getEmailCn() + "<br>");
    			sContent.append("■ Menu Path : CONSTRUCTION MANAGEMENT - DAILY REPORT - APPROVAL DAILY REPORT <br>");
    		}
    	} else if (gubun.equals("WORK ORDER")) {	//작업지시서 반려
    		if(lang.equals("ko")) {
    			mailSubject = "[SGC PORTAL] 반려 : 작업지시서 (" + ccNm + ")";
    			subSubject = "작업지시서 반려 알림 메일 입니다.";
    			sContent.append("아래와 같이 반려 되었습니다. <br>");
    			sContent.append("해당 화면에서 확인하시기 바랍니다. <br>");
    			sContent.append("■ 시스템 : SGC PORTAL <br>");
    			sContent.append("■ 현장명 : " + ccNm + "<br>");
    			sContent.append("■ 반려의견 :  <br>");
    			sContent.append(smComAprv.getEmailCn() + "<br>");
    			sContent.append("■ 화면경로 : 공사관리 - 작업지시 - 작업지시서승인 <br>");
    		} else if(lang.equals("en")) {
    			mailSubject = "[SGC PORTAL] Returned : WORK ORDER (" + ccNm + ")";
    			subSubject = "This is a notification email that the WORK ORDER has been returned.";
    			sContent.append("The document has been returned as below.  <br>");
    			sContent.append("Please check it on the screen. <br>");
    			sContent.append("■ System : SGC PORTAL <br>");
    			sContent.append("■ Project : " + ccNm + "<br>");
    			sContent.append("■ Return opinion : <br>");
    			sContent.append(smComAprv.getEmailCn() + "<br>");
    			sContent.append("■ Menu Path : CONSTRUCTION MANAGEMENT - WORK ORDER - APPROVAL WORK ORDER <br>");
    		}
    	} else if (gubun.equals("COMPLETION")) {	//작업확인서 반려
    		if(lang.equals("ko")) {
    			mailSubject = "[SGC PORTAL] 반려 : 작업확인서 (" + ccNm + ")";
    			subSubject = "작업확인서 반려 알림 메일 입니다.";
    			sContent.append("아래와 같이 반려 되었습니다. <br>");
    			sContent.append("해당 화면에서 확인하시기 바랍니다. <br>");
    			sContent.append("■ 시스템 : SGC PORTAL <br>");
    			sContent.append("■ 현장명 : " + ccNm + "<br>");
    			sContent.append("■ 반려의견 :  <br>");
    			sContent.append(smComAprv.getEmailCn() + "<br>");
    			sContent.append("■ 화면경로 : 공사관리 - 작업지시 - 작업확인서승인 <br>");
    		} else if(lang.equals("en")) {
    			mailSubject = "[SGC PORTAL] Returned : COMPLETION OF WORK ORDER (" + ccNm + ")";
    			subSubject = "This is a notification email that the COMPLETION OF WORKORDER has been returned.";
    			sContent.append("The document has been returned as below.  <br>");
    			sContent.append("Please check it on the screen. <br>");
    			sContent.append("■ System : SGC PORTAL <br>");
    			sContent.append("■ Project : " + ccNm + "<br>");
    			sContent.append("■ Return opinion : <br>");
    			sContent.append(smComAprv.getEmailCn() + "<br>");
    			sContent.append("■ Menu Path : CONSTRUCTION MANAGEMENT - WORK ORDER - APPROVAL COMPLETION OF WORK ORDER <br>");
    		}
    	}

    	mailService.mailSendApproval(cmpnCd, recvAddress, sendUser, mailSubject, subSubject, sContent);
    	//-----------------------------------------------------------
    	
    	return result;
    }
    
    /*
     * 최근 결재내역 조회 LIST
     */
    public List<EasyHashMap<String, Object>> getLastAprvList(ApprovalSearch search) {
        return mapper.getLastAprvList(search);
    }
}
