package sgc.portal.cw.cw0102.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.cw0102.dto.CW01020001USearch;
import sgc.portal.cw.cw0102.model.CW01020001U;
import sgc.portal.cw.cw0102.persistence.CW01020001UMapper;
import sgc.portal.cw.cw0102.model.CW01020001U;
import sgc.portal.cw.cw0102.model.CW01020001U;

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

@Service
public class CW01020001UService {
    private final CW01020001UMapper mapper;

    public CW01020001UService(CW01020001UMapper mapper) {
        this.mapper = mapper;
    }

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

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

    private void insert(CW01020001U cW01020001U) {
        mapper.insert(cW01020001U);
    }

    private void update(CW01020001U cW01020001U) {
        mapper.update(cW01020001U);
    }

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