Commit f575e641 by 范富荣

feat:优化产品自动创建定时任务,新增延保服务属性默认值

1 parent e46a3b53
......@@ -94,7 +94,7 @@ public class AutoCreateGoodsMappingService {
// 3. 在创建前,先预计算新服务编码(insertOrUpdateExtend内部会自动生成,这里提前计算用于后续查询)
String newServiceCode = serviceItemsService.getNextExtendServiceCode();
log.info("旧服务编码[{}]对应映射新服务编码[{}]", oldServiceCode, newServiceCode);
// 4. 构建ServiceItemsExtendCreatedDTO(服务项目信息)
ServiceItemsExtendCreatedDTO dto = buildServiceItemsExtendCreatedDTO(oldServiceItem);
......@@ -144,52 +144,28 @@ public class AutoCreateGoodsMappingService {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "调用insertOrUpdateExtend创建服务项目失败");
}
// 7.1 插入延保服务属性模板数据
serviceItemsService.insertExtendServiceAttributeTemplate(newServiceCode, CREATED_BY);
// 8. 查询新创建的规格配置id列表
List<ExtendItemSpecificationConfig> newConfigs =
extendItemSpecificationConfigService.listByServiceCode(newServiceCode);
if (CollectionUtils.isEmpty(newConfigs)) {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "创建新配置失败,新服务编码[" + newServiceCode + "]下无规格配置");
}
// 9. 建立旧配置id到新配置id的映射关系
// 按规格数据(specificationData)匹配新旧配置
Map<String, List<ExtendItemSpecificationConfig>> newConfigMap = newConfigs.stream()
.collect(Collectors.groupingBy(ExtendItemSpecificationConfig::getSpecificationData));
// 10. 更新映射记录
Date now = new Date();
List<Integer> newConfigIds = new ArrayList<>();
List<Integer> oldConfigIds = new ArrayList<>();
for (AutoCreateGoodsMapping mapping : configMappings) {
//ExtendItemSpecificationConfig oldConfig = queryOldConfig(mapping.getOldConfigId());
//Integer newConfigId = matchNewConfigId(oldConfig, newConfigMap);
AutoCreateGoodsMapping update = new AutoCreateGoodsMapping();
update.setId(mapping.getId());
update.setNewServiceCode(newServiceCode);
//update.setNewConfigId(newConfigId);
update.setProcessStatus(1);
update.setUpdatedBy(CREATED_BY);
update.setUpdatedDate(now);
autoCreateGoodsMappingMapper.updateNewCodeAndConfigId(update);
// if (newConfigId != null) {
// newConfigIds.add(newConfigId);
// }
oldConfigIds.add(mapping.getOldConfigId());
}
// 11. 根据新配置id,软删除extend_item_specification_config记录
//extendItemSpecificationConfigService.batchDeleteByIds(newConfigIds, CREATED_BY);
// log.info("根据新配置id软删除extend_item_specification_config,数量: {}", newConfigIds.size());
//
// // 12. 根据旧配置id,软删除extend_item_specification_config记录
// extendItemSpecificationConfigService.batchDeleteByIds(oldConfigIds, CREATED_BY);
// log.info("根据旧配置id软删除extend_item_specification_config,数量: {}", oldConfigIds.size());
//
// // 13. 根据旧配置id,软删除items_vehicle_series_config记录
// extendItemSpecificationConfigService.batchDeleteVehicleSeriesByConfigIds(oldConfigIds, CREATED_BY);
// log.info("根据旧配置id软删除items_vehicle_series_config,数量: {}", oldConfigIds.size());
}
/**
......
......@@ -41,6 +41,7 @@ import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
......@@ -495,4 +496,47 @@ public class ServiceItemsService {
public ServiceItems selectOneByExample(Example example) {
return serviceItemsMapper.selectOneByExample(example);
}
/**
* 插入延保服务属性模板数据
* @param newServiceCode 新服务编码
* @param createdBy 创建人
*/
public void insertExtendServiceAttributeTemplate(String newServiceCode, String createdBy) {
Date now = new Date();
List<ServiceItemsAttribute> attributes = new ArrayList<>();
attributes.add(buildAttribute(newServiceCode, "服务期限", "service_period", "select", "service_period", 1, 1, 1, createdBy, now));
attributes.add(buildAttribute(newServiceCode, "购车发票日期", "vehicle_buy_date", "datePicker", "vehicle_buy_date", 1, 20, 2, createdBy, now));
attributes.add(buildAttribute(newServiceCode, "原厂质保开始日期", "vehicle_begin_date", "datePicker", "vehicle_begin_date", 1, 20, 2, createdBy, now));
attributes.add(buildAttribute(newServiceCode, "原厂质保年限", "warranty_period", "input", "warranty_period", 1, 7, 5, createdBy, now));
attributes.add(buildAttribute(newServiceCode, "原厂质保公里(万公里)", "warranty_km", "input", "warranty_km", 1, 10, 6, createdBy, now));
attributes.add(buildAttribute(newServiceCode, "排量或者功率", "vehicle_displacement_power", "select", "vehicle_displacement_power", 1, 10, 4, createdBy, now));
attributes.add(buildAttribute(newServiceCode, "排量或者功率值", "vehicle_displacement_power_value", "input", "vehicle_displacement_power_value", 1, 10, 4, createdBy, now));
attributes.add(buildAttribute(newServiceCode, "保障销售时行驶里程(km)", "mileage", "input", "mileage", 1, 10, 7, createdBy, now));
attributes.add(buildAttribute(newServiceCode, "购车类型", "buy_car_type", "select", "buy_car_type", 1, 1, 3, createdBy, now));
attributes.add(buildAttribute(newServiceCode, "第一受益人", "compensation_name", "input", "compensation_name", 1, 50, 8, createdBy, now));
serviceItemsAttributeMapper.insertList(attributes);
}
private ServiceItemsAttribute buildAttribute(String serviceCode, String desc, String name,
String type, String group,
int quantity, int maxLength, int sort,
String createdBy, Date now) {
return ServiceItemsAttribute.builder()
.serviceCode(serviceCode)
.attributeDesc(desc)
.attributeName(name)
.attributeType(type)
.attributeGroup(group)
.quantity(quantity)
.maxLength(maxLength)
.sort(sort)
.required(1)
.createdBy(createdBy)
.createdDate(now)
.updatedBy(createdBy)
.updatedDate(now)
.deletedFlag(BaseModel.DeletedFlagEnum.N.name())
.build();
}
}
  • @wangxw1
    commented
    Owner

    一、关键问题与优化建议(Top 3)

    1. 性能瓶颈:for 循环中执行单条数据库更新

    • 问题描述:在 AutoCreateGoodsMappingService.java 的第 155 行起,代码在 for (AutoCreateGoodsMapping mapping : configMappings) 循环内部直接调用了 autoCreateGoodsMappingMapper.updateNewCodeAndConfigId(update) 进行数据库单条更新。
    • 影响分析:如果 configMappings 集合数据量较大,会导致频繁获取数据库连接并产生大量网络 I/O 开销,严重影响执行性能,甚至可能耗尽数据库连接池。
    • 优化建议:建议在 MyBatis 层实现批量更新(Batch Update),或者在业务层提取需要更新的 id 集合,通过一条 SQL 语句完成 newServiceCode 等字段的更新。 代码示例: ```java // 业务层收集ID List mappingIds = configMappings.stream() .map(AutoCreateGoodsMapping::getId) .collect(Collectors.toList());

    if (CollectionUtils.isNotEmpty(mappingIds)) { AutoCreateGoodsMapping updateParam = new AutoCreateGoodsMapping(); updateParam.setNewServiceCode(newServiceCode); updateParam.setProcessStatus(1); updateParam.setUpdatedBy(CREATED_BY); updateParam.setUpdatedDate(now); // 调用批量更新 Mapper autoCreateGoodsMappingMapper.batchUpdateNewCodeByIds(updateParam, mappingIds); }

    
    **2. 健壮性风险:多步写入操作的事务一致性**
    - **问题描述**:当前方法依次执行了 `insertOrUpdateExtend`(可能包含写操作)、`insertExtendServiceAttributeTemplate`(批量插入 10 条数据)以及后续的映射表状态更新操作。
    - **影响分析**:由于未在 Diff 中看到 `@Transactional` 注解(可能在方法签名或类上,需确认),如果后续的映射表更新(步骤 10)发生异常,可能会导致前面插入的“延保服务属性模板数据”成为无主或不一致的脏数据。
    - **优化建议**:请确认当前方法(或其调用链路的入口)是否被 `@Transactional(rollbackFor = Exception.class)` 覆盖。由于涉及多个表的强一致性写入,必须确保整个方法在一个事务中执行。
    
    **3. 可维护性:遗留大段被注释的废弃代码**
    - **问题描述**:在 `AutoCreateGoodsMappingService.java` 中,删除了部分原有逻辑,但留下了大量被注释的代码(如 `//ExtendItemSpecificationConfig oldConfig = ...` 以及步骤 11、12、13 的软删除逻辑)。
    - **影响分析**:大段无用的注释代码会产生“破窗效应”,严重干扰代码阅读,降低代码的可维护性。Git 等版本控制系统已经记录了历史,无需在代码中保留废弃逻辑。
    - **优化建议**:直接删除这些被注释掉的无用代码,保持代码整洁。同时,`ServiceItemsService.java` 中引入的 `java.util.Collections` 未被使用,建议一并清理。
    
    ---
    
    #### 二、评分明细
    
    - **功能实现的正确性与健壮性(36 / 40分)**
      **理由**:核心业务逻辑(插入属性默认值、更新映射)实现正确,使用了 `Builder` 模式构建对象且利用了批量插入(`insertList`),逻辑清晰。扣除 4 分是因为多步数据库写操作的事务一致性需要严格保障(如上文问题2所述)。未触犯子查询和 Dubbo 接口变更的红线。
    
    - **安全性与潜在风险(30 / 30分)**
      **理由**:代码中未见 SQL 拼接注入风险,属性构建使用了强类型和常量,未发现越权或敏感信息泄露的安全隐患。
    
    - **最佳实践与可维护性(16 / 20分)**
      **理由**:`ServiceItemsService` 中抽离了 `buildAttribute` 方法,复用性好,符合单一职责原则。扣除 4 分是因为代码中遗留了大段被注释的废弃代码,以及存在无用的 `import`,不符合 Clean Code 规范。
    
    - **性能与资源利用(2 / 5分)**
      **理由**:在 `for` 循环中循环调用 Mapper 进行单条 Update 操作是典型的性能反模式,在数据量上升时会成为明显的性能瓶颈。
    
    - **提交信息质量(5 / 5分)**
      **理由**:Commit 信息 `feat:优化产品自动创建定时任务,新增延保服务属性默认值 (by fanfr)` 格式规范,清晰说明了变更的目的与内容,具有良好的可追溯性。
    
    总分:89分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!