package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProductEncodeCfgExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPartExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MatchRuleCodeModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProductEncodeCfg;
import cn.estsh.i3plus.pojo.mes.repository.MesProductEncodeCfgRepository;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: wangjie
 * @CreateDate: 2019/8/23 1:16 PM
 * @Description: 编码规则
 **/

@Service
public class ProductEncodeCfgExtService implements IProductEncodeCfgExtService {

    @Autowired
    private MesProductEncodeCfgRepository productEncodeCfgRepository;

    @Override
    public String getRuleCodeByMatchType(String organizeCode, Integer codeType, MesPartExt partDb, String workCenterCode) {
        MatchRuleCodeModel model = new MatchRuleCodeModel();
        model.setOrganizeCode(organizeCode);
        model.setCodeType(codeType);
        switch (MesPcnExtEnumUtil.ENCODE_CODE_TYPE.getByValue(codeType)) {
            case SERIAL_SN :
                model.setMatchType(partDb.getProcessMatchType());
                break;
            case STANDARD_SERIAL_SN :
            case REWORK_SERIAL_SN :
            case ATTEMPT_SERIAL_SN :
                model.setMatchType(partDb.getProductMatchType());
                break;
            case PACK_SN :
                model.setMatchType(partDb.getPackageMatchType());
                break;
            case BIG_PACK_SN :
            case BIG_PACK_ATTEMPT_SN :
            case BIG_PACK_REWORK_SN :
                model.setMatchType(partDb.getBigPackageMatchType());
                break;
            default :
                break;
        }
        if (!StringUtils.isEmpty(model.getMatchType())) {
            switch (MesPcnExtEnumUtil.ENCODE_MATCH_TYPE.getByValue(model.getMatchType())) {
                case ORGANIZATION :
                    break;
                case PART :
                    model.setPartNo(partDb.getPartNo());
                    break;
                case WORK_CENTER :
                    model.setWorkCenterCode(workCenterCode);
                    break;
                case PPT_CODE :
                    model.setPptCode(partDb.getPptCode());
                    break;
                case CATEGORY_CODE1 :
                    model.setCategoryCode1(partDb.getCategoryCode1());
                    break;
                case CATEGORY_CODE2 :
                    model.setCategoryCode2(partDb.getCategoryCode2());
                    break;
                case CATEGORY_CODE3 :
                    model.setCategoryCode3(partDb.getCategoryCode3());
                    break;
                default :
                    break;
            }
        }
        return getRuleCodeByMatchType(model);
    }

