package sgc.portal.cw.cw0602.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.cw0602.model.CW06020001U;
import sgc.portal.cw.cw0602.dto.CW06020001USearch;
import sgc.portal.cw.cw0602.persistence.CW06020001UMapper;

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

@Service
public class CW06020001UService {
    private final CW06020001UMapper mapper;

    public CW06020001UService(CW06020001UMapper mapper) {
        this.mapper = mapper;
    }

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

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

    public List<EasyHashMap<String, Object>> getListQuetion(CW06020001USearch search) {
        return mapper.getListQuetion(search);
    }
    
    @Transactional
    public Map<String, String> save(List<CW06020001U> CW06020001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (CW06020001U cW06020001U : CW06020001U) {
            switch (cW06020001U.getRowStatus()) {
                case GridRowStatus.CREATED:
                    insert(cW06020001U);
                    break;
                case GridRowStatus.UPDATED:
                    update(cW06020001U);
                    break;
                case GridRowStatus.DELETED:
                    delete(cW06020001U);
                    break;
                default:
                    break;
            }
        }
    	return result;
    }

    private void insert(CW06020001U cW06020001U) {

		int seq = mapper.selectSeq(cW06020001U);
		cW06020001U.setSeq(seq);
        mapper.insert(cW06020001U);
    }

    private void update(CW06020001U cW06020001U) {
        mapper.update(cW06020001U);
    }

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