优化异常抛出

This commit is contained in:
liss
2025-09-30 10:56:58 +08:00
parent 41250ab127
commit 88b5ae2b64
25 changed files with 684 additions and 588 deletions

View File

@@ -1,7 +1,7 @@
package com.zt.plat.module.erp.api;
import com.zt.plat.module.erp.api.dto.ErpSubmitReqDTO;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.utils.ErpConfig;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

View File

@@ -1,299 +0,0 @@
package com.zt.plat.module.erp.common.conf;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zt.plat.module.erp.api.dto.ErpSubmitReqDTO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.*;
import java.util.stream.Collectors;
import static dm.jdbc.util.DriverUtil.log;
@Configuration
public class ErpConfig {
@Value("${erp.address}")
private String erpAddress;
@Value("${erp.sapsys}")
private String sapsys;
@Resource
private RedisTemplate redisTemplate;
/**
* 调用ERP接口获取erp数据
*/
public JSONArray fetchDataFromERP(String funcnr, Map<String, Object> req) {
try {
// 构建完整URL
String url = "http://" + erpAddress + "/api/rfc/get";
// 构建请求参数
JSONObject requestBody = new JSONObject();
requestBody.put("sapsys", sapsys);
requestBody.put("funcnr", funcnr); // 获取枚举值
if (req != null) {
requestBody.put("req", req);
}
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 创建HTTP请求实体
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody.toJSONString(), headers);
// 发送POST请求
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
// 解析响应结果
String responseBody = response.getBody();
if (responseBody.isEmpty()) {
log.warn("无所选条件的查询数据" + req);
}
JSONObject jsonResponse = JSON.parseObject(responseBody);
if (jsonResponse == null) {
log.warn("ERP接口响应无法解析为JSON");
}
// 正确获取E_DATA数组
JSONObject dataObject = jsonResponse.getJSONObject("data");
if (dataObject != null && "S".equals(dataObject.getString("E_FLAG"))) {
return dataObject.getJSONArray("E_DATA");
} else {
return null;
}
} catch (Exception e) {
log.error("调用ERP RFC接口失败: {}", e);
return null;
}
}
// GET请求处理
// public HashMap<String, String> getResult(SAPInterfaceResult result) {
// HashMap<String, String> resMap = new HashMap<>();
// String E_RSLT = null;
// JSONObject jsonObject = result.getData();
// //判断 result里的succeed是否为true
// boolean succeed = result.isSucceed();
// if (succeed && "S".equals(jsonObject.getString("E_FLAG"))) {
// JSONObject data = result.getData();
// String flag = "S";
// String E_RESP = data.getString("E_DATA");
//
// String E_MSG = result.getMsg();
//// log.info("请求SAP成功 E_RESP{}", E_RESP);
//// log.info("请求SAP成功 E_MSG{}", E_MSG);
// resMap.put("E_RESP", E_RESP);
// resMap.put("resStr", E_MSG);
// resMap.put("flag", flag);
// return resMap;
// } else {
// String E_MSG = result.getMsg();
// String flag = "E";
// resMap.put("resStr", E_MSG);
// resMap.put("flag", flag);
//// log.info("请求SAP失败 E_MSG{}", E_MSG);
// return resMap;
// }
// }
/**
* 调用ERP接口更新erp数据
*/
public HashMap<String, String> pushDataToErp(ErpSubmitReqDTO reqDTO) {
try {
// 构建完整URL
String url = "http://" + erpAddress + "/api/rfc/post";
// 构建请求参数
JSONObject requestBody = new JSONObject();
requestBody.put("uuid", UUID.randomUUID().toString());
requestBody.put("sapsys", sapsys);
requestBody.put("srcsys", "DSC");
requestBody.put("funcnr", reqDTO.getFuncnr());
requestBody.put("bskey", reqDTO.getBskey());
requestBody.put("usrid", reqDTO.getUsrid());
requestBody.put("usrnm", reqDTO.getUsrnm());
if (reqDTO.getSign() != null) {
requestBody.put("sign", reqDTO.getSign());
}
if (reqDTO.getReq() != null) {
requestBody.put("req", reqDTO.getReq());
}
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 创建HTTP请求实体
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody.toJSONString(), headers);
// 发送POST请求
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
// 解析响应结果
String responseBody = response.getBody();
if (responseBody.isEmpty()) {
throw new RuntimeException("ERP接口返回结果为空");
}
JSONObject jsonResponse = JSON.parseObject(responseBody);
if (jsonResponse == null) {
throw new RuntimeException("ERP接口响应无法解析为JSON");
}
return postResult(jsonResponse);
} catch (Exception e) {
throw new RuntimeException("调用ERP RFC接口失败: " + e);
}
}
// POST请求处理
public HashMap<String, String> postResult(JSONObject result) {
HashMap<String, String> resMap = new HashMap<>();
boolean succeed = result.getBoolean("succeed");
JSONObject data = result.getJSONObject("data");
if (data == null) {
throw new RuntimeException("SAP接口返回值为空," + result.getString("msg"));
} else if (succeed && "S".equals(data.getString("E_FLAG"))) {
String flag = "S";
String E_RESP = data.getString("E_RESP");
String E_MSG = data.getString("ET_MSG");
resMap.put("E_RESP", E_RESP);
resMap.put("resStr", E_MSG);
resMap.put("flag", flag);
return resMap;
} else if (!succeed && "E".equals(data.getString("E_FLAG"))) {
String flag = "E";
String E_MSG = data.getString("ET_MSG");
if (StrUtil.isBlank(E_MSG)) {
E_MSG = result.getString("msg");
}
resMap.put("resStr", E_MSG);
resMap.put("flag", flag);
return resMap;
} else {
throw new RuntimeException("SAP接口异常," + result);
}
}
//list
public Map<String, List<String>> numbers(JSONArray dataArray, String key, String dataKey) {
// 使用 Redis 获取缓存数据
Map<String, List<String>> numbers = new HashMap<>();
List<String> cachedNumbers = (List<String>) redisTemplate.opsForValue().get(key);
if (cachedNumbers == null) {
cachedNumbers = new ArrayList<>();
}
// 提取有效的 BUKRS 编号
List<String> existingNumbers;
if (dataArray != null) {
// 将dataKey按"-"分割成多个部分
String[] keyParts = dataKey.split("-");
existingNumbers = dataArray.stream()
.filter(Objects::nonNull)
.map(dataJson -> {
JSONObject jsonObject = (JSONObject) dataJson;
// 根据每个部分逐级获取值并拼接
StringBuilder sb = new StringBuilder();
for (String part : keyParts) {
String value = jsonObject.getString(part);
if (value != null) {
if (sb.length() > 0) {
sb.append("-");
}
sb.append(value.trim());
} else {
// 如果某一部分为空,则整个值视为无效
return null;
}
}
return sb.toString();
})
.filter(Objects::nonNull) // 过滤掉无效值
.collect(Collectors.toList());
} else {
existingNumbers = new ArrayList<>();
}
// 找出共同存在的编号
Set<String> cachedNumberSet = new HashSet<>(cachedNumbers != null ? cachedNumbers : new ArrayList<>());
List<String> commonNumbers = existingNumbers.stream()
.filter(cachedNumberSet::contains)
.collect(Collectors.toList());
numbers.put("com", commonNumbers);
//找出需要删除的字段。只有erp查询全部的接口能用到
List<String> deleteNumbers = cachedNumberSet.stream()
.filter(num -> !existingNumbers.contains(num))
.collect(Collectors.toList());
numbers.put("delete", deleteNumbers);
// 找出需要新增的编号
List<String> newNumbers = existingNumbers.stream()
.filter(num -> !cachedNumberSet.contains(num))
.toList();
// 合并所有编号
List<String> allNumbers = new ArrayList<>(cachedNumbers);
allNumbers.addAll(newNumbers);
numbers.put("all", allNumbers);
return numbers;
}
public void updateRedisCache(String key, List<String> allnumbers) {
// 使用 Redis 更新缓存数据
redisTemplate.opsForValue().set(key, allnumbers);
}
public List<String> getRedisCache(String key) {
// 使用 Redis 更新缓存数据
return (List<String>) redisTemplate.opsForValue().get(key);
}
//map
public Map<String, Long> getRedisCacheMap(String key) {
// 使用 Redis 获取缓存数据
Map<String, Long> result = (Map<String, Long>) redisTemplate.opsForHash().entries(key);
return result;
}
public void addRedisCacheMap(String key, Map<String, Long> allnumbers) {
// 使用 Redis 更新缓存数据
redisTemplate.opsForHash().putAll(key, allnumbers);
}
public void deleteRedisCacheMap(String key, List<String> deleteNumbers) {
if (deleteNumbers == null || deleteNumbers.isEmpty()) {
log.debug("No items to delete from Redis hash: {}", key);
return;
}
try {
Object[] keysToDelete = deleteNumbers.toArray(new String[0]);
Long deletedCount = redisTemplate.opsForHash().delete(key, keysToDelete);
log.debug("Deleted" + deletedCount + "items from Redis hash:" + key);
} catch (Exception e) {
log.error("Failed to delete items from Redis hash:" + key, e);
throw e;
}
}
}

View File

