package com.sw.mes.production.service.impl;

import com.sw.mes.base.global.GlobalSwitch;
import com.sw.mes.base.global.GlobalSwitchNamespace;
import com.sw.mes.core.exception.BusinessException;
import com.sw.mes.production.dto.DecompositionSchemeParam;
import com.sw.mes.production.dto.FormulaSchemeData;
import com.sw.mes.production.service.FormulaSchemeOutService;
import com.sw.mes.production.service.FormulaSchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zhaowenjie
 */
@Service
public class FormulaSchemeOutImplServiceImpl implements FormulaSchemeOutService {

    @Autowired
    private FormulaSchemeService formulaSchemeService;

    @Override
    public FormulaSchemeData decompositionScheme(DecompositionSchemeParam decompositionSchemeParam) {
        FormulaSchemeData schemeData = formulaSchemeService.decompositionScheme(decompositionSchemeParam);
        Boolean checkR1 = this.checkSplitMaterialClass(schemeData);
        if (!checkR1) throw new BusinessException("首尾桶内不能包含颗粒硅，请联系工艺更换配方");
        return schemeData;
    }

    @Override
    public Boolean checkSplitMaterialClass(FormulaSchemeData schemeData) {
        // 1/2桶时，物料分类不能出现颗粒硅
        if (GlobalSwitch.enable(GlobalSwitchNamespace.NAMESPACE_YB_CHECK, "checkSplitMaterialClass", false)
                && !ObjectUtils.isEmpty(schemeData)){
            for (FormulaSchemeData.FormulaSchemeDataDetail dataDetail : schemeData.getDetail()) {
                if (dataDetail.getIndex() == 1 || dataDetail.getIndex() == schemeData.getDetail().size()){
                    for (FormulaSchemeData.LoadingDetail loadingDetail : dataDetail.getLoadingDetails()) {
                        if (loadingDetail.getMaterialClassName().contains("颗粒硅")){
                            return false;
                        }
                    }
                }
            }
        }

        return true;
    }

//    @Override
//    public FormulaSchemeData decompositionScheme(String schemeCode, String schemeVersion, String weightSchemeCode, List<FormulaSchemeData.LoadingDetail> loadingDetails, BigDecimal weight) {
//        return formulaSchemeService.decompositionScheme(schemeCode, schemeVersion, weightSchemeCode, loadingDetails, weight);
//    }
}
