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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesPackageRuleContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.api.iservice.busi.INumberRulePackAttributeStrategyService;
import cn.estsh.i3plus.pojo.mes.model.GenSerialNoModel;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @Description : MES-包装条码
 * @Reference :
 * @Author : wangjie
 * @CreateDate 2024/10/28 16:47
 * @Modify:
 **/
@Component
public class YfaiPackageNumberRuleStrategyService implements INumberRulePackAttributeStrategyService {

// YFWHU|YFNSC901C241223042722|C901|401007574AAABN|9.0|EA|C901|20241223|
//YFWHU|YFNS   {Prefix}
//C901  {ORG}
//C  {SPILTRULE}
//241223  {DYNAMICRULE} 2位年月日
//042722 {SERIALNO} 6位
//|   {SPILTRULE}
//C901  {ORG}
//|   {SPILTRULE}
//401007574AAABN  {PARTNO}  -----  20250612调整 wms会扫描 packageOneCode, 这个里面没有零件号, 所以变更 此段内容 为 {DYNAMICRULE}
//|  {SPILTRULE}
//9.0 {DYNAMICRULE} 标包 一位小数
//|  {SPILTRULE}
//EA   {DYNAMICRULE} 单位
//|  {SPILTRULE}
//C901  {ORG}
//|  {SPILTRULE}
//20241223  {YEAR}{MONTH}{DAY}
//| {SPILTRULE}

    private final static String DR_QTY = "DR_QTY";
    private final static String DR_PARTNO = "DR_PARTNO";

    @Override
    public GenSerialNoModel execute(GenSerialNoModel genSerialNoModel) {
        Map<String, Object> dataMap = genSerialNoModel.getDataMap();
        MesPackageRuleContext packageRuleContext = !CollectionUtils.isEmpty(dataMap) ? (MesPackageRuleContext) dataMap.get(MesPackageRuleContext.class.getSimpleName()) : null;
        Double qty = (!CollectionUtils.isEmpty(dataMap) && dataMap.containsKey(MesPcnExtConstWords.QTY)) ? (Double) dataMap.get(MesPcnExtConstWords.QTY) : null;
        if (StringUtils.isEmpty(qty)) qty = null != packageRuleContext ? packageRuleContext.getPackSpecQty() : null;
        if (StringUtils.isEmpty(qty)) MesPcnException.throwBusiException("生成包装条码缺少必要参数[包装数量]");
        String unit = (!CollectionUtils.isEmpty(dataMap) && dataMap.containsKey(MesPcnExtConstWords.UNIT)) ? (String) dataMap.get(MesPcnExtConstWords.UNIT) : null;
        if (StringUtils.isEmpty(unit)) unit = null != packageRuleContext ? packageRuleContext.getUnit() : null;
        if (StringUtils.isEmpty(unit)) MesPcnException.throwBusiException("生成包装条码缺少必要参数[单位]");
        genSerialNoModel.setDynamicRule(
                new StringJoiner(MesPcnExtConstWords.COMMA)
                        .add(((new SimpleDateFormat(MesPcnExtConstWords.DATE_FORMAT_SHORT)).format(new Date())))
                        .add(DR_PARTNO)
                        //.add(String.format("%.1f", qty))
                        .add(DR_QTY)
                        .add(unit)
                        .toString()
        );
        return genSerialNoModel;
    }

    // 生成一维码,存到dataMap中
    // YFWHU|YFNSC901C241223042722|C901|401007574AAABN|9.0|EA|C901|20241223|
    // YFNSC901C241223042722 截取原始条码的 第7位到27位
    @Override
    public String transform(GenSerialNoModel genSerialNoModel, String serialNo) {
        String packageOneCode;
//        packageOneCode = serialNo.length() >= 27 ? serialNo.substring(6, 27) : serialNo;
        try {
            //截取前2个 "|" 之间的字符串
            Integer indexVerticalBar = serialNo.indexOf(MesPcnExtConstWords.VERTICAL_BAR) + 1;
            packageOneCode = serialNo.substring(indexVerticalBar, serialNo.indexOf(MesPcnExtConstWords.VERTICAL_BAR, indexVerticalBar));
        } catch (Exception e) {
            packageOneCode = serialNo;
        }

        Map<String, Object> dataMap = genSerialNoModel.getDataMap();
        MesPackageRuleContext packageRuleContext = !CollectionUtils.isEmpty(dataMap) ? (MesPackageRuleContext) dataMap.get(MesPackageRuleContext.class.getSimpleName()) : null;
        Double qty = (!CollectionUtils.isEmpty(dataMap) && dataMap.containsKey(MesPcnExtConstWords.QTY)) ? (Double) dataMap.get(MesPcnExtConstWords.QTY) : null;
        if (StringUtils.isEmpty(qty)) qty = null != packageRuleContext ? packageRuleContext.getPackSpecQty() : new Double(1);
        serialNo = serialNo.replace(DR_QTY, String.format("%.1f", qty));
        serialNo = serialNo.replace(DR_PARTNO, genSerialNoModel.getPartNo());
        genSerialNoModel.putDataMap(serialNo, packageOneCode);
        return serialNo;
    }
}