Browse Source

库存新增数量脚本

24282 1 year ago
parent
commit
751f435f40
1 changed files with 109 additions and 0 deletions
  1. 109 0
      sd-starter/src/test/java/C3_SyncInventoryTest.java

+ 109 - 0
sd-starter/src/test/java/C3_SyncInventoryTest.java

@@ -0,0 +1,109 @@
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.annotation.ExcelProperty;
+import com.alibaba.excel.read.builder.ExcelReaderBuilder;
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
+import com.ruoyi.common.core.domain.BaseIdPo;
+import com.sd.SdApplication;
+import com.sd.business.entity.bom.po.BomSpec;
+import com.sd.business.entity.in.po.InOutStorageDetails;
+import com.sd.business.entity.inventory.po.Inventory;
+import com.sd.business.service.bom.BomSpecService;
+import com.sd.business.service.in.InOutStorageDetailsService;
+import com.sd.business.service.inventory.InventoryService;
+import com.sd.framework.util.excel.listener.DataListener;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.SneakyThrows;
+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 java.io.FileInputStream;
+import java.math.BigDecimal;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 库存新增数量
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = SdApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
+public class C3_SyncInventoryTest {
+
+    @Autowired
+    private BomSpecService bomSpecService;
+
+    @Autowired
+    private InventoryService inventoryService;
+
+    @Autowired
+    private InOutStorageDetailsService inOutStorageDetailsService;
+
+
+    /**
+     * excel修改库存
+     */
+    @SneakyThrows
+    @DSTransactional
+    @Test
+    public void test() {
+
+        DataListener<ExcelData> listener = new DataListener<>();
+
+        FileInputStream fileInputStream = new FileInputStream("E:\\入库明细表.xlsx");
+
+        ExcelReaderBuilder read = EasyExcel.read(fileInputStream, ExcelData.class, listener);
+        read.sheet(0).doRead();
+        List<ExcelData> list = listener.getDataList();
+
+        Map<String, Long> bomSpecMap = bomSpecService.mapKV(BomSpec::getCode, BaseIdPo::getId, null);
+
+        Map<Long, Inventory> inventoryMap = inventoryService.mapKEntity(
+                Inventory::getBomSpecId,
+                q -> q.eq(Inventory::getWarehouseId, 1684037244354052098L).eq(Inventory::getDepartmentId, 0L));
+
+
+        Map<Long, InOutStorageDetails> inOutStorageDetailsMap = inOutStorageDetailsService.mapKEntity(
+                InOutStorageDetails::getBomSpecId,
+                q -> q.eq(InOutStorageDetails::getWarehouseId, 1684037244354052098L).eq(InOutStorageDetails::getDepartmentId, 0L));
+
+        for (ExcelData excelData : list) {
+            String code = excelData.getCode();
+            Long bomSpecId = bomSpecMap.get(code);
+            if (bomSpecId == null) {
+                System.out.println("没有找到品号:" + code);
+                continue;
+            }
+
+            Inventory inventory = inventoryMap.get(bomSpecId);
+            if (inventory == null) {
+                System.out.println("没有库存:" + code);
+                continue;
+            }
+
+            inventory.setQuantity(inventory.getQuantity().add(excelData.getQuantity()));
+
+            InOutStorageDetails inOutStorageDetails = inOutStorageDetailsMap.get(bomSpecId);
+            inOutStorageDetails.setQuantity(inOutStorageDetails.getQuantity().add(excelData.getQuantity()));
+
+        }
+
+        inventoryService.updateBatchById(inventoryMap.values());
+        inOutStorageDetailsService.updateBatchById(inOutStorageDetailsMap.values());
+    }
+
+    @Getter
+    @Setter
+    public static final class ExcelData {
+
+        @ExcelProperty("品号")
+        private String code;
+
+        @ExcelProperty("数量")
+        private BigDecimal quantity;
+
+    }
+
+}