Commit 57f2a9aa by 杨卫

feat:人员适配场景到4s店维度,历史数据处理代码

1 parent f0cccef6
package cn.com.glsx.dj.smartcarlife.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class StaffInfoBackUp {
private String phoneNumber;
private String staffUuid;
private Integer merchantId;
private String deletedFlag;
}
package cn.com.glsx.dj.smartcarlife.mapper;
import cn.com.glsx.dj.smartcarlife.model.SceneMerchantRefNew;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface SceneMerchantRefNewBackUpMapper {
List<SceneMerchantRefNew> query4sByYqMerchantList(@Param("yqMerchantIdList") List<Integer> yqMerchantIdList);
}
package cn.com.glsx.dj.smartcarlife.mapper;
import cn.com.glsx.dj.smartcarlife.model.StaffAccountAssignRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface StaffAccountAssignRecordHisDataMapper {
int insertSelective(StaffAccountAssignRecord record);
int countByStaffPhoneAndYqMerchantIdAndMerchantId(@Param("staffPhone") String staffPhone,
@Param("yqMerchantId") Integer yqMerchantId,
@Param("merchantId") Integer merchantId);
}
package cn.com.glsx.dj.smartcarlife.mapper;
import cn.com.glsx.dj.smartcarlife.model.StaffAccountAssignScene;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StaffAccountAssignSceneHisDataMapper {
int insertSelective(StaffAccountAssignScene scene);
int batchInsert(@org.apache.ibatis.annotations.Param("list") List<StaffAccountAssignScene> list);
}
package cn.com.glsx.dj.smartcarlife.mapper;
import cn.com.glsx.dj.smartcarlife.model.StaffInfoBackUp;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StaffInfoBackUpMapper {
List<StaffInfoBackUp> selectAllByPage(@org.apache.ibatis.annotations.Param("offset") int offset,
@org.apache.ibatis.annotations.Param("limit") int limit);
}
...@@ -234,6 +234,9 @@ public class StaffInfoService { ...@@ -234,6 +234,9 @@ public class StaffInfoService {
staffTargetCheckService.initTargetCheckData(staffInfo); staffTargetCheckService.initTargetCheckData(staffInfo);
} }
// 同步staffAccountAssignRecord的deleted_flag,与staffInfo保持一致
syncStaffAccountAssignRecordDeleteFlag(staffInfo);
try { try {
//前店后厂权限更新 //前店后厂权限更新
updateQdhcPermission(staffInfo); updateQdhcPermission(staffInfo);
...@@ -456,6 +459,29 @@ public class StaffInfoService { ...@@ -456,6 +459,29 @@ public class StaffInfoService {
return relation; return relation;
} }
/**
* 同步staffAccountAssignRecord的deleted_flag,与staffInfo保持一致
*/
public void syncStaffAccountAssignRecordDeleteFlag(StaffInfo staffInfo) {
String deletedFlag = staffInfo.getDeletedFlag();
String staffPhone = staffInfo.getPhoneNumber();
Integer yqMerchantId = staffInfo.getMerchantId();
if (StringUtils.isBlank(staffPhone) || StringUtils.isBlank(deletedFlag) || yqMerchantId == null) {
return;
}
Example example = new Example(StaffAccountAssignRecord.class);
example.createCriteria()
.andEqualTo("staffPhone", staffPhone)
.andEqualTo("yqMerchantId", yqMerchantId);
StaffAccountAssignRecord updateRecord = new StaffAccountAssignRecord();
updateRecord.setDeletedFlag(deletedFlag);
updateRecord.setYqMerchantName(staffInfo.getMerchantName());
updateRecord.setUpdatedBy(CommonConstants.DEFAULT_UPDATE_BY);
updateRecord.setUpdatedDate(new Date());
staffAccountAssignRecordMapper.updateByExampleSelective(updateRecord, example);
log.info("同步员工账号分配记录deleted_flag, staffPhone={}, deletedFlag={}", staffPhone, deletedFlag);
}
public Page<StaffInfoDTO> listWithPage(RpcPagination<StaffInfoDTO> pagination){ public Page<StaffInfoDTO> listWithPage(RpcPagination<StaffInfoDTO> pagination){
RpcAssert.assertNotNull(pagination, DefaultErrorEnum.DATA_NULL, "RpcPagination not be null"); RpcAssert.assertNotNull(pagination, DefaultErrorEnum.DATA_NULL, "RpcPagination not be null");
......
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.com.glsx.dj.smartcarlife.mapper.SceneMerchantRefNewBackUpMapper">
<resultMap id="BaseResultMap" type="cn.com.glsx.dj.smartcarlife.model.SceneMerchantRefNew">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="main_merchant_id" property="mainMerchantId" jdbcType="INTEGER" />
<result column="main_merchant_name" property="mainMerchantName" jdbcType="VARCHAR" />
<result column="sub_merchant_id" property="subMerchantId" jdbcType="INTEGER" />
<result column="sub_merchant_name" property="subMerchantName" jdbcType="VARCHAR" />
<result column="scene" property="scene" jdbcType="TINYINT" />
<result column="base_scene_code" property="baseSceneCode" jdbcType="VARCHAR" />
<result column="main_merchant_type" property="mainMerchantType" jdbcType="TINYINT" />
<result column="sub_merchant_type" property="subMerchantType" jdbcType="TINYINT" />
<result column="status" property="status" jdbcType="TINYINT" />
<result column="created_by" property="createdBy" jdbcType="VARCHAR" />
<result column="created_date" property="createdDate" jdbcType="TIMESTAMP" />
<result column="updated_by" property="updatedBy" jdbcType="VARCHAR" />
<result column="updated_date" property="updatedDate" jdbcType="TIMESTAMP" />
<result column="deleted_flag" property="deletedFlag" jdbcType="CHAR" />
</resultMap>
<select id="query4sByYqMerchantList" resultMap="BaseResultMap">
SELECT for4s.main_merchant_id, for4s.main_merchant_name, for4s.base_scene_code, for4s.scene,
for4s.sub_merchant_id, for4s.sub_merchant_name
FROM dj_car_life_scene_merchant_ref_new_back_up for4s
WHERE for4s.sub_merchant_id IN
<foreach collection="yqMerchantIdList" item="item" open="(" separator="," close=")">
#{item}
</foreach>
AND for4s.main_merchant_type = 2
AND for4s.deleted_flag = 'N'
AND for4s.status = 1
</select>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.com.glsx.dj.smartcarlife.mapper.StaffAccountAssignRecordHisDataMapper">
<resultMap id="BaseResultMap" type="cn.com.glsx.dj.smartcarlife.model.StaffAccountAssignRecord">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="staff_uuid" property="staffUuid" jdbcType="VARCHAR" />
<result column="staff_phone" property="staffPhone" jdbcType="VARCHAR" />
<result column="job_type" property="jobType" jdbcType="TINYINT" />
<result column="staff_scene_label" property="staffSceneLabel" jdbcType="VARCHAR" />
<result column="merchant_id" property="merchantId" jdbcType="INTEGER" />
<result column="merchant_name" property="merchantName" jdbcType="VARCHAR" />
<result column="yq_merchant_id" property="yqMerchantId" jdbcType="INTEGER" />
<result column="yq_merchant_name" property="yqMerchantName" jdbcType="VARCHAR" />
<result column="created_source" property="createdSource" jdbcType="TINYINT" />
<result column="station_type" property="stationType" jdbcType="TINYINT" />
<result column="created_by" property="createdBy" jdbcType="VARCHAR" />
<result column="created_date" property="createdDate" jdbcType="TIMESTAMP" />
<result column="updated_by" property="updatedBy" jdbcType="VARCHAR" />
<result column="updated_date" property="updatedDate" jdbcType="TIMESTAMP" />
<result column="deleted_flag" property="deletedFlag" jdbcType="CHAR" />
</resultMap>
<insert id="insertSelective" parameterType="cn.com.glsx.dj.smartcarlife.model.StaffAccountAssignRecord" useGeneratedKeys="true" keyProperty="id">
INSERT INTO dj_car_life_staff_account_assign_record_handle_his_data
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="staffUuid != null">staff_uuid,</if>
<if test="staffPhone != null">staff_phone,</if>
<if test="jobType != null">job_type,</if>
<if test="staffSceneLabel != null">staff_scene_label,</if>
<if test="merchantId != null">merchant_id,</if>
<if test="merchantName != null">merchant_name,</if>
<if test="yqMerchantId != null">yq_merchant_id,</if>
<if test="yqMerchantName != null">yq_merchant_name,</if>
<if test="createdSource != null">created_source,</if>
<if test="createdBy != null">created_by,</if>
<if test="createdDate != null">created_date,</if>
<if test="updatedBy != null">updated_by,</if>
<if test="updatedDate != null">updated_date,</if>
<if test="deletedFlag != null">deleted_flag,</if>
</trim>
<trim prefix="VALUES (" suffix=")" suffixOverrides=",">
<if test="staffUuid != null">#{staffUuid,jdbcType=VARCHAR},</if>
<if test="staffPhone != null">#{staffPhone,jdbcType=VARCHAR},</if>
<if test="jobType != null">#{jobType,jdbcType=TINYINT},</if>
<if test="staffSceneLabel != null">#{staffSceneLabel,jdbcType=VARCHAR},</if>
<if test="merchantId != null">#{merchantId,jdbcType=INTEGER},</if>
<if test="merchantName != null">#{merchantName,jdbcType=VARCHAR},</if>
<if test="yqMerchantId != null">#{yqMerchantId,jdbcType=INTEGER},</if>
<if test="yqMerchantName != null">#{yqMerchantName,jdbcType=VARCHAR},</if>
<if test="createdSource != null">#{createdSource,jdbcType=TINYINT},</if>
<if test="createdBy != null">#{createdBy,jdbcType=VARCHAR},</if>
<if test="createdDate != null">#{createdDate,jdbcType=TIMESTAMP},</if>
<if test="updatedBy != null">#{updatedBy,jdbcType=VARCHAR},</if>
<if test="updatedDate != null">#{updatedDate,jdbcType=TIMESTAMP},</if>
<if test="deletedFlag != null">#{deletedFlag,jdbcType=CHAR},</if>
</trim>
</insert>
<select id="countByStaffPhoneAndYqMerchantIdAndMerchantId" resultType="java.lang.Integer">
SELECT COUNT(1)
FROM dj_car_life_staff_account_assign_record_handle_his_data
WHERE staff_phone = #{staffPhone,jdbcType=VARCHAR}
AND yq_merchant_id = #{yqMerchantId,jdbcType=INTEGER}
AND merchant_id = #{merchantId,jdbcType=INTEGER}
AND deleted_flag = 'N'
</select>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.com.glsx.dj.smartcarlife.mapper.StaffAccountAssignSceneHisDataMapper">
<resultMap id="BaseResultMap" type="cn.com.glsx.dj.smartcarlife.model.StaffAccountAssignScene">
<id column="id" property="id" jdbcType="BIGINT" />
<result column="account_record_id" property="accountRecordId" jdbcType="INTEGER" />
<result column="base_scene_code" property="baseSceneCode" jdbcType="VARCHAR" />
<result column="scene_type" property="sceneType" jdbcType="TINYINT" />
<result column="deleted_flag" property="deletedFlag" jdbcType="CHAR" />
<result column="created_by" property="createdBy" jdbcType="VARCHAR" />
<result column="created_date" property="createdDate" jdbcType="TIMESTAMP" />
<result column="updated_by" property="updatedBy" jdbcType="VARCHAR" />
<result column="updated_date" property="updatedDate" jdbcType="TIMESTAMP" />
</resultMap>
<insert id="insertSelective" parameterType="cn.com.glsx.dj.smartcarlife.model.StaffAccountAssignScene" useGeneratedKeys="true" keyProperty="id">
INSERT INTO dj_car_life_staff_account_assign_scene_handle_his_data
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="accountRecordId != null">account_record_id,</if>
<if test="baseSceneCode != null">base_scene_code,</if>
<if test="sceneType != null">scene_type,</if>
<if test="deletedFlag != null">deleted_flag,</if>
<if test="createdBy != null">created_by,</if>
<if test="createdDate != null">created_date,</if>
<if test="updatedBy != null">updated_by,</if>
<if test="updatedDate != null">updated_date,</if>
</trim>
<trim prefix="VALUES (" suffix=")" suffixOverrides=",">
<if test="accountRecordId != null">#{accountRecordId,jdbcType=INTEGER},</if>
<if test="baseSceneCode != null">#{baseSceneCode,jdbcType=VARCHAR},</if>
<if test="sceneType != null">#{sceneType,jdbcType=TINYINT},</if>
<if test="deletedFlag != null">#{deletedFlag,jdbcType=CHAR},</if>
<if test="createdBy != null">#{createdBy,jdbcType=VARCHAR},</if>
<if test="createdDate != null">#{createdDate,jdbcType=TIMESTAMP},</if>
<if test="updatedBy != null">#{updatedBy,jdbcType=VARCHAR},</if>
<if test="updatedDate != null">#{updatedDate,jdbcType=TIMESTAMP},</if>
</trim>
</insert>
<insert id="batchInsert" parameterType="java.util.List">
INSERT INTO dj_car_life_staff_account_assign_scene_handle_his_data
(account_record_id, base_scene_code, scene_type, deleted_flag, created_by, created_date, updated_by, updated_date)
VALUES
<foreach collection="list" item="item" separator=",">
(#{item.accountRecordId,jdbcType=INTEGER},
#{item.baseSceneCode,jdbcType=VARCHAR},
#{item.sceneType,jdbcType=TINYINT},
#{item.deletedFlag,jdbcType=CHAR},
#{item.createdBy,jdbcType=VARCHAR},
#{item.createdDate,jdbcType=TIMESTAMP},
#{item.updatedBy,jdbcType=VARCHAR},
#{item.updatedDate,jdbcType=TIMESTAMP})
</foreach>
</insert>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.com.glsx.dj.smartcarlife.mapper.StaffInfoBackUpMapper">
<resultMap id="BaseResultMap" type="cn.com.glsx.dj.smartcarlife.model.StaffInfoBackUp">
<result column="phone_number" property="phoneNumber" jdbcType="VARCHAR" />
<result column="staff_uuid" property="staffUuid" jdbcType="VARCHAR" />
<result column="merchant_id" property="merchantId" jdbcType="INTEGER" />
<result column="deleted_flag" property="deletedFlag" jdbcType="CHAR" />
</resultMap>
<select id="selectAllByPage" resultMap="BaseResultMap">
SELECT phone_number, staff_uuid, merchant_id, deleted_flag
FROM dj_car_life_staff_info_back_up
LIMIT #{offset}, #{limit}
</select>
</mapper>
package cn.com.glsx.dj.smartcarlife.service;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@Slf4j
class StaffHistoryDataHandlerTest extends BaseServiceTest {
@Autowired
private StaffHistoryDataHandler staffHistoryDataHandler;
@Test
void executeTest() {
staffHistoryDataHandler.execute();
log.info("历史数据处理完成");
}
}
...@@ -96,4 +96,13 @@ class StaffInfoServiceTest extends BaseServiceTest { ...@@ -96,4 +96,13 @@ class StaffInfoServiceTest extends BaseServiceTest {
staffInfo.setStaffUuid("9cc1346958ee423da342ad402d93dcfb"); staffInfo.setStaffUuid("9cc1346958ee423da342ad402d93dcfb");
staffInfoService.syncStaffAccountAssignScene(staffInfo); staffInfoService.syncStaffAccountAssignScene(staffInfo);
} }
@Test
void syncStaffDeleteFlagTest() {
StaffInfo staffInfo = new StaffInfo();
staffInfo.setMerchantId(44211545);
staffInfo.setPhoneNumber("17823412376");
staffInfo.setDeletedFlag(StaffStatusEnum.INVALID.getName());
staffInfoService.syncStaffAccountAssignRecordDeleteFlag(staffInfo);
}
} }
\ No newline at end of file \ No newline at end of file
  • @wangxw1
    commented
    Master

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

    1. 分页查询未指定 ORDER BY,可能导致数据重复或遗漏(功能与稳定性)

    • 问题描述:StaffInfoBackUpMapper.xml 中的 selectAllByPage 方法仅使用了 LIMIT #{offset}, #{limit} 进行分页,但没有指定 ORDER BY 排序规则。
    • 影响分析:在关系型数据库中,未指定排序规则的查询结果顺序是不可靠的。随着数据的读取或内部存储页面的变化,多次查询同一张表可能返回乱序的数据,导致历史数据清洗时部分记录被重复处理,部分记录被完全遗漏。
    • 优化建议:在 SQL 中加上明确的、具备唯一性的排序字段(如自增主键或 staff_uuid)。 xml <select id="selectAllByPage" resultMap="BaseResultMap"> SELECT phone_number, staff_uuid, merchant_id, deleted_flag FROM dj_car_life_staff_info_back_up ORDER BY staff_uuid ASC LIMIT #{offset}, #{limit} </select>

    2. 线程池拒绝策略可能导致主线程永久阻塞(健壮性)

    • 问题描述:在 StaffHistoryDataHandler.execute() 中,使用 taskExecutor.execute(...) 提交任务,并依赖 latch.countDown() 释放主线程。如果线程池队列已满并触发拒绝策略(抛出 RejectedExecutionException),任务根本不会进入线程执行,finally 块中的 latch.countDown() 也不会被调用。
    • 影响分析:主线程在最后执行 latch.await() 时,由于 CountDownLatch 的计数器永远无法归零,主线程将陷入永久阻塞(死锁),导致应用假死或定时任务卡住。
    • 优化建议:在包裹 execute 的外层捕获异常,确保发生拒绝策略时也能正确减少计数。 java CountDownLatch latch = new CountDownLatch(1); latches.add(latch); try { taskExecutor.execute(() -> { try { self.processBatch(batchCopy); } finally { latch.countDown(); } }); } catch (Exception e) { log.error("提交异步任务失败", e); latch.countDown(); // 确保异常时释放 latch }

    3. 循环内逐条插入数据库,存在严重性能瓶颈(性能与最佳实践)

    • 问题描述:在 StaffHistoryDataHandler.processOneStaff 方法中,遍历 shopDto.getSceneList() 时,逐条调用 sceneHisDataMapper.insertSelective(scene) 插入数据。
    • 影响分析:在循环中执行单条数据库 DML 操作会产生大量的网络 I/O 开销和数据库连接消耗。这在处理数十万历史数据时,会极大拖慢整体执行速度,甚至造成数据库连接池被打满。
    • 优化建议:利用已经写好的 batchInsert 方法,将对象收集到 List 中后进行批量插入。 java List<StaffAccountAssignScene> sceneListToInsert = new ArrayList<>(); for (YqMerchant4sSceneDTO.SceneDetailDTO sceneDto : shopDto.getSceneList()) { StaffAccountAssignScene scene = StaffAccountAssignScene.builder() // ... 属性赋值 .build(); sceneListToInsert.add(scene); } if (CollectionUtils.isNotEmpty(sceneListToInsert)) { sceneHisDataMapper.batchInsert(sceneListToInsert); }

    二、评分明细

    1. 功能实现的正确性与健壮性(40分):25分
      • 扣分理由:分页查询缺失排序字段导致数据处理不可靠;多线程协调逻辑存在严重的死锁隐患。
    2. 安全性与潜在风险(30分):30分
      • 评分理由:未发现 SQL 注入、越权等明显安全漏洞。未触发 MyBatis 子查询规则,Dubbo 接口无违规变更。
    3. 最佳实践与可维护性(20分):12分
      • 扣分理由:多线程任务使用 CountDownLatch 的姿势不够严谨;在 XML 中 query4sByYqMerchantList 的 <foreach> 未对集合判空,若传入空集合会导致 SQL 语法错误,缺乏防御性。
    4. 性能与资源利用(5分):2分
      • 扣分理由:在 for 循环内部逐条调用单条 insert 插入数据库,未充分利用批量插入能力,性能较差。
    5. 提交信息质量(5分):5分
      • 评分理由:Commit 信息("feat:人员适配场景到4s店维度,历史数据处理代码")清晰明了,准确描述了改动目的。

    总分:74分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!