package sgc.portal.ei.ei0105.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.ei0105.dto.EI01050001USearch;
import sgc.portal.ei.ei0105.model.EI01050001U;
import sgc.portal.ei.ei0105.persistence.EI01050001UMapper;

@Service
public class EI01050001UService {
    private final EI01050001UMapper mapper;

    public EI01050001UService(EI01050001UMapper mapper) {
        this.mapper = mapper;
    }
    
    /*
     * SEARCH LIST
     */
    public List<EasyHashMap<String, Object>> getList(EI01050001USearch search) {
        return mapper.getList(search);
    }
    
    /*
     * SAVE LIST
     */
    @Transactional
    public Map<String, String> save(List<EI01050001U> eI01050001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (EI01050001U vo : eI01050001U) {
            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("BZPROF".equals(vo.getGbCd())) {  // 영업이익
                		update8(vo);
                	}
                	else {
                		throw new BusinessException("업무구분 코드 정보 오류 [" + vo.getGbCd() + "]");
                	}
                	break;    
                default:
                    break;
            }
        }
    	return result;
    }
    
    /*
     * UPDATE
     */
    private void update1(EI01050001U eI01050001U) {
        mapper.insertOrUpdate1(eI01050001U);
    }
    
    private void update2(EI01050001U eI01050001U) {
        mapper.insertOrUpdate2(eI01050001U);
    }
    
    private void update3(EI01050001U eI01050001U) {
        mapper.insertOrUpdate3(eI01050001U);
    }
    
    private void update4(EI01050001U eI01050001U) {
        mapper.insertOrUpdate4(eI01050001U);
    }
    
    private void update5(EI01050001U eI01050001U) {
        mapper.insertOrUpdate5(eI01050001U);
    }
    
    private void update6(EI01050001U eI01050001U) {
        mapper.insertOrUpdate6(eI01050001U);
    }
    
    private void update7(EI01050001U eI01050001U) {
        mapper.insertOrUpdate7(eI01050001U);
    }
    
    private void update8(EI01050001U eI01050001U) {
        mapper.insertOrUpdate8(eI01050001U);
    }
}
