package sgc.portal.cs.cs0502.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.cs.cs0502.persistence.CS05020001UMapper;
import sgc.portal.cs.cs0502.dto.CS05020001USearch;
import sgc.portal.cs.cs0502.model.CS05020001U;

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

@Service
public class CS05020001UService {
    
	private final CS05020001UMapper mapper;

    public CS05020001UService(CS05020001UMapper mapper) {
        this.mapper = mapper;
    }
    
    //카테고리 트리 조회
    public List<EasyHashMap<String, Object>> selectTcsDocTree(CS05020001USearch search) {
        return mapper.selectTcsDocTree(search);
    }
    
    //카테고리 내역 조회
    public List<EasyHashMap<String, Object>> selectTcsDocGrd(CS05020001USearch search) {
        return mapper.selectTcsDocGrd(search);
    }
    
    //카테고리 내역 조회
    public List<EasyHashMap<String, Object>> selectTcsDocGrdY(CS05020001USearch search) {
        return mapper.selectTcsDocGrdY(search);
    }
    
    //카테고리 내역 수정
    @Transactional
    public Map<String, String> saveTcsDoc(List<CS05020001U> CS05020001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
        for (CS05020001U cS05020001U : CS05020001U) {
        	
            switch (cS05020001U.getRowStatus()) {
            
                case GridRowStatus.CREATED:
                	insertTcsDoc(cS05020001U);
                    break;
                    
                case GridRowStatus.UPDATED:
                	updateTcsDoc(cS05020001U);
                    break;
                    
                case GridRowStatus.DELETED:
                    deleteTcsDoc(cS05020001U);
                    break;
                default:
                    break;
            }
        }
        return result;
    }

    private void insertTcsDoc(CS05020001U cS05020001U) {
        mapper.insertTcsDoc(cS05020001U);
    }

    private void updateTcsDoc(CS05020001U cS05020001U) {
        mapper.updateTcsDoc(cS05020001U);
    }

    private void deleteTcsDoc(CS05020001U cS05020001U) {
    	mapper.deleteTcsDoc(cS05020001U);
    }
}