package sgc.portal.cw.cw0802.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.cw0802.persistence.CW08020001UMapper;
import sgc.portal.cw.cw0702.model.CW07020001U;
import sgc.portal.cw.cw0802.dto.CW08020001USearch;
import sgc.portal.cw.cw0802.model.CW08020001U;
import sgc.portal.cw.cw0802.model.CW08020002U;
import sgc.portal.com.sym.mail.persistence.MailMapper;
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 CW08020001UService {

	private final CW08020001UMapper mapper;
	private final MailService mailService;

    public CW08020001UService(CW08020001UMapper mapper, MailService mailService) {
        this.mapper = mapper;
        this.mailService = mailService;
    }
    
    //예약승인  조회
    public List<EasyHashMap<String, Object>> selectZehrRomResrvtnStatusList(CW08020001USearch search) {
        return mapper.selectZehrRomResrvtnStatusList(search);
    }
    
    //예약이력  조회
    public List<EasyHashMap<String, Object>> selectZehrRomHistrList(CW08020001USearch search) {
        return mapper.selectZehrRomHistrList(search);
    }
    
    //예약승인반려 디스플레이
    @Transactional
    public Map<String, String> saveZehrRomResrvtnStatusList30 (List<CW08020002U> CW08020002U) {
    	
    	String sSubject = null;
    	String ssubSubject = null;
    	String srecvAddress = null;
    	String sSender = null;
    	StringBuffer sContent = new StringBuffer();
    	StringBuffer sqlstr = new StringBuffer();
    	String cmpnCd = null;
    	Map<String,String> result = new HashMap<>();

        for (CW08020002U cW08020002U : CW08020002U) {
        	
            switch (cW08020002U.getRowStatus()) {
            
                case GridRowStatus.CREATED:
                		
                	break;
                    
                case GridRowStatus.UPDATED:
                	
                	String colname= "T" + cW08020002U.getPhm().substring(0, 5).replace(":", "").substring(0, 3);
                	String col = "";
                	cW08020002U.setStatus("30");
                	mapper.updateZehrRomResrvtn(cW08020002U);
            		mapper.insertZehrRomHistr(cW08020002U);
            		
            		if(cW08020002U.getGubun().equals("30")) {
            			
            			String cdept = mapper.selectZehrRomResrvtnCdeptList(cW08020002U);
            			if(cdept == "") {
            				cdept = "Q";
            			}
            			
            			cW08020002U.setCdept(cdept);
            			String name = mapper.selectZehrRomResrvtnStatusInfoList(cW08020002U);
                		cW08020002U.setDiplayNm(name);
                		cW08020002U.setPymdf(cW08020002U.getPymd().substring(0, 10).replace("-", ""));
                		cW08020002U.setPymdt(cW08020002U.getPymd().substring(13).replace("-", ""));
            			mapper.updateZehrRomDispl2(cW08020002U);
            		}else {
            			
            			int cnt = disColCount(cW08020002U);
            			String cdept = mapper.selectZehrRomResrvtnCdeptList(cW08020002U);
            			if(cdept == "") {
            				cdept = "Q";
            			}
            			
            			cW08020002U.setCdept(cdept);
            			String name = mapper.selectZehrRomResrvtnStatusInfoList(cW08020002U);
                		cW08020002U.setDiplayNm(name);
                		cW08020002U.setPymdf(cW08020002U.getPymd().substring(0, 10).replace("-", ""));
            			
                		col 	= colname + " = '" + name + "'";
                		
            			for( int i=1;i<cnt;i++ ){
            				
                			if(colname.length()<5){
            	        		colname = ","+colname;
            	        	}

            				if(colname.substring(4).equals("0")){
            	        		colname = colname.substring(0, 4)+"3";
            	        	}else{
            	        		colname = String.valueOf(Integer.parseInt(colname.substring(2, 4))+1)+"0";
            	        		if( colname.length() < 3 ){
            	        			colname= ",T0" + colname;
            	        		}else{
            	        			colname= ",T" + colname;
            	        		}
            	        	}
            				col = col + colname + " = '" + name + "'";
            			}
            			cW08020002U.setColNm(col);
            			mapper.updateZehrRomDispl1(cW08020002U);
            		}
            		
            		List<EasyHashMap<String, Object>> userInfo = mapper.selectUserEmailList(cW08020002U);
            		
            		sContent = new StringBuffer();
            		cmpnCd = cW08020002U.getCmpnCd();
            		srecvAddress = (String)userInfo.get(0).get("eMail");
            		sSender = cW08020002U.getUserId();		
            		sSubject = "회의실 예약이 승인되었습니다.";
            		ssubSubject = "예약 승인";
            		
            		sContent.append("회의실 예약이 승인되었습니다.<br>");
            		sContent.append("회   의    명  : " + cW08020002U.getReName() +  "<br>");
            		sContent.append("예   약    자  : " + cW08020002U.getSnm() +    "<br>");
            		sContent.append("부        서  : " +  cW08020002U.getDeptnm() +  "<br>");
            		sContent.append("기        간  : " +  cW08020002U.getPymd() +     "<br>");
    				sContent.append("열쇠사용 여부  : "+ cW08020002U.getKeyYn() +  "<br>");
                	
    				mailService.mailSend(cmpnCd, sSubject, ssubSubject, sContent, sSender, srecvAddress);	
                    break;
                case GridRowStatus.DELETED:

                    break;
                default:
                    break;
            }
        }
        return result;
    }
    
    public Map<String, String> saveZehrRomResrvtnStatusList40 (List<CW08020002U> CW08020002U) {
    	
    	String sSubject = null;
    	String ssubSubject = null;
    	String srecvAddress = null;
    	String sSender = null;
    	StringBuffer sContent = new StringBuffer();
    	StringBuffer sqlstr = new StringBuffer();
    	String cmpnCd = null;
    	Map<String,String> result = new HashMap<>();

        for (CW08020002U cW08020002U : CW08020002U) {
        	
            switch (cW08020002U.getRowStatus()) {
            
                case GridRowStatus.CREATED:
                		
                	break;
                    
                case GridRowStatus.UPDATED:
                	
                	String colname= "T" + cW08020002U.getPhm().substring(0, 5).replace(":", "").substring(0, 3);
                	String col = "";
                	cW08020002U.setStatus("40");
                	mapper.updateZehrRomResrvtn(cW08020002U);
            		mapper.insertZehrRomHistr(cW08020002U);
            		
            		if(cW08020002U.getGubun().equals("30")) {
                		cW08020002U.setDiplayNm("N");
                		cW08020002U.setPymdf(cW08020002U.getPymd().substring(0, 10).replace("-", ""));
                		cW08020002U.setPymdt(cW08020002U.getPymd().substring(13).replace("-", ""));
            			mapper.updateZehrRomDispl2(cW08020002U);
            		}else {
            			
            			int cnt = disColCount(cW08020002U);
                		cW08020002U.setPymdf(cW08020002U.getPymd().substring(0, 10).replace("-", ""));
            			
                		col 	= colname + " = '" + "N" + "'";
                		
            			for( int i=1;i<cnt;i++ ){
            				
                			if(colname.length()<5){
            	        		colname = ","+colname;
            	        	}

            				if(colname.substring(4).equals("0")){
            	        		colname = colname.substring(0, 4)+"3";
            	        	}else{
            	        		colname = String.valueOf(Integer.parseInt(colname.substring(2, 4))+1)+"0";
            	        		if( colname.length() < 3 ){
            	        			colname= ",T0" + colname;
            	        		}else{
            	        			colname= ",T" + colname;
            	        		}
            	        	}
            				col = col + colname + " = '" + "N" + "'";
            			}
            			cW08020002U.setColNm(col);
            			cW08020002U.setPymdf(cW08020002U.getPymd().substring(0, 10).replace("-", ""));
            			mapper.updateZehrRomDispl1(cW08020002U);
            		}
            		
            		List<EasyHashMap<String, Object>> userInfo = mapper.selectUserEmailList(cW08020002U);
            		
            		sContent = new StringBuffer();
            		cmpnCd = cW08020002U.getCmpnCd();
            		srecvAddress = (String)userInfo.get(0).get("eMail");
            		sSender = cW08020002U.getUserId();		
            		sSubject = "회의실 예약이 반려되었습니다.";
            		ssubSubject = "예약 반려"; 
            		
            		sContent.append("회의실 예약이 반려되었습니다.<br>");
            		sContent.append("회   의    명  : " + cW08020002U.getReName() +  "<br>");
            		sContent.append("예   약    자  : " + cW08020002U.getSnm() +    "<br>");
            		sContent.append("부        서  : " +  cW08020002U.getDeptnm() +  "<br>");
            		sContent.append("기        간  : " +  cW08020002U.getPymd() +     "<br>");
    				sContent.append("열쇠사용 여부  : "+ cW08020002U.getKeyYn() +  "<br>");

            		mailService.mailSend(cmpnCd, sSubject, ssubSubject, sContent, sSender, srecvAddress);	
                    break;
                    
                case GridRowStatus.DELETED:

                    break;
                default:
                    break;
            }
        }
        return result;
    }
    
    
    //디스플레이 칼럼 카운트
    public int disColCount(CW08020002U cW08020002U) {
    	
    	int cnt=0;
    	String start_t = "";
        String end_t="";
        String cnt_t="";
        int i_start_t=0;
        int i_end_t=0;
        int h=0;
        int m=0;
        
        start_t = cW08020002U.getPhm().substring(0, 5).replace(":", "");
    	end_t = cW08020002U.getPhm().substring(8).replace(":", "");
		
    	i_start_t=Integer.parseInt(start_t);
    	i_end_t=Integer.parseInt(end_t);
		
    	cnt=i_end_t - i_start_t;
    	cnt_t=String.valueOf(cnt);
    	
    	if(cnt_t.length()>3){
    		h=Integer.parseInt(cnt_t.substring(0, 2));
    		m=Integer.parseInt(cnt_t.substring(2));
    	}else if(cnt_t.length()>2){
    		h=Integer.parseInt(cnt_t.substring(0, 1));
    		m=Integer.parseInt(cnt_t.substring(1));
    	}else if(cnt_t.length()>1){
    		m=Integer.parseInt(cnt_t);
    	}
    	
    	if(m == 0){
    		cnt=h*2;
    	}else{
    		cnt=(h*2)+1;
    	}
    	
    	return cnt;
    }
    
}