package sgc.portal.cw.cw0624.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.cw0624.dto.CW06240001USearch;
import sgc.portal.cw.cw0624.dto.CW06240001USearch;
import sgc.portal.cw.cw0624.model.CW06240001U;
import sgc.portal.cw.cw0624.persistence.CW06240001UMapper;

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

@Service
public class CW06240001UService {
    private final CW06240001UMapper mapper;

    public CW06240001UService(CW06240001UMapper mapper) {
        this.mapper = mapper;
    }

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

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

    private void insert(CW06240001U cW06240001U) {
    	if(cW06240001U.getSeq() == null) {
    		int seq = mapper.selectSeq(cW06240001U);
    		cW06240001U.setSeq(seq);
    		cW06240001U.setSubSeq(1);
            mapper.insert(cW06240001U);
    	}else {
    		if(cW06240001U.getSubSeq() == null) {
        		int subSeq = mapper.selectSubSeq(cW06240001U);
        		cW06240001U.setSubSeq(subSeq);
                mapper.insert(cW06240001U);
    		}
    	}
    }

    private void update(CW06240001U cW06240001U) {
        mapper.update(cW06240001U);
    }

    private void delete(CW06240001U cW06240001U) {
        mapper.delete(cW06240001U);
    }
    
    @Transactional
    public Map<String, String> approve(List<CW06240001U> CW06240001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
        for (CW06240001U cW06240001U : CW06240001U) {
        	approve(cW06240001U);
        }
        return result;
    }
    
    private void approve(CW06240001U cW06240001U) {
        mapper.approve(cW06240001U);
    }

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

    public List<EasyHashMap<String, Object>> selectInitData(CW06240001USearch search) {
        return mapper.selectInitData(search);
    }
    
    public List<EasyHashMap<String, Object>> selectGoalList(CW06240001USearch search) {
        return mapper.selectGoalList(search);
    }

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