perf(wms): 优化钢卷质量改判原因查询性能
- 在WmsCoilQualityRejudgeMapper中新增原生SQL查询方法selectMapsBySql - 添加对应的XML映射配置执行动态SQL查询 - 将原有的Java端分组逻辑改为数据库端SQL聚合查询 - 使用内连接和子查询直接获取每个钢卷的最新改判原因 - 避免大量数据传输和客户端分组处理,提升查询效率
This commit is contained in:
@@ -3,6 +3,10 @@ package com.klp.mapper;
|
|||||||
import com.klp.domain.WmsCoilQualityRejudge;
|
import com.klp.domain.WmsCoilQualityRejudge;
|
||||||
import com.klp.domain.vo.WmsCoilQualityRejudgeVo;
|
import com.klp.domain.vo.WmsCoilQualityRejudgeVo;
|
||||||
import com.klp.common.core.mapper.BaseMapperPlus;
|
import com.klp.common.core.mapper.BaseMapperPlus;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 钢卷质量改判记录Mapper接口
|
* 钢卷质量改判记录Mapper接口
|
||||||
@@ -12,4 +16,12 @@ import com.klp.common.core.mapper.BaseMapperPlus;
|
|||||||
*/
|
*/
|
||||||
public interface WmsCoilQualityRejudgeMapper extends BaseMapperPlus<WmsCoilQualityRejudgeMapper, WmsCoilQualityRejudge, WmsCoilQualityRejudgeVo> {
|
public interface WmsCoilQualityRejudgeMapper extends BaseMapperPlus<WmsCoilQualityRejudgeMapper, WmsCoilQualityRejudge, WmsCoilQualityRejudgeVo> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行原生SQL查询,返回Map列表
|
||||||
|
*
|
||||||
|
* @param sql 原生SQL语句
|
||||||
|
* @return Map列表
|
||||||
|
*/
|
||||||
|
List<Map<String, Object>> selectMapsBySql(@Param("sql") String sql);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5318,28 +5318,29 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. 批量查询改判原因(最新的一条)
|
// 4. 批量查询改判原因(最新的一条) - 使用SQL优化
|
||||||
Map<Long, String> rejudgeReasonMap = new HashMap<>();
|
Map<Long, String> rejudgeReasonMap = new HashMap<>();
|
||||||
if (!coilIds.isEmpty()) {
|
if (!coilIds.isEmpty()) {
|
||||||
LambdaQueryWrapper<WmsCoilQualityRejudge> rejudgeQuery = new LambdaQueryWrapper<>();
|
// 使用原生SQL查询每个钢卷的最新改判原因,避免Java分组处理
|
||||||
rejudgeQuery.in(WmsCoilQualityRejudge::getCoilId, coilIds);
|
String coilIdsStr = coilIds.stream().map(String::valueOf).collect(Collectors.joining(","));
|
||||||
rejudgeQuery.eq(WmsCoilQualityRejudge::getDelFlag, "0");
|
String sql = "SELECT t1.coil_id, t1.rejudge_reason " +
|
||||||
rejudgeQuery.orderByDesc(WmsCoilQualityRejudge::getCreateTime);
|
"FROM wms_coil_quality_rejudge t1 " +
|
||||||
List<WmsCoilQualityRejudge> allRejudges = wmsCoilQualityRejudgeMapper.selectList(rejudgeQuery);
|
"INNER JOIN (" +
|
||||||
|
" SELECT coil_id, MAX(create_time) as max_time " +
|
||||||
|
" FROM wms_coil_quality_rejudge " +
|
||||||
|
" WHERE coil_id IN (" + coilIdsStr + ") " +
|
||||||
|
" AND del_flag = '0' " +
|
||||||
|
" GROUP BY coil_id" +
|
||||||
|
") t2 ON t1.coil_id = t2.coil_id AND t1.create_time = t2.max_time " +
|
||||||
|
"WHERE t1.del_flag = '0' AND t1.rejudge_reason IS NOT NULL";
|
||||||
|
|
||||||
if (allRejudges != null && !allRejudges.isEmpty()) {
|
List<Map<String, Object>> results = wmsCoilQualityRejudgeMapper.selectMapsBySql(sql);
|
||||||
// 按钢卷ID分组,每组取最新的一条
|
if (results != null) {
|
||||||
Map<Long, List<WmsCoilQualityRejudge>> rejudgeGroupMap = allRejudges.stream()
|
for (Map<String, Object> result : results) {
|
||||||
.collect(Collectors.groupingBy(WmsCoilQualityRejudge::getCoilId));
|
Long coilId = (Long) result.get("coil_id");
|
||||||
|
String rejudgeReason = (String) result.get("rejudge_reason");
|
||||||
for (Map.Entry<Long, List<WmsCoilQualityRejudge>> entry : rejudgeGroupMap.entrySet()) {
|
if (coilId != null && rejudgeReason != null) {
|
||||||
List<WmsCoilQualityRejudge> rejudges = entry.getValue();
|
rejudgeReasonMap.put(coilId, rejudgeReason);
|
||||||
if (!rejudges.isEmpty()) {
|
|
||||||
// 由于已按创建时间降序排列,第一条就是最新的
|
|
||||||
WmsCoilQualityRejudge latestRejudge = rejudges.get(0);
|
|
||||||
if (latestRejudge.getRejudgeReason() != null) {
|
|
||||||
rejudgeReasonMap.put(entry.getKey(), latestRejudge.getRejudgeReason());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,5 +18,9 @@
|
|||||||
<result property="remark" column="remark"/>
|
<result property="remark" column="remark"/>
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
|
<!-- 执行原生SQL查询,返回Map列表 -->
|
||||||
|
<select id="selectMapsBySql" resultType="java.util.HashMap">
|
||||||
|
${sql}
|
||||||
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
Reference in New Issue
Block a user