@@ -1,61 +0,0 @@
package com.zt.plat.module.erp.common.conf;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author wuxz
* @create 2022-06-07 20:54
*/
@Primary
@Configuration
public class MyRedisConfig {
@Bean(value = "MyRedisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 通过 Jackson 组件进行序列化
RedisSerializer<Object> serializer = redisSerializer();
// key 和 value
// 一般来说, redis-key采用字符串序列化
// redis-value采用json序列化 json的体积小可读性高不需要实现serializer接口。
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(serializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(serializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
public RedisSerializer<Object> redisSerializer() {
//创建JSON序列化器
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// objectMapper.enableDefaultTyping()被弃用
objectMapper.activateDefaultTyping(
LaissezFaireSubTypeValidator.instance,
ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.WRAPPER_ARRAY);
serializer.setObjectMapper(objectMapper);
return serializer;
}
}

View File

@@ -1,32 +0,0 @@
package com.zt.plat.module.erp.common.task;
import com.zt.plat.module.erp.service.erp.ErpCompanyService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
/**
* @ClassName energyTask
* @Description TODO
* @Author chen
* @Date 2023/9/25
**/
@Configuration
@EnableScheduling
public class statisticstask {
@Resource
private ErpCompanyService erpCompanyService;
//能源定时每日获取成本配置机台水电数据
@Scheduled(cron = "0 0 12 * * ?")
@Transactional
public void erpCompany(){
erpCompanyService.callErpRfcInterface();
}
}

View File

@@ -1,4 +1,4 @@
package com.zt.plat.module.erp.common.enums;
package com.zt.plat.module.erp.enums;
import lombok.Data;

View File

@@ -7,8 +7,9 @@ import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpAssetPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpAssetRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpAssetSaveReqVO;
@@ -28,6 +29,7 @@ import java.util.stream.Collectors;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_ASSET_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_COMPANY_REDIS_NOT_EXISTS;
import static dm.jdbc.util.DriverUtil.log;
/**
@@ -42,6 +44,8 @@ public class ErpAssetServiceImpl implements ErpAssetService {
@Resource
private ErpAssetMapper erpAssetMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@@ -110,33 +114,30 @@ public class ErpAssetServiceImpl implements ErpAssetService {
OftenEnum.FuncnrEnum funcnrEnum = OftenEnum.FuncnrEnum.资产卡片;
String funcnr = funcnrEnum.getFuncnr();
String key = "erpMap" + funcnr;
if (erpConfig.getRedisCacheMap(key).isEmpty()) {
if (myRedisConfig.getRedisCacheMap(key).isEmpty()) {
initializeMap(key);
}
// 构建req参数
Map<String, Object> req = new HashMap<>();
// List<Map<String, String>> datumList = new ArrayList<>();
// Map<String, String> datumEntry = new HashMap<>();
// datumEntry.put("sign", "I");
// datumEntry.put("option", "EQ");
// datumEntry.put("low", LocalDate.now().toString());
// datumList.add(datumEntry);
// req.put(funcnrEnum.getDatekey(), datumList);
JSONArray dataArrayALL = new JSONArray();
String companyKey ="erpMap"+ OftenEnum.FuncnrEnum.公司代码.getFuncnr();
Map<String,Long> redisCache = erpConfig.getRedisCacheMap(companyKey);
Map<String,Long> redisCache = myRedisConfig.getRedisCacheMap(companyKey);
if (CollUtil.isEmpty(redisCache)) {
return;
throw exception(ERP_COMPANY_REDIS_NOT_EXISTS);
}
for (String number : redisCache.keySet()) {
req.put("BUKRS", number);
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
if (dataArray == null || dataArray.isEmpty()) {
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (dataArray==null) {
continue;
}
dataArrayALL.addAll(dataArray);
}
if (CollUtil.isEmpty(dataArrayALL)) {
throw exception(ERP_ASSET_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
ProcessingResult result = processData(dataArrayALL, funcnrEnum);
@@ -155,7 +156,7 @@ public class ErpAssetServiceImpl implements ErpAssetService {
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnrEnum) {
String key = "erpMap" + funcnrEnum.getFuncnr();
Map<String, Long> numbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> numbers = myRedisConfig.getRedisCacheMap(key);
List<ErpAssetDO> toUpdate = new ArrayList<>();
List<ErpAssetDO> toInsert = new ArrayList<>();
@@ -222,7 +223,7 @@ public class ErpAssetServiceImpl implements ErpAssetService {
);
Map<String, Long> numberIdMap = insertedRecords.stream()
.collect(Collectors.toMap(asset -> asset.getCompanyNumber() + "-" + asset.getMainAssetNumber(), ErpAssetDO::getId));
erpConfig.addRedisCacheMap(result.key,numberIdMap);
myRedisConfig.addRedisCacheMap(result.key,numberIdMap);
}
if (!result.toUpdate.isEmpty()) {
erpAssetMapper.updateBatch(result.toUpdate);
@@ -231,7 +232,7 @@ public class ErpAssetServiceImpl implements ErpAssetService {
// 使用流式处理和批处理优化删除逻辑
List<Long> idsToDelete = new ArrayList<>(result.deleteNumbers.values());
erpAssetMapper.deleteByIds(idsToDelete);
erpConfig.deleteRedisCacheMap(result.key, new ArrayList<>(result.deleteNumbers.keySet()));
myRedisConfig.deleteRedisCacheMap(result.key, new ArrayList<>(result.deleteNumbers.keySet()));
}
}
@@ -259,7 +260,7 @@ public class ErpAssetServiceImpl implements ErpAssetService {
String mapKey = asset.getCompanyNumber() + "-" + asset.getMainAssetNumber();
existingNumbers.put(mapKey, asset.getId());
}
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}
}

View File

@@ -4,8 +4,9 @@ import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpBomDetailPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpBomDetailRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpBomDetailSaveReqVO;
@@ -36,6 +37,8 @@ public class ErpBomDetailServiceImpl implements ErpBomDetailService {
@Resource
private ErpBomDetailMapper erpBomDetailMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@@ -107,14 +110,14 @@ public class ErpBomDetailServiceImpl implements ErpBomDetailService {
}
private ProcessingResult processData(List<ErpBomDetailDO> updateReqVOS, String key) {
if (erpConfig.getRedisCacheMap(key).isEmpty()) {
if (myRedisConfig.getRedisCacheMap(key).isEmpty()) {
initializeMap(key);
}
List<ErpBomDetailDO> toUpdate = new ArrayList<>();
List<ErpBomDetailDO> toInsert = new ArrayList<>();
List<String> dataArrayNumbers = new ArrayList<>();
Map<String, Long> existingNumbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> existingNumbers = myRedisConfig.getRedisCacheMap(key);
for (ErpBomDetailDO updateReqVO : updateReqVOS) {
String mapKey = updateReqVO.getBomId() + "-" + updateReqVO.getErpBomId();
if (existingNumbers.containsKey(mapKey)) {
@@ -150,7 +153,7 @@ public class ErpBomDetailServiceImpl implements ErpBomDetailService {
);
Map<String, Long> numberIdMap = insertedRecords.stream()
.collect(Collectors.toMap(asset -> asset.getBomId() + "-" + asset.getErpBomId(), ErpBomDetailDO::getId));
erpConfig.addRedisCacheMap(result.key, numberIdMap);
myRedisConfig.addRedisCacheMap(result.key, numberIdMap);
}
if (!result.toUpdate.isEmpty()) {
erpBomDetailMapper.updateBatch(result.toUpdate);
@@ -161,7 +164,7 @@ public class ErpBomDetailServiceImpl implements ErpBomDetailService {
if (!idsToDelete.isEmpty()) {
erpBomDetailMapper.deleteByIds(idsToDelete);
}
erpConfig.deleteRedisCacheMap(result.key, new ArrayList<>(result.deleteNumbers.keySet()));
myRedisConfig.deleteRedisCacheMap(result.key, new ArrayList<>(result.deleteNumbers.keySet()));
}
}
@@ -186,6 +189,6 @@ public class ErpBomDetailServiceImpl implements ErpBomDetailService {
String mapKey = asset.getBomId() + "-" + asset.getErpBomId();
existingNumbers.put(mapKey, asset.getId());
}
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}
}

View File

@@ -9,8 +9,9 @@ import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.api.ErpExternalApi;
import com.zt.plat.module.erp.api.dto.ErpSubmitReqDTO;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpBomPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpBomRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpBomSaveReqVO;
@@ -28,7 +29,7 @@ import java.util.List;
import java.util.Map;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_BOM_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.*;
import static dm.jdbc.util.DriverUtil.log;
/**
@@ -43,6 +44,9 @@ public class ErpBomServiceImpl implements ErpBomService {
@Resource
private ErpBomMapper erpBomMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@@ -117,25 +121,29 @@ public class ErpBomServiceImpl implements ErpBomService {
OftenEnum.FuncnrEnum funcnrEnum = OftenEnum.FuncnrEnum.BOM清单;
String funcnr = funcnrEnum.getFuncnr();
String key = "erpMap" + funcnr;
if (erpConfig.getRedisCacheMap(key).isEmpty()) {
if (myRedisConfig.getRedisCacheMap(key).isEmpty()) {
initializeMap(key);
}
Map<String, Object> req = new HashMap<>();
JSONArray dataArrayALL = new JSONArray();
String factKey = "erpMap" + OftenEnum.FuncnrEnum.工厂信息.getFuncnr();
Map<String, Long> redisCache = erpConfig.getRedisCacheMap(factKey);
Map<String, Long> redisCache = myRedisConfig.getRedisCacheMap(factKey);
if (CollUtil.isEmpty(redisCache)) {
throw exception(ERP_BOM_NOT_EXISTS);
throw exception(ERP_FACTORY_REDIS_NOT_EXISTS);
}
for (String factoryNumber : redisCache.keySet()) {
req.put("WERKS", factoryNumber);
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (dataArray == null || dataArray.isEmpty()) {
continue;
}
dataArrayALL.addAll(dataArray);
}
if (CollUtil.isEmpty(dataArrayALL)) {
throw exception(ERP_BOM_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
ProcessingResult result = processData(dataArrayALL, funcnrEnum);
@@ -154,7 +162,7 @@ public class ErpBomServiceImpl implements ErpBomService {
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnrEnum) {
String key = "erpMap" + funcnrEnum.getFuncnr();
Map<String, Long> numbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> numbers = myRedisConfig.getRedisCacheMap(key);
List<ErpBomDO> toUpdate = new ArrayList<>();
List<ErpBomDetailDO> erpBomDetailDOList = new ArrayList<>();
@@ -231,11 +239,11 @@ public class ErpBomServiceImpl implements ErpBomService {
// 更新Redis缓存
if (!result.addnumbers.isEmpty()) {
erpConfig.addRedisCacheMap(result.key, result.addnumbers);
myRedisConfig.addRedisCacheMap(result.key, result.addnumbers);
}
if (!result.deleteNumbers.isEmpty()) {
erpConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
myRedisConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
}
}
@@ -266,7 +274,7 @@ public class ErpBomServiceImpl implements ErpBomService {
String mapKey = bom.getFactoryNumber() + "-" + bom.getUpMaterial() + "-" + bom.getUseItem();
existingNumbers.put(mapKey, bom.getId());
}
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}

View File

@@ -7,8 +7,9 @@ import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCompanyPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCompanyRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCompanySaveReqVO;
@@ -20,11 +21,13 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_BOM_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_COMPANY_NOT_EXISTS;
import static dm.jdbc.util.DriverUtil.log;
@@ -40,6 +43,8 @@ public class ErpCompanyServiceImpl implements ErpCompanyService {
@Resource
private ErpCompanyMapper erpCompanyMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@@ -109,13 +114,14 @@ public class ErpCompanyServiceImpl implements ErpCompanyService {
String funcnr = funcnrEnum.getFuncnr();
//防止缓存数据丢失
String key = "erpMap" + funcnrEnum.getFuncnr();
if (erpConfig.getRedisCacheMap(key).isEmpty()) {
if (myRedisConfig.getRedisCacheMap(key).isEmpty()) {
initializeMap(key);
}
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, null);
if (dataArray == null || dataArray.isEmpty()) {
return;
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, null);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (CollUtil.isEmpty(dataArray)) {
throw exception(ERP_COMPANY_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
@@ -135,7 +141,7 @@ public class ErpCompanyServiceImpl implements ErpCompanyService {
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnr) {
String key = "erpMap" + funcnr.getFuncnr();
Map<String, Long> numbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> numbers = myRedisConfig.getRedisCacheMap(key);
List<ErpCompanyDO> toUpdate = new ArrayList<>();
List<ErpCompanyDO> toInsert = new ArrayList<>();
@@ -187,7 +193,7 @@ public class ErpCompanyServiceImpl implements ErpCompanyService {
);
Map<String, Long> numberIdMap = insertedRecords.stream()
.collect(Collectors.toMap(ErpCompanyDO::getNumber, ErpCompanyDO::getId));
erpConfig.addRedisCacheMap(result.key,numberIdMap);
myRedisConfig.addRedisCacheMap(result.key,numberIdMap);
}
if (!result.toUpdate.isEmpty()) {
erpCompanyMapper.updateBatch(result.toUpdate);
@@ -195,7 +201,7 @@ public class ErpCompanyServiceImpl implements ErpCompanyService {
if (!result.deleteNumbers.isEmpty()) {
// 使用 in 条件批量删除,提高删除效率
erpCompanyMapper.delete(new LambdaQueryWrapperX<ErpCompanyDO>().in(ErpCompanyDO::getNumber, result.deleteNumbers));
erpConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
myRedisConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
}
}
@@ -221,7 +227,7 @@ public class ErpCompanyServiceImpl implements ErpCompanyService {
Map<String, Long> existingNumbers = erpCompanyMapper.selectList(new LambdaQueryWrapperX<ErpCompanyDO>())
.stream()
.collect(Collectors.toMap(ErpCompanyDO::getNumber, ErpCompanyDO::getId));
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}
}

View File

@@ -6,8 +6,9 @@ import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpContractPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpContractRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpContractSaveReqVO;
@@ -19,10 +20,12 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_COMPANY_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_CONTRACT_NOT_EXISTS;
import static dm.jdbc.util.DriverUtil.log;
@@ -38,6 +41,8 @@ public class ErpContractServiceImpl implements ErpContractService {
@Resource
private ErpContractMapper erpContractMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@@ -106,9 +111,10 @@ public class ErpContractServiceImpl implements ErpContractService {
OftenEnum.FuncnrEnum funcnrEnum =OftenEnum.FuncnrEnum.合同信息;
String funcnr = funcnrEnum.getFuncnr();
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, null);
if (dataArray == null || dataArray.isEmpty()) {
return;
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, null);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (CollUtil.isEmpty(dataArray)) {
throw exception(ERP_CONTRACT_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
@@ -128,7 +134,7 @@ public class ErpContractServiceImpl implements ErpContractService {
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnr) {
String key = "erp" + funcnr.getFuncnr();
Map<String,List<String>> numbers = erpConfig.numbers(dataArray, key,funcnr.getDatakey());
Map<String,List<String>> numbers = myRedisConfig.numbers(dataArray, key,funcnr.getDatakey());
List<String> allnumbers = numbers.get("all");
List<String> comnumbers = numbers.get("com");
List<ErpContractDO> toUpdate = new ArrayList<>();
@@ -168,7 +174,7 @@ public class ErpContractServiceImpl implements ErpContractService {
if (!result.toUpdate.isEmpty()) {
erpContractMapper.updateBatch(result.toUpdate);
}
erpConfig.updateRedisCache(result.key,result.allnumbers);
myRedisConfig.updateRedisCache(result.key,result.allnumbers);
}
/**

View File

@@ -7,8 +7,9 @@ import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCostcenterPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCostcenterRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCostcenterSaveReqVO;
@@ -27,6 +28,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_CONTRACT_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_COSTCENTER_NOT_EXISTS;
import static dm.jdbc.util.DriverUtil.log;
@@ -42,6 +44,8 @@ public class ErpCostcenterServiceImpl implements ErpCostcenterService {
@Resource
private ErpCostcenterMapper erpCostcenterMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@@ -111,13 +115,13 @@ public class ErpCostcenterServiceImpl implements ErpCostcenterService {
String funcnr = funcnrEnum.getFuncnr();
//防止缓存数据丢失
String key = "erpMap" + funcnr;
if (erpConfig.getRedisCacheMap( key).isEmpty()){
if (myRedisConfig.getRedisCacheMap( key).isEmpty()){
initializeMap(key);
}
Map<String, Object> req = new HashMap<>();
JSONArray dataArrayALL = new JSONArray();
String commanyKey ="erpMap"+ OftenEnum.FuncnrEnum.公司代码.getFuncnr();
Map<String,Long> redisCache = erpConfig.getRedisCacheMap(commanyKey);
Map<String,Long> redisCache = myRedisConfig.getRedisCacheMap(commanyKey);
if (CollUtil.isEmpty(redisCache)) {
return;
}
@@ -125,12 +129,16 @@ public class ErpCostcenterServiceImpl implements ErpCostcenterService {
for (String number : redisCache.keySet()) {
req.put("BUKRS", number);
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (dataArray == null || dataArray.isEmpty()) {
continue;
}
dataArrayALL.addAll(dataArray);
}
if (CollUtil.isEmpty(dataArrayALL)) {
throw exception(ERP_COSTCENTER_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
ProcessingResult result = processData(dataArrayALL,funcnrEnum);
@@ -149,7 +157,7 @@ public class ErpCostcenterServiceImpl implements ErpCostcenterService {
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnr) {
String key = "erpMap" + funcnr.getFuncnr();
Map<String, Long> numbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> numbers = myRedisConfig.getRedisCacheMap(key);
List<ErpCostcenterDO> toUpdate = new ArrayList<>();
List<ErpCostcenterDO> toInsert = new ArrayList<>();
@@ -208,7 +216,7 @@ public class ErpCostcenterServiceImpl implements ErpCostcenterService {
);
Map<String, Long> numberIdMap = insertedRecords.stream()
.collect(Collectors.toMap(ErpCostcenterDO::getNumber, ErpCostcenterDO::getId));
erpConfig.addRedisCacheMap(result.key,numberIdMap);
myRedisConfig.addRedisCacheMap(result.key,numberIdMap);
}
if (!result.toUpdate.isEmpty()) {
erpCostcenterMapper.updateBatch(result.toUpdate);
@@ -216,7 +224,7 @@ public class ErpCostcenterServiceImpl implements ErpCostcenterService {
if (!result.deleteNumbers.isEmpty()) {
// 使用 in 条件批量删除,提高删除效率
erpCostcenterMapper.delete(new LambdaQueryWrapperX<ErpCostcenterDO>().in(ErpCostcenterDO::getNumber, result.deleteNumbers));
erpConfig.deleteRedisCacheMap(result.key,result.deleteNumbers);
myRedisConfig.deleteRedisCacheMap(result.key,result.deleteNumbers);
}
}
@@ -240,6 +248,6 @@ public class ErpCostcenterServiceImpl implements ErpCostcenterService {
Map<String, Long> existingNumbers = erpCostcenterMapper.selectList(new LambdaQueryWrapperX<ErpCostcenterDO>())
.stream()
.collect(Collectors.toMap(ErpCostcenterDO::getNumber, ErpCostcenterDO::getId));
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}
}

View File

@@ -7,8 +7,9 @@ import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCustomerPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCustomerRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCustomerSaveReqVO;
@@ -41,10 +42,12 @@ import static dm.jdbc.util.DriverUtil.log;
public class ErpCustomerServiceImpl implements ErpCustomerService {
@Resource
private ErpConfig erpConfig;
private MyRedisConfig myRedisConfig;
@Resource
private ErpCustomerMapper erpCustomerMapper;
@Resource
private ErpConfig erpConfig;
@Override
public ErpCustomerRespVO createErpCustomer(ErpCustomerSaveReqVO createReqVO) {
@@ -112,7 +115,7 @@ public class ErpCustomerServiceImpl implements ErpCustomerService {
String funcnr = funcnrEnum.getFuncnr();
String key = "erpMap" + funcnrEnum.getFuncnr();
if (erpConfig.getRedisCacheMap(key).isEmpty()) {
if (myRedisConfig.getRedisCacheMap(key).isEmpty()) {
initializeMap(key);
}
// 构建req参数
@@ -130,12 +133,16 @@ public class ErpCustomerServiceImpl implements ErpCustomerService {
JSONArray dataArrayALL = new JSONArray();
for (OftenEnum.ModeTypeEnum type : OftenEnum.ModeTypeEnum.values()) {
req.put("mode", type.modetype);
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get(funcnrEnum.getDatakey());
if (dataArray == null || dataArray.isEmpty()) {
continue;
}
dataArrayALL.addAll(dataArray);
}
if (dataArrayALL.isEmpty()){
throw exception(ERP_CUSTOMER_NOT_EXISTS);
}
// 2. 处理数据,区分新增和更新
ProcessingResult result = processData(dataArrayALL, funcnrEnum);
@@ -154,7 +161,7 @@ public class ErpCustomerServiceImpl implements ErpCustomerService {
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnrEnum) {
String key = "erpMap" + funcnrEnum.getFuncnr();
Map<String, Long> numbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> numbers = myRedisConfig.getRedisCacheMap(key);
List<ErpCustomerDO> toUpdate = new ArrayList<>();
List<ErpCustomerDO> toInsert = new ArrayList<>();
@@ -211,7 +218,7 @@ public class ErpCustomerServiceImpl implements ErpCustomerService {
);
Map<String, Long> numberIdMap = insertedRecords.stream()
.collect(Collectors.toMap(ErpCustomerDO::getNumber, ErpCustomerDO::getId));
erpConfig.addRedisCacheMap(result.key,numberIdMap);
myRedisConfig.addRedisCacheMap(result.key,numberIdMap);
}
if (!result.toUpdate.isEmpty()) {
erpCustomerMapper.updateBatch(result.toUpdate);
@@ -237,6 +244,6 @@ public class ErpCustomerServiceImpl implements ErpCustomerService {
Map<String, Long> existingNumbers = erpCustomerMapper.selectList(new LambdaQueryWrapperX<ErpCustomerDO>())
.stream()
.collect(Collectors.toMap(ErpCustomerDO::getNumber, ErpCustomerDO::getId));
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}
}

View File

@@ -7,8 +7,9 @@ import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpFactoryPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpFactoryRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpFactorySaveReqVO;
@@ -26,7 +27,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_FACTORY_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.*;
import static dm.jdbc.util.DriverUtil.log;
/**
@@ -41,6 +42,8 @@ public class ErpFactoryServiceImpl implements ErpFactoryService {
@Resource
private ErpFactoryMapper erpFactoryMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@@ -109,21 +112,22 @@ public class ErpFactoryServiceImpl implements ErpFactoryService {
String funcnr = funcnrEnum.getFuncnr();
//防止缓存数据丢失
String key = "erpMap" + funcnrEnum.getFuncnr();
if (erpConfig.getRedisCacheMap(key).isEmpty()) {
if (myRedisConfig.getRedisCacheMap(key).isEmpty()) {
initializeMap(key);
}
// 1. 调用ERP接口获取数据
Map<String, Object> req = new HashMap<>();
JSONArray dataArrayALL = new JSONArray();
String companykey = "erp" + OftenEnum.FuncnrEnum.公司代码.getFuncnr();
Map<String,Long> redisCache = erpConfig.getRedisCacheMap(companykey);
Map<String,Long> redisCache = myRedisConfig.getRedisCacheMap(companykey);
if (CollUtil.isEmpty(redisCache)) {
throw new RuntimeException("ERP公司代码缓存数据丢失,请重新同步公司代码");
throw exception(ERP_COMPANY_REDIS_NOT_EXISTS);
}
for (String companyNumber : redisCache.keySet()) {
req.put("BUKRS", companyNumber);
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (dataArray == null || dataArray.isEmpty()) {
continue;
}
@@ -134,9 +138,11 @@ public class ErpFactoryServiceImpl implements ErpFactoryService {
item.put("BUKRS", companyNumber);
}
}
dataArrayALL.addAll(dataArray);
}
if (CollUtil.isEmpty(dataArrayALL)) {
throw exception(ERP_FACTORY_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
ProcessingResult result = processData(dataArrayALL,funcnrEnum);
@@ -155,7 +161,7 @@ public class ErpFactoryServiceImpl implements ErpFactoryService {
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnr) {
String key = "erpMap" + funcnr.getFuncnr();
Map<String, Long> numbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> numbers = myRedisConfig.getRedisCacheMap(key);
List<ErpFactoryDO> toUpdate = new ArrayList<>();
List<ErpFactoryDO> toInsert = new ArrayList<>();
@@ -207,7 +213,7 @@ public class ErpFactoryServiceImpl implements ErpFactoryService {
);
Map<String, Long> numberIdMap = insertedRecords.stream()
.collect(Collectors.toMap(ErpFactoryDO::getNumber, ErpFactoryDO::getId));
erpConfig.addRedisCacheMap(result.key,numberIdMap);
myRedisConfig.addRedisCacheMap(result.key,numberIdMap);
}
if (!result.toUpdate.isEmpty()) {
erpFactoryMapper.updateBatch(result.toUpdate);
@@ -215,7 +221,7 @@ public class ErpFactoryServiceImpl implements ErpFactoryService {
if (!result.deleteNumbers.isEmpty()) {
// 使用 in 条件批量删除,提高删除效率
erpFactoryMapper.delete(new LambdaQueryWrapperX<ErpFactoryDO>().in(ErpFactoryDO::getNumber, result.deleteNumbers));
erpConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
myRedisConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
}
}
@@ -240,6 +246,6 @@ public class ErpFactoryServiceImpl implements ErpFactoryService {
Map<String, Long> existingNumbers = erpFactoryMapper.selectList(new LambdaQueryWrapperX<ErpFactoryDO>())
.stream()
.collect(Collectors.toMap(ErpFactoryDO::getNumber, ErpFactoryDO::getId));
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}
}

View File

@@ -7,8 +7,9 @@ import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpInternalOrderPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpInternalOrderRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpInternalOrderSaveReqVO;
@@ -26,7 +27,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_INTERNAL_ORDER_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.*;
import static dm.jdbc.util.DriverUtil.log;
/**
@@ -41,6 +42,8 @@ public class ErpInternalOrderServiceImpl implements ErpInternalOrderService {
@Resource
private ErpInternalOrderMapper erpInternalOrderMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@@ -110,27 +113,31 @@ public class ErpInternalOrderServiceImpl implements ErpInternalOrderService {
String funcnr = funcnrEnum.getFuncnr();
//防止缓存数据丢失
String key = "erpMap" + funcnrEnum.getFuncnr();
if (erpConfig.getRedisCacheMap(key).isEmpty()) {
if (myRedisConfig.getRedisCacheMap(key).isEmpty()) {
initializeMap(key);
}
Map<String, Object> req = new HashMap<>();
JSONArray dataArrayALL = new JSONArray();
String companyCode = "erpMap" + OftenEnum.FuncnrEnum.公司代码.getFuncnr();
Map<String,Long> redisCache = erpConfig.getRedisCacheMap(companyCode);
Map<String,Long> redisCache = myRedisConfig.getRedisCacheMap(companyCode);
if (CollUtil.isEmpty(redisCache)) {
return;
throw exception(ERP_COMPANY_REDIS_NOT_EXISTS);
}
// 1. 调用ERP接口获取数据
for (String number : redisCache.keySet()) {
req.put("BUKRS", number);
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (dataArray == null || dataArray.isEmpty()) {
continue;
}
dataArrayALL.addAll(dataArray);
}
if (CollUtil.isEmpty(dataArrayALL)) {
throw exception(ERP_INTERNAL_ORDER_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
ProcessingResult result = processData(dataArrayALL, funcnrEnum);
@@ -149,7 +156,7 @@ public class ErpInternalOrderServiceImpl implements ErpInternalOrderService {
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnr) {
String key = "erpMap" + funcnr.getFuncnr();
Map<String, Long> numbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> numbers = myRedisConfig.getRedisCacheMap(key);
List<ErpInternalOrderDO> toUpdate = new ArrayList<>();
List<ErpInternalOrderDO> toInsert = new ArrayList<>();
@@ -203,7 +210,7 @@ public class ErpInternalOrderServiceImpl implements ErpInternalOrderService {
);
Map<String, Long> numberIdMap = insertedRecords.stream()
.collect(Collectors.toMap(ErpInternalOrderDO::getNumber, ErpInternalOrderDO::getId));
erpConfig.addRedisCacheMap(result.key,numberIdMap);
myRedisConfig.addRedisCacheMap(result.key,numberIdMap);
}
if (!result.toUpdate.isEmpty()) {
erpInternalOrderMapper.updateBatch(result.toUpdate);
@@ -211,7 +218,7 @@ public class ErpInternalOrderServiceImpl implements ErpInternalOrderService {
if (!result.deleteNumbers.isEmpty()) {
// 使用 in 条件批量删除,提高删除效率
erpInternalOrderMapper.delete(new LambdaQueryWrapperX<ErpInternalOrderDO>().in(ErpInternalOrderDO::getNumber, result.deleteNumbers));
erpConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
myRedisConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
}
}
@@ -236,6 +243,6 @@ public class ErpInternalOrderServiceImpl implements ErpInternalOrderService {
Map<String, Long> existingNumbers = erpInternalOrderMapper.selectList(new LambdaQueryWrapperX<ErpInternalOrderDO>())
.stream()
.collect(Collectors.toMap(ErpInternalOrderDO::getNumber, ErpInternalOrderDO::getId));
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}
}

View File

@@ -7,8 +7,9 @@ import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpMaterialPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpMaterialRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpMaterialSaveReqVO;
@@ -28,6 +29,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_INTERNAL_ORDER_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_MATERIAL_NOT_EXISTS;
import static dm.jdbc.util.DriverUtil.log;
@@ -43,6 +45,8 @@ public class ErpMaterialServiceImpl implements ErpMaterialService {
@Resource
private ErpMaterialMapper erpMaterialMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@Override
@@ -111,7 +115,7 @@ public class ErpMaterialServiceImpl implements ErpMaterialService {
String funcnr = funcnrEnum.getFuncnr();
//防止缓存数据丢失
String key = "erp" + funcnrEnum.getFuncnr();
if (erpConfig.getRedisCache(key)==null) {
if (myRedisConfig.getRedisCache(key)==null) {
initialize(key);
}
@@ -127,9 +131,10 @@ public class ErpMaterialServiceImpl implements ErpMaterialService {
req.put(funcnrEnum.getDatekey(), datumList);
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
if (dataArray == null || dataArray.isEmpty()) {
return;
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (CollUtil.isEmpty(dataArray)) {
throw exception(ERP_MATERIAL_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
@@ -149,7 +154,7 @@ public class ErpMaterialServiceImpl implements ErpMaterialService {
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnr) {
String key = "erp" + funcnr.getFuncnr();
Map<String,List<String>> numbers = erpConfig.numbers(dataArray, key,funcnr.getDatakey());
Map<String,List<String>> numbers = myRedisConfig.numbers(dataArray, key,funcnr.getDatakey());
List<String> allnumbers = numbers.get("all");
List<String> comnumbers = numbers.get("com");
List<ErpMaterialDO> toUpdate = new ArrayList<>();
@@ -200,7 +205,7 @@ public class ErpMaterialServiceImpl implements ErpMaterialService {
if (!result.toUpdate.isEmpty()) {
erpMaterialMapper.updateBatchByNumber(result.toUpdate);
}
erpConfig.updateRedisCache(result.key,result.allnumbers);
myRedisConfig.updateRedisCache(result.key,result.allnumbers);
}
/**
@@ -225,6 +230,6 @@ public class ErpMaterialServiceImpl implements ErpMaterialService {
.stream()
.map(ErpMaterialDO::getDownCenterNumber)
.collect(Collectors.toList());
erpConfig.updateRedisCache(key, existingNumbers);
myRedisConfig.updateRedisCache(key, existingNumbers);
}
}

View File

@@ -4,8 +4,8 @@ import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProcessDetailPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProcessDetailRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProcessDetailSaveReqVO;
@@ -37,7 +37,7 @@ public class ErpProcessDetailServiceImpl implements ErpProcessDetailService {
private ErpProcessDetailMapper erpProcessDetailMapper;
@Resource
private ErpConfig erpConfig;
private MyRedisConfig myRedisConfig;
@Override
public ErpProcessDetailRespVO createErpProcessDetail(ErpProcessDetailSaveReqVO createReqVO) {
@@ -106,16 +106,19 @@ public class ErpProcessDetailServiceImpl implements ErpProcessDetailService {
}
private ProcessingResult processData(List<ErpProcessDetailDO> updateReqVOS, String key) {
if (erpConfig.getRedisCacheMap(key).isEmpty()) {
if (myRedisConfig.getRedisCacheMap(key).isEmpty()) {
initializeMap(key);
}
List<ErpProcessDetailDO> toUpdate = new ArrayList<>();
List<ErpProcessDetailDO> toInsert = new ArrayList<>();
List<String> dataArrayNumbers = new ArrayList<>();
Map<String, Long> existingNumbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> existingNumbers = myRedisConfig.getRedisCacheMap(key);
for (ErpProcessDetailDO updateReqVO : updateReqVOS) {
String mapKey = updateReqVO.getProcessId() + "-" + updateReqVO.getProcessingNumber()+"-"+updateReqVO.getProcessingName();
if (updateReqVO.getProcessingName() == null|| updateReqVO.getProcessingName().isEmpty()){
continue;
}
String mapKey = updateReqVO.getProcessId() + "-" + updateReqVO.getProcessingNumber();
if (existingNumbers.containsKey(mapKey)) {
updateReqVO.setId(existingNumbers.get(mapKey));
}
@@ -146,11 +149,11 @@ public class ErpProcessDetailServiceImpl implements ErpProcessDetailService {
new LambdaQueryWrapperX<ErpProcessDetailDO>()
.in(ErpProcessDetailDO::getProcessId, result.toInsert.stream().map(ErpProcessDetailDO::getProcessId).distinct().collect(Collectors.toList()))
.in(ErpProcessDetailDO::getProcessingNumber, result.toInsert.stream().map(ErpProcessDetailDO::getProcessingNumber).distinct().collect(Collectors.toList()))
.in(ErpProcessDetailDO::getProcessingName, result.toInsert.stream().map(ErpProcessDetailDO::getProcessingName).distinct().collect(Collectors.toList()))
// .in(ErpProcessDetailDO::getProcessingName, result.toInsert.stream().map(ErpProcessDetailDO::getProcessingName).distinct().collect(Collectors.toList()))
);
Map<String, Long> numberIdMap = insertedRecords.stream()
.collect(Collectors.toMap(asset -> asset.getProcessId() + "-" + asset.getProcessingNumber()+"-"+asset.getProcessingName(), ErpProcessDetailDO::getId));
erpConfig.addRedisCacheMap(result.key, numberIdMap);
.collect(Collectors.toMap(asset -> asset.getProcessId() + "-" + asset.getProcessingNumber(), ErpProcessDetailDO::getId));
myRedisConfig.addRedisCacheMap(result.key, numberIdMap);
}
if (!result.toUpdate.isEmpty()) {
erpProcessDetailMapper.updateBatch(result.toUpdate);
@@ -161,7 +164,7 @@ public class ErpProcessDetailServiceImpl implements ErpProcessDetailService {
if (!idsToDelete.isEmpty()) {
erpProcessDetailMapper.deleteByIds(idsToDelete);
}
erpConfig.deleteRedisCacheMap(result.key, new ArrayList<>(result.deleteNumbers.keySet()));
myRedisConfig.deleteRedisCacheMap(result.key, new ArrayList<>(result.deleteNumbers.keySet()));
}
}
@@ -183,9 +186,9 @@ public class ErpProcessDetailServiceImpl implements ErpProcessDetailService {
List<ErpProcessDetailDO> assets = erpProcessDetailMapper.selectList(new LambdaQueryWrapperX<ErpProcessDetailDO>());
Map<String, Long> existingNumbers = new HashMap<>();
for (ErpProcessDetailDO asset : assets) {
String mapKey = asset.getProcessId() + "-" + asset.getProcessingNumber()+"-"+asset.getProcessingName();
String mapKey = asset.getProcessId() + "-" + asset.getProcessingNumber();
existingNumbers.put(mapKey, asset.getId());
}
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}
}

View File

@@ -7,8 +7,9 @@ import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProcessPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProcessRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProcessSaveReqVO;
@@ -26,6 +27,7 @@ import java.util.List;
import java.util.Map;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_FACTORY_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_PROCESS_NOT_EXISTS;
import static dm.jdbc.util.DriverUtil.log;
@@ -41,6 +43,9 @@ public class ErpProcessServiceImpl implements ErpProcessService {
@Resource
private ErpProcessMapper erpProcessMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@@ -113,26 +118,30 @@ public class ErpProcessServiceImpl implements ErpProcessService {
OftenEnum.FuncnrEnum funcnrEnum = OftenEnum.FuncnrEnum.工艺路线;
String funcnr = funcnrEnum.getFuncnr();
String key = "erpMap" + funcnr;
if (erpConfig.getRedisCacheMap(key).isEmpty()) {
if (myRedisConfig.getRedisCacheMap(key).isEmpty()) {
initializeMap(key);
}
Map<String, Object> req = new HashMap<>();
JSONArray dataArrayALL = new JSONArray();
String factKey = "erpMap" + OftenEnum.FuncnrEnum.工厂信息.getFuncnr();
Map<String, Long> redisCache = erpConfig.getRedisCacheMap(factKey);
Map<String, Long> redisCache = myRedisConfig.getRedisCacheMap(factKey);
if (CollUtil.isEmpty(redisCache)) {
throw exception(ERP_PROCESS_NOT_EXISTS);
throw exception(ERP_FACTORY_NOT_EXISTS);
}
// 1. 调用ERP接口获取数据
for (String factoryNumber : redisCache.keySet()) {
req.put("WERKS", factoryNumber);
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (dataArray == null || dataArray.isEmpty()) {
continue;
}
dataArrayALL.addAll(dataArray);
}
if (dataArrayALL.isEmpty()){
throw exception(ERP_PROCESS_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
ProcessingResult result = processData(dataArrayALL, funcnrEnum);
@@ -151,7 +160,7 @@ public class ErpProcessServiceImpl implements ErpProcessService {
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnrEnum) {
String key = "erpMap" + funcnrEnum.getFuncnr();
Map<String, Long> numbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> numbers = myRedisConfig.getRedisCacheMap(key);
List<ErpProcessDO> toUpdate = new ArrayList<>();
List<ErpProcessDetailDO> erpProcessDetailDOList = new ArrayList<>();
@@ -175,6 +184,7 @@ public class ErpProcessServiceImpl implements ErpProcessService {
dataArrayNumbers.add(number);
if (numbers.get(number) != null) {
// 更新
DO.setId(numbers.get(number));
toUpdate.add(DO);
} else {
// 新增
@@ -229,11 +239,11 @@ public class ErpProcessServiceImpl implements ErpProcessService {
// 更新Redis缓存
if (!result.addnumbers.isEmpty()) {
erpConfig.addRedisCacheMap(result.key, result.addnumbers);
myRedisConfig.addRedisCacheMap(result.key, result.addnumbers);
}
if (!result.deleteNumbers.isEmpty()) {
erpConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
myRedisConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
}
}
@@ -264,6 +274,6 @@ public class ErpProcessServiceImpl implements ErpProcessService {
String mapKey = bom.getFactoryNumber() + "-" + bom.getMaterialNumber() + "-" + bom.getBlineGroup();
existingNumbers.put(mapKey, bom.getId());
}
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}
}

View File

@@ -6,8 +6,9 @@ import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProductiveOrderPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProductiveOrderRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProductiveOrderSaveReqVO;
@@ -27,7 +28,7 @@ import java.util.List;
import java.util.Map;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_PRODUCTIVE_ORDER_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.*;
import static dm.jdbc.util.DriverUtil.log;
/**
@@ -44,6 +45,8 @@ public class ErpProductiveOrderServiceImpl implements ErpProductiveOrderService
@Resource
private ErpConfig erpConfig;
@Resource
private MyRedisConfig myRedisConfig;
@Override
public ErpProductiveOrderRespVO createErpProductiveOrder(ErpProductiveOrderSaveReqVO createReqVO) {
@@ -116,7 +119,8 @@ public class ErpProductiveOrderServiceImpl implements ErpProductiveOrderService
}
// 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (dataArray == null || dataArray.isEmpty()) {
// 返回空结果而不是抛出异常
return new PageResult<>(new ArrayList<>(), 0L);
@@ -193,20 +197,24 @@ public class ErpProductiveOrderServiceImpl implements ErpProductiveOrderService
datumList.add(datumEntry);
req.put(funcnrEnum.getDatekey(), datumList);
JSONArray dataArrayALL = new JSONArray();
List<String> redisCache = erpConfig.getRedisCache(OftenEnum.FuncnrEnum.工厂信息.getFuncnr());
List<String> redisCache = myRedisConfig.getRedisCache(OftenEnum.FuncnrEnum.工厂信息.getFuncnr());
if (CollUtil.isEmpty(redisCache)) {
return;
throw exception(ERP_FACTORY_NOT_EXISTS);
}
// 1. 调用ERP接口获取数据
for (String number : redisCache) {
req.put("WERKS", number);
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (dataArray == null || dataArray.isEmpty()) {
continue;
}
dataArrayALL.addAll(dataArray);
}
if (dataArrayALL.isEmpty()){
throw exception(ERP_PRODUCTIVE_ORDER_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
ProcessingResult result = processData(dataArrayALL, funcnrEnum);
@@ -225,7 +233,7 @@ public class ErpProductiveOrderServiceImpl implements ErpProductiveOrderService
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnr) {
String key = "erp" + funcnr.getFuncnr();
Map<String, List<String>> numbers = erpConfig.numbers(dataArray, key, funcnr.getDatakey());
Map<String, List<String>> numbers = myRedisConfig.numbers(dataArray, key, funcnr.getDatakey());
List<String> allnumbers = numbers.get("all");
List<String> comnumbers = numbers.get("com");
List<ErpProductiveOrderDO> toUpdate = new ArrayList<>();
@@ -265,7 +273,7 @@ public class ErpProductiveOrderServiceImpl implements ErpProductiveOrderService
if (!result.toUpdate.isEmpty()) {
erpProductiveOrderMapper.updateBatch(result.toUpdate);
}
erpConfig.updateRedisCache(result.key, result.allnumbers);
myRedisConfig.updateRedisCache(result.key, result.allnumbers);
}
/**

View File

@@ -7,8 +7,9 @@ import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProductiveVersionPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProductiveVersionRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProductiveVersionSaveReqVO;
@@ -26,7 +27,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_PRODUCTIVE_VERSION_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.*;
import static dm.jdbc.util.DriverUtil.log;
/**
@@ -41,6 +42,8 @@ public class ErpProductiveVersionServiceImpl implements ErpProductiveVersionServ
@Resource
private ErpProductiveVersionMapper erpProductiveVersionMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@@ -110,27 +113,31 @@ public class ErpProductiveVersionServiceImpl implements ErpProductiveVersionServ
String funcnr = funcnrEnum.getFuncnr();
//防止缓存数据丢失
String key = "erpMap" + funcnrEnum.getFuncnr();
if (erpConfig.getRedisCacheMap(key).isEmpty()) {
if (myRedisConfig.getRedisCacheMap(key).isEmpty()) {
initializeMap(key);
}
Map<String, Object> req = new HashMap<>();
JSONArray dataArrayALL = new JSONArray();
String factKey ="erpMap"+ OftenEnum.FuncnrEnum.工厂信息.getFuncnr();
Map<String,Long> redisCache = erpConfig.getRedisCacheMap(factKey);
Map<String,Long> redisCache = myRedisConfig.getRedisCacheMap(factKey);
if (CollUtil.isEmpty(redisCache)) {
return;
throw exception(ERP_FACTORY_NOT_EXISTS);
}
// 1. 调用ERP接口获取数据
for (String number : redisCache.keySet()) {
req.put("WERKS", number);
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (dataArray == null || dataArray.isEmpty()) {
continue;
}
dataArrayALL.addAll(dataArray);
}
if (dataArrayALL.isEmpty()){
throw exception(ERP_PRODUCTIVE_VERSION_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
ProcessingResult result = processData(dataArrayALL,funcnrEnum);
@@ -149,7 +156,7 @@ public class ErpProductiveVersionServiceImpl implements ErpProductiveVersionServ
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnr) {
String key = "erpMap" + funcnr.getFuncnr();
Map<String, Long> numbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> numbers = myRedisConfig.getRedisCacheMap(key);
List<ErpProductiveVersionDO> toUpdate = new ArrayList<>();
List<ErpProductiveVersionDO> toInsert = new ArrayList<>();
@@ -204,7 +211,7 @@ public class ErpProductiveVersionServiceImpl implements ErpProductiveVersionServ
);
Map<String, Long> numberIdMap = insertedRecords.stream()
.collect(Collectors.toMap(asset -> asset.getFactoryNumber() + "-" + asset.getMaterialNumber()+ "-" + asset.getProductiveVersionNumber(), ErpProductiveVersionDO::getId));
erpConfig.addRedisCacheMap(result.key,numberIdMap);
myRedisConfig.addRedisCacheMap(result.key,numberIdMap);
}
if (!result.toUpdate.isEmpty()) {
erpProductiveVersionMapper.updateBatch(result.toUpdate);
@@ -213,7 +220,7 @@ public class ErpProductiveVersionServiceImpl implements ErpProductiveVersionServ
// 使用流式处理和批处理优化删除逻辑
List<Long> idsToDelete = new ArrayList<>(result.deleteNumbers.values());
erpProductiveVersionMapper.deleteByIds(idsToDelete);
erpConfig.deleteRedisCacheMap(result.key, new ArrayList<>(result.deleteNumbers.keySet()));
myRedisConfig.deleteRedisCacheMap(result.key, new ArrayList<>(result.deleteNumbers.keySet()));
}
}
@@ -241,6 +248,6 @@ public class ErpProductiveVersionServiceImpl implements ErpProductiveVersionServ
String mapKey = asset.getFactoryNumber() + "-" + asset.getMaterialNumber() + "-" + asset.getProductiveVersionNumber();
existingNumbers.put(mapKey, asset.getId());
}
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}
}

View File

@@ -7,8 +7,9 @@ import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpPurchaseOrganizationPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpPurchaseOrganizationRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpPurchaseOrganizationSaveReqVO;
@@ -26,7 +27,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_PURCHASE_ORGANIZATION_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.*;
import static dm.jdbc.util.DriverUtil.log;
/**
@@ -41,6 +42,8 @@ public class ErpPurchaseOrganizationServiceImpl implements ErpPurchaseOrganizati
@Resource
private ErpPurchaseOrganizationMapper erpPurchaseOrganizationMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@@ -110,7 +113,7 @@ public class ErpPurchaseOrganizationServiceImpl implements ErpPurchaseOrganizati
String funcnr = funcnrEnum.getFuncnr();
//防止缓存数据丢失
String key = "erpMap" + funcnrEnum.getFuncnr();
if (erpConfig.getRedisCacheMap(key).isEmpty()) {
if (myRedisConfig.getRedisCacheMap(key).isEmpty()) {
initializeMap(key);
}
@@ -118,15 +121,16 @@ public class ErpPurchaseOrganizationServiceImpl implements ErpPurchaseOrganizati
JSONArray dataArrayALL = new JSONArray();
// String factKey ="erpMap"+ OftenEnum.FuncnrEnum.工厂信息.getFuncnr();
String factKey ="erpMap"+ OftenEnum.FuncnrEnum.公司代码.getFuncnr();
Map<String,Long> redisCache = erpConfig.getRedisCacheMap(factKey);
Map<String,Long> redisCache = myRedisConfig.getRedisCacheMap(factKey);
if (CollUtil.isEmpty(redisCache)) {
return;
throw exception(ERP_COMPANY_REDIS_NOT_EXISTS);
}
// 1. 调用ERP接口获取数据
for (String number : redisCache.keySet()) {
req.put("BUKRS", number);
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (dataArray == null || dataArray.isEmpty()) {
continue;
}
@@ -139,6 +143,9 @@ public class ErpPurchaseOrganizationServiceImpl implements ErpPurchaseOrganizati
}
dataArrayALL.addAll(dataArray);
}
if (dataArrayALL.isEmpty()){
throw exception(ERP_PURCHASE_ORGANIZATION_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
ProcessingResult result = processData(dataArrayALL,funcnrEnum);
@@ -157,7 +164,7 @@ public class ErpPurchaseOrganizationServiceImpl implements ErpPurchaseOrganizati
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnr) {
String key = "erpMap" + funcnr.getFuncnr();
Map<String, Long> numbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> numbers = myRedisConfig.getRedisCacheMap(key);
List<ErpPurchaseOrganizationDO> toUpdate = new ArrayList<>();
List<ErpPurchaseOrganizationDO> toInsert = new ArrayList<>();
@@ -209,7 +216,7 @@ public class ErpPurchaseOrganizationServiceImpl implements ErpPurchaseOrganizati
);
Map<String, Long> numberIdMap = insertedRecords.stream()
.collect(Collectors.toMap(ErpPurchaseOrganizationDO::getNumber, ErpPurchaseOrganizationDO::getId));
erpConfig.addRedisCacheMap(result.key,numberIdMap);
myRedisConfig.addRedisCacheMap(result.key,numberIdMap);
}
if (!result.toUpdate.isEmpty()) {
erpPurchaseOrganizationMapper.updateBatch(result.toUpdate);
@@ -217,7 +224,7 @@ public class ErpPurchaseOrganizationServiceImpl implements ErpPurchaseOrganizati
if (!result.deleteNumbers.isEmpty()){
// 使用 in 条件批量删除,提高删除效率
erpPurchaseOrganizationMapper.delete(new LambdaQueryWrapperX<ErpPurchaseOrganizationDO>().in(ErpPurchaseOrganizationDO::getNumber, result.deleteNumbers));
erpConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
myRedisConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
}
}
@@ -242,6 +249,6 @@ public class ErpPurchaseOrganizationServiceImpl implements ErpPurchaseOrganizati
Map<String, Long> existingNumbers = erpPurchaseOrganizationMapper.selectList(new LambdaQueryWrapperX<ErpPurchaseOrganizationDO>())
.stream()
.collect(Collectors.toMap(ErpPurchaseOrganizationDO::getNumber, ErpPurchaseOrganizationDO::getId));
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}
}

View File

@@ -7,8 +7,9 @@ import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpSalesOrganizationPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpSalesOrganizationRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpSalesOrganizationSaveReqVO;
@@ -26,7 +27,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_SALES_ORGANIZATION_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.*;
import static dm.jdbc.util.DriverUtil.log;
/**
@@ -41,6 +42,8 @@ public class ErpSalesOrganizationServiceImpl implements ErpSalesOrganizationServ
@Resource
private ErpSalesOrganizationMapper erpSalesOrganizationMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@@ -110,22 +113,23 @@ public class ErpSalesOrganizationServiceImpl implements ErpSalesOrganizationServ
String funcnr = funcnrEnum.getFuncnr();
//防止缓存数据丢失
String key = "erpMap" + funcnrEnum.getFuncnr();
if (erpConfig.getRedisCacheMap(key).isEmpty()) {
if (myRedisConfig.getRedisCacheMap(key).isEmpty()) {
initializeMap(key);
}
Map<String, Object> req = new HashMap<>();
JSONArray dataArrayALL = new JSONArray();
String factKey ="erpMap"+ OftenEnum.FuncnrEnum.公司代码.getFuncnr();
Map<String,Long> redisCache = erpConfig.getRedisCacheMap(factKey);
Map<String,Long> redisCache = myRedisConfig.getRedisCacheMap(factKey);
if (CollUtil.isEmpty(redisCache)) {
return;
throw exception(ERP_COMPANY_REDIS_NOT_EXISTS);
}
// 1. 调用ERP接口获取数据
for (String number : redisCache.keySet()) {
req.put("BUKRS", number);
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (dataArray == null || dataArray.isEmpty()) {
continue;
}
@@ -138,6 +142,9 @@ public class ErpSalesOrganizationServiceImpl implements ErpSalesOrganizationServ
}
dataArrayALL.addAll(dataArray);
}
if (dataArrayALL.isEmpty()){
throw exception(ERP_SALES_ORGANIZATION_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
ProcessingResult result = processData(dataArrayALL,funcnrEnum);
@@ -156,7 +163,7 @@ public class ErpSalesOrganizationServiceImpl implements ErpSalesOrganizationServ
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnr) {
String key = "erpMap" + funcnr.getFuncnr();
Map<String, Long> numbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> numbers = myRedisConfig.getRedisCacheMap(key);
List<ErpSalesOrganizationDO> toUpdate = new ArrayList<>();
List<ErpSalesOrganizationDO> toInsert = new ArrayList<>();
@@ -207,7 +214,7 @@ public class ErpSalesOrganizationServiceImpl implements ErpSalesOrganizationServ
);
Map<String, Long> numberIdMap = insertedRecords.stream()
.collect(Collectors.toMap(ErpSalesOrganizationDO::getNumber, ErpSalesOrganizationDO::getId));
erpConfig.addRedisCacheMap(result.key,numberIdMap);
myRedisConfig.addRedisCacheMap(result.key,numberIdMap);
}
if (!result.toUpdate.isEmpty()) {
erpSalesOrganizationMapper.updateBatch(result.toUpdate);
@@ -215,7 +222,7 @@ public class ErpSalesOrganizationServiceImpl implements ErpSalesOrganizationServ
if (!result.deleteNumbers.isEmpty()) {
// 使用 in 条件批量删除,提高删除效率
erpSalesOrganizationMapper.delete(new LambdaQueryWrapperX<ErpSalesOrganizationDO>().in(ErpSalesOrganizationDO::getNumber, result.deleteNumbers));
erpConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
myRedisConfig.deleteRedisCacheMap(result.key, result.deleteNumbers);
}
}
@@ -240,6 +247,6 @@ public class ErpSalesOrganizationServiceImpl implements ErpSalesOrganizationServ
Map<String, Long> existingNumbers = erpSalesOrganizationMapper.selectList(new LambdaQueryWrapperX<ErpSalesOrganizationDO>())
.stream()
.collect(Collectors.toMap(ErpSalesOrganizationDO::getNumber, ErpSalesOrganizationDO::getId));
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}
}

View File

@@ -7,8 +7,9 @@ import com.xxl.job.core.handler.annotation.XxlJob;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.common.conf.ErpConfig;
import com.zt.plat.module.erp.common.enums.OftenEnum;
import com.zt.plat.module.erp.utils.ErpConfig;
import com.zt.plat.module.erp.utils.MyRedisConfig;
import com.zt.plat.module.erp.enums.OftenEnum;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpWarehousePageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpWarehouseRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpWarehouseSaveReqVO;
@@ -26,7 +27,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.ERP_WAREHOUSE_NOT_EXISTS;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.*;
import static dm.jdbc.util.DriverUtil.log;
/**
@@ -41,6 +42,8 @@ public class ErpWarehouseServiceImpl implements ErpWarehouseService {
@Resource
private ErpWarehouseMapper erpWarehouseMapper;
@Resource
private MyRedisConfig myRedisConfig;
@Resource
private ErpConfig erpConfig;
@@ -110,22 +113,23 @@ public class ErpWarehouseServiceImpl implements ErpWarehouseService {
String funcnr = funcnrEnum.getFuncnr();
//防止缓存数据丢失
String key = "erpMap" + funcnrEnum.getFuncnr();
if (erpConfig.getRedisCacheMap(key).isEmpty()) {
if (myRedisConfig.getRedisCacheMap(key).isEmpty()) {
initializeMap(key);
}
Map<String, Object> req = new HashMap<>();
JSONArray dataArrayALL = new JSONArray();
String factKey = "erpMap" + OftenEnum.FuncnrEnum.工厂信息.getFuncnr();
Map<String, Long> redisCache = erpConfig.getRedisCacheMap(factKey);
Map<String, Long> redisCache = myRedisConfig.getRedisCacheMap(factKey);
if (CollUtil.isEmpty(redisCache)) {
return;
throw exception(ERP_FACTORY_NOT_EXISTS);
}
// 1. 调用ERP接口获取数据
for (String number : redisCache.keySet()) {
req.put("WERKS", number);
// 1. 调用ERP接口获取数据
JSONArray dataArray = erpConfig.fetchDataFromERP(funcnr, req);
HashMap<String, Object> dataFromERP = erpConfig.fetchDataFromERP(funcnr, req);
JSONArray dataArray = (JSONArray) dataFromERP.get("E_RESP");
if (dataArray == null || dataArray.isEmpty()) {
continue;
}
@@ -136,9 +140,11 @@ public class ErpWarehouseServiceImpl implements ErpWarehouseService {
item.put("WERKS", number);
}
}
dataArrayALL.addAll(dataArray);
}
if (dataArrayALL.isEmpty()){
throw exception(ERP_WAREHOUSE_NOT_EXISTS);
}
// 2. 处理公司数据,区分新增和更新
ProcessingResult result = processData(dataArrayALL, funcnrEnum);
@@ -157,7 +163,7 @@ public class ErpWarehouseServiceImpl implements ErpWarehouseService {
*/
private ProcessingResult processData(JSONArray dataArray, OftenEnum.FuncnrEnum funcnr) {
String key = "erpMap" + funcnr.getFuncnr();
Map<String, Long> numbers = erpConfig.getRedisCacheMap(key);
Map<String, Long> numbers = myRedisConfig.getRedisCacheMap(key);
List<ErpWarehouseDO> toUpdate = new ArrayList<>();
List<ErpWarehouseDO> toInsert = new ArrayList<>();
@@ -207,7 +213,7 @@ public class ErpWarehouseServiceImpl implements ErpWarehouseService {
);
Map<String, Long> numberIdMap = insertedRecords.stream()
.collect(Collectors.toMap(asset -> asset.getFactoryNumber() + "-" + asset.getNumber(), ErpWarehouseDO::getId));
erpConfig.addRedisCacheMap(result.key,numberIdMap);
myRedisConfig.addRedisCacheMap(result.key,numberIdMap);
}
if (!result.toUpdate.isEmpty()) {
erpWarehouseMapper.updateBatch(result.toUpdate);
@@ -216,7 +222,7 @@ public class ErpWarehouseServiceImpl implements ErpWarehouseService {
// 使用流式处理和批处理优化删除逻辑
List<Long> idsToDelete = new ArrayList<>(result.deleteNumbers.values());
erpWarehouseMapper.deleteByIds(idsToDelete);
erpConfig.deleteRedisCacheMap(result.key, new ArrayList<>(result.deleteNumbers.keySet()));
myRedisConfig.deleteRedisCacheMap(result.key, new ArrayList<>(result.deleteNumbers.keySet()));
}
}
@@ -244,6 +250,6 @@ public class ErpWarehouseServiceImpl implements ErpWarehouseService {
String mapKey = asset.getFactoryNumber() + "-" + asset.getNumber();
existingNumbers.put(mapKey, asset.getId());
}
erpConfig.addRedisCacheMap(key, existingNumbers);
myRedisConfig.addRedisCacheMap(key, existingNumbers);
}
}

View File

@@ -0,0 +1,170 @@
package com.zt.plat.module.erp.utils;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zt.plat.module.erp.api.dto.ErpSubmitReqDTO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.*;
import java.util.stream.Collectors;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.erp.enums.ErrorCodeConstants.*;
import static dm.jdbc.util.DriverUtil.log;
@Configuration
public class ErpConfig {
@Value("${erp.address}")
private String erpAddress;
@Value("${erp.sapsys}")
private String sapsys;
/**
* 调用ERP接口获取erp数据
*/
public HashMap<String, Object> fetchDataFromERP(String funcnr, Map<String, Object> req) {
try {
// 构建完整URL
String url = "http://" + erpAddress + "/api/rfc/get";
// 构建请求参数
JSONObject requestBody = new JSONObject();
requestBody.put("sapsys", sapsys);
requestBody.put("funcnr", funcnr); // 获取枚举值
if (req != null) {
requestBody.put("req", req);
}
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 创建HTTP请求实体
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody.toJSONString(), headers);
// 发送POST请求
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
// 解析响应结果
String responseBody = response.getBody();
if (responseBody.isEmpty()) {
throw exception(ERP_NOT_EXISTS);
}
HashMap<String, Object> resMap = new HashMap<>();
JSONObject jsonResponse = JSON.parseObject(responseBody);
if (jsonResponse == null) {
throw exception(ERP_NOT_JSON_EXISTS);
}
// 正确获取E_DATA数组
JSONObject dataObject = jsonResponse.getJSONObject("data");
//判断 result里的succeed是否为true
boolean succeed = jsonResponse.getBoolean("succeed");
if (succeed && "S".equals(dataObject.getString("E_FLAG"))) {
String flag = "S";
JSONArray E_RESP = dataObject.getJSONArray("E_DATA");
String E_MSG = dataObject.getString("E_MSG");
resMap.put("E_RESP", E_RESP);
resMap.put("resStr", E_MSG);
resMap.put("flag", flag);
return resMap;
} else {
String E_MSG = dataObject.getString("E_MSG");
String flag = "E";
resMap.put("E_RESP", null);
resMap.put("resStr", E_MSG);
resMap.put("flag", flag);
return resMap;
}
} catch (Exception e) {
log.error("调用ERP RFC接口失败: {}", e);
throw exception(ERP_ERROR_EXISTS);
}
}
/**
* 调用ERP接口更新erp数据
*/
public HashMap<String, String> pushDataToErp(ErpSubmitReqDTO reqDTO) {
try {
// 构建完整URL
String url = "http://" + erpAddress + "/api/rfc/post";
// 构建请求参数
JSONObject requestBody = new JSONObject();
requestBody.put("uuid", UUID.randomUUID().toString());
requestBody.put("sapsys", sapsys);
requestBody.put("srcsys", "DSC");
requestBody.put("funcnr", reqDTO.getFuncnr());
requestBody.put("bskey", reqDTO.getBskey());
requestBody.put("usrid", reqDTO.getUsrid());
requestBody.put("usrnm", reqDTO.getUsrnm());
if (reqDTO.getSign() != null) {
requestBody.put("sign", reqDTO.getSign());
}
if (reqDTO.getReq() != null) {
requestBody.put("req", reqDTO.getReq());
}
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 创建HTTP请求实体
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody.toJSONString(), headers);
// 发送POST请求
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
// 解析响应结果
String responseBody = response.getBody();
if (responseBody.isEmpty()) {
throw exception(ERP_NOT_EXISTS);
}
JSONObject jsonResponse = JSON.parseObject(responseBody);
if (jsonResponse == null) {
throw exception(ERP_NOT_JSON_EXISTS);
}
HashMap<String, String> resMap = new HashMap<>();
boolean succeed = jsonResponse.getBoolean("succeed");
JSONObject data = jsonResponse.getJSONObject("data");
if (data == null) {
log.error("SAP接口返回值为空," + jsonResponse.getString("msg"));
throw exception(ERP_NOT_EXISTS);
} else if (succeed && "S".equals(data.getString("E_FLAG"))) {
String flag = "S";
String E_RESP = data.getString("E_RESP");
String E_MSG = data.getString("ET_MSG");
resMap.put("E_RESP", E_RESP);
resMap.put("resStr", E_MSG);
resMap.put("flag", flag);
} else if (!succeed && "E".equals(data.getString("E_FLAG"))) {
String flag = "E";
String E_MSG = data.getString("ET_MSG");
if (StrUtil.isBlank(E_MSG)) {
E_MSG = jsonResponse.getString("msg");
}
resMap.put("resStr", E_MSG);
resMap.put("flag", flag);
}
return resMap;
} catch (Exception e) {
log.error("调用ERP RFC接口失败: {}", e);
throw exception(ERP_ERROR_EXISTS);
}
}
}

