|
@@ -0,0 +1,202 @@
|
|
|
|
+package com.ruoyi.admin.test.base;
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
+import cn.hutool.core.comparator.CompareUtil;
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;
|
|
|
|
+import com.ruoyi.RuoYiApplication;
|
|
|
|
+import com.ruoyi.ams.inv.domain.InvLotLocId;
|
|
|
|
+import com.ruoyi.ams.inv.service.IInvLotAttService;
|
|
|
|
+import com.ruoyi.ams.inv.service.IInvLotLocIdService;
|
|
|
|
+import com.ruoyi.base.domain.BaseLocationInfo;
|
|
|
|
+import com.ruoyi.base.domain.BaseSku;
|
|
|
|
+import com.ruoyi.base.service.IBaseLocationInfoService;
|
|
|
|
+import com.ruoyi.base.service.IBaseSkuService;
|
|
|
|
+import com.ruoyi.common.constant.Constants;
|
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
|
+import io.netty.util.internal.StringUtil;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.compress.utils.Lists;
|
|
|
|
+import org.junit.Test;
|
|
|
|
+import org.junit.runner.RunWith;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
+import org.springframework.util.Assert;
|
|
|
|
+
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.regex.Matcher;
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Created by IntelliJ IDEA.
|
|
|
|
+ * User: andy.qu
|
|
|
|
+ * Date: 2022/2/9
|
|
|
|
+ */
|
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
|
+@SpringBootTest(classes = RuoYiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
|
|
|
+@Slf4j
|
|
|
|
+public class ProcessLocationTest {
|
|
|
|
+
|
|
|
|
+ private static final String FREE = "00";
|
|
|
|
+ @Autowired
|
|
|
|
+ private IBaseLocationInfoService baseLocationInfoService;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private IInvLotAttService invLotAttService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private IInvLotLocIdService invLotLocIdService;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private IBaseSkuService baseSkuService;
|
|
|
|
+
|
|
|
|
+ //仓库ID的默认值
|
|
|
|
+ private static final Long WAREHOUSE_ID = 1L;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 选矿项目调整库存专用脚本
|
|
|
|
+ *
|
|
|
|
+ * @author ChenYang
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void handleLoacationInventory() throws Exception {
|
|
|
|
+
|
|
|
|
+ //1.获取物料号(sku)和物料类型(type)的映射
|
|
|
|
+ List<BaseSku> skuList = baseSkuService.querySkuList();
|
|
|
|
+ Map<String, String> skuTypeMapping = skuList.stream().filter(item -> StringUtils.isNotBlank(item.getSku())).collect(Collectors.toMap(BaseSku::getSku, BaseSku::getSkuType));
|
|
|
|
+ Assert.isTrue(CollectionUtil.isNotEmpty(skuTypeMapping), "skuTypeMapping is empty");
|
|
|
|
+
|
|
|
|
+ //2.到Excel中查询数据
|
|
|
|
+ //TODO
|
|
|
|
+ /*MultipartFile multipartFile = null;
|
|
|
|
+ ExcelUtil<ExcelLocLotModel> locLotModelExcelUtil = new ExcelUtil<ExcelLocLotModel>(ExcelLocLotModel.class);
|
|
|
|
+ List<ExcelLocLotModel> ExcelLocLotList = locLotModelExcelUtil.importExcel(multipartFile.getInputStream());*/
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ String LocLotJsonInfo = "[{\"locationNo\":\"A-01-04\",\"bindSku\":\"EMP_TRAY_BAG\",\"quantity\":2},{\"locationNo\":\"A-01-08\"},{\"locationNo\":\"A-05-03\",\"bindSku\":\"EMP_TRAY_BUC\"},{\"locationNo\":\"A-02-08\",\"bindSku\":\"(NH4)2SO4\",\"quantity\":10}]";
|
|
|
|
+ List<ExcelLocLotModel> ExcelLocLotList = JSONObject.parseArray(LocLotJsonInfo, ExcelLocLotModel.class);
|
|
|
|
+ List<ExcelLocLotModel> errorLocLotList = Lists.newArrayList();
|
|
|
|
+ for(ExcelLocLotModel locLotModel : ExcelLocLotList){
|
|
|
|
+ try {
|
|
|
|
+ String count = locLotModel.getQuantity();
|
|
|
|
+ BigDecimal quantity = StringUtils.isNotBlank(count) ? new BigDecimal(count) : null;
|
|
|
|
+ //所需的sku,库位号locationNo以及数量即可操作、
|
|
|
|
+ String locationNo = handleLocationNo(locLotModel.getLocationNo());
|
|
|
|
+ redressInventory(locationNo, locLotModel.getBindSku(), quantity, skuTypeMapping);
|
|
|
|
+ }catch (RuntimeException ex){
|
|
|
|
+ log.warn("ex is {}", ex);
|
|
|
|
+ errorLocLotList.add(locLotModel);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ System.err.println(JSONObject.toJSONString(errorLocLotList));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 重置一个库的库位库存信息
|
|
|
|
+ * @param locationNo 库位号
|
|
|
|
+ * @param bindSku
|
|
|
|
+ * @param quantity
|
|
|
|
+ * @param skuTypeMapping
|
|
|
|
+ */
|
|
|
|
+ private void redressInventory(String locationNo, String bindSku, BigDecimal quantity, Map<String, String> skuTypeMapping) {
|
|
|
|
+ //1.通过库位号获得库位的ID
|
|
|
|
+ BaseLocationInfo baseLocationInfo = baseLocationInfoService.selectBaseLocationInfoByIdOrNo(locationNo, WAREHOUSE_ID);
|
|
|
|
+ if (ObjectUtil.isNull(baseLocationInfo)) return;
|
|
|
|
+ if(StringUtils.isBlank(bindSku) && ObjectUtil.isNotNull(quantity)) return;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //2.1 如果当前库位是空的,或者库位数量是空的,则删除inv_lot_loc_id中的库存数据并重置库位数据
|
|
|
|
+ if((ObjectUtil.isNull(quantity) || CompareUtil.compare(quantity, BigDecimal.ZERO)<=0)){
|
|
|
|
+ invLotLocIdService.deleteInvLotLocList(baseLocationInfo.getId());
|
|
|
|
+ reSetLocationInfo(baseLocationInfo, bindSku, Constants.YES, skuTypeMapping);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(StringUtils.isNotBlank(bindSku)
|
|
|
|
+ && ObjectUtil.isNotNull(quantity)
|
|
|
|
+ && CompareUtil.compare(quantity, BigDecimal.ZERO) > 0){
|
|
|
|
+ //查出原先的库存情况,如果当前库存SKU不符合,则生成新的批次记录
|
|
|
|
+ List<InvLotLocId> invLotLocList = invLotLocIdService.getInvLotLocList(baseLocationInfo.getId());
|
|
|
|
+ if(CollectionUtil.isNotEmpty(invLotLocList)){
|
|
|
|
+ //此时数据是错的都要重新生成库存
|
|
|
|
+ List<String> skuList = invLotLocList.stream().map(item -> item.getSku()).collect(Collectors.toList());
|
|
|
|
+ if(CollectionUtil.isEmpty(skuList) || skuList.size() > 1 || !StringUtils.equals(skuList.get(0), bindSku)){
|
|
|
|
+ //生成新的批次记录
|
|
|
|
+ String lotnum = invLotAttService.insertInvLotAtt(bindSku);
|
|
|
|
+ //删除原先的库存记录,生成新的库存记录
|
|
|
|
+ invLotLocIdService.deleteInvLotLocList(baseLocationInfo.getId());
|
|
|
|
+ invLotLocIdService.insertInvLotLocId(baseLocationInfo.getId(), lotnum, bindSku, quantity);
|
|
|
|
+ //重置库位数据
|
|
|
|
+ reSetLocationInfo(baseLocationInfo, bindSku, Constants.NO, skuTypeMapping);
|
|
|
|
+ return;
|
|
|
|
+ } else {
|
|
|
|
+ //此时只有数量是错的,那么update一下数量就好
|
|
|
|
+ InvLotLocId invLotLocId = invLotLocList.get(0);
|
|
|
|
+ invLotLocId.setQty(quantity);
|
|
|
|
+ //TODO 其他关于数量的字段要改嘛
|
|
|
|
+ invLotLocId.setQtyEach(quantity);
|
|
|
|
+ invLotLocIdService.updateInvLotLocId(invLotLocId);
|
|
|
|
+ //重置库存数据
|
|
|
|
+ reSetLocationInfo(baseLocationInfo, bindSku, Constants.NO, skuTypeMapping);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 重置库位表字段
|
|
|
|
+ * @param baseLocationInfo
|
|
|
|
+ * @param bindSku 物料号
|
|
|
|
+ * @param isEmpty
|
|
|
|
+ * @param skuTypeMapping
|
|
|
|
+ */
|
|
|
|
+ private Boolean reSetLocationInfo(BaseLocationInfo baseLocationInfo, String bindSku, String isEmpty, Map<String, String> skuTypeMapping) {
|
|
|
|
+ //重置当前的库位信息
|
|
|
|
+ baseLocationInfo.setIsEmpty(isEmpty);
|
|
|
|
+ baseLocationInfo.setStockStatus(FREE);
|
|
|
|
+ baseLocationInfo.setUserdefine10(StringUtils.isNotBlank(bindSku) ? bindSku : null);
|
|
|
|
+ baseLocationInfo.setBindSku(StringUtils.isNotBlank(bindSku) ? skuTypeMapping.get(bindSku) : null );
|
|
|
|
+ return baseLocationInfoService.reSetLocationInfo(baseLocationInfo);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /*public static void main(String[] args) {
|
|
|
|
+ System.out.println(handleLocationNo("A\t-02\t-08"));
|
|
|
|
+ System.out.println(handleLocationNo(" A-02-08"));
|
|
|
|
+ System.out.println(handleLocationNo(" -02-08"));
|
|
|
|
+ System.out.println(handleLocationNo("Out-02-cache"));
|
|
|
|
+ System.out.println(handleLocationNo("STAGE01"));
|
|
|
|
+ }*/
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private static String handleLocationNo(String locationNo) {
|
|
|
|
+ if (StringUtils.isBlank(locationNo)) return StringUtil.EMPTY_STRING;
|
|
|
|
+ Pattern pattern = Pattern.compile("\\s*|\t|\r|\n");
|
|
|
|
+ Matcher matcher = pattern.matcher(locationNo);
|
|
|
|
+ String lotNo = matcher.replaceAll(StringUtil.EMPTY_STRING);
|
|
|
|
+ String start = StringUtils.split(lotNo, "-")[0];
|
|
|
|
+ if(start.length() == 1 && StringUtils.isAllUpperCase(start)){
|
|
|
|
+ lotNo = StringUtils.replace(lotNo, "-", "\t-");
|
|
|
|
+ }
|
|
|
|
+ return lotNo;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|