package sgc.portal.cw.cw0801.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.cw0801.persistence.CW08010001UMapper;
import sgc.portal.cw.cw0702.model.CW07020001U;
import sgc.portal.cw.cw0801.dto.CW08010001USearch;
import sgc.portal.cw.cw0801.model.CW08010001U;
import sgc.portal.cw.cw0801.model.CW08010002U;

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

@Service
public class CW08010001UService {

	private final CW08010001UMapper mapper;

    public CW08010001UService(CW08010001UMapper mapper) {
        this.mapper = mapper;
    }
    
    //회의실등록  조회
    public List<EasyHashMap<String, Object>> selectZehrRomInfoList(CW08010001USearch search) {
        return mapper.selectZehrRomInfoList(search);
    }
    
    //위치등록  조회
    public List<EasyHashMap<String, Object>> selectZehrRomLoctnList(CW08010001USearch search) {
        return mapper.selectZehrRomLoctnList(search);
    }
    
    //위치 공통코드
    public List<EasyHashMap<String, Object>> selectZehrRomLoctnCommList() {
        return mapper.selectZehrRomLoctnCommList();
    }
    
    public List<EasyHashMap<String, Object>> selectZherRomDisplList() {
        return mapper.selectZherRomDisplList();
    }
    
    //회의실등록  수정삭체추가
    @Transactional
    public Map<String, String> saveZehrRomInfoList (List<CW08010001U> CW08010001U) {
    	
    	Map<String,String> result = new HashMap<>();

        for (CW08010001U cW08010001U : CW08010001U) {
        	
            switch (cW08010001U.getRowStatus()) {
            
                case GridRowStatus.CREATED:
                	int count = mapper.selectZehrRomInfoBunhoCount(cW08010001U);
                	//int count1 = mapper.selectZehrRomInfoTelCount(cW08010001U);
                	
                	if(count > 0 ) {
                		result.put("count", "0");
                		result.put("message", "회의실명이 중복됩니다.");	
                	//}else if(count1 > 0 ) {
                	//	result.put("count1", "1");
                	//	result.put("message", "전화번호가 중복됩니다. ");	
                	}else {
                		mapper.insertZehrRomInfo(cW08010001U);

                		List<EasyHashMap<String, Object>> info = mapper.selectZherRomDisplList();
                		cW08010001U.setInDate((String)info.get(0).get("lday"));
                    	mapper.updateZherRomDispl1(cW08010001U);
                	}
                	break;
                    
                case GridRowStatus.UPDATED:
                	int count2 = mapper.selectZehrRomInfoTelCount(cW08010001U);
                	if(count2 == 1 ) {
                		mapper.updateZehrRomInfo(cW08010001U);	
                	}else if(count2 > 1 ) {
                		result.put("count2", "2");
                		result.put("message", "전화번호가 중복됩니다.");
                	}
                	
                    break;
                    
                case GridRowStatus.DELETED:
                	
                	int count3 = mapper.selectZehrRomResrvtnCount(cW08010001U);
                	if(count3 == 0 ) {
                		mapper.deleteZehrRomHistr(cW08010001U);
                    	mapper.deleteZehrRomResrvtn(cW08010001U);
                    	mapper.deleteZehrRomDispl(cW08010001U);
                    	mapper.deleteZehrRomInfo(cW08010001U);
                	}else if(count3 > 0 ) {
                		result.put("count3", "3");
                		result.put("message", "삭제 하려는 회의실에 예약 정보가 있습니다.");
                	}
                    break;
                default:
                    break;
            }
        }
        return result;
    }
    
    //위치등록  수정삭체추가
    public Map<String, String> saveZehrRomLoctnList (List<CW08010002U> CW08010002U) {
    	
    	Map<String,String> result = new HashMap<>();

        for (CW08010002U cW08010002U : CW08010002U) {
        	
            switch (cW08010002U.getRowStatus()) {
            
                case GridRowStatus.CREATED:
                	mapper.insertZehrRomLoctn(cW08010002U);
                    break;
                    
                case GridRowStatus.UPDATED:
                	
                	int count3 = mapper.selectAtchfileIdZehrRomLoctnCount(cW08010002U);
                	if(count3 != 0) {
                		mapper.deleteZehrRomLoctnlAtchfileIdAdmin(cW08010002U);	
                	}
                	
                	mapper.updateZehrRomLoctn(cW08010002U);
                    break;
                    
                case GridRowStatus.DELETED:
                	int count = mapper.selectZehrRomInfoLocationCount(cW08010002U);
                	if(count == 0 ) {
                		int count1 = mapper.selectAtchfileIdZehrRomLoctnCount(cW08010002U);
                    	if(count1 != 0) {
                    		mapper.deleteZehrRomLoctnlAtchfileIdAdmin(cW08010002U);	
                    	}
                		mapper.deleteZehrRomLoctn(cW08010002U);
                	}else if(count > 0 ) {
                		result.put("count", "0");
                		result.put("message", "삭제 하려는 위치에  회의실이 등록되어 있습니다.");
                	}
                    break;
                default:
                    break;
            }
        }
        return result;
    }
    
    //디스플레이  수정삭체추가
    public void  saveZherRomDisplList(CW08010001U cW08010001U) {
    	
    	 List<EasyHashMap<String, Object>> info = mapper.selectZherRomDisplList();
    	 cW08010001U.setInDate((String)info.get(0).get("lday"));
    	 
    	 mapper.updateZherRomDispl1(cW08010001U);
    }
    
    //디스플레이  수정삭체추가
    public  Map<String, String>  saveZherRomDisplAllList(CW08010001U cW08010001U) {
    	 
    	 Map<String,String> result = new HashMap<>();
    	 List<EasyHashMap<String, Object>> info = mapper.selectZherRomDisplList();
    	 mapper.updateZherRomDisplAll(cW08010001U);
    	 
    	 result.put("asSuccessCd", cW08010001U.getAsSuccessCd());
 		 result.put("asMessage", cW08010001U.getAsMessage());
 		 return result;
    }

}