package sgc.portal.cd.cd0102.service;

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

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.cd.cd0102.dto.CD01020001USearch;
import sgc.portal.cd.cd0102.persistence.CD01020001UMapper;
import sgc.portal.cd.cd0102.model.CD01020001U;

@Service
public class CD01020001UService {

	private final CD01020001UMapper mapper;

	public CD01020001UService(CD01020001UMapper mapper) {
		this.mapper = mapper;
	}

	//CON_DAY_SITE 조회
	public List<EasyHashMap<String, Object>> selectConDaySiteList(CD01020001USearch search) {
		return mapper.selectConDaySiteList(search);
	}
	
	@Transactional
    public Map<String, String> saveConDaySite(List<CD01020001U> cD01020001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (CD01020001U vo : cD01020001U) {
            switch (vo.getRowStatus()) {
                case GridRowStatus.CREATED:
                	mapper.insertConDaySite(vo);
                    break;
                case GridRowStatus.UPDATED:
                	mapper.updateConDaySite(vo);
                    break;
                case GridRowStatus.DELETED:
                	int count = mapper.selectConDaySiteCount(vo);
                	if (count > 0) {
    					result.put("result", "N");
    				} else if (count == 0) {
    					result.put("result", "Y");
    					mapper.deleteConDaySiteCovendAll(vo);
    					mapper.deleteConDaySite(vo);
    				}
                    break;
                
                default:
                    break;
            }
        }
    	return result;
    }
	
	//CON_DAY_SITE_COVEND 조회
	public List<EasyHashMap<String, Object>> selectConDaySiteCovendList(CD01020001USearch search) {
		return mapper.selectConDaySiteCovendList(search);
	}
	
	@Transactional
    public Map<String, String> saveConDaySiteCovend(List<CD01020001U> cD01020001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (CD01020001U vo : cD01020001U) {
            switch (vo.getRowStatus()) {
                case GridRowStatus.CREATED:
                	mapper.insertConDaySiteCovend(vo);
                    break;
                case GridRowStatus.UPDATED:
                	mapper.updateConDaySiteCovend(vo);
                    break;
                case GridRowStatus.DELETED:
                	int count = mapper.selectConDaySiteCovendCount(vo);
                	if (count > 0) {
    					result.put("result", "N");
    				} else if (count == 0) {
    					result.put("result", "Y");
    					mapper.deleteConDaySiteCovend(vo);
    				}
                    break;
                
                default:
                    break;
            }
        }
    	return result;
    }
}