package sgc.portal.cw.cw0614.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.cw0614.model.CW06140001U;
import sgc.portal.cw.cw0614.dto.CW06140001USearch;
import sgc.portal.cw.cw0614.persistence.CW06140001UMapper;

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

@Service
public class CW06140001UService {
    private final CW06140001UMapper mapper;

    public CW06140001UService(CW06140001UMapper mapper) {
        this.mapper = mapper;
    }

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

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

    private void insert(CW06140001U cW06140001U) {
		int seq = mapper.selectSeq(cW06140001U);
		cW06140001U.setMainSeq(seq);
        mapper.insert(cW06140001U);
    }

    private void update(CW06140001U cW06140001U) {
        mapper.update(cW06140001U);
    }

    private void delete(CW06140001U cW06140001U) {
        mapper.delete(cW06140001U);
    }

    @Transactional
    public void copy(CW06140001USearch search) {
    	mapper.copy(search);	
    }
}
