Commit 0435a039 by 林永健

refactor(staff): 优化员工信息查询逻辑

- 添加员工业务场景类型查询方法 findStaffBusinessSceneTypes
- 添加员工关联门店信息查询方法 findStaffMerchantInfo
- 添加门店区域运营信息查询方法 findMerchantSuperiors
- 重构 listStaffStudyInfo 方法实现分步查询策略
- 新增 appendStaffBusinessSceneTypes 方法补充业务场景类型
- 新增 appendStaffMerchantInfo 方法补充门店信息
- 新增 appendMerchantSuperiors 方法补充上级信息
- 简化 findByStaffStudy 查询只获取员工基本信息
- 添加业务场景类型、门店信息等新的查询接口
- 优化SQL查询避免复杂连接导致的索引失效问题
1 parent bfbfb1b6
...@@ -39,6 +39,9 @@ public class StaffStudyDetailInfoDTO implements Serializable { ...@@ -39,6 +39,9 @@ public class StaffStudyDetailInfoDTO implements Serializable {
private String merchantIds; private String merchantIds;
@JsonIgnore
private Integer merchantId;
@ApiModelProperty(notes = "业务类型") @ApiModelProperty(notes = "业务类型")
private String businessSceneTypeNames; private String businessSceneTypeNames;
......
...@@ -51,6 +51,27 @@ public interface StaffInfoMapper extends OreMapper<StaffInfo> { ...@@ -51,6 +51,27 @@ public interface StaffInfoMapper extends OreMapper<StaffInfo> {
List<StaffStudyDetailInfoDTO> findByStaffStudy(StaffStudyDetailInfoDTO dto); List<StaffStudyDetailInfoDTO> findByStaffStudy(StaffStudyDetailInfoDTO dto);
/**
* 查询员工的业务场景类型(近30天订单)
* @param staffPhones 员工手机号列表
* @return 员工业务场景类型信息
*/
List<StaffStudyDetailInfoDTO> findStaffBusinessSceneTypes(@Param("staffPhones") List<String> staffPhones);
/**
* 查询员工关联的门店信息
* @param staffPhones 员工手机号列表
* @return 员工门店信息
*/
List<StaffStudyDetailInfoDTO> findStaffMerchantInfo(@Param("staffPhones") List<String> staffPhones);
/**
* 查询门店的区域运营(上级)信息
* @param merchantIds 门店ID列表
* @return 门店上级信息
*/
List<StaffStudyDetailInfoDTO> findMerchantSuperiors(@Param("merchantIds") List<Integer> merchantIds);
StaffInfo getLatestOrderStaff(@Param("staffPhone") String staffPhone, StaffInfo getLatestOrderStaff(@Param("staffPhone") String staffPhone,
@Param("startTime") Date startTime, @Param("startTime") Date startTime,
@Param("endTime") Date endTime, @Param("endTime") Date endTime,
......
...@@ -3143,18 +3143,158 @@ public class ContentManagerTaskService { ...@@ -3143,18 +3143,158 @@ public class ContentManagerTaskService {
return res.get(0); return res.get(0);
} }
public List<StaffStudyDetailInfoDTO> listStaffStudyInfo(StaffStudyDetailInfoDTO dto) { public List<StaffStudyDetailInfoDTO> listStaffStudyInfo(StaffStudyDetailInfoDTO dto) {
// 查询员工关联导师,和服务门店,再查门店下的区域运营 // 优化后的查询:分步查询,避免复杂SQL导致的索引失效问题
// 1. 查询员工基本信息
List<StaffStudyDetailInfoDTO> res = staffInfoMapper.findByStaffStudy(dto); List<StaffStudyDetailInfoDTO> res = staffInfoMapper.findByStaffStudy(dto);
if (CollectionUtils.isEmpty(res)) { if (CollectionUtils.isEmpty(res)) {
log.info("该手机号查询上级、导师等信息失败. dto:{}", dto); log.info("该手机号查询上级、导师等信息失败. dto:{}", dto);
return null; return null;
} }
// 2. 补充员工的业务场景类型(近30天订单)
appendStaffBusinessSceneTypes(res);
// 3. 补充员工关联的门店信息
appendStaffMerchantInfo(res);
// 4. 补充门店的区域运营(上级)信息
appendMerchantSuperiors(res);
// 5. 补充指标信息
appendStaffIndicatorInfo(dto, res); appendStaffIndicatorInfo(dto, res);
// 6. 补充主营品牌信息
appendStaffMainBrand(dto, res); appendStaffMainBrand(dto, res);
return res; return res;
} }
/**
* 补充员工的业务场景类型(近30天订单)
*/
private void appendStaffBusinessSceneTypes(List<StaffStudyDetailInfoDTO> res) {
List<String> staffPhones = res.stream()
.map(StaffStudyDetailInfoDTO::getStaffPhone)
.filter(StringUtils::isNotBlank)
.distinct()
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(staffPhones)) {
return;
}
try {
List<StaffStudyDetailInfoDTO> sceneTypes = staffInfoMapper.findStaffBusinessSceneTypes(staffPhones);
if (CollectionUtils.isNotEmpty(sceneTypes)) {
Map<String, String> phoneToSceneMap = sceneTypes.stream()
.filter(s -> StringUtils.isNotBlank(s.getStaffPhone()))
.collect(Collectors.toMap(
StaffStudyDetailInfoDTO::getStaffPhone,
s -> s.getBusinessSceneTypeNames() != null ? s.getBusinessSceneTypeNames() : "",
(existing, replacement) -> existing
));
log.info("查询员工业务场景类型成功. staffPhones: {}, phoneToSceneMap: {}", staffPhones, phoneToSceneMap);
res.forEach(item -> {
if (StringUtils.isNotBlank(item.getStaffPhone()) && phoneToSceneMap.containsKey(item.getStaffPhone())) {
item.setBusinessSceneTypeNames(phoneToSceneMap.get(item.getStaffPhone()));
}
});
}
} catch (Exception e) {
log.error("查询员工业务场景类型失败, staffPhones: {}, error: {}", staffPhones, e.getMessage(), e);
}
}
/**
* 补充员工关联的门店信息
*/
private void appendStaffMerchantInfo(List<StaffStudyDetailInfoDTO> res) {
List<String> staffPhones = res.stream()
.map(StaffStudyDetailInfoDTO::getStaffPhone)
.filter(StringUtils::isNotBlank)
.distinct()
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(staffPhones)) {
return;
}
try {
List<StaffStudyDetailInfoDTO> merchantInfos = staffInfoMapper.findStaffMerchantInfo(staffPhones);
if (CollectionUtils.isNotEmpty(merchantInfos)) {
Map<String, StaffStudyDetailInfoDTO> phoneToMerchantMap = merchantInfos.stream()
.filter(s -> StringUtils.isNotBlank(s.getStaffPhone()))
.collect(Collectors.toMap(
StaffStudyDetailInfoDTO::getStaffPhone,
s -> s,
(existing, replacement) -> existing
));
log.info("查询员工门店信息成功. staffPhones: {}, phoneToMerchantMap: {}", staffPhones, phoneToMerchantMap);
res.forEach(item -> {
if (StringUtils.isNotBlank(item.getStaffPhone()) && phoneToMerchantMap.containsKey(item.getStaffPhone())) {
StaffStudyDetailInfoDTO merchantInfo = phoneToMerchantMap.get(item.getStaffPhone());
item.setMerchantIds(merchantInfo.getMerchantIds());
item.setMerchantNames(merchantInfo.getMerchantNames());
}
});
}
} catch (Exception e) {
log.error("查询员工门店信息失败, staffPhones: {}, error: {}", staffPhones, e.getMessage(), e);
}
}
/**
* 补充门店的区域运营(上级)信息
*/
private void appendMerchantSuperiors(List<StaffStudyDetailInfoDTO> res) {
// 收集所有门店ID
List<Integer> merchantIds = res.stream()
.map(StaffStudyDetailInfoDTO::getMerchantIds)
.filter(StringUtils::isNotBlank)
.flatMap(ids -> Arrays.stream(ids.split(",")))
.map(String::trim)
.filter(StringUtils::isNotBlank)
.map(Integer::parseInt)
.distinct()
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(merchantIds)) {
return;
}
try {
List<StaffStudyDetailInfoDTO> superiors = staffInfoMapper.findMerchantSuperiors(merchantIds);
if (CollectionUtils.isNotEmpty(superiors)) {
// 构建门店ID到上级的映射
Map<Integer, String> merchantIdToSuperiorMap = superiors.stream()
.filter(s -> s.getMerchantId() != null && StringUtils.isNotBlank(s.getSuperiors()))
.collect(Collectors.toMap(
StaffStudyDetailInfoDTO::getMerchantId,
StaffStudyDetailInfoDTO::getSuperiors,
(existing, replacement) -> existing + "," + replacement
));
log.info("查询门店上级信息成功. merchantIds: {}, merchantIdToSuperiorMap: {}", merchantIds, merchantIdToSuperiorMap);
// 为每个员工设置上级信息
res.forEach(item -> {
if (StringUtils.isNotBlank(item.getMerchantIds())) {
List<String> superiorList = new ArrayList<>();
String[] ids = item.getMerchantIds().split(",");
for (String id : ids) {
try {
Integer merchantId = Integer.parseInt(id.trim());
if (merchantIdToSuperiorMap.containsKey(merchantId)) {
superiorList.add(merchantIdToSuperiorMap.get(merchantId));
}
} catch (NumberFormatException e) {
log.warn("门店ID格式错误: {}", id);
}
}
if (!superiorList.isEmpty()) {
item.setSuperiors(String.join(",", superiorList));
}
}
});
}
} catch (Exception e) {
log.error("查询门店上级信息失败, merchantIds: {}, error: {}", merchantIds, e.getMessage(), e);
}
}
private void appendStaffMainBrand(StaffStudyDetailInfoDTO dto, List<StaffStudyDetailInfoDTO> res) { private void appendStaffMainBrand(StaffStudyDetailInfoDTO dto, List<StaffStudyDetailInfoDTO> res) {
if (!Objects.equals(dto.getQueryMainBrand(), Boolean.TRUE)) { if (!Objects.equals(dto.getQueryMainBrand(), Boolean.TRUE)) {
return; return;
......
...@@ -546,52 +546,15 @@ ...@@ -546,52 +546,15 @@
and scene_type = #{sceneType} and scene_type = #{sceneType}
</select> </select>
<!-- 优化后的基础查询:只查询员工基本信息 -->
<select id="findByStaffStudy" resultType="cn.com.glsx.dj.smartcarlife.dto.study.StaffStudyDetailInfoDTO"> <select id="findByStaffStudy" resultType="cn.com.glsx.dj.smartcarlife.dto.study.StaffStudyDetailInfoDTO">
select select
sbi.staff_phone as staffPhone, sbi.staff_phone as staffPhone,
group_concat(distinct sbi.staff_name) as staffName, sbi.staff_name as staffName,
group_concat(distinct sbi.staff_scene_label) as staffSL, sbi.staff_scene_label as staffSL,
group_concat(distinct sdrc.merchant_id) as merchantIds, sbi.mentor_name as mentors,
group_concat(distinct sdrc.merchant_name) as merchantNames,
group_concat(distinct sbi.mentor_name) as mentors,
group_concat(distinct if(smdc.director_type in (8,9), smdc.director_name, null)) as superiors,
tempTable.staff_scene_label as businessSceneTypeNames,
sbi.entry_time as entryTime sbi.entry_time as entryTime
from dj_car_life_staff_base_info sbi from dj_car_life_staff_base_info sbi
left join
(
select
si.phone_number,
coalesce(
group_concat(
distinct case
when go.scene_type = 1 then '轻改'
when go.scene_type = 0 and go.business_type = 1 then '权益'
when go.scene_type = 0 and go.business_type = 2 then '延保'
end
order by case
when go.scene_type = 1 then '1'
when go.scene_type = 0 and go.business_type = 1 then '权益'
when go.scene_type = 0 and go.business_type = 2 then '延保'
end
separator ', '
),
''
) as staff_scene_label
from dj_car_life_staff_info si
left join dj_car_life_goods_order go on si.staff_uuid = go.seller_user_id and go.install_upgrade_time >= DATE_SUB(CURDATE(), interval 30 day)
where si.phone_number is not null
group by si.phone_number
) as tempTable on sbi.staff_phone = tempTable.phone_number
left join
(
select
sdrc.staff_phone , sdrc.merchant_id , sdrc.merchant_name
from dj_car_life_staff_daily_report_config sdrc
inner join dj_car_life_staff_daily_report_effect_record sdrer on sdrc.id = sdrer.daily_report_config_id and sdrer.deleted_flag = 'N' and ((sdrer.start_effective_time &lt;= NOW() and sdrer.end_effective_time is null) or (sdrer.start_effective_time &lt;= NOW() and sdrer.end_effective_time >= NOW()))
where sdrc.deleted_flag = 'N'
) as sdrc on sdrc.staff_phone = sbi.staff_phone
left join dj_car_life_shop_merchant_director_config smdc on sdrc.merchant_id = smdc.merchant_id and smdc.deleted_flag = "N"
<where> <where>
sbi.deleted_flag = "N" sbi.deleted_flag = "N"
<if test="staffPhone != null and staffPhone != ''"> <if test="staffPhone != null and staffPhone != ''">
...@@ -604,7 +567,72 @@ ...@@ -604,7 +567,72 @@
</foreach> </foreach>
</if> </if>
</where> </where>
group by sbi.staff_phone </select>
<!-- 查询员工的业务场景类型(近30天订单) -->
<select id="findStaffBusinessSceneTypes" resultType="cn.com.glsx.dj.smartcarlife.dto.study.StaffStudyDetailInfoDTO">
select
si.phone_number as staffPhone,
group_concat(
distinct case
when go.scene_type = 1 then '轻改'
when go.scene_type = 0 and go.business_type = 1 then '权益'
when go.scene_type = 0 and go.business_type = 2 then '延保'
end
order by case
when go.scene_type = 1 then '1'
when go.scene_type = 0 and go.business_type = 1 then '2'
when go.scene_type = 0 and go.business_type = 2 then '3'
end
separator ', '
) as businessSceneTypeNames
from dj_car_life_staff_info si
inner join dj_car_life_goods_order go on si.staff_uuid = go.seller_user_id
where si.phone_number is not null
and si.deleted_flag = 'N'
and go.deleted_flag = 'N'
and go.install_upgrade_time >= DATE_SUB(CURDATE(), interval 30 day)
and si.phone_number in
<foreach close=")" collection="staffPhones" item="phone" open="(" separator=",">
#{phone}
</foreach>
group by si.phone_number
</select>
<!-- 查询员工关联的门店信息 -->
<select id="findStaffMerchantInfo" resultType="cn.com.glsx.dj.smartcarlife.dto.study.StaffStudyDetailInfoDTO">
select
sdrc.staff_phone as staffPhone,
group_concat(distinct sdrc.merchant_id) as merchantIds,
group_concat(distinct sdrc.merchant_name) as merchantNames
from dj_car_life_staff_daily_report_config sdrc
inner join dj_car_life_staff_daily_report_effect_record sdrer on sdrc.id = sdrer.daily_report_config_id
where sdrc.deleted_flag = 'N'
and sdrer.deleted_flag = 'N'
and (
(sdrer.start_effective_time &lt;= NOW() and sdrer.end_effective_time is null) or
(sdrer.start_effective_time &lt;= NOW() and sdrer.end_effective_time >= NOW())
)
and sdrc.staff_phone in
<foreach close=")" collection="staffPhones" item="phone" open="(" separator=",">
#{phone}
</foreach>
group by sdrc.staff_phone
</select>
<!-- 查询门店的区域运营(上级)信息 -->
<select id="findMerchantSuperiors" resultType="cn.com.glsx.dj.smartcarlife.dto.study.StaffStudyDetailInfoDTO">
select
smdc.merchant_id as merchantId,
group_concat(distinct smdc.director_name) as superiors
from dj_car_life_shop_merchant_director_config smdc
where smdc.deleted_flag = 'N'
and smdc.director_type in (8, 9)
and smdc.merchant_id in
<foreach close=")" collection="merchantIds" item="merchantId" open="(" separator=",">
#{merchantId}
</foreach>
group by smdc.merchant_id
</select> </select>
<select id="getLatestOrderStaff" resultMap="BaseResultMap"> <select id="getLatestOrderStaff" resultMap="BaseResultMap">
......
  • @wangxw1
    commented
    Master

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

    1. 流处理中缺少异常捕获,存在中断业务链路的风险(健壮性问题)

    • 问题描述:在 appendMerchantSuperiors 方法中提取 merchantIds 列表时,直接使用了 .map(Integer::parseInt)。如果 merchantIds 字符串中包含非数字字符(例如多余的空格或其他非法字符),将抛出 NumberFormatException,直接导致整个接口异常返回。
    • 影响分析:部分脏数据会导致整个列表查询失败,降低了系统的容错性和可用性。
    • 优化建议:在流处理中加入 try-catch 进行过滤,或者使用工具类进行安全转换。 代码示例: java List<Integer> merchantIds = res.stream() .map(StaffStudyDetailInfoDTO::getMerchantIds) .filter(StringUtils::isNotBlank) .flatMap(ids -> Arrays.stream(ids.split(","))) .map(String::trim) .filter(StringUtils::isNotBlank) .map(id -> { try { return Integer.parseInt(id); } catch (NumberFormatException e) { log.warn("门店ID格式错误, 忽略该ID: {}", id); return null; } }) .filter(Objects::nonNull) .distinct() .collect(Collectors.toList());

    2. 数据库 IN 查询未分批,存在 SQL 报错及性能隐患(稳定性与性能问题)

    • 问题描述:在 findStaffBusinessSceneTypes、findStaffMerchantInfo 等 MyBatis 查询中,使用了 <foreach> 拼接 IN 条件。如果入参 res 列表数据量较大(例如不分页的导出场景,达到几千条),会导致 staffPhones 或 merchantIds 集合过大。
    • 影响分析:MySQL 对 IN 语句的长度和参数个数有一定限制(如受 max_allowed_packet 影响),集合过大会导致 SQL 报错或执行计划全表扫描,严重拖慢数据库性能。
    • 优化建议:在 Java 代码层面对集合进行分批(如每批 500 个)查询,然后再合并结果。可以使用 Guava 的 Lists.partition 或 Apache Commons 的 ListUtils.partition。 代码示例: java List<StaffStudyDetailInfoDTO> sceneTypes = new ArrayList<>(); List<List<String>> partitions = Lists.partition(staffPhones, 500); for (List<String> batch : partitions) { sceneTypes.addAll(staffInfoMapper.findStaffBusinessSceneTypes(batch)); }

    3. 异常日志中直接打印大集合,存在刷爆日志的风险(最佳实践)

    • 问题描述:在捕获异常时,如 log.error("查询员工业务场景类型失败, staffPhones: {}, error: {}", staffPhones, e.getMessage(), e);,将整个 staffPhones 列表打印在日志中。
    • 影响分析:如果 staffPhones 包含大量数据,每次异常都会打印极其冗长的日志,不仅消耗磁盘空间和 I/O 性能,还可能将重要的错误堆栈信息淹没。
    • 优化建议:在日志中仅打印集合的大小或截取前几个元素作为排查参考,而不是打印全量数据。 代码示例: java log.error("查询员工业务场景类型失败, staffPhones size: {}, error: {}", staffPhones.size(), e.getMessage(), e);

    二、评分明细

    • 功能实现的正确性与健壮性(32 / 40分): 将复杂的 LEFT JOIN 拆分为内存组装,有效避免了索引失效问题,整体逻辑正确。扣分在于流处理中存在未捕获 NumberFormatException 的隐患,可能因为个别脏数据导致整个查询失败。

    • 安全性与潜在风险(28 / 30分): SQL 均采用参数化(#{})查询,无 SQL 注入风险。权限和数据安全方面无明显越权漏洞。

    • 最佳实践与可维护性(16 / 20分): 代码结构清晰,方法拆分合理,职责单一。扣分在于大集合打印日志的习惯不佳,且部分代码(如从字符串提取 ID 并转整型)在上下文中重复出现,可提取为公共方法。

    • 性能与资源利用(4 / 5分): 通过“化大为小”的查询策略减轻了数据库的 JOIN 压力,性能有明显提升。但未考虑 IN 集合可能过大的问题,在极端情况下可能成为新的性能瓶颈。

    • 提交信息质量(5 / 5分): Commit 信息(refactor(staff): ...)非常规范,详细列出了新增的方法和重构的逻辑,清晰可追溯。

    总分:85分

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