package sgc.portal.cw.cw1304.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.cw1304.model.CW13040001U;
import sgc.portal.cw.cw1304.dto.CW13040001USearch;
import sgc.portal.cw.cw1304.model.CW13040001U;
import sgc.portal.cw.cw1304.persistence.CW13040001UMapper;

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

@Service
public class CW13040001UService {
    private final CW13040001UMapper mapper;

    public CW13040001UService(CW13040001UMapper mapper) {
        this.mapper = mapper;
    }

    public List<EasyHashMap<String, Object>> getList(CW13040001USearch search) {
        return mapper.getList(search);
    }
    
    @Transactional
    public Map<String, String> save(List<CW13040001U> CW13040001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (CW13040001U cW13040001U : CW13040001U) {
            switch (cW13040001U.getRowStatus()) {
                case GridRowStatus.CREATED:
                    insert(cW13040001U);
                    break;
                case GridRowStatus.UPDATED:
                    update(cW13040001U);
                    break;
                case GridRowStatus.DELETED:
                    delete(cW13040001U);
                    break;
                default:
                    break;
            }
        }
    	return result;
    }

    private void insert(CW13040001U cW13040001U) {
		int seq = mapper.selectSeq(cW13040001U);
		cW13040001U.setNoSeq(seq);
        mapper.insert(cW13040001U);
    }

    private void update(CW13040001U cW13040001U) {
        mapper.update(cW13040001U);
    }

    private void delete(CW13040001U cW13040001U) {
        mapper.delete(cW13040001U);
    }
}
