修复成本问题

This commit is contained in:
2026-01-27 16:54:46 +08:00
parent bfe8750b86
commit a1f8b0c60b
9 changed files with 268 additions and 205 deletions

View File

@@ -133,7 +133,7 @@ public class EnergyCostReportServiceImpl implements IEnergyCostReportService {
long offset = (pageNum - 1) * pageSize;
// 手工 count 与数据分页,避免复杂 SQL 自动 count 解析
Long total = coilDailyMapper.selectCoilTotalMergedCount(bo);
java.util.List<CoilTotalCostVo> rows = coilDailyMapper.selectCoilTotalMerged(bo, offset, pageSize);
List<CoilTotalCostVo> rows = coilDailyMapper.selectCoilTotalMerged(bo, offset, pageSize);
Page<CoilTotalCostVo> page = new Page<>(pageNum, pageSize);
page.setTotal(total == null ? 0L : total);
page.setRecords(rows);

View File

@@ -29,6 +29,73 @@
SELECT * FROM wms_energy_coil_daily WHERE task_id = #{taskId}
</select>
<!-- ========================= -->
<!-- 公共筛选片段(卷号/时间范围) -->
<!-- 说明:尽量避免使用 ${} 以免引入 SQL 注入风险,所以按常用别名拆分多个片段 -->
<!-- ========================= -->
<sql id="FilterCoilNo_Material_NoAlias">
<if test="bo != null and bo.enterCoilNo != null and bo.enterCoilNo != ''">
AND enter_coil_no LIKE CONCAT('%', #{bo.enterCoilNo}, '%')
</if>
<if test="bo != null and bo.currentCoilNo != null and bo.currentCoilNo != ''">
AND current_coil_no LIKE CONCAT('%', #{bo.currentCoilNo}, '%')
</if>
</sql>
<sql id="FilterDate_CreateTime_NoAlias">
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND create_time <![CDATA[>=]]> CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND create_time <![CDATA[<=]]> CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
</sql>
<sql id="FilterCoilNo_Pa">
<if test="bo != null and bo.enterCoilNo != null and bo.enterCoilNo != ''">
AND c.enter_coil_no LIKE CONCAT('%', #{bo.enterCoilNo}, '%')
</if>
<if test="bo != null and bo.currentCoilNo != null and bo.currentCoilNo != ''">
AND pa.current_coil_no LIKE CONCAT('%', #{bo.currentCoilNo}, '%')
</if>
</sql>
<sql id="FilterDate_PaCreateTime">
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND pa.create_time <![CDATA[>=]]> CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND pa.create_time <![CDATA[<=]]> CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
</sql>
<sql id="FilterDate_PaAllCreateTime">
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND pa_all.create_time >= CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND pa_all.create_time &lt;= CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
</sql>
<sql id="FilterDate_ChangeTime">
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND change_time >= CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND change_time &lt;= CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
</sql>
<!-- 统一的“有效分钟数”表达式:基于库区抄表结束时间/完工时间择优 -->
<sql id="ExprPaEffectiveMinutes">
CASE
WHEN pa.create_time <![CDATA[<]]> COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW())
THEN TIMESTAMPDIFF(MINUTE, pa.create_time, COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW()))
ELSE 0
END
</sql>
<!-- 入场卷号维度能源+囤积成本汇总(分页) -->
<select id="selectCoilTotalMerged" parameterType="map"
resultType="com.klp.ems.domain.vo.CoilTotalCostVo">
@@ -54,18 +121,8 @@
WHERE data_type = 1
AND del_flag = 0
AND status = 0
<if test="bo != null and bo.enterCoilNo != null and bo.enterCoilNo != ''">
AND enter_coil_no LIKE CONCAT('%', #{bo.enterCoilNo}, '%')
</if>
<if test="bo != null and bo.currentCoilNo != null and bo.currentCoilNo != ''">
AND current_coil_no LIKE CONCAT('%', #{bo.currentCoilNo}, '%')
</if>
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND create_time <![CDATA[>=]]> CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND create_time <![CDATA[<=]]> CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
<include refid="FilterCoilNo_Material_NoAlias"/>
<include refid="FilterDate_CreateTime_NoAlias"/>
GROUP BY current_coil_no
) m
LEFT JOIN (
@@ -73,14 +130,10 @@
pa.current_coil_no,
COUNT(DISTINCT pa.coil_id) AS coil_count,
SUM(
CASE
WHEN pa.create_time <![CDATA[<]]> COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW())
THEN CAST(TIMESTAMPDIFF(MINUTE,
pa.create_time,
COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW())
) AS DECIMAL(20,6)) / 60.0
ELSE 0
END
CAST(
<include refid="ExprPaEffectiveMinutes"/>
AS DECIMAL(20,6)
) / 60.0
) AS total_duration,
SUM(
CASE
@@ -89,14 +142,7 @@
AND wd_prod.total_prod_minutes > 0
THEN wd.total_warehouse_consumption *
(CAST(
CASE
WHEN pa.create_time <![CDATA[<]]> COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW())
THEN TIMESTAMPDIFF(MINUTE,
pa.create_time,
COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW())
)
ELSE 0
END AS DECIMAL(20,6)
<include refid="ExprPaEffectiveMinutes"/> AS DECIMAL(20,6)
) / wd_prod.total_prod_minutes)
ELSE 0
END
@@ -108,14 +154,7 @@
AND wd_prod.total_prod_minutes > 0
THEN wd.total_warehouse_cost *
(CAST(
CASE
WHEN pa.create_time <![CDATA[<]]> COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW())
THEN TIMESTAMPDIFF(MINUTE,
pa.create_time,
COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW())
)
ELSE 0
END AS DECIMAL(20,6)
<include refid="ExprPaEffectiveMinutes"/> AS DECIMAL(20,6)
) / wd_prod.total_prod_minutes)
ELSE 0
END
@@ -160,18 +199,8 @@
WHERE pa.action_status IN (0, 1, 2)
and pa.del_flag='0'
AND pa.warehouse_id IS NOT NULL
<if test="bo != null and bo.enterCoilNo != null and bo.enterCoilNo != ''">
AND c.enter_coil_no LIKE CONCAT('%', #{bo.enterCoilNo}, '%')
</if>
<if test="bo != null and bo.currentCoilNo != null and bo.currentCoilNo != ''">
AND pa.current_coil_no LIKE CONCAT('%', #{bo.currentCoilNo}, '%')
</if>
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND pa.create_time <![CDATA[>=]]> CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND pa.create_time <![CDATA[<=]]> CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
<include refid="FilterCoilNo_Pa"/>
<include refid="FilterDate_PaCreateTime"/>
GROUP BY pa.current_coil_no
) e ON m.current_coil_no = e.current_coil_no
LEFT JOIN (
@@ -253,27 +282,12 @@
) AS total_minutes
FROM wms_coil_pending_action pa_all
WHERE pa_all.action_status IN (0,1,2)
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND pa_all.create_time >= CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND pa_all.create_time &lt;= CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
<include refid="FilterDate_PaAllCreateTime"/>
GROUP BY DATE(pa_all.create_time)
) td ON td.day_key = DATE(pa.create_time)
WHERE pa.action_status IN (0,1,2)
<if test="bo != null and bo.enterCoilNo != null and bo.enterCoilNo != ''">
AND c.enter_coil_no LIKE CONCAT('%', #{bo.enterCoilNo}, '%')
</if>
<if test="bo != null and bo.currentCoilNo != null and bo.currentCoilNo != ''">
AND pa.current_coil_no LIKE CONCAT('%', #{bo.currentCoilNo}, '%')
</if>
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND pa.create_time >= CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND pa.create_time &lt;= CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
<include refid="FilterCoilNo_Pa"/>
<include refid="FilterDate_PaCreateTime"/>
GROUP BY DATE(pa.create_time), pa.current_coil_no, td.total_minutes
) dm
LEFT JOIN (
@@ -283,12 +297,7 @@
FROM eqp_auxiliary_material_change
WHERE del_flag = '0'
AND change_type = '减少'
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND change_time >= CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND change_time &lt;= CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
<include refid="FilterDate_ChangeTime"/>
GROUP BY DATE(change_time)
) a ON dm.day_key = a.day_key
LEFT JOIN (
@@ -298,17 +307,11 @@
FROM eqp_spare_parts_change
WHERE del_flag = '0'
AND change_type = '减少'
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND change_time >= CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND change_time &lt;= CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
<include refid="FilterDate_ChangeTime"/>
GROUP BY DATE(change_time)
) p ON dm.day_key = p.day_key
GROUP BY dm.current_coil_no
) x ON m.current_coil_no = x.current_coil_no
WHERE s.current_coil_no IS NOT NULL
ORDER BY totalCost DESC
LIMIT #{offset}, #{pageSize}
</select>
@@ -367,18 +370,8 @@
WHERE data_type IN (0,1)
AND del_flag = 0
AND status = 0
<if test="bo != null and bo.enterCoilNo != null and bo.enterCoilNo != ''">
AND enter_coil_no LIKE CONCAT('%', #{bo.enterCoilNo}, '%')
</if>
<if test="bo != null and bo.currentCoilNo != null and bo.currentCoilNo != ''">
AND current_coil_no LIKE CONCAT('%', #{bo.currentCoilNo}, '%')
</if>
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND create_time <![CDATA[>=]]> CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND create_time <![CDATA[<=]]> CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
<include refid="FilterCoilNo_Material_NoAlias"/>
<include refid="FilterDate_CreateTime_NoAlias"/>
GROUP BY current_coil_no
) m
LEFT JOIN (
@@ -386,14 +379,10 @@
pa.current_coil_no,
COUNT(DISTINCT pa.coil_id) AS coil_count,
SUM(
CASE
WHEN pa.create_time <![CDATA[<]]> COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW())
THEN CAST(TIMESTAMPDIFF(MINUTE,
pa.create_time,
COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW())
) AS DECIMAL(20,6)) / 60.0
ELSE 0
END
CAST(
<include refid="ExprPaEffectiveMinutes"/>
AS DECIMAL(20,6)
) / 60.0
) AS total_duration,
SUM(
CASE
@@ -402,14 +391,7 @@
AND wd_prod.total_prod_minutes > 0
THEN wd.total_warehouse_consumption *
(CAST(
CASE
WHEN pa.create_time <![CDATA[<]]> COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW())
THEN TIMESTAMPDIFF(MINUTE,
pa.create_time,
COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW())
)
ELSE 0
END AS DECIMAL(20,6)
<include refid="ExprPaEffectiveMinutes"/> AS DECIMAL(20,6)
) / wd_prod.total_prod_minutes)
ELSE 0
END
@@ -421,14 +403,7 @@
AND wd_prod.total_prod_minutes > 0
THEN wd.total_warehouse_cost *
(CAST(
CASE
WHEN pa.create_time <![CDATA[<]]> COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW())
THEN TIMESTAMPDIFF(MINUTE,
pa.create_time,
COALESCE(pa.complete_time, wmt.warehouse_end_time, NOW())
)
ELSE 0
END AS DECIMAL(20,6)
<include refid="ExprPaEffectiveMinutes"/> AS DECIMAL(20,6)
) / wd_prod.total_prod_minutes)
ELSE 0
END
@@ -472,6 +447,8 @@
) wd_prod ON pa.warehouse_id = wd_prod.warehouse_id
WHERE pa.action_status IN (0, 1, 2)
AND pa.warehouse_id IS NOT NULL
<include refid="FilterCoilNo_Pa"/>
<include refid="FilterDate_PaCreateTime"/>
GROUP BY c.enter_coil_no
) e ON m.enter_coil_no = e.enter_coil_no
LEFT JOIN (
@@ -541,21 +518,11 @@
) AS total_minutes
FROM wms_coil_pending_action pa_all
WHERE pa_all.action_status IN (0,1,2)
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND pa_all.create_time >= CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND pa_all.create_time &lt;= CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
<include refid="FilterDate_PaAllCreateTime"/>
GROUP BY DATE(pa_all.create_time)
) td ON td.day_key = DATE(pa.create_time)
WHERE pa.action_status IN (0,1,2)
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND pa.create_time >= CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND pa.create_time &lt;= CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
<include refid="FilterDate_PaCreateTime"/>
GROUP BY DATE(pa.create_time), c.enter_coil_no, td.total_minutes
) dm
LEFT JOIN (
@@ -565,12 +532,7 @@
FROM eqp_auxiliary_material_change
WHERE del_flag = '0'
AND change_type = '减少'
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND change_time >= CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND change_time &lt;= CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
<include refid="FilterDate_ChangeTime"/>
GROUP BY DATE(change_time)
) a ON dm.day_key = a.day_key
LEFT JOIN (
@@ -580,12 +542,7 @@
FROM eqp_spare_parts_change
WHERE del_flag = '0'
AND change_type = '减少'
<if test="bo != null and bo.startDate != null and bo.startDate != ''">
AND change_time >= CONCAT(#{bo.startDate}, ' 00:00:00')
</if>
<if test="bo != null and bo.endDate != null and bo.endDate != ''">
AND change_time &lt;= CONCAT(#{bo.endDate}, ' 23:59:59')
</if>
<include refid="FilterDate_ChangeTime"/>
GROUP BY DATE(change_time)
) p ON dm.day_key = p.day_key
GROUP BY dm.enter_coil_no