package sgc.portal.cw.cw0629.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.cw0629.model.CW06290001U;
import sgc.portal.cw.cw0629.dto.CW06290001USearch;
import sgc.portal.cw.cw0629.persistence.CW06290001UMapper;

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

@Service
public class CW06290001UService {
    private final CW06290001UMapper mapper;

    public CW06290001UService(CW06290001UMapper mapper) {
        this.mapper = mapper;
    }

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

    private void insert(CW06290001U cW06290001U) {
        mapper.insert(cW06290001U);
    }

    private void update(CW06290001U cW06290001U) {
        mapper.update(cW06290001U);
    }

    private void delete(CW06290001U cW06290001U) {
        mapper.delete(cW06290001U);
    }

    @Transactional
    public Map<String, String> updateYn(List<CW06290001U> CW06290001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (CW06290001U cW06290001U : CW06290001U) {
    		updateYn(cW06290001U);
        }
    	return result;
    }

    private void updateYn(CW06290001U cW06290001U) {
        mapper.updateYn(cW06290001U);
    }
}
