package sgc.portal.cw.cw0605.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.cw0605.dto.CW06050001USearch;
import sgc.portal.cw.cw0605.model.CW06050001U;
import sgc.portal.cw.cw0605.persistence.CW06050001UMapper;

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

@Service
public class CW06050001UService {
    private final CW06050001UMapper mapper;

    public CW06050001UService(CW06050001UMapper mapper) {
        this.mapper = mapper;
    }

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

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

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

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

    private void insert(CW06050001U cW06050001U) {
		int seq = mapper.selectSeq(cW06050001U);
		int coid = mapper.selectCoId(cW06050001U);
		cW06050001U.setSeq(seq);
		cW06050001U.setCoId(coid);
        mapper.insert(cW06050001U);
    }

    private void update(CW06050001U cW06050001U) {
        mapper.update(cW06050001U);
    }

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