    @Override
    public String getRuleCodeByMatchType(MatchRuleCodeModel matchRuleCodeModel) {
        if (null == matchRuleCodeModel) {
            throw ImppExceptionBuilder.newInstance()
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("获取编码规则未封装入参")
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .build();
        }
        if (StringUtils.isEmpty(matchRuleCodeModel.getOrganizeCode())) {
            throw ImppExceptionBuilder.newInstance()
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("获取编码规则入参缺少工厂")
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .build();
        }
        if (StringUtils.isEmpty(matchRuleCodeModel.getMatchType()) || null == MesPcnExtEnumUtil.ENCODE_MATCH_TYPE.getByValue(matchRuleCodeModel.getMatchType())) {
            throw ImppExceptionBuilder.newInstance()
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("获取编码规则入参匹配类型无效")
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .build();
        }
        if (StringUtils.isEmpty(matchRuleCodeModel.getCodeType()) || null == MesPcnExtEnumUtil.ENCODE_CODE_TYPE.getByValue(matchRuleCodeModel.getCodeType())) {
            throw ImppExceptionBuilder.newInstance()
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("获取编码规则入参编码类型无效")
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .build();
        }

        List<MesProductEncodeCfg> encodeCfgList = productEncodeCfgRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                        MesPcnExtConstWords.CODE_TYPE, MesPcnExtConstWords.MATCH_TYPE},
                new Object[]{matchRuleCodeModel.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        matchRuleCodeModel.getCodeType(), matchRuleCodeModel.getMatchType()});
        if (CollectionUtils.isEmpty(encodeCfgList)) {
            throw ImppExceptionBuilder.newInstance().setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("根据编码类型[%s]匹配类型[%s]未查询到产品编码配置",
                            MesPcnExtEnumUtil.ENCODE_CODE_TYPE.valueOfDescription(matchRuleCodeModel.getCodeType()),
                            MesPcnExtEnumUtil.ENCODE_MATCH_TYPE.valueOfDescription(matchRuleCodeModel.getMatchType()))
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .build();
        }

        switch (MesPcnExtEnumUtil.ENCODE_MATCH_TYPE.getByValue(matchRuleCodeModel.getMatchType())) {
            case ORGANIZATION :
                return getRuleCode(encodeCfgList, matchRuleCodeModel.getCodeType(), matchRuleCodeModel.getMatchType(), matchRuleCodeModel.getOrganizeCode());
            case PART :
                return getRuleCode(encodeCfgList, matchRuleCodeModel.getCodeType(), matchRuleCodeModel.getMatchType(), matchRuleCodeModel.getPartNo());
            case WORK_CENTER :
                return getRuleCode(encodeCfgList, matchRuleCodeModel.getCodeType(), matchRuleCodeModel.getMatchType(), matchRuleCodeModel.getWorkCenterCode());
            case PPT_CODE :
                return getRuleCode(encodeCfgList, matchRuleCodeModel.getCodeType(), matchRuleCodeModel.getMatchType(), matchRuleCodeModel.getPptCode());
            case CATEGORY_CODE1 :
                return getRuleCode(encodeCfgList, matchRuleCodeModel.getCodeType(), matchRuleCodeModel.getMatchType(), matchRuleCodeModel.getCategoryCode1());
            case CATEGORY_CODE2 :
                return getRuleCode(encodeCfgList, matchRuleCodeModel.getCodeType(), matchRuleCodeModel.getMatchType(), matchRuleCodeModel.getCategoryCode2());
            case CATEGORY_CODE3 :
                return getRuleCode(encodeCfgList, matchRuleCodeModel.getCodeType(), matchRuleCodeModel.getMatchType(), matchRuleCodeModel.getCategoryCode3());
            default :
                throw ImppExceptionBuilder.newInstance().setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("根据编码类型[%s]匹配类型[%s]未获取到匹配的编码规则代码",
                                MesPcnExtEnumUtil.ENCODE_CODE_TYPE.valueOfDescription(matchRuleCodeModel.getCodeType()),
                                MesPcnExtEnumUtil.ENCODE_MATCH_TYPE.valueOfDescription(matchRuleCodeModel.getMatchType()))
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .build();
        }
    }

    private String getRuleCode(List<MesProductEncodeCfg> encodeCfgList, Integer codeType, Integer matchType, String matchValue) {
        if (StringUtils.isEmpty(matchValue)) {
            throw ImppExceptionBuilder.newInstance().setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("获取编码规则入参缺少编码类型[%s]匹配类型[%s]的匹配值",
                            MesPcnExtEnumUtil.ENCODE_CODE_TYPE.valueOfDescription(codeType),
                            MesPcnExtEnumUtil.ENCODE_MATCH_TYPE.valueOfDescription(matchType))
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .build();
        }
        for (MesProductEncodeCfg encodeCfg : encodeCfgList) {
            if (null == encodeCfg || StringUtils.isEmpty(encodeCfg.getMatchValue()) || StringUtils.isEmpty(encodeCfg.getRuleCode())) {
                continue;
            }
            if (matchValue.equals(encodeCfg.getMatchValue())) {
                return encodeCfg.getRuleCode();
            }
        }
        throw ImppExceptionBuilder.newInstance().setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                .setErrorDetail("根据编码类型[%s]匹配类型[%s]未获取到匹配的编码规则代码",
                        MesPcnExtEnumUtil.ENCODE_CODE_TYPE.valueOfDescription(codeType),
                        MesPcnExtEnumUtil.ENCODE_MATCH_TYPE.valueOfDescription(matchType))
                .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                .build();
    }


}
