CASE WHEN r.scrap_cycle IS NULL THEN 0 WHEN SYSDATE >= r.scrap_cycle THEN 1 ELSE 0 END AS expireStatus,
-- 核心新增:临期天数(到期时间-当前时间,处理空值/负数)
CASE
-- 到期时间为空 → 临期天数为0
WHEN r.scrap_cycle IS NULL THEN 0
-- 直接计算天数差,TRUNC取整;未过期为正,已过期为负
ELSE TRUNC(r.scrap_cycle - SYSDATE)
END AS expireDays
FROM ST_REALTIME_STOCK r -- 新主表:实时库存表
-- 左关联物料表:库存表goods_id关联物料表主键,过滤已删除数据
LEFT JOIN ST_GOODS g ON r.goods_id = g.id AND g.is_deleted = 0
@ -51,13 +60,14 @@
<where>
r.is_deleted = 0
AND r.is_scrap = 0
<iftest="stExpireRecord.goodsCode == null and stExpireRecord.startDueDate = null and stExpireRecord.endDueDate = null and stExpireRecord.expireStatus = null">
<iftest="stExpireRecord.goodsCode == null and stExpireRecord.startDueDate == null and stExpireRecord.endDueDate == null and stExpireRecord.expireStatus == null">
AND (
(r.scrap_cycle IS NOT NULL AND TRUNC(r.scrap_cycle - SYSDATE) <= 30)
OR
SYSDATE >= r.scrap_cycle
)
</if>
<!-- 1. 物料编号模糊查询(页面输入框) -->
<iftest="stExpireRecord.goodsCode != null and stExpireRecord.goodsCode != ''">
AND g.goods_code LIKE '%' || #{stExpireRecord.goodsCode} || '%'