package sgc.portal.ei.ei0104.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 lombok.extern.slf4j.Slf4j;
import sgc.portal.base.exception.BusinessException;
import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.websquare.GridRowStatus;
import sgc.portal.ei.ei0104.dto.EI01040001USearch;
import sgc.portal.ei.ei0104.model.EI01040001U;
import sgc.portal.ei.ei0104.persistence.EI01040001UMapper;

@Slf4j
@Service
public class EI01040001UService {
    private final EI01040001UMapper mapper;

    public EI01040001UService(EI01040001UMapper mapper) {
        this.mapper = mapper;
    }
    
    /*
     * SEARCH LIST
     */
    public List<EasyHashMap<String, Object>> getList(EI01040001USearch search) {
    	return mapper.getList(search);
    }
    
    /*
     * SAVE LIST
     */
    @Transactional
    public Map<String, String> save(List<EI01040001U> eI01040001U) {
    	
    	Map<String,String> result = new HashMap<>();
    	
    	for (EI01040001U vo : eI01040001U) {
            switch (vo.getRowStatus()) {
                
                case GridRowStatus.UPDATED:
                	if("SLAMT".equals(vo.getDtlGbCd())) {               // 매출액 매출액
                		update1(vo);
                	}
                	else if("SLPCOST".equals(vo.getDtlGbCd())) {        // 매출원가 매출원가
                		update2(vo);
                	}
                	else if("SLPCOST_MTRCT".equals(vo.getDtlGbCd())) {  // 매출원가 재료비등
                		update3(vo);
                	}
                	else if("SLPCOST_RPA".equals(vo.getDtlGbCd())) {    // 매출원가 수선비
                		update4(vo);
                	}
                	else if("SLPCOST_GHGE".equals(vo.getDtlGbCd())) {   // 매출원가 온실가스배출
                		update5(vo);
                	}
                	else if("SLPCOST_DPRE".equals(vo.getDtlGbCd())) {   // 매출원가 감가상각비
                		update6(vo);
                	}
                	else if("SLPCOST_ETC".equals(vo.getDtlGbCd())) {    // 매출원가 기타
                		update7(vo);
                	}
                	else if("SLPROF".equals(vo.getDtlGbCd())) {         // 매출총이익
                		update8(vo);
                	}
                	else if("SMCOST".equals(vo.getDtlGbCd())) {         // 판매관리비
                		update9(vo);
                	}
                	else if("SMCOST_LAB".equals(vo.getDtlGbCd())) {     // 판매관리비 인건비
                		update10(vo);
                	}
                	else if("SMCOST_DPRE".equals(vo.getDtlGbCd())) {    // 판매관리비 감가상각
                		update11(vo);
                	}
                	else if("SMCOST_ETC".equals(vo.getDtlGbCd())) {     // 판매관리비 기타판관비
                		update12(vo);
                	}
                	else if("BZPROF".equals(vo.getDtlGbCd())) {         // 영업이익
                		update13(vo);
                	}
                	else {
                		throw new BusinessException("업무구분 코드 정보 오류 [" + vo.getDtlGbCd() + "]");
                	}
                	break;    
                default:
                    break;
            }
        }
    	return result;
    }
    
    /*
     * UPDATE
     */
    private void update1(EI01040001U eI01040001U) {
        mapper.insertOrUpdate1(eI01040001U);
    }
    
    private void update2(EI01040001U eI01040001U) {
        mapper.insertOrUpdate2(eI01040001U);
    }
    
    private void update3(EI01040001U eI01040001U) {
        mapper.insertOrUpdate3(eI01040001U);
    }
    
    private void update4(EI01040001U eI01040001U) {
        mapper.insertOrUpdate4(eI01040001U);
    }
    
    private void update5(EI01040001U eI01040001U) {
        mapper.insertOrUpdate5(eI01040001U);
    }
    
    private void update6(EI01040001U eI01040001U) {
        mapper.insertOrUpdate6(eI01040001U);
    }
    
    private void update7(EI01040001U eI01040001U) {
        mapper.insertOrUpdate7(eI01040001U);
    }
    
    private void update8(EI01040001U eI01040001U) {
        mapper.insertOrUpdate8(eI01040001U);
    }
    
    private void update9(EI01040001U eI01040001U) {
        mapper.insertOrUpdate9(eI01040001U);
    }
    
    private void update10(EI01040001U eI01040001U) {
        mapper.insertOrUpdate10(eI01040001U);
    }
    
    private void update11(EI01040001U eI01040001U) {
        mapper.insertOrUpdate11(eI01040001U);
    }
    
    private void update12(EI01040001U eI01040001U) {
        mapper.insertOrUpdate12(eI01040001U);
    }
    
    private void update13(EI01040001U eI01040001U) {
        mapper.insertOrUpdate13(eI01040001U);
    }
    
}
