Files
smart_management_dev/src/main/resources/mybatis/wisdom/RkInfoMapper.xml
2026-01-20 14:02:20 +08:00

1650 lines
61 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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="com.zg.project.wisdom.mapper.RkInfoMapper">
<resultMap type="RkInfo" id="RkInfoResult">
<result property="id" column="id" />
<result property="rkType" column="rk_type" />
<result property="wlType" column="wl_type" />
<result property="cangku" column="cangku" />
<result property="rkTime" column="rk_time" />
<result property="lihuoY" column="lihuo_y" />
<result property="isChuku" column="is_chuku" />
<result property="isBorrowed" column="is_borrowed"/>
<result property="billNo" column="bill_no"/>
<result property="billNoCk" column="bill_no_ck"/>
<result property="rkTypeName" column="rk_type_name"/>
<result property="wlTypeName" column="wl_type_name"/>
<result property="cangkuName" column="cangku_name"/>
<!-- 新增:大仓/小仓编码和名称 -->
<result property="parentWarehouseCode" column="parent_warehouse_code"/>
<result property="parentWarehouseName" column="parent_warehouse_name"/>
<result property="warehouseCode" column="warehouse_code"/>
<result property="warehouseName" column="warehouse_name"/>
<result property="remark" column="remark" />
<result property="xj" column="xj" />
<result property="xmNo" column="xm_no" />
<result property="gysJhId" column="gys_jh_id"/>
<result property="xmMs" column="xm_ms" />
<result property="xmNoCk" column="xm_no_ck"/>
<result property="xmMsCk" column="xm_ms_ck"/>
<result property="wlNo" column="wl_no" />
<result property="wlMs" column="wl_ms" />
<result property="gysNo" column="gys_no" />
<result property="gysMc" column="gys_mc" />
<result property="jhAmt" column="jh_amt" />
<result property="htDj" column="ht_dj" />
<result property="sapNo" column="sap_no" />
<result property="xh" column="xh" />
<result property="jhQty" column="jh_qty" />
<result property="htQty" column="ht_qty" />
<result property="dw" column="dw" />
<result property="realQty" column="real_qty" />
<result property="pcode" column="pcode" />
<result property="pcodeId" column="pcode_id"/>
<result property="trayCode" column="tray_code" />
<result property="entityId" column="entity_id" />
<result property="ckLihuoY" column="ck_lihuo_y"/>
<result property="ckType" column="ck_type"/>
<result property="ckTypeName" column="ck_type_name"/>
<result property="teamCode" column="team_code"/>
<result property="teamName" column="team_name"/>
<result property="lyTime" column="ly_time"/>
<result property="borrowTime" column="borrow_time"/>
<result property="returnTime" column="return_time"/>
<result property="ckRemark" column="ck_remark"/>
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="isDelete" column="is_delete" />
<result property="hasMoved" column="has_moved"/>
<result property="lihuoYName" column="lihuo_y_name"/>
<result property="fycde1" column="fycde_1"/>
<result property="fycde2" column="fycde_2"/>
<result property="isDelivery" column="is_delivery"/>
<!-- ✅ 库龄(天) -->
<result property="stockAge" column="stock_age"/>
<!-- 总金额 -->
<result property="totalAmount" column="total_amount"/>
</resultMap>
<!-- 明细查询SQL包含多表JOIN用于普通明细分页等 -->
<sql id="selectRkInfoVo">
SELECT
ri.id,
ri.bill_no,
ri.bill_no_ck,
ri.rk_type, st.type_name AS rk_type_name,
ri.wl_type, mt.type_name AS wl_type_name,
ri.cangku,
wh.warehouse_name AS cangku_name,
wh.parent_warehouse_code AS parent_warehouse_code,
wh.parent_warehouse_name AS parent_warehouse_name,
wh.warehouse_code AS warehouse_code,
wh.warehouse_name AS warehouse_name,
ri.rk_time,
ri.lihuo_y,
ri.is_chuku,
ri.is_borrowed,
ri.is_delivery,
ri.remark,
ri.ck_lihuo_y,
ri.ck_type,
sot.type_name AS ck_type_name,
ri.team_code,
ct.team_name,
ri.ck_remark,
ri.ly_time,
ri.borrow_time,
ri.return_time,
ri.xj,
ri.xm_no,
ri.xm_ms,
ri.xm_no_ck,
ri.xm_ms_ck,
ri.wl_no,
ri.wl_ms,
ri.gys_no,
ri.gys_mc,
ri.jh_amt,
ri.ht_dj,
ri.sap_no,
ri.xh,
ri.gys_jh_id,
ri.jh_qty,
ri.ht_qty,
ri.dw,
ri.real_qty,
ri.pcode,
ri.pcode_id,
ri.tray_code,
ri.entity_id,
ri.status,
ri.has_moved,
ri.create_by,
ri.create_time,
ri.update_by,
ri.update_time,
ri.is_delete,
u.user_name AS lihuo_y_name,
-- ✅ 库龄(天)
DATEDIFF(CURDATE(), ri.rk_time) AS stock_age,
-- 总金额 = 单价 × 实际数量
COALESCE(ri.ht_dj, 0) * COALESCE(ri.real_qty, 0) AS total_amount
FROM rk_info ri
LEFT JOIN stock_in_type st ON ri.rk_type = st.type_code
LEFT JOIN material_type mt ON ri.wl_type = mt.type_code
LEFT JOIN warehouse_info wh ON ri.cangku = wh.warehouse_code
LEFT JOIN stock_out_type sot ON ri.ck_type = sot.type_code
LEFT JOIN construction_team ct ON ri.team_code = ct.team_code
LEFT JOIN sys_user u ON ri.lihuo_y = u.user_id
</sql>
<!-- 轻量分组专用SQL仅rk_info字段不做JOIN供按单据分组内层使用 -->
<sql id="selectRkInfoForGroup">
SELECT
ri.id,
ri.bill_no,
ri.bill_no_ck,
ri.rk_type,
ri.wl_type,
ri.cangku,
ri.rk_time,
ri.lihuo_y,
ri.is_chuku,
ri.xj,
ri.xm_no,
ri.xm_ms,
ri.xm_no_ck,
ri.xm_ms_ck,
ri.gys_mc,
ri.wl_no,
ri.wl_ms,
ri.gys_no,
ri.sap_no,
ri.ck_type,
ri.ly_time,
ri.ck_lihuo_y,
ri.pcode,
ri.is_delete
FROM rk_info ri
</sql>
<insert id="batchInsertRkInfo"
parameterType="java.util.List"
useGeneratedKeys="true"
keyProperty="id">
INSERT INTO rk_info (
bill_no,
rk_type, wl_type, cangku, lihuo_y, rk_time,
wl_no, wl_ms, xm_no, xm_ms,
xm_no_ck, xm_ms_ck, xj, sap_no, gys_no, gys_mc,
jh_qty, ht_qty, jh_amt, ht_dj, dw,
borrow_time, return_time, status,
pcode, pcode_id, tray_code, real_qty, entity_id, fycde_1, fycde_2,
remark, is_chuku, is_borrowed, gys_jh_id,
is_delete, create_by, create_time
)
VALUES
<foreach collection="list" item="item" separator=",">
(
#{item.billNo},
#{item.rkType}, #{item.wlType}, #{item.cangku}, #{item.lihuoY}, #{item.rkTime},
#{item.wlNo}, #{item.wlMs}, #{item.xmNo}, #{item.xmMs},
#{item.xmNoCk}, #{item.xmMsCk}, #{item.xj}, #{item.sapNo}, #{item.gysNo}, #{item.gysMc},
#{item.jhQty}, #{item.htQty}, #{item.jhAmt}, #{item.htDj}, #{item.dw},
#{item.borrowTime}, #{item.returnTime}, #{item.status},
#{item.pcode}, #{item.pcodeId}, #{item.trayCode}, #{item.realQty}, #{item.entityId},
#{item.fycde1}, #{item.fycde2},
#{item.remark}, #{item.isChuku}, #{item.isBorrowed}, #{item.gysJhId},
#{item.isDelete}, #{item.createBy}, #{item.createTime}
)
</foreach>
</insert>
<!--还料入库-->
<insert id="insertRkInfo" parameterType="RkInfo" useGeneratedKeys="true" keyProperty="id">
INSERT INTO rk_info (
rk_type, wl_type, cangku, rk_time, lihuo_y,
is_chuku, remark, bill_no, xj, xm_no, xm_ms, is_borrowed,
wl_no, wl_ms, gys_no, gys_mc, jh_amt, ht_dj,
sap_no, xh, jh_qty, ht_qty, dw, real_qty,
pcode, tray_code, entity_id,
ck_lihuo_y, ck_type, team_code, ck_remark,
ly_time, bill_no_ck, has_moved, borrow_time,
gys_jh_id,
status,
create_by, create_time, update_by, update_time, is_delete
) VALUES (
#{rkType}, #{wlType}, #{cangku}, #{rkTime}, #{lihuoY},
#{isChuku}, #{remark}, #{billNo}, #{xj}, #{xmNo}, #{xmMs}, #{isBorrowed},
#{wlNo}, #{wlMs}, #{gysNo}, #{gysMc}, #{jhAmt}, #{htDj},
#{sapNo}, #{xh}, #{jhQty}, #{htQty}, #{dw}, #{realQty},
#{pcode}, #{trayCode}, #{entityId},
#{ckLihuoY}, #{ckType}, #{teamCode}, #{ckRemark},
#{lyTime}, #{billNoCk}, #{hasMoved}, #{borrowTime},
#{gysJhId},
#{status},
#{createBy}, #{createTime}, #{updateBy}, #{updateTime}, #{isDelete}
)
</insert>
<update id="cancelStockOut">
UPDATE rk_info
SET
is_chuku = '0',
bill_no_ck = NULL,
ck_lihuo_y = NULL,
ck_type = NULL,
team_code = NULL,
ly_time = NULL,
ck_remark = NULL,
is_borrowed = NULL,
update_time = NOW()
WHERE bill_no_ck = #{billNoCk}
</update>
<delete id="deleteByBillNo">
DELETE FROM rk_info
WHERE bill_no = #{billNo}
</delete>
<select id="selectRkInfoList" parameterType="RkInfo" resultMap="RkInfoResult">
<include refid="selectRkInfoVo"/>
<where>
<!-- is_chuku 多状态 / 单状态 -->
<choose>
<when test="isChukuList != null and isChukuList.size > 0">
AND ri.is_chuku IN
<foreach collection="isChukuList" item="val" open="(" separator="," close=")">
#{val}
</foreach>
</when>
<when test="isChuku != null and isChuku != ''">
AND ri.is_chuku = #{isChuku}
</when>
</choose>
<!-- 关键字搜索 -->
<if test="keyword != null and keyword != ''">
AND (
ri.xm_no LIKE concat('%', #{keyword}, '%')
OR ri.xm_ms LIKE concat('%', #{keyword}, '%')
OR ri.fycde_1 LIKE concat('%', #{keyword}, '%')
OR ri.fycde_2 LIKE concat('%', #{keyword}, '%')
OR ri.wl_no LIKE concat('%', #{keyword}, '%')
OR ri.wl_ms LIKE concat('%', #{keyword}, '%')
OR ri.gys_no LIKE concat('%', #{keyword}, '%')
OR ri.gys_mc LIKE concat('%', #{keyword}, '%')
OR ri.sap_no LIKE concat('%', #{keyword}, '%')
OR ri.bill_no LIKE concat('%', #{keyword}, '%')
OR ri.bill_no_ck LIKE concat('%', #{keyword}, '%')
OR ri.ck_type LIKE concat('%', #{keyword}, '%')
OR ri.pcode LIKE concat('%', #{keyword}, '%')
)
</if>
<if test="remark != null and remark != ''">
AND ri.remark LIKE concat('%', #{remark}, '%')
</if>
<if test="rkType != null and rkType != ''">
AND ri.rk_type LIKE concat('%', #{rkType}, '%')
</if>
<if test="wlType != null and wlType != ''">
AND ri.wl_type LIKE concat('%', #{wlType}, '%')
</if>
<if test="cangku != null and cangku != ''">
AND ri.cangku LIKE concat('%', #{cangku}, '%')
</if>
<if test="fycde1 != null and fycde1 != ''">
AND ri.fycde_1 LIKE concat('%', #{fycde1}, '%')
</if>
<if test="fycde2 != null and fycde2 != ''">
AND ri.fycde_2 LIKE concat('%', #{fycde2}, '%')
</if>
<if test="ids != null and ids.size > 0">
AND ri.id IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</if>
<if test="startTime != null">
<![CDATA[
AND ri.rk_time >= #{startTime}
]]>
</if>
<if test="endTime != null">
<![CDATA[
AND ri.rk_time <= #{endTime}
]]>
</if>
<if test="lyStartTime != null">
<![CDATA[ AND ri.ly_time >= #{lyStartTime} ]]>
</if>
<if test="lyEndTime != null">
<![CDATA[ AND ri.ly_time <= #{lyEndTime} ]]>
</if>
<if test="lihuoY != null and lihuoY != ''">
AND ri.lihuo_y LIKE concat('%', #{lihuoY}, '%')
</if>
<if test="xj != null and xj != ''">
AND ri.xj LIKE concat('%', #{xj}, '%')
</if>
<if test="billNo != null and billNo != ''">
AND ri.bill_no = #{billNo}
</if>
<if test="billNoCk != null and billNoCk != ''">
AND ri.bill_no_ck = #{billNoCk}
</if>
<if test="xmNo != null and xmNo != ''">
AND ri.xm_no LIKE concat('%', #{xmNo}, '%')
</if>
<if test="xmMs != null and xmMs != ''">
AND ri.xm_ms LIKE concat('%', #{xmMs}, '%')
</if>
<if test="wlNo != null and wlNo != ''">
AND ri.wl_no LIKE concat('%', #{wlNo}, '%')
</if>
<if test="wlMs != null and wlMs != ''">
AND ri.wl_ms LIKE concat('%', #{wlMs}, '%')
</if>
<if test="gysNo != null and gysNo != ''">
AND ri.gys_no LIKE concat('%', #{gysNo}, '%')
</if>
<if test="gysMc != null and gysMc != ''">
AND ri.gys_mc LIKE concat('%', #{gysMc}, '%')
</if>
<if test="jhAmt != null">
AND ri.jh_amt = #{jhAmt}
</if>
<if test="htDj != null">
AND ri.ht_dj = #{htDj}
</if>
<if test="sapNo != null and sapNo != ''">
AND ri.sap_no LIKE concat('%', #{sapNo}, '%')
</if>
<if test="xh != null and xh != ''">
AND ri.xh LIKE concat('%', #{xh}, '%')
</if>
<if test="jhQty != null">
AND ri.jh_qty = #{jhQty}
</if>
<if test="htQty != null">
AND ri.ht_qty = #{htQty}
</if>
<if test="dw != null and dw != ''">
AND ri.dw LIKE concat('%', #{dw}, '%')
</if>
<if test="realQty != null">
AND ri.real_qty = #{realQty}
</if>
<if test="pcode != null and pcode != ''">
AND ri.pcode LIKE concat('%', #{pcode}, '%')
</if>
<if test="lyTime != null">
AND ri.ly_time = #{lyTime}
</if>
<if test="returnTime != null">
AND ri.return_time = #{returnTime}
</if>
<if test="trayCode != null and trayCode != ''">
AND ri.tray_code LIKE concat('%', #{trayCode}, '%')
</if>
<if test="entityId != null and entityId != ''">
AND ri.entity_id LIKE concat('%', #{entityId}, '%')
</if>
<if test="ckType != null and ckType != ''">
AND ri.ck_type LIKE concat('%', #{ckType}, '%')
</if>
<choose>
<when test="isDelete != null and isDelete != ''">
AND ri.is_delete = #{isDelete}
</when>
<otherwise>
AND ri.is_delete = 0
</otherwise>
</choose>
</where>
ORDER BY ri.rk_time DESC
</select>
<!-- 借料 / 还料专用列表查询 -->
<select id="selectBorrowAndReturnList"
parameterType="RkInfo"
resultMap="RkInfoResult">
<include refid="selectRkInfoVo"/>
<where>
<!-- 只查未删除 -->
AND ri.is_delete = 0
<!-- ✅ 关键:只允许未出库的数据 -->
<if test="isChuku != null and isChuku != ''">
AND ri.is_chuku = #{isChuku}
</if>
<!-- 借料状态 -->
<choose>
<when test="isBorrowed != null and isBorrowed != ''">
AND ri.is_borrowed = #{isBorrowed}
</when>
<otherwise>
AND (ri.is_borrowed = 0 OR ri.is_borrowed IS NULL)
</otherwise>
</choose>
<if test="xmNo != null and xmNo != ''">
AND ri.xm_no LIKE concat('%', #{xmNo}, '%')
</if>
<if test="sapNo != null and sapNo != ''">
AND ri.sap_no LIKE concat('%', #{sapNo}, '%')
</if>
<if test="billNo != null and billNo != ''">
AND ri.bill_no LIKE concat('%', #{billNo}, '%')
</if>
</where>
ORDER BY ri.borrow_time DESC, ri.return_time DESC, ri.id DESC
</select>
<select id="selectGroupedByBill"
resultMap="RkInfoResult"
parameterType="map">
SELECT
a.id,
a.bill_no,
a.bill_no_ck,
a.rk_type,
si.type_name AS rk_type_name,
a.wl_type,
mt.type_name AS wl_type_name,
a.cangku,
a.rk_time,
a.lihuo_y,
a.is_chuku,
a.xj,
a.xm_no,
a.xm_ms,
a.xm_no_ck,
a.xm_ms_ck,
a.gys_mc,
a.wl_no,
a.wl_ms,
a.gys_no,
a.sap_no,
a.ck_type,
so.type_name AS ck_type_name,
a.ly_time,
a.ck_lihuo_y,
u.user_name AS ck_lihuo_y_name,
ru.user_name AS lihuo_y_name,
a.is_delivery AS is_delivery,
a.team_code,
ct.team_name AS team_name
FROM (
SELECT
MIN(t.id) AS id,
t.show_bill_no AS bill_no,
MIN(t.bill_no_ck) AS bill_no_ck,
MIN(t.rk_type) AS rk_type,
MIN(t.wl_type) AS wl_type,
MIN(t.cangku) AS cangku,
MIN(t.rk_time) AS rk_time,
MIN(t.lihuo_y) AS lihuo_y,
MAX(t.is_chuku) AS is_chuku,
MIN(t.xj) AS xj,
MIN(t.xm_no) AS xm_no,
MIN(t.xm_ms) AS xm_ms,
MIN(t.xm_no_ck) AS xm_no_ck,
MIN(t.xm_ms_ck) AS xm_ms_ck,
MIN(t.gys_mc) AS gys_mc,
MIN(t.wl_no) AS wl_no,
MIN(t.wl_ms) AS wl_ms,
MIN(t.gys_no) AS gys_no,
MIN(t.sap_no) AS sap_no,
MIN(t.ck_type) AS ck_type,
MAX(t.ly_time) AS ly_time,
MIN(t.ck_lihuo_y) AS ck_lihuo_y,
MIN(t.is_delivery) AS is_delivery,
MIN(t.team_code) AS team_code,
MIN(t.show_bill_type) AS show_bill_type,
MAX(t.create_time) AS create_time
FROM (
SELECT
ri.id,
ri.bill_no,
ri.bill_no_ck,
ri.rk_type,
ri.wl_type,
ri.cangku,
ri.rk_time,
ri.lihuo_y,
ri.is_chuku,
ri.xj,
ri.xm_no,
ri.xm_ms,
ri.xm_no_ck,
ri.xm_ms_ck,
ri.gys_mc,
ri.wl_no,
ri.wl_ms,
ri.gys_no,
ri.sap_no,
ri.ck_type,
ri.ly_time,
ri.ck_lihuo_y,
ri.pcode,
ri.is_delete,
ri.is_delivery,
ri.team_code,
ri.create_time,
CASE
WHEN ri.is_chuku IN ('1','3')
AND ri.bill_no_ck IS NOT NULL
AND ri.bill_no_ck != ''
THEN ri.bill_no_ck
ELSE ri.bill_no
END AS show_bill_no,
CASE
WHEN ri.is_chuku IN ('1','3')
AND ri.bill_no_ck IS NOT NULL
AND ri.bill_no_ck != ''
THEN 'CK'
ELSE 'RK'
END AS show_bill_type
FROM rk_info ri
<where>
AND ri.is_delete = 0
<!-- isChukuList 优先生效 -->
<if test="q.isChukuList != null and q.isChukuList.size() > 0">
AND ri.is_chuku IN
<foreach collection="q.isChukuList" item="s" open="(" separator="," close=")">
#{s}
</foreach>
</if>
<!-- isChukuList 为空时才用 isChuku 单值 -->
<if test="(q.isChukuList == null or q.isChukuList.size() == 0) and q.isChuku != null and q.isChuku != ''">
AND ri.is_chuku = #{q.isChuku}
</if>
<if test="q.keyword != null and q.keyword != ''">
AND (
ri.bill_no LIKE concat('%', #{q.keyword}, '%')
OR ri.bill_no_ck LIKE concat('%', #{q.keyword}, '%')
OR ri.xm_no LIKE concat('%', #{q.keyword}, '%')
OR ri.xm_ms LIKE concat('%', #{q.keyword}, '%')
OR ri.wl_no LIKE concat('%', #{q.keyword}, '%')
OR ri.wl_ms LIKE concat('%', #{q.keyword}, '%')
OR ri.sap_no LIKE concat('%', #{q.keyword}, '%')
OR ri.gys_mc LIKE concat('%', #{q.keyword}, '%')
)
</if>
<if test="q.cangku != null and q.cangku != ''">
AND ri.cangku = #{q.cangku}
</if>
<if test="q.startTime != null and q.startTime != ''">
AND ri.rk_time &gt;= #{q.startTime}
</if>
<if test="q.endTime != null and q.endTime != ''">
AND ri.rk_time &lt;= #{q.endTime}
</if>
</where>
) t
GROUP BY t.show_bill_no
) a
LEFT JOIN stock_in_type si ON a.rk_type = si.type_code
LEFT JOIN stock_out_type so ON a.ck_type = so.type_code
LEFT JOIN sys_user u ON a.ck_lihuo_y = u.user_id
LEFT JOIN material_type mt ON a.wl_type = mt.type_code
LEFT JOIN sys_user ru ON a.lihuo_y = ru.user_id
LEFT JOIN construction_team ct ON a.team_code = ct.team_code
ORDER BY a.create_time DESC
</select>
<select id="selectBillHeaderList" resultMap="RkInfoResult">
SELECT
-- 3. 外层:关联字典表获取名称
a.show_bill_no AS bill_no, -- 最终展示的单据号
a.bill_no AS bill_no, -- 原始入库单号(兼容实体)
a.bill_no_ck AS bill_no_ck, -- 原始出库单号
-- 状态与标识
a.is_chuku,
a.is_delivery,
a.id, -- 取聚合后的最小ID作为主键标识
-- 入库维度的公共字段
a.rk_type,
si.type_name AS rk_type_name,
a.wl_type,
mt.type_name AS wl_type_name,
a.rk_time,
a.lihuo_y,
ru.user_name AS lihuo_y_name,
-- 仓库信息
a.cangku,
wh.warehouse_name AS cangku_name,
wh.parent_warehouse_code AS parent_warehouse_code,
wh.parent_warehouse_name AS parent_warehouse_name,
wh.warehouse_code AS warehouse_code,
wh.warehouse_name AS warehouse_name,
-- 出库维度的公共字段
a.ck_type,
so.type_name AS ck_type_name,
a.ly_time,
a.ck_lihuo_y,
u.user_name AS ck_lihuo_y_name, -- 注意:这里对应 XML 里的 u 表别名
a.team_code,
ct.team_name AS team_name,
-- 创建时间
a.create_time
FROM (
SELECT
-- 2. 中间层:按单据号聚合
t.show_bill_no,
MIN(t.id) AS id,
-- 单据头信息聚合(取最大值或最小值均可,因为同一单据号下这些字段是一致的)
MAX(t.bill_no) AS bill_no,
MAX(t.bill_no_ck) AS bill_no_ck,
MAX(t.is_chuku) AS is_chuku,
MAX(t.is_delivery) AS is_delivery,
MIN(t.rk_type) AS rk_type,
MIN(t.wl_type) AS wl_type,
MIN(t.rk_time) AS rk_time,
MIN(t.lihuo_y) AS lihuo_y,
MIN(t.cangku) AS cangku,
MIN(t.ck_type) AS ck_type,
MIN(t.ly_time) AS ly_time,
MIN(t.ck_lihuo_y) AS ck_lihuo_y,
MIN(t.team_code) AS team_code,
MAX(t.create_time) AS create_time
FROM (
-- 1. 内层:过滤并计算 show_bill_no
SELECT
ri.id,
ri.bill_no,
ri.bill_no_ck,
ri.rk_type,
ri.wl_type,
ri.cangku,
ri.rk_time,
ri.lihuo_y,
ri.is_chuku,
ri.is_delivery,
ri.ck_type,
ri.ly_time,
ri.ck_lihuo_y,
ri.team_code,
ri.create_time,
-- 计算逻辑:如果是出库状态且有出库单号,则以出库单号为主
CASE
WHEN ri.is_chuku IN ('1','3') AND ri.bill_no_ck IS NOT NULL AND ri.bill_no_ck != ''
THEN ri.bill_no_ck
ELSE ri.bill_no
END AS show_bill_no
FROM rk_info ri
<where>
AND ri.is_delete = 0
<!-- 关键字搜索:依然保留对项目号、物料号等的支持,方便用户查找 -->
<if test="q.keyword != null and q.keyword != ''">
AND (
ri.xm_no LIKE concat('%', #{q.keyword}, '%')
OR ri.wl_no LIKE concat('%', #{q.keyword}, '%')
OR ri.gys_mc LIKE concat('%', #{q.keyword}, '%')
)
</if>
<if test="q.isChukuList != null and q.isChukuList.size() > 0">
AND ri.is_chuku IN
<foreach collection="q.isChukuList" item="s" open="(" separator="," close=")">
#{s}
</foreach>
</if>
<if test="(q.isChukuList == null or q.isChukuList.size() == 0) and q.isChuku != null and q.isChuku != ''">
AND ri.is_chuku = #{q.isChuku}
</if>
<if test="q.ckType != null and q.ckType != ''">
AND ri.ck_type = #{q.ckType}
</if>
<if test="q.cangku != null and q.cangku != ''">
AND ri.cangku = #{q.cangku}
</if>
<if test="q.billNoCk != null and q.billNoCk != ''">
AND ri.bill_no_ck = #{q.billNoCk}
</if>
<if test="q.billNo != null and q.billNo != ''">
AND ri.bill_no = #{q.billNo}
</if>
<if test="q.startTime != null">
AND ri.rk_time &gt;= #{q.startTime}
</if>
<if test="q.endTime != null">
AND ri.rk_time &lt;= #{q.endTime}
</if>
<if test="q.statDate != null">
AND ri.ly_time &gt;= #{q.statDate}
</if>
<if test="q.endDate != null">
AND ri.ly_time &lt;= #{q.endDate}
</if>
</where>
) t
GROUP BY t.show_bill_no
) a
-- 字典关联
LEFT JOIN stock_in_type si ON a.rk_type = si.type_code
LEFT JOIN stock_out_type so ON a.ck_type = so.type_code
LEFT JOIN warehouse_info wh ON a.cangku = wh.warehouse_code
LEFT JOIN sys_user u ON a.ck_lihuo_y = u.user_id -- 出库理货员
LEFT JOIN sys_user ru ON a.lihuo_y = ru.user_id -- 入库理货员
LEFT JOIN construction_team ct ON a.team_code = ct.team_code
LEFT JOIN material_type mt ON a.wl_type = mt.type_code
-- 排序优化:优先按出库时间排序;若无出库时间(入库单),则按入库时间排序
ORDER BY COALESCE(a.ly_time, a.rk_time) DESC, a.id DESC
</select>
<!-- ================== /按单据分组查询 ================== -->
<select id="selectRkInfoById" parameterType="Long" resultMap="RkInfoResult">
<include refid="selectRkInfoVo"/>
where ri.id = #{id} and ri.is_delete = 0
</select>
<select id="countRkInfoByLocationCode" resultType="int">
SELECT COUNT(1)
FROM rk_info ri
WHERE ri.pcode = #{locationCode} AND ri.is_delete = 0
</select>
<select id="selectUsedPcodes" resultType="java.lang.String">
SELECT DISTINCT pcode
FROM rk_info ri
WHERE ri.is_delete = '0' AND ri.is_chuku != '1'
</select>
<select id="selectSapNoByBillNo" resultType="java.lang.String">
SELECT DISTINCT sap_no FROM rk_info
WHERE bill_no = #{billNo} AND sap_no IS NOT NULL
</select>
<select id="countOverdueStock" resultType="int">
SELECT COUNT(*) FROM rk_info
WHERE is_delete = 0 AND is_chuku = 0
<![CDATA[
AND DATE(rk_time) <= DATE_SUB(CURDATE(), INTERVAL 20 DAY)
]]>
</select>
<select id="selectTopOverdueStock" resultMap="RkInfoResult">
SELECT * FROM rk_info
WHERE is_delete = 0 AND is_chuku = 0
<![CDATA[
AND DATE(rk_time) <= DATE_SUB(CURDATE(), INTERVAL 20 DAY)
]]>
ORDER BY rk_time DESC
LIMIT #{limit}
</select>
<select id="selectSapNoByCkBillNo" resultType="java.lang.String">
SELECT DISTINCT sap_no FROM rk_info
WHERE bill_no = #{billNo} AND sap_no IS NOT NULL
</select>
<!-- 正常数据:扫描到的库位(限定场景) -->
<select id="getByPcodeIdList" parameterType="map" resultMap="RkInfoResult">
SELECT
ri.id, ri.rk_type, ri.wl_type, ri.cangku, ri.rk_time, ri.lihuo_y,
ri.is_chuku, ri.bill_no, ri.bill_no_ck,
ri.remark, ri.xj, ri.xm_no, ri.xm_ms, ri.wl_no, ri.wl_ms, ri.is_borrowed,
ri.gys_no, ri.gys_mc, ri.jh_amt, ri.ht_dj, ri.sap_no, ri.xh,
ri.jh_qty, ri.ht_qty, ri.dw, ri.real_qty, ri.borrow_time, ri.return_time,
ri.pcode, ri.pcode_id, ri.tray_code, ri.entity_id,
ri.ck_lihuo_y, ri.ck_type, ri.team_code, ri.ly_time, ri.ck_remark,
ri.create_by, ri.create_time, ri.update_by, ri.update_time, ri.is_delete
FROM rk_info ri
JOIN pcde_detail pd ON ri.pcode_id = pd.encoded_id
WHERE ri.is_delete = '0'
AND pd.is_delete = '0'
AND ri.is_chuku = '0'
AND pd.scene = #{sceneId}
AND ri.pcode_id IN
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
<!-- 未扫描到的数据(限定场景) -->
<select id="getMissedPcodeIds" parameterType="map" resultMap="RkInfoResult">
SELECT
ri.id, ri.rk_type, ri.wl_type, ri.cangku, ri.rk_time, ri.lihuo_y,
ri.is_chuku, ri.bill_no, ri.bill_no_ck,
ri.remark, ri.xj, ri.xm_no, ri.xm_ms, ri.wl_no, ri.wl_ms, ri.is_borrowed,
ri.gys_no, ri.gys_mc, ri.jh_amt, ri.ht_dj, ri.sap_no, ri.xh,
ri.jh_qty, ri.ht_qty, ri.dw, ri.real_qty,
ri.pcode, ri.pcode_id, ri.tray_code, ri.entity_id,
ri.ck_lihuo_y, ri.ck_type, ri.team_code, ri.ly_time, ri.ck_remark,
ri.create_by, ri.create_time, ri.update_by, ri.update_time, ri.is_delete
FROM rk_info ri
JOIN pcde_detail pd ON ri.pcode_id = pd.encoded_id
WHERE ri.is_delete = '0'
AND ri.is_chuku = '0'
AND pd.is_delete = '0'
AND pd.scene = #{sceneId}
<if test="list != null and list.size() > 0">
AND ri.pcode_id NOT IN
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</if>
</select>
<!-- 判断某个库位在场景内是否存在 -->
<select id="existsByPcodeId" parameterType="map" resultType="java.lang.Integer">
SELECT COUNT(1)
FROM rk_info ri
JOIN pcde_detail pd ON ri.pcode_id = pd.encoded_id
WHERE ri.is_delete = '0'
AND pd.is_delete = '0'
AND ri.is_chuku = '0'
AND ri.pcode_id = #{pcodeId}
AND pd.scene = #{sceneId}
</select>
<select id="countGetByWh" resultType="java.lang.Integer" parameterType="java.lang.String">
SELECT COUNT(1) FROM rk_info
WHERE is_delete = '0'
AND cangku = #{warehouse}
AND is_chuku = '0'
</select>
<select id="getUnscannedPcodeByScene"
resultType="com.zg.project.inventory.domain.vo.RkInfoMatchVO">
SELECT
r.pcode AS rkPcode,
COALESCE(SUM(r.real_qty), 0) AS realQty
FROM pcde_detail d
JOIN rk_info r ON r.pcode = d.pcode
LEFT JOIN (
SELECT DISTINCT pcode
FROM inventory_match_scan
WHERE task_id = #{taskId}
AND status = '0'
) s ON s.pcode = r.pcode
WHERE (d.is_delete IS NULL OR d.is_delete = '0')
AND d.scene = #{sceneId}
AND r.is_chuku = '0'
AND s.pcode IS NULL
GROUP BY r.pcode
ORDER BY MAX(r.create_time) DESC
</select>
<select id="selectPcdeCntFromRkInfo" resultType="com.zg.project.inventory.domain.vo.PcdeCntVO">
SELECT pcode AS pcde,
COUNT(*) AS cnt
FROM rk_info
WHERE pcode_id IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
GROUP BY pcode
</select>
<select id="selectOneForEachBillNo" parameterType="java.util.List" resultType="com.zg.project.wisdom.domain.RkInfo">
SELECT
COALESCE(r.bill_no_ck, r.bill_no) AS billNo,
MAX(r.lihuo_y) AS lihuoY,
MAX(u.user_name) AS lihuoYName,
MAX(rkType.type_name) AS rkTypeName,
MAX(cangku.warehouse_name) AS cangkuName,
-- 新增:大仓/小仓编码和名称
MAX(cangku.parent_warehouse_code) AS parentWarehouseCode,
MAX(cangku.parent_warehouse_name) AS parentWarehouseName,
MAX(cangku.warehouse_code) AS warehouseCode,
MAX(cangku.warehouse_name) AS warehouseName,
MAX(wlType.type_name) AS wlTypeName,
MAX(ckType.type_name) AS ckTypeName,
MAX(team.team_name) AS teamName
FROM rk_info r
LEFT JOIN stock_in_type rkType ON r.rk_type = rkType.type_code
LEFT JOIN stock_out_type ckType ON r.ck_type = ckType.type_code
LEFT JOIN warehouse_info cangku ON r.cangku = cangku.warehouse_code
LEFT JOIN material_type wlType ON r.wl_type = wlType.type_code
LEFT JOIN construction_team team ON r.team_code = team.team_code
LEFT JOIN sys_user u ON r.lihuo_y = u.user_id
WHERE r.is_delete = '0'
AND (
r.bill_no IN
<foreach collection="billNos" item="billNo" open="(" separator="," close=")">
#{billNo}
</foreach>
OR
r.bill_no_ck IN
<foreach collection="billNos" item="billNo" open="(" separator="," close=")">
#{billNo}
</foreach>
)
GROUP BY COALESCE(r.bill_no_ck, r.bill_no)
</select>
<select id="selectRkInfoListByBillNo" resultMap="RkInfoResult"
parameterType="java.lang.String">
SELECT
*
FROM rk_info
WHERE is_delete = '0'
AND bill_no = #{billNo}
</select>
<select id="selectRkInfoListByBillNoCk" resultMap="RkInfoResult" parameterType="java.lang.String">
SELECT *
FROM rk_info
WHERE is_delete = '0'
AND bill_no_ck = #{billNo}
</select>
<select id="selectRkInfoByIds" resultMap="RkInfoResult">
SELECT * FROM rk_info
WHERE is_delete = '0'
AND id IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
<update id="updateRkInfo" parameterType="RkInfo">
UPDATE rk_info
<trim prefix="SET" suffixOverrides=",">
<if test="rkType != null">rk_type = #{rkType},</if>
<if test="wlType != null">wl_type = #{wlType},</if>
<!-- ✅ 仓库字段统一使用 cangku -->
<if test="cangku != null and cangku != ''">
cangku = #{cangku},
</if>
<if test="rkTime != null">rk_time = #{rkTime},</if>
<if test="lihuoY != null">lihuo_y = #{lihuoY},</if>
<if test="isChuku != null">is_chuku = #{isChuku},</if>
<if test="isBorrowed != null">is_borrowed = #{isBorrowed},</if>
<if test="billNo != null">bill_no = #{billNo},</if>
<if test="billNoCk != null">bill_no_ck = #{billNoCk},</if>
<if test="remark != null">remark = #{remark},</if>
<if test="xj != null">xj = #{xj},</if>
<if test="xmNo != null">xm_no = #{xmNo},</if>
<if test="xmMs != null">xm_ms = #{xmMs},</if>
<if test="xmNoCk != null">xm_no_ck = #{xmNoCk},</if>
<if test="xmMsCk != null">xm_ms_ck = #{xmMsCk},</if>
<if test="wlNo != null">wl_no = #{wlNo},</if>
<if test="wlMs != null">wl_ms = #{wlMs},</if>
<if test="gysNo != null">gys_no = #{gysNo},</if>
<if test="gysMc != null">gys_mc = #{gysMc},</if>
<if test="jhAmt != null">jh_amt = #{jhAmt},</if>
<if test="htDj != null">ht_dj = #{htDj},</if>
<if test="sapNo != null">sap_no = #{sapNo},</if>
<if test="xh != null">xh = #{xh},</if>
<if test="jhQty != null">jh_qty = #{jhQty},</if>
<if test="htQty != null">ht_qty = #{htQty},</if>
<if test="dw != null">dw = #{dw},</if>
<if test="realQty != null">real_qty = #{realQty},</if>
<if test="pcode != null">pcode = #{pcode},</if>
<if test="pcodeId != null">pcode_id = #{pcodeId},</if>
<if test="trayCode != null">tray_code = #{trayCode},</if>
<if test="entityId != null">entity_id = #{entityId},</if>
<if test="ckLihuoY != null">ck_lihuo_y = #{ckLihuoY},</if>
<if test="ckType != null">ck_type = #{ckType},</if>
<if test="teamCode != null">team_code = #{teamCode},</if>
<if test="lyTime != null">ly_time = #{lyTime},</if>
<if test="borrowTime != null">borrow_time = #{borrowTime},</if>
<if test="returnTime != null">return_time = #{returnTime},</if>
<if test="ckRemark != null">ck_remark = #{ckRemark},</if>
<if test="isDelivery != null">is_delivery = #{isDelivery},</if>
<if test="hasMoved != null">has_moved = #{hasMoved},</if>
<!-- 新增:封样号 -->
<if test="fycde1 != null">fycde_1 = #{fycde1},</if>
<if test="fycde2 != null">fycde_2 = #{fycde2},</if>
<!-- 关联计划 -->
<if test="gysJhId != null">gys_jh_id = #{gysJhId},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="isDelete != null">is_delete = #{isDelete},</if>
</trim>
WHERE id = #{id}
</update>
<update id="updateByBillNoCk" parameterType="com.zg.project.wisdom.domain.RkInfo">
UPDATE rk_info
<trim prefix="SET" suffixOverrides=",">
<if test="rkType != null">rk_type = #{rkType},</if>
<if test="wlType != null">wl_type = #{wlType},</if>
<if test="cangku != null and cangku != ''">
cangku = #{cangku},
</if>
<if test="rkTime != null">rk_time = #{rkTime},</if>
<if test="lihuoY != null">lihuo_y = #{lihuoY},</if>
<if test="isChuku != null">is_chuku = #{isChuku},</if>
<if test="isBorrowed != null">is_borrowed = #{isBorrowed},</if>
<if test="remark != null">remark = #{remark},</if>
<if test="xmNoCk != null">xm_no_ck = #{xmNoCk},</if>
<if test="xmMsCk != null">xm_ms_ck = #{xmMsCk},</if>
<if test="ckLihuoY != null">ck_lihuo_y = #{ckLihuoY},</if>
<if test="ckType != null">ck_type = #{ckType},</if>
<if test="teamCode != null">team_code = #{teamCode},</if>
<if test="ckRemark != null">ck_remark = #{ckRemark},</if>
<if test="lyTime != null">ly_time = #{lyTime},</if>
<if test="borrowTime != null">borrow_time = #{borrowTime},</if>
<if test="returnTime != null">return_time = #{returnTime},</if>
<if test="isDelivery != null">is_delivery = #{isDelivery},</if>
<if test="hasMoved != null">has_moved = #{hasMoved},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
</trim>
WHERE bill_no_ck = #{billNoCk}
AND is_delete = '0'
</update>
<update id="deleteRkInfoById" parameterType="Long">
update rk_info
<set>
is_delete = 1
</set>
where id = #{id}
</update>
<update id="deleteRkInfoByIds" parameterType="java.util.List">
update rk_info
set is_delete = 1
where id in
<foreach item="id" collection="list" open="(" separator="," close=")">
#{id}
</foreach>
</update>
<update id="updateById" parameterType="com.zg.project.wisdom.domain.RkInfo">
UPDATE rk_info
<set>
<if test="isChuku != null">is_chuku = #{isChuku},</if>
<if test="isBorrowed != null">is_borrowed = #{isBorrowed},</if>
<if test="billNoCk != null">bill_no_ck = #{billNoCk},</if>
<if test="ckType != null">ck_type = #{ckType},</if>
<if test="isDelivery != null">is_delivery = #{isDelivery},</if>
<if test="ckLihuoY != null">ck_lihuo_y = #{ckLihuoY},</if>
<if test="teamCode != null">team_code = #{teamCode},</if>
<if test="ckRemark != null">ck_remark = #{ckRemark},</if>
<if test="xmNoCk != null">xm_no_ck = #{xmNoCk},</if>
<if test="xmMsCk != null">xm_ms_ck = #{xmMsCk},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="lyTime != null">ly_time = #{lyTime},</if>
<if test="borrowTime != null">borrow_time = #{borrowTime},</if>
<if test="returnTime != null">return_time = #{returnTime},</if>
<if test="pcode != null and pcode != ''">pcode = #{pcode},</if>
<if test="cangku != null and cangku != ''">cangku = #{cangku},</if>
<if test="status != null">status = #{status},</if>
<if test="realQty != null">real_qty = #{realQty},</if>
</set>
WHERE id = #{id}
</update>
<update id="updateStatusByBillNo" parameterType="RkInfo">
UPDATE rk_info
<set>
<if test="status != null">status = #{status},</if>
<if test="isChuku != null">is_chuku = #{isChuku},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
</set>
<where>
<if test="billNo != null">
bill_no = #{billNo}
</if>
<if test="billNo == null and billNoCk != null">
bill_no_ck = #{billNoCk}
</if>
</where>
</update>
<update id="markDeleteByBillNo" parameterType="RkInfo">
UPDATE rk_info
SET is_delete = #{isDelete},
status = #{status},
update_by = #{updateBy},
update_time = #{updateTime}
WHERE bill_no = #{billNo}
AND status IN ('0', '2')
</update>
<update id="markDeleteByBillNoCk" parameterType="RkInfo">
UPDATE rk_info
SET is_delete = #{isDelete},
status = #{status},
update_by = #{updateBy},
update_time = #{updateTime}
WHERE bill_no_ck = #{billNoCk}
AND status IN ('0', '2')
</update>
<update id="revertRkInfoStatusByBillNoCk" parameterType="RkInfo">
UPDATE rk_info
SET status = #{status},
is_chuku = #{isChuku},
update_by = #{updateBy},
update_time = #{updateTime}
WHERE bill_no_ck = #{billNoCk}
AND is_delete = '0'
AND bill_no IS NOT NULL
</update>
<select id="listRkInfoByPcode"
parameterType="string"
resultMap="RkInfoResult">
SELECT
t.*,
wh.warehouse_name AS cangku_name,
-- 新增:大仓/小仓编码和名称
wh.parent_warehouse_code AS parent_warehouse_code,
wh.parent_warehouse_name AS parent_warehouse_name,
wh.warehouse_code AS warehouse_code,
wh.warehouse_name AS warehouse_name
FROM rk_info t
LEFT JOIN warehouse_info wh
ON wh.warehouse_code = t.cangku
WHERE t.is_delete = 0
AND t.is_chuku = 0
AND t.pcode = #{pcode}
ORDER BY t.rk_time DESC, t.id DESC
</select>
<select id="selectAllRkInfo"
parameterType="com.zg.project.wisdom.domain.RkInfo"
resultMap="RkInfoResult">
<include refid="selectRkInfoVo"/>
<where>
(ri.is_delete = '0' OR ri.is_delete = 0 OR ri.is_delete IS NULL)
<if test="isChukuList != null and isChukuList.size() > 0">
AND ri.is_chuku IN
<foreach collection="isChukuList"
item="item"
open="("
separator=","
close=")">
#{item}
</foreach>
</if>
<if test="(isChukuList == null or isChukuList.size() == 0)
and isChuku != null">
AND ri.is_chuku = #{isChuku}
</if>
<if test="warehouseCode != null and warehouseCode != ''">
AND ri.cangku = #{warehouseCode}
</if>
<if test="parentWarehouseCode != null and parentWarehouseCode != ''">
AND wh.parent_warehouse_code = #{parentWarehouseCode}
</if>
<!-- 入库时间 -->
<if test="startTime != null">
AND ri.rk_time <![CDATA[ >= ]]> #{startTime}
</if>
<if test="endTime != null">
AND ri.rk_time <![CDATA[ < ]]> DATE_ADD(#{endTime}, INTERVAL 1 SECOND)
</if>
<!-- 归还时间 -->
<if test="statDate != null">
AND ri.return_time <![CDATA[ >= ]]> #{statDate}
</if>
<if test="endDate != null">
AND ri.return_time <![CDATA[ <= ]]> #{endDate}
</if>
<if test="rkType != null and rkType != ''">
AND ri.rk_type = #{rkType}
</if>
<if test="wlType != null and wlType != ''">
AND ri.wl_type = #{wlType}
</if>
<!-- 出库时间 -->
<if test="lyStartTime != null">
AND ri.ly_time <![CDATA[ >= ]]> #{lyStartTime}
</if>
<if test="lyEndTime != null">
AND ri.ly_time <![CDATA[ < ]]> DATE_ADD(#{lyEndTime}, INTERVAL 1 SECOND)
</if>
<if test="xmNo != null and xmNo != ''">
AND ri.xm_no LIKE CONCAT('%', #{xmNo}, '%')
</if>
<if test="xmMs != null and xmMs != ''">
AND ri.xm_ms LIKE CONCAT('%', #{xmMs}, '%')
</if>
<if test="wlNo != null and wlNo != ''">
AND ri.wl_no LIKE CONCAT('%', #{wlNo}, '%')
</if>
<if test="wlMs != null and wlMs != ''">
AND ri.wl_ms LIKE CONCAT('%', #{wlMs}, '%')
</if>
<if test="gysNo != null and gysNo != ''">
AND ri.gys_no LIKE CONCAT('%', #{gysNo}, '%')
</if>
<if test="gysMc != null and gysMc != ''">
AND ri.gys_mc LIKE CONCAT('%', #{gysMc}, '%')
</if>
<if test="sapNo != null and sapNo != ''">
AND ri.sap_no LIKE CONCAT('%', #{sapNo}, '%')
</if>
<if test="billNo != null and billNo != ''">
AND ri.bill_no LIKE CONCAT('%', #{billNo}, '%')
</if>
<if test="billNoCk != null and billNoCk != ''">
AND ri.bill_no_ck LIKE CONCAT('%', #{billNoCk}, '%')
</if>
<if test="ckType != null and ckType != ''">
AND ri.ck_type LIKE CONCAT('%', #{ckType}, '%')
</if>
<if test="pcode != null and pcode != ''">
AND ri.pcode LIKE CONCAT('%', #{pcode}, '%')
</if>
<if test="fycde1 != null and fycde1 != ''">
AND ri.fycde_1 LIKE CONCAT('%', #{fycde1}, '%')
</if>
<if test="fycde2 != null and fycde2 != ''">
AND ri.fycde_2 LIKE CONCAT('%', #{fycde2}, '%')
</if>
</where>
<!-- ✅ 核心排序规则 -->
ORDER BY
COALESCE(ri.ly_time, ri.rk_time) DESC,
ri.id DESC
</select>
<select id="selectRkInfoPageList"
parameterType="com.zg.project.wisdom.domain.RkInfo"
resultMap="RkInfoResult">
<include refid="selectRkInfoVo"/>
<where>
(ri.is_delete = '0' OR ri.is_delete = 0 OR ri.is_delete IS NULL)
<if test="isChukuList != null and isChukuList.size() > 0">
AND ri.is_chuku IN
<foreach collection="isChukuList"
item="item"
open="("
separator=","
close=")">
#{item}
</foreach>
</if>
<if test="(isChukuList == null or isChukuList.size() == 0)
and isChuku != null">
AND ri.is_chuku = #{isChuku}
</if>
<if test="warehouseCode != null and warehouseCode != ''">
AND ri.cangku = #{warehouseCode}
</if>
<if test="parentWarehouseCode != null and parentWarehouseCode != ''">
AND wh.parent_warehouse_code = #{parentWarehouseCode}
</if>
<if test="rkType != null and rkType != ''">
AND ri.rk_type = #{rkType}
</if>
<if test="wlType != null and wlType != ''">
AND ri.wl_type = #{wlType}
</if>
<!-- 时间范围查询:入库时间 OR 出库时间 -->
<if test="statDate != null and endDate != null">
AND (
(
ri.rk_time <![CDATA[ >= ]]> #{statDate}
AND ri.rk_time <![CDATA[ < ]]> DATE_ADD(#{endDate}, INTERVAL 1 SECOND)
)
OR
(
ri.ly_time <![CDATA[ >= ]]> #{statDate}
AND ri.ly_time <![CDATA[ < ]]> DATE_ADD(#{endDate}, INTERVAL 1 SECOND)
)
)
</if>
<!-- &lt;!&ndash; 入库时间 &ndash;&gt;-->
<!-- <if test="startTime != null">-->
<!-- AND ri.rk_time <![CDATA[ >= ]]> #{startTime}-->
<!-- </if>-->
<!-- <if test="endTime != null">-->
<!-- AND ri.rk_time <![CDATA[ < ]]> DATE_ADD(#{endTime}, INTERVAL 1 SECOND)-->
<!-- </if>-->
<!-- &lt;!&ndash; 出库时间 &ndash;&gt;-->
<!-- <if test="lyStartTime != null">-->
<!-- AND ri.ly_time <![CDATA[ >= ]]> #{lyStartTime}-->
<!-- </if>-->
<!-- <if test="lyEndTime != null">-->
<!-- AND ri.ly_time <![CDATA[ < ]]> DATE_ADD(#{lyEndTime}, INTERVAL 1 SECOND)-->
<!-- </if>-->
<if test="xmNo != null and xmNo != ''">
AND ri.xm_no LIKE CONCAT('%', #{xmNo}, '%')
</if>
<if test="xmMs != null and xmMs != ''">
AND ri.xm_ms LIKE CONCAT('%', #{xmMs}, '%')
</if>
<if test="wlNo != null and wlNo != ''">
AND ri.wl_no LIKE CONCAT('%', #{wlNo}, '%')
</if>
<if test="wlMs != null and wlMs != ''">
AND ri.wl_ms LIKE CONCAT('%', #{wlMs}, '%')
</if>
<if test="gysNo != null and gysNo != ''">
AND ri.gys_no LIKE CONCAT('%', #{gysNo}, '%')
</if>
<if test="gysMc != null and gysMc != ''">
AND ri.gys_mc LIKE CONCAT('%', #{gysMc}, '%')
</if>
<if test="sapNo != null and sapNo != ''">
AND ri.sap_no LIKE CONCAT('%', #{sapNo}, '%')
</if>
<if test="billNo != null and billNo != ''">
AND ri.bill_no LIKE CONCAT('%', #{billNo}, '%')
</if>
<if test="billNoCk != null and billNoCk != ''">
AND ri.bill_no_ck LIKE CONCAT('%', #{billNoCk}, '%')
</if>
<if test="ckType != null and ckType != ''">
AND ri.ck_type LIKE CONCAT('%', #{ckType}, '%')
</if>
<if test="pcode != null and pcode != ''">
AND ri.pcode LIKE CONCAT('%', #{pcode}, '%')
</if>
<if test="fycde1 != null and fycde1 != ''">
AND ri.fycde_1 LIKE CONCAT('%', #{fycde1}, '%')
</if>
<if test="fycde2 != null and fycde2 != ''">
AND ri.fycde_2 LIKE CONCAT('%', #{fycde2}, '%')
</if>
</where>
<!-- ✅ 核心排序规则 -->
ORDER BY
COALESCE(ri.ly_time, ri.rk_time) DESC,
ri.id DESC
</select>
<select id="selectDeliveryCkList"
parameterType="com.zg.project.wisdom.domain.RkInfo"
resultMap="RkInfoResult">
<include refid="selectRkInfoVo"/>
<where>
AND ri.is_delete = 0
AND ri.is_delivery = '1'
<if test="isChuku != null and isChuku != ''">
AND ri.is_chuku = #{isChuku}
</if>
<if test="billNoCk != null and billNoCk != ''">
AND ri.bill_no_ck LIKE concat('%', #{billNoCk}, '%')
</if>
</where>
ORDER BY ri.rk_time DESC
</select>
<!-- 根据入库单号查询单据头公共信息 -->
<select id="selectHeaderByBillNo"
parameterType="java.lang.String"
resultMap="RkInfoResult">
SELECT
ri.id,
ri.bill_no,
ri.bill_no_ck,
ri.is_chuku,
ri.rk_type,
st.type_name AS rk_type_name,
ri.wl_type,
mt.type_name AS wl_type_name,
ri.rk_time,
ri.ly_time,
ri.lihuo_y,
u.user_name AS lihuo_y_name,
ri.cangku,
wh.warehouse_name AS cangku_name,
wh.parent_warehouse_code AS parent_warehouse_code,
wh.parent_warehouse_name AS parent_warehouse_name,
wh.warehouse_code AS warehouse_code,
wh.warehouse_name AS warehouse_name
FROM rk_info ri
LEFT JOIN stock_in_type st ON ri.rk_type = st.type_code
LEFT JOIN material_type mt ON ri.wl_type = mt.type_code
LEFT JOIN warehouse_info wh ON ri.cangku = wh.warehouse_code
LEFT JOIN sys_user u ON ri.lihuo_y = u.user_id
WHERE ri.is_delete = '0'
AND ri.bill_no = #{billNo}
ORDER BY ri.rk_time ASC, ri.id ASC
LIMIT 1
</select>
<select id="selectMaxBillNo" resultType="java.lang.String">
SELECT MAX(bill_no)
FROM rk_info
WHERE is_delete = 0
AND bill_no LIKE CONCAT(#{prefix}, '%')
</select>
<!-- 聚合统计:总金额、总数量、库位数 -->
<select id="selectStockStatistics" resultType="java.util.Map">
SELECT
-- 1. 总金额:合同单价 * 实际入库数量 (处理 null 为 0)
COALESCE(SUM(ri.ht_dj * ri.real_qty), 0) AS sumMoney,
-- 2. 总数量:实际入库数量求和
COALESCE(SUM(ri.real_qty), 0) AS sumQty,
-- 3. 库位数:去重统计 pcode
COUNT(DISTINCT ri.pcode) AS pcdeCount
FROM rk_info ri
<where>
-- 基础过滤:未删除
(ri.is_delete = '0' OR ri.is_delete = 0 OR ri.is_delete IS NULL)
-- 核心过滤:在库状态 (由Service传参或此处硬编码 '0')
<if test="isChuku != null and isChuku != ''">
AND ri.is_chuku = #{isChuku}
</if>
<!-- 以下为动态查询条件,直接复用你 selectRkInfoList 的逻辑 -->
<if test="cangku != null and cangku != ''">
AND ri.cangku = #{cangku}
</if>
<if test="rkType != null and rkType != ''">
AND ri.rk_type = #{rkType}
</if>
<if test="wlType != null and wlType != ''">
AND ri.wl_type = #{wlType}
</if>
<!-- 时间范围 -->
<if test="startTime != null">
AND ri.rk_time &gt;= #{startTime}
</if>
<if test="endTime != null">
AND ri.rk_time &lt;= #{endTime}
</if>
<!-- 模糊搜索 (复用原来的逻辑,确保结果与列表一致) -->
<if test="keyword != null and keyword != ''">
AND (
ri.xm_no LIKE concat('%', #{keyword}, '%')
OR ri.xm_ms LIKE concat('%', #{keyword}, '%')
OR ri.wl_no LIKE concat('%', #{keyword}, '%')
OR ri.wl_ms LIKE concat('%', #{keyword}, '%')
OR ri.gys_no LIKE concat('%', #{keyword}, '%')
OR ri.gys_mc LIKE concat('%', #{keyword}, '%')
OR ri.sap_no LIKE concat('%', #{keyword}, '%')
OR ri.bill_no LIKE concat('%', #{keyword}, '%')
OR ri.bill_no_ck LIKE concat('%', #{keyword}, '%')
OR ri.ck_type LIKE concat('%', #{keyword}, '%')
OR ri.pcode LIKE concat('%', #{keyword}, '%')
)
</if>
<!-- 单字段精确/模糊匹配 -->
<if test="xmNo != null and xmNo != ''">
AND ri.xm_no LIKE CONCAT('%', #{xmNo}, '%')
</if>
<if test="xmMs != null and xmMs != ''">
AND ri.xm_ms LIKE CONCAT('%', #{xmMs}, '%')
</if>
<if test="wlNo != null and wlNo != ''">
AND ri.wl_no LIKE CONCAT('%', #{wlNo}, '%')
</if>
<if test="wlMs != null and wlMs != ''">
AND ri.wl_ms LIKE CONCAT('%', #{wlMs}, '%')
</if>
<if test="gysNo != null and gysNo != ''">
AND ri.gys_no LIKE CONCAT('%', #{gysNo}, '%')
</if>
<if test="gysMc != null and gysMc != ''">
AND ri.gys_mc LIKE CONCAT('%', #{gysMc}, '%')
</if>
<if test="sapNo != null and sapNo != ''">
AND ri.sap_no LIKE CONCAT('%', #{sapNo}, '%')
</if>
<if test="billNo != null and billNo != ''">
AND ri.bill_no LIKE CONCAT('%', #{billNo}, '%')
</if>
<if test="pcode != null and pcode != ''">
AND ri.pcode LIKE CONCAT('%', #{pcode}, '%')
</if>
</where>
</select>
<update id="updateBillInfo" parameterType="com.zg.project.wisdom.domain.RkInfo">
UPDATE rk_info
<set>
<if test="rkType != null">
rk_type = #{rkType},
</if>
<if test="wlType != null">
wl_type = #{wlType},
</if>
<if test="warehouseCode != null and warehouseCode != ''">
cangku = #{warehouseCode},
</if>
<if test="rkTime != null">
rk_time = #{rkTime},
</if>
</set>
WHERE bill_no = #{billNo}
</update>
<update id="updateDeliveryStatus">
UPDATE rk_info
SET is_delivery = #{isDelivery}
WHERE is_delete = '0'
AND id IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</update>
</mapper>