package com.cim.idm.utils;

import com.cim.idm.model.BarCodeDto;
import com.cim.idm.model.MaterialPackingCollectDto;
import com.cim.idm.wmspackage.materialpacking.MaterialPackingServiceProxy;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class BarCodeAnalyUtils   {

  //解析条码 470110856|1|C|75mm*1000m|SHBP01JT12023052400092|20230524|11|75 按照 | 解析每个字符
  public static BarCodeDto AnalyBarCode(String barCode) {
    BarCodeDto barCodeDto = new BarCodeDto();
    try {
      String[] barCodeArray = barCode.split("\\|");
      barCodeDto.setMaterialSpecName(barCodeArray[0]);
      barCodeDto.setVersion(barCodeArray[1]);
      barCodeDto.setPhase(barCodeArray[2]);
      barCodeDto.setSdkSpecName(barCodeArray[3]);
      barCodeDto.setCharge(barCodeArray[4]);
      barCodeDto.setMakedateChar(barCodeArray[5]);
      barCodeDto.setUnit(barCodeArray[6]);
      barCodeDto.setMaterialQuantity(barCodeArray[7]);
    } catch (Exception e) {
      log.error("解析条码失败", e);
    }
    return barCodeDto;
  }

  //根据料号和批次号获取条码信息
  public static MaterialPacking getBarCodeByMaterialPackingId(String materialSpecName, String charge) {
    log.info("根据料号和批次号获取条码信息 {} {}", materialSpecName,charge);
    String  condition="WHERE MATERIALSPECNAME =? AND CHARGE = ? ";
    String[] bindSet={materialSpecName,charge};
    //根据物料凭证找到所有的待冲销数据，更新库存状态为创建
    List<MaterialPacking> list = MaterialPackingServiceProxy.getMaterialPackingService().select(condition, bindSet);
    if (list.size() > 0) {
      return list.get(0);
    } else {
      return null;
    }
  }
}
