package sgc.portal.cw.cw0201.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.cw0201.dto.CW02010001USearch;
import sgc.portal.cw.cw0201.model.CW02010001U;
import sgc.portal.cw.cw0201.persistence.CW02010001UMapper;

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

@Service
public class CW02010001UService {
    private final CW02010001UMapper mapper;

    public CW02010001UService(CW02010001UMapper mapper) {
        this.mapper = mapper;
    }

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

    @Transactional
    public Map<String, String> save(List<CW02010001U> CW02010001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	int checkDay 	= 0;
    	int checkToDay 	= 0;
    	int check64 	= 0;
    	
        for (CW02010001U cW02010001U : CW02010001U) {
        	
        	//근무요일 중복 체크
        	checkDay 	= mapper.getCheckDay(cW02010001U);
        	//근무요일 미래일자 체크
        	checkToDay 	= mapper.getCheckToDay(cW02010001U);
        	//주당근무시간 64시간 체크
        	check64 	= mapper.getCheck64(cW02010001U);
        	
            switch (cW02010001U.getRowStatus()) {
            
                case GridRowStatus.CREATED:
                	
                	if(checkDay == 0) {
                		if(checkToDay == 0) {
                			if(check64 < 64) {
                				insert(cW02010001U);
                			} else {
                				result.put("result", "주당 근무시간 64시간을 초과할 수 없습니다.");
                			}
                		} else {
                			result.put("result", "미래 근무일은 입력할 수 없습니다.");
                		}
                	} else {
                		result.put("result", "근무요일을 중복 입력 하였습니다.");
                	}
                    break;
                    
                case GridRowStatus.UPDATED:
                	
                	if(checkDay == 0) {
                		if(checkToDay == 0) {
                			if(check64 < 64) {
                				update(cW02010001U);
                			} else {
                				result.put("result", "주당 근무시간 64시간을 초과할 수 없습니다.");
                			}
                		} else {
                			result.put("result", "미래 근무일은 입력할 수 없습니다.");
                		}
                	} else {
                		result.put("result", "근무요일을 중복 입력 하였습니다.");
                	}
                    break;
                    
                case GridRowStatus.DELETED:
                	
                    delete(cW02010001U);
                    break;
                    
                default:
                    break;
            }
        }
        return result;
    }

    private void insert(CW02010001U cW02010001U) {
        mapper.insert(cW02010001U);
    }

    private void update(CW02010001U cW02010001U) {
        mapper.update(cW02010001U);
    }

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