package sgc.portal.cw.cw0204.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.cw0204.dto.CW02040001USearch;
import sgc.portal.cw.cw0204.model.CW02040001U;
import sgc.portal.cw.cw0204.persistence.CW02040001UMapper;

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

@Service
public class CW02040001UService {
    private final CW02040001UMapper mapper;

    public CW02040001UService(CW02040001UMapper mapper) {
        this.mapper = mapper;
    }

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

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

    private void insert(CW02040001U cW02040001U) {
        mapper.insert(cW02040001U);
    }

    private void update(CW02040001U cW02040001U) {
        mapper.update(cW02040001U);
    }

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