package sgc.portal.cw.cw0807.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.cw0807.dto.CW08070001USearch;
import sgc.portal.cw.cw0807.model.CW08070001U;
import sgc.portal.cw.cw0807.persistence.CW08070001UMapper;

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

@Service
public class CW08070001UService {
    private final CW08070001UMapper mapper;

    public CW08070001UService(CW08070001UMapper mapper) {
        this.mapper = mapper;
    }

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

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

    @Transactional
    public Map<String, String> save(List<CW08070001U> CW08070001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (CW08070001U cW08070001U : CW08070001U) {
            switch (cW08070001U.getRowStatus()) {
                case GridRowStatus.CREATED:
                	int seq = mapper.selectSeq(cW08070001U);
                	cW08070001U.setSeq(seq);
                	System.out.println("==========================================" + cW08070001U.getHAmt());
                    insert(cW08070001U);
                    break;
                case GridRowStatus.UPDATED:
                    update(cW08070001U);
                    break;
                case GridRowStatus.DELETED:
                    delete(cW08070001U);
                    break;
                default:
                    break;
            }
        }
    	return result;
    }

    private void insert(CW08070001U cW08070001U) {
        mapper.insert(cW08070001U);
    }

    private void update(CW08070001U cW08070001U) {
        mapper.update(cW08070001U);
    }

    private void delete(CW08070001U cW08070001U) {
        mapper.delete(cW08070001U);
    }

    @Transactional
    public Map<String, String> save2(List<CW08070001U> CW08070001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (CW08070001U cW08070001U : CW08070001U) {
            switch (cW08070001U.getRowStatus()) {
                case GridRowStatus.CREATED:
                    insert2(cW08070001U);
                    break;
                case GridRowStatus.UPDATED:
                    update2(cW08070001U);
                    break;
                case GridRowStatus.DELETED:
                    delete2(cW08070001U);
                    break;
                default:
                    break;
            }
        }
    	return result;
    }

    private void insert2(CW08070001U cW08070001U) {
        mapper.insert2(cW08070001U);
    }

    private void update2(CW08070001U cW08070001U) {
        mapper.update2(cW08070001U);
    }

    private void delete2(CW08070001U cW08070001U) {
        mapper.delete2(cW08070001U);
    }
}
