package sgc.portal.cw.cw1402.service;

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.cw.cw1402.model.CW14020001U;
import sgc.portal.cw.cw1402.dto.CW14020001USearch;
import sgc.portal.cw.cw1402.persistence.CW14020001UMapper;

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

@Service
public class CW14020001UService {
    private final CW14020001UMapper mapper;

    public CW14020001UService(CW14020001UMapper mapper) {
        this.mapper = mapper;
    }

    public List<EasyHashMap<String, Object>> getList(CW14020001USearch search) {
    	if(search.getEmpNo().isEmpty()) {
            return mapper.getList(search);
    	} else {
    		String resiNo = mapper.selectResiNo(search);
    		search.setResiNo(resiNo);
            return mapper.getList(search);
    	}
    }

    public List<EasyHashMap<String, Object>> getList2(CW14020001USearch search) {
        return mapper.getList2(search);
    }

    public List<EasyHashMap<String, Object>> getList3(CW14020001USearch search) {
        return mapper.getList3(search);
    }

    @Transactional
    public Map<String, String> save(List<CW14020001U> CW14020001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (CW14020001U cW14020001U : CW14020001U) {
            switch (cW14020001U.getRowStatus()) {
                case GridRowStatus.CREATED:
            		String resiNo = mapper.selectResiNo(cW14020001U);
            		int seq = mapper.selectSeq(cW14020001U);
            		cW14020001U.setResiNo(resiNo);
            		cW14020001U.setSeq(seq);
            		int cnt = mapper.dupChk(cW14020001U); //당월 중복체크 -> 리조트 통합 중복체크 23.06.12 수정
            		int cnt2 = mapper.drawChk(cW14020001U); //당해 당첨체크
            		
            		if(cnt < 1) {
            			if(cnt2 < 1) {
                	        mapper.insert(cW14020001U);
            			} else {
                    		result.put("count", "1");
                    		result.put("message", "당해년도 해당 리조트에 이미 당첨되셨습니다.");
            			}
            		} else {
                		result.put("count", "1");
                		result.put("message", "해당년월에 이미 신청하셨습니다."); //당월 중복체크 -> 리조트 통합 중복체크 23.06.12 수정
            		}
            		
                    if(cW14020001U.getResortCd() == "10") {
                		int cnt3 = mapper.ruleChk(cW14020001U); //5년간 이용지침 위반자 체크

                		if(cnt3 < 1) {
                	        mapper.insert(cW14020001U);
                		} else {
                    		result.put("count", "1");
                    		result.put("message", "5년이내 이용지침 위반 대상자로써 신청불가합니다.");
            			}
                    } else if(cW14020001U.getResortCd() == "20") {
                		int cnt4 = mapper.cancelChk(cW14020001U); //전년도 취소규정 위반자 체크
                		
                		if(cnt4 < 1) {
                	        mapper.insert(cW14020001U);
                		} else {
                    		result.put("count", "1");
                    		result.put("message", "작년 취소규정 위반 대상자로써 신청불가합니다.");
            			}
                    }
                    break;
                case GridRowStatus.UPDATED:
                    update(cW14020001U);
                    break;
                case GridRowStatus.DELETED:
                    delete(cW14020001U);
                    break;
                default:
                    break;
            }
        }
    	return result;
    }

    private void update(CW14020001U cW14020001U) {
		String resiNo = mapper.selectResiNo(cW14020001U);
		cW14020001U.setResiNo(resiNo);
        mapper.update(cW14020001U);
    }

    private void delete(CW14020001U cW14020001U) {
        mapper.delete(cW14020001U);
    }
    
    @Transactional
    public Map<String, String> updateStatus(List<CW14020001U> CW14020001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
        for (CW14020001U cW14020001U : CW14020001U) {
        	updateStatus(cW14020001U);
        }
        return result;
    }
    
    private void updateStatus(CW14020001U cW14020001U) {
        mapper.updateStatus(cW14020001U);
    }
}
