package sgc.portal.sample.sample0101.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.sample.sample0101.dto.SAMPLE01010001USearch;
import sgc.portal.sample.sample0101.model.SAMPLE01010001U;
import sgc.portal.sample.sample0101.persistence.SAMPLE01010001UMapper;

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

@Service
public class SAMPLE01010001UService {
    private final SAMPLE01010001UMapper mapper;

    public SAMPLE01010001UService(SAMPLE01010001UMapper mapper) {
        this.mapper = mapper;
    }
    
    /*
     * 조회 LIST
     */
    public List<EasyHashMap<String, Object>> getList(SAMPLE01010001USearch search) {
        return mapper.getList(search);
    }
    
    /*
     * SAVE LIST
     */
    @Transactional
    public Map<String, String> save(List<SAMPLE01010001U> sAMPLE01010001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (SAMPLE01010001U vo : sAMPLE01010001U) {
            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(SAMPLE01010001U sAMPLE01010001U) {
        mapper.insert(sAMPLE01010001U);
    }
    
    /*
     * UPDATE
     */
    private void update(SAMPLE01010001U sAMPLE01010001U) {
        mapper.update(sAMPLE01010001U);
    }
    
    /*
     * DELETE
     */
    private void delete(SAMPLE01010001U sAMPLE01010001U) {
        mapper.delete(sAMPLE01010001U);
    }
    
    /*
     * draft
     */
    @Transactional
    public Map<String, String> draft(SAMPLE01010001U sAMPLE01010001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	mapper.updateDraft(sAMPLE01010001U);
    	
    	return result;
    }
    
}
