package sgc.portal.ei.ei0106.service;

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

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sgc.portal.base.exception.BusinessException;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.websquare.GridRowStatus;
import sgc.portal.ei.ei0106.dto.EI01060001USearch;
import sgc.portal.ei.ei0106.model.EI01060001U;
import sgc.portal.ei.ei0106.persistence.EI01060001UMapper;

@Service
public class EI01060001UService {
    private final EI01060001UMapper mapper;

    public EI01060001UService(EI01060001UMapper mapper) {
        this.mapper = mapper;
    }
    
    /*
     * SEARCH LIST
     */
    public List<EasyHashMap<String, Object>> getList(EI01060001USearch search) {
        return mapper.getList(search);
    }
    
    public List<EasyHashMap<String, Object>> pivotList(EI01060001USearch search) {
        return mapper.pivotList(search);
    }
    
    
    /*
     * SAVE LIST
     */
    @Transactional
    public Map<String, String> save(List<EI01060001U> eI01060001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (EI01060001U vo : eI01060001U) {
            switch (vo.getRowStatus()) {
                
                case GridRowStatus.UPDATED:
                	if("ASSET_CURR".equals(vo.getGbCd())) {        // 유동자산
                		update1(vo);
                	}
                	else if("ASSET_NCURR".equals(vo.getGbCd())) {  // 비유동자산
                		update2(vo);
                	}
                	else if("ASSET_CASH".equals(vo.getGbCd())) {   // CASH
                		update3(vo);
                	}
                	else if("LIAB_CURR".equals(vo.getGbCd())) {    // 유동부채
                		update4(vo);
                	}
                	else if("LIAB_NCURR".equals(vo.getGbCd())) {   // 비유동부채
                		update5(vo);
                	}
                	else if("LIAB_BR".equals(vo.getGbCd())) {      // 차입금
                		update6(vo);
                	}
                	else if("INTE_COST".equals(vo.getGbCd())) {    // 이자비용
                		update7(vo);
                	}
                	else if("NINCM".equals(vo.getGbCd())) {        // 당기순이익
                		update8(vo);
                	}
                	else {
                		throw new BusinessException("업무구분 코드 정보 오류 [" + vo.getGbCd() + "]");
                	}
                	break;    
                default:
                    break;
            }
        }
    	return result;
    }
    
    /*
     * UPDATE
     */
    private void update1(EI01060001U eI01060001U) {
        mapper.update1(eI01060001U);
    }
    
    private void update2(EI01060001U eI01060001U) {
        mapper.update2(eI01060001U);
    }
    
    private void update3(EI01060001U eI01060001U) {
        mapper.update3(eI01060001U);
    }
    
    private void update4(EI01060001U eI01060001U) {
        mapper.update4(eI01060001U);
    }
    
    private void update5(EI01060001U eI01060001U) {
        mapper.update5(eI01060001U);
    }
    
    private void update6(EI01060001U eI01060001U) {
        mapper.update6(eI01060001U);
    }
    
    private void update7(EI01060001U eI01060001U) {
        mapper.update7(eI01060001U);
    }
    
    private void update8(EI01060001U eI01060001U) {
        mapper.update8(eI01060001U);
    }
}
