优化异常抛出

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

@@ -6,21 +6,43 @@ import com.zt.plat.framework.common.exception.ErrorCode;
public interface ErrorCodeConstants {
// ========== 示例模块 1-001-000-000 ==========
ErrorCode ERP_NOT_EXISTS = new ErrorCode(1_000_000_001, "获取ERP数据为空");
ErrorCode ERP_NOT_JSON_EXISTS = new ErrorCode(1_000_000_002, "ERP接口响应无法解析为JSON");
ErrorCode ERP_ERROR_EXISTS = new ErrorCode(1_000_000_003, "调用ERP RFC接口失败");
ErrorCode ERP_CUSTOMER_NOT_EXISTS = new ErrorCode(1_001_000_001, "ERP客商主数据不存在");
ErrorCode ERP_MATERIAL_NOT_EXISTS = new ErrorCode(1_001_000_002, "ERP物料数据不存在");
ErrorCode ERP_COMPANY_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_BOM_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_BOM_DETAIL_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_PROCESS_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_PROCESS_DETAIL_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_FACTORY_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_COSTCENTER_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_PRODUCTIVE_VERSION_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_PURCHASE_ORGANIZATION_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_INTERNAL_ORDER_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_SALES_ORGANIZATION_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_WAREHOUSE_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_ASSET_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_CONTRACT_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_PRODUCTIVE_ORDER_NOT_EXISTS = new ErrorCode(1_001_000_003, "ERP物料数据不存在");
ErrorCode ERP_MATERIAL_NOT_EXISTS = new ErrorCode(1_002_000_001, "ERP物料数据不存在");
ErrorCode ERP_COMPANY_NOT_EXISTS = new ErrorCode(1_003_000_001, "ERP公司数据不存在");
ErrorCode ERP_COMPANY_REDIS_NOT_EXISTS = new ErrorCode(1_003_000_002, "ERP公司代码缓存数据丢失,请重新同步公司代码");
ErrorCode ERP_BOM_NOT_EXISTS = new ErrorCode(1_004_000_001, "ERPBOM数据不存在");
ErrorCode ERP_BOM_DETAIL_NOT_EXISTS = new ErrorCode(1_005_000_001, "ERPBOM明细数据不存在");
ErrorCode ERP_PROCESS_NOT_EXISTS = new ErrorCode(1_006_000_001, "ERP工艺路线数据不存在");
ErrorCode ERP_PROCESS_DETAIL_NOT_EXISTS = new ErrorCode(1_007_000_001, "ERP工艺路线明细数据不存在");
ErrorCode ERP_FACTORY_NOT_EXISTS = new ErrorCode(1_008_000_001, "ERP工厂数据不存在");
ErrorCode ERP_FACTORY_REDIS_NOT_EXISTS = new ErrorCode(1_008_000_002, "ERP工厂redis数据不存在");
ErrorCode ERP_COSTCENTER_NOT_EXISTS = new ErrorCode(1_009_000_001, "ERP成本中心数据不存在");
ErrorCode ERP_PRODUCTIVE_VERSION_NOT_EXISTS = new ErrorCode(1_010_000_001, "ERP生产版本数据不存在");
ErrorCode ERP_PURCHASE_ORGANIZATION_NOT_EXISTS = new ErrorCode(1_011_000_001, "ERP采购组织数据不存在");
ErrorCode ERP_INTERNAL_ORDER_NOT_EXISTS = new ErrorCode(1_012_000_001, "ERP内部订单数据不存在");
ErrorCode ERP_SALES_ORGANIZATION_NOT_EXISTS = new ErrorCode(1_013_000_001, "ERP销售组织数据不存在");
ErrorCode ERP_WAREHOUSE_NOT_EXISTS = new ErrorCode(1_014_000_001, "ERP库位数据不存在");
ErrorCode ERP_ASSET_NOT_EXISTS = new ErrorCode(1_015_000_001, "ERP资产卡片数据不存在");
ErrorCode ERP_CONTRACT_NOT_EXISTS = new ErrorCode(1_016_000_001, "ERP合同数据不存在");
ErrorCode ERP_PRODUCTIVE_ORDER_NOT_EXISTS = new ErrorCode(1_017_000_001, "ERP生产订单数据不存在");
}

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,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

@@ -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);
}
}

Some files were not shown because too many files have changed in this diff Show More