package sgc.portal.ei.ei0101.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.ei.ei0101.dto.EI01010001USearch;
import sgc.portal.ei.ei0101.model.EI01010001U;
import sgc.portal.ei.ei0101.persistence.EI01010001UMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EI01010001UService {
    private final EI01010001UMapper mapper;

    public EI01010001UService(EI01010001UMapper mapper) {
        this.mapper = mapper;
    }
    
    /*
     * SEARCH LIST
     */
    public List<EasyHashMap<String, Object>> getList(EI01010001USearch search) {
        return mapper.getList(search);
    }
    
    /*
     * SAVE LIST
     */
    @Transactional
    public Map<String, String> save(List<EI01010001U> eI01010001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (EI01010001U vo : eI01010001U) {
            switch (vo.getRowStatus()) {
                case GridRowStatus.CREATED:
                    insert(vo);
                    break;
                case GridRowStatus.UPDATED:
                    update(vo);
                    break;
                case GridRowStatus.DELETED:
                    delete(vo);
                    break;
                default:
                    break;
            }
        }
    	return result;
    }
    
    /*
     * INSERT
     */
    private void insert(EI01010001U eI01010001U) {
        mapper.insert(eI01010001U);
    }
    
    /*
     * UPDATE
     */
    private void update(EI01010001U eI01010001U) {
        mapper.update(eI01010001U);
    }
    
    /*
     * DELETE
     */
    private void delete(EI01010001U eI01010001U) {
        mapper.delete(eI01010001U);
    }
    
}
