package sgc.portal.cw.cw1401.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.cw1401.model.CW14010001U;
import sgc.portal.cw.cw1401.dto.CW14010001USearch;
import sgc.portal.cw.cw1401.persistence.CW14010001UMapper;

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

@Service
public class CW14010001UService {
    private final CW14010001UMapper mapper;

    public CW14010001UService(CW14010001UMapper mapper) {
        this.mapper = mapper;
    }

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

    @Transactional
    public Map<String, String> save(List<CW14010001U> CW14010001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (CW14010001U cW14010001U : CW14010001U) {
            switch (cW14010001U.getRowStatus()) {
                case GridRowStatus.CREATED:
                    insert(cW14010001U);
                    break;
                case GridRowStatus.UPDATED:
                    update(cW14010001U);
                    break;
                case GridRowStatus.DELETED:
                    delete(cW14010001U);
                    break;
                default:
                    break;
            }
        }
    	return result;
    }

    private void insert(CW14010001U cW14010001U) {
        mapper.insert(cW14010001U);
    }

    private void update(CW14010001U cW14010001U) {
        mapper.update(cW14010001U);
    }

    private void delete(CW14010001U cW14010001U) {
        mapper.delete(cW14010001U);
    }

    @Transactional
    public Map<String, String> updateYn(List<CW14010001U> CW14010001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
        for (CW14010001U cW14010001U : CW14010001U) {
        	updateYn(cW14010001U);
        }
        return result;
    }
    
    private void updateYn(CW14010001U cW14010001U) {
        mapper.updateYn(cW14010001U);
    }
}
