package sgc.portal.ei.ei0103.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.ei0103.dto.EI01030001USearch;
import sgc.portal.ei.ei0103.model.EI01030001U;
import sgc.portal.ei.ei0103.persistence.EI01030001UMapper;

@Service
public class EI01030001UService {
    private final EI01030001UMapper mapper;

    public EI01030001UService(EI01030001UMapper mapper) {
        this.mapper = mapper;
    }
    
    /*
     * SEARCH LIST
     */
    public List<EasyHashMap<String, Object>> getList(EI01030001USearch search) {
        return mapper.getList(search);
    }
    
    /*
     * SAVE LIST
     */
    @Transactional
    public Map<String, String> save(List<EI01030001U> eI01030001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (EI01030001U vo : eI01030001U) {
            switch (vo.getRowStatus()) {
                
                case GridRowStatus.UPDATED:
                	if("SLAMT".equals(vo.getDtlGbCd())) {            // 매출액 매출액
                		update1(vo);
                	}
                	else if("SLPCOST".equals(vo.getDtlGbCd())) {     // 매출원가 매출원가
                		update2(vo);
                	}
                	else if("SLPROF".equals(vo.getDtlGbCd())) {      // 매출총이익 매출총이익
                		update3(vo);
                	}
                	else if("SMCOST".equals(vo.getDtlGbCd())) {      // 판매관리비 판매관리비
                		update4(vo);
                	}
                	else if("SMCOST_BZ".equals(vo.getDtlGbCd())) {   // 판매관리비 사업부
                		update5(vo);
                	}
                	else if("SMCOST_SPT".equals(vo.getDtlGbCd())) {  // 판매관리비 지원부서
                		update6(vo);
                	}
                	else if("SMCOST_TMP".equals(vo.getDtlGbCd())) {  // 판매관리비 일시적비용
                		update7(vo);
                	}
                	else if("SMCOST_ONSF".equals(vo.getDtlGbCd())) { // 판매관리비 자체사업비용
                		update8(vo);
                	}
                	else if("BZPROF".equals(vo.getDtlGbCd())) {      // 영업이익 영업이익
                		update9(vo);
                	}
                	else {
                		throw new BusinessException("업무구분 코드 정보 오류 [" + vo.getDtlGbCd() + "]");
                	}
                	break;    
                default:
                    break;
            }
        }
    	return result;
    }
    
    /*
     * UPDATE
     */
    private void update1(EI01030001U eI01030001U) {
        mapper.insertOrUpdate1(eI01030001U);
    }
    
    private void update2(EI01030001U eI01030001U) {
        mapper.insertOrUpdate2(eI01030001U);
    }
    
    private void update3(EI01030001U eI01030001U) {
        mapper.insertOrUpdate3(eI01030001U);
    }
    
    private void update4(EI01030001U eI01030001U) {
        mapper.insertOrUpdate4(eI01030001U);
    }
    
    private void update5(EI01030001U eI01030001U) {
        mapper.insertOrUpdate5(eI01030001U);
    }
    
    private void update6(EI01030001U eI01030001U) {
        mapper.insertOrUpdate6(eI01030001U);
    }
    
    private void update7(EI01030001U eI01030001U) {
        mapper.insertOrUpdate7(eI01030001U);
    }
    
    private void update8(EI01030001U eI01030001U) {
        mapper.insertOrUpdate8(eI01030001U);
    }
    
    private void update9(EI01030001U eI01030001U) {
        mapper.insertOrUpdate9(eI01030001U);
    }
    
}