View File

@@ -0,0 +1,191 @@
package com.zt.plat.module.erp.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.util.*;
import java.util.stream.Collectors;
import static dm.jdbc.util.DriverUtil.log;
/**
* @author wuxz
* @create 2022-06-07 20:54
*/
@Primary
@Configuration
public class MyRedisConfig {
@Resource
private RedisTemplate redisTemplate;
@Bean(value = "MyRedisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 通过 Jackson 组件进行序列化
RedisSerializer<Object> serializer = redisSerializer();
// key 和 value
// 一般来说, redis-key采用字符串序列化
// redis-value采用json序列化 json的体积小可读性高不需要实现serializer接口。
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(serializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(serializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
public RedisSerializer<Object> redisSerializer() {
//创建JSON序列化器
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// objectMapper.enableDefaultTyping()被弃用
objectMapper.activateDefaultTyping(
LaissezFaireSubTypeValidator.instance,
ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.WRAPPER_ARRAY);
serializer.setObjectMapper(objectMapper);
return serializer;
}
//list
public Map<String, List<String>> numbers(JSONArray dataArray, String key, String dataKey) {
// 使用 Redis 获取缓存数据
Map<String, List<String>> numbers = new HashMap<>();
List<String> cachedNumbers = (List<String>) redisTemplate.opsForValue().get(key);
if (cachedNumbers == null) {
cachedNumbers = new ArrayList<>();
}
// 提取有效的 BUKRS 编号
List<String> existingNumbers;
if (dataArray != null) {
// 将dataKey按"-"分割成多个部分
String[] keyParts = dataKey.split("-");
existingNumbers = dataArray.stream()
.filter(Objects::nonNull)
.map(dataJson -> {
JSONObject jsonObject = (JSONObject) dataJson;
// 根据每个部分逐级获取值并拼接
StringBuilder sb = new StringBuilder();
for (String part : keyParts) {
String value = jsonObject.getString(part);
if (value != null) {
if (sb.length() > 0) {
sb.append("-");
}
sb.append(value.trim());
} else {
// 如果某一部分为空,则整个值视为无效
return null;
}
}
return sb.toString();
})
.filter(Objects::nonNull) // 过滤掉无效值
.collect(Collectors.toList());
} else {
existingNumbers = new ArrayList<>();
}
// 找出共同存在的编号
Set<String> cachedNumberSet = new HashSet<>(cachedNumbers != null ? cachedNumbers : new ArrayList<>());
List<String> commonNumbers = existingNumbers.stream()
.filter(cachedNumberSet::contains)
.collect(Collectors.toList());
numbers.put("com", commonNumbers);
//找出需要删除的字段。只有erp查询全部的接口能用到
List<String> deleteNumbers = cachedNumberSet.stream()
.filter(num -> !existingNumbers.contains(num))
.collect(Collectors.toList());
numbers.put("delete", deleteNumbers);
// 找出需要新增的编号
List<String> newNumbers = existingNumbers.stream()
.filter(num -> !cachedNumberSet.contains(num))
.toList();
// 合并所有编号
List<String> allNumbers = new ArrayList<>(cachedNumbers);
allNumbers.addAll(newNumbers);
numbers.put("all", allNumbers);
return numbers;
}
public void updateRedisCache(String key, List<String> allnumbers) {
// 使用 Redis 更新缓存数据
redisTemplate.opsForValue().set(key, allnumbers);
}
public List<String> getRedisCache(String key) {
// 使用 Redis 更新缓存数据
return (List<String>) redisTemplate.opsForValue().get(key);
}
//map
public Map<String, Long> getRedisCacheMap(String key) {
// 使用 Redis 获取缓存数据
Map<String, Long> result = (Map<String, Long>) redisTemplate.opsForHash().entries(key);
return result;
}
public void addRedisCacheMap(String key, Map<String, Long> allnumbers) {
// 使用 Redis 更新缓存数据
redisTemplate.opsForHash().putAll(key, allnumbers);
}
public void deleteRedisCacheMap(String key, List<String> deleteNumbers) {
if (deleteNumbers == null || deleteNumbers.isEmpty()) {
log.debug("No items to delete from Redis hash: {}", key);
return;
}
try {
Object[] keysToDelete = deleteNumbers.toArray(new String[0]);
Long deletedCount = redisTemplate.opsForHash().delete(key, keysToDelete);
log.debug("Deleted" + deletedCount + "items from Redis hash:" + key);
} catch (Exception e) {
log.error("Failed to delete items from Redis hash:" + key, e);
throw e;
}
}
}