|
|
@@ -2,7 +2,10 @@ package com.github.zuihou.business.edgeLibrary.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.github.zuihou.base.R;
|
|
|
import com.github.zuihou.base.service.SuperServiceImpl;
|
|
|
@@ -29,6 +32,7 @@ import com.github.zuihou.business.productionResourceCenter.dao.ToolDisplayMapper
|
|
|
import com.github.zuihou.business.productionResourceCenter.entity.ToolDisplay;
|
|
|
import com.github.zuihou.business.util.CommonUtil;
|
|
|
import com.github.zuihou.business.util.MsgUtil;
|
|
|
+import com.github.zuihou.business.util.WmsApiClientUtil;
|
|
|
import com.github.zuihou.common.constant.BizConstant;
|
|
|
import com.github.zuihou.common.constant.CodeRuleModule;
|
|
|
import com.github.zuihou.common.constant.DictionaryKey;
|
|
|
@@ -37,6 +41,7 @@ import com.github.zuihou.database.mybatis.auth.DataScope;
|
|
|
import com.github.zuihou.database.mybatis.conditions.Wraps;
|
|
|
import com.github.zuihou.database.mybatis.conditions.query.LbqWrapper;
|
|
|
import com.github.zuihou.database.mybatis.conditions.query.QueryWrap;
|
|
|
+import com.github.zuihou.exception.BizException;
|
|
|
import com.github.zuihou.injection.annonation.InjectionResult;
|
|
|
import com.github.zuihou.tenant.service.CodeRuleService;
|
|
|
import com.github.zuihou.tenant.vo.DeviceResourceDetailVo;
|
|
|
@@ -45,9 +50,12 @@ import com.google.common.collect.Maps;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.compress.utils.Lists;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
|
@@ -92,6 +100,8 @@ public class StockInfoServiceImpl extends SuperServiceImpl<StockInfoMapper, Stoc
|
|
|
@Autowired
|
|
|
private TrayService trayService;
|
|
|
|
|
|
+ @Value("${dk.url}")
|
|
|
+ private String dkUrl;
|
|
|
|
|
|
@Autowired
|
|
|
private MsgUtil msgUtil;
|
|
|
@@ -99,6 +109,8 @@ public class StockInfoServiceImpl extends SuperServiceImpl<StockInfoMapper, Stoc
|
|
|
|
|
|
@Autowired
|
|
|
private ToolDisplayMapper toolDisplayMapper;
|
|
|
+ @Autowired
|
|
|
+ private WmsApiClientUtil wmsApiClientUtil;
|
|
|
|
|
|
@Override
|
|
|
// 启用属性自动注入
|
|
|
@@ -1028,6 +1040,21 @@ public class StockInfoServiceImpl extends SuperServiceImpl<StockInfoMapper, Stoc
|
|
|
|
|
|
@Override
|
|
|
public Map toolListDisplay(Map map) {
|
|
|
+ JSONArray dataArray = new JSONArray();
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ String responseStr = restTemplate.getForObject(dkUrl, String.class);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(responseStr);
|
|
|
+ dataArray = jsonObject.getJSONArray("data");
|
|
|
+ Integer code = jsonObject.getInteger("code");
|
|
|
+ if (code != 0){
|
|
|
+ throw new BizException("获取刀库信息失败");
|
|
|
+ }
|
|
|
+ List<ToolDisplay> toolList = new ArrayList<>();
|
|
|
+ if (!dataArray.isEmpty()) {
|
|
|
+ toolList = dataArray.toJavaList(ToolDisplay.class);
|
|
|
+ }
|
|
|
+ syncToolDataToDb(toolList);
|
|
|
+
|
|
|
Map returnMap = new HashMap();
|
|
|
|
|
|
String orgIdsStr = CommonUtil.getOrgIdsStr();
|
|
|
@@ -1038,25 +1065,114 @@ public class StockInfoServiceImpl extends SuperServiceImpl<StockInfoMapper, Stoc
|
|
|
List<Map> listMap = new ArrayList<>();
|
|
|
|
|
|
//获取所有刀库库位
|
|
|
- List<ToolDisplay> toolList = toolDisplayMapper.selectList(null);
|
|
|
+ List<ToolDisplay> toolLists = toolDisplayMapper.selectList(null);
|
|
|
|
|
|
//10元素为一组
|
|
|
- Map<Integer, List<ToolDisplay>> groupedMap = IntStream.range(0, toolList.size())
|
|
|
+ Map<Integer, List<ToolDisplay>> groupedMap = IntStream.range(0, toolLists.size())
|
|
|
.boxed()
|
|
|
.collect(Collectors.groupingBy(
|
|
|
i -> i / 10, // 分组键:索引除以10(整数除法),每10个一组
|
|
|
LinkedHashMap::new, // 使用 LinkedHashMap 保持顺序
|
|
|
Collectors.mapping(
|
|
|
- toolList::get, // 把索引 i 映射为 toolList.get(i)
|
|
|
+ toolLists::get, // 把索引 i 映射为 toolList.get(i)
|
|
|
Collectors.toList()
|
|
|
)
|
|
|
));
|
|
|
|
|
|
|
|
|
- returnMap.put("shelvesCount", toolList.size()); //仓库个数
|
|
|
+ returnMap.put("shelvesCount", toolLists.size()); //仓库个数
|
|
|
returnMap.put("shelvesTreeList", groupedMap);
|
|
|
return returnMap;
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 同步刀库数据到数据库(存在则更新,不存在则新增)
|
|
|
+ */
|
|
|
+ private void syncToolDataToDb(List<ToolDisplay> toolListFromApi) {
|
|
|
+ // 1. 获取接口返回的所有库位编号
|
|
|
+ Set<String> apiAllocationNoSet = new HashSet<>();
|
|
|
+ if (!CollectionUtils.isEmpty(toolListFromApi)) {
|
|
|
+ apiAllocationNoSet = toolListFromApi.stream()
|
|
|
+ .map(ToolDisplay::getAllocationNo)
|
|
|
+ .filter(StringUtils::isNotEmpty) // 过滤空编号
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 查询数据库中所有记录
|
|
|
+ List<ToolDisplay> allDbTools = toolDisplayMapper.selectList(null);
|
|
|
+ if (CollectionUtils.isEmpty(allDbTools)) {
|
|
|
+ // 数据库中没有任何记录,直接批量插入接口返回的数据
|
|
|
+ if (!CollectionUtils.isEmpty(toolListFromApi)) {
|
|
|
+ toolListFromApi.forEach(apiTool -> {
|
|
|
+ if (StringUtils.isEmpty(apiTool.getAllocationNo())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ToolDisplay newTool = new ToolDisplay();
|
|
|
+ BeanUtils.copyProperties(apiTool, newTool);
|
|
|
+ toolDisplayMapper.insert(newTool);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 分离需要更新和需要清空的记录
|
|
|
+ List<ToolDisplay> needUpdateList = new ArrayList<>();
|
|
|
+ List<ToolDisplay> needClearList = new ArrayList<>();
|
|
|
+
|
|
|
+ // 3.1 构建数据库记录的映射
|
|
|
+ Map<String, ToolDisplay> dbToolMap = allDbTools.stream()
|
|
|
+ .filter(tool -> StringUtils.isNotEmpty(tool.getAllocationNo()))
|
|
|
+ .collect(Collectors.toMap(ToolDisplay::getAllocationNo, tool -> tool));
|
|
|
+
|
|
|
+ // 3.2 处理接口返回的数据(更新或新增)
|
|
|
+ if (!CollectionUtils.isEmpty(toolListFromApi)) {
|
|
|
+ for (ToolDisplay apiTool : toolListFromApi) {
|
|
|
+ String allocationNo = apiTool.getAllocationNo();
|
|
|
+ if (StringUtils.isEmpty(allocationNo)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ ToolDisplay dbTool = dbToolMap.get(allocationNo);
|
|
|
+ if (dbTool == null) {
|
|
|
+ // 新增记录
|
|
|
+ ToolDisplay newTool = new ToolDisplay();
|
|
|
+ BeanUtils.copyProperties(apiTool, newTool);
|
|
|
+ toolDisplayMapper.insert(newTool);
|
|
|
+ } else {
|
|
|
+ // 更新记录
|
|
|
+ dbTool.setMaterialNo(apiTool.getMaterialNo());
|
|
|
+ dbTool.setMaterialName(apiTool.getMaterialName());
|
|
|
+ dbTool.setQty(apiTool.getQty());
|
|
|
+ needUpdateList.add(dbTool);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3.3 处理数据库中存在但接口未返回的记录(清空字段)
|
|
|
+ for (ToolDisplay dbTool : allDbTools) {
|
|
|
+ String allocationNo = dbTool.getAllocationNo();
|
|
|
+ if (StringUtils.isEmpty(allocationNo)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 接口中没有该库位编号,需要清空字段
|
|
|
+ if (!apiAllocationNoSet.contains(allocationNo)) {
|
|
|
+ dbTool.setMaterialNo(null); // 清空编码
|
|
|
+ dbTool.setMaterialName(null); // 清空名称
|
|
|
+ dbTool.setQty(0); // 清空数量(或设为null,根据字段类型决定)
|
|
|
+ needClearList.add(dbTool);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 批量执行更新操作
|
|
|
+ if (!needUpdateList.isEmpty()) {
|
|
|
+ needUpdateList.forEach(tool -> toolDisplayMapper.updateById(tool));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 批量执行清空操作
|
|
|
+ if (!needClearList.isEmpty()) {
|
|
|
+ needClearList.forEach(tool -> toolDisplayMapper.updateById(tool));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
@@ -1064,7 +1180,6 @@ public class StockInfoServiceImpl extends SuperServiceImpl<StockInfoMapper, Stoc
|
|
|
|
|
|
@Override
|
|
|
public ToolDisplay getToolByNo(Map map) {
|
|
|
-
|
|
|
return baseMapper.gettoolByNo(map);
|
|
|
}
|
|
|
}
|