You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

306 lines
11 KiB

<?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.gaotao.modules.handlingunit.dao.HandlingUnitMapper">
<!-- 查询处理单元列表 -->
<select id="selectHandlingUnitList" resultType="com.gaotao.modules.handlingunit.entity.dto.HandlingUnitDto">
SELECT
h.unit_id,
h.site,
h.parent_unit_id,
h.unit_type,
h.unit_type_db,
h.part_no,
h.part_desc,
h.qty,
h.batch_no,
h.location_id,
h.warehouse_id,
h.wdr,
h.availability_control_id,
h.status,
h.status_db,
h.freeze_flag,
h.merged_flag,
h.in_stock_flag,
h.created_date,
h.created_by,
h.remark,
h.print_count,
h.last_print_date,
p.unit_id as parent_unit_info,
(SELECT COUNT(1) FROM handling_unit c WITH (NOLOCK) WHERE c.site = h.site AND c.parent_unit_id = h.unit_id) as children_count,
(SELECT COUNT(1) FROM handling_unit_detail d WITH (NOLOCK) WHERE d.site = h.site AND d.unit_id = h.unit_id) as detail_count
FROM handling_unit h WITH (NOLOCK)
LEFT JOIN handling_unit p WITH (NOLOCK) ON h.site = p.site AND h.parent_unit_id = p.unit_id
WHERE h.site = #{dto.site}
<if test="dto.unitIdLike != null and dto.unitIdLike != ''">
AND h.unit_id LIKE CONCAT('%', #{dto.unitIdLike}, '%')
</if>
<if test="dto.partNoLike != null and dto.partNoLike != ''">
AND h.part_no LIKE CONCAT('%', #{dto.partNoLike}, '%')
</if>
<if test="dto.batchNoLike != null and dto.batchNoLike != ''">
AND h.batch_no LIKE CONCAT('%', #{dto.batchNoLike}, '%')
</if>
<if test="dto.locationIdLike != null and dto.locationIdLike != ''">
AND h.location_id LIKE CONCAT('%', #{dto.locationIdLike}, '%')
</if>
<if test="dto.statusList != null and dto.statusList != ''">
AND h.status_db IN
<foreach collection="dto.statusList.split(',')" item="status" open="(" separator="," close=")">
#{status}
</foreach>
</if>
<if test="dto.createdDateFrom != null">
AND h.created_date >= #{dto.createdDateFrom}
</if>
<if test="dto.createdDateTo != null">
AND h.created_date &lt;= #{dto.createdDateTo}
</if>
ORDER BY h.created_date DESC
LIMIT 100
</select>
<!-- 查询处理单元详情 -->
<select id="selectHandlingUnitDetail" resultType="com.gaotao.modules.handlingunit.entity.dto.HandlingUnitDto">
SELECT
h.*,
p.unit_id as parent_unit_info,
(SELECT COUNT(1) FROM handling_unit c WITH (NOLOCK) WHERE c.site = h.site AND c.parent_unit_id = h.unit_id) as children_count,
(SELECT COUNT(1) FROM handling_unit_detail d WITH (NOLOCK) WHERE d.site = h.site AND d.unit_id = h.unit_id) as detail_count
FROM handling_unit h WITH (NOLOCK)
LEFT JOIN handling_unit p WITH (NOLOCK) ON h.site = p.site AND h.parent_unit_id = p.unit_id
WHERE h.unit_id = #{unitId}
<if test="site != null and site != ''">
AND h.site = #{site}
</if>
</select>
<select id="selectByUnitIdNoLock" resultType="com.gaotao.modules.handlingunit.entity.HandlingUnit">
SELECT unit_id,
site,
parent_unit_id,
unit_type,
unit_type_db,
part_no,
part_desc,
qty,
batch_no,
location_id,
warehouse_id,
wdr,
availability_control_id,
status,
status_db,
freeze_flag,
merged_flag,
in_stock_flag,
supplier_manufacture_date,
supplier_batch_no,um_id
FROM handling_unit WITH (NOLOCK)
WHERE unit_id = #{unitId}
</select>
<!-- 查询子处理单元 -->
<!-- 注意:不能用NOLOCK,结果用于装箱/拆箱后updateHandlingUnit,脏读可能导致数据不一致 - rqrq -->
<select id="selectChildUnits" resultType="com.gaotao.modules.handlingunit.entity.dto.HandlingUnitDto">
SELECT
unit_id,
site,
unit_type,
part_no,
part_desc,
qty,
batch_no,
location_id,
status,
status_db,
created_date
FROM handling_unit
WHERE site = #{site} AND parent_unit_id = #{parentUnitId}
ORDER BY created_date
</select>
<!-- 查询可装箱的单元 -->
<!-- 优化:添加 WITH (NOLOCK) 避免死锁 - rqrq -->
<select id="selectAvailableUnitsForPacking" resultType="com.gaotao.modules.handlingunit.entity.dto.HandlingUnitDto">
SELECT
unit_id,
site,
unit_type,
part_no,
part_desc,
qty,
batch_no,
location_id,
warehouse_id,
status,
status_db,
created_date
FROM handling_unit
WHERE site = #{site}
AND status_db = 'A'
AND parent_unit_id IS NULL
<if test="locationId != null and locationId != ''">
AND location_id = #{locationId}
</if>
<if test="warehouseId != null and warehouseId != ''">
AND warehouse_id = #{warehouseId}
</if>
ORDER BY created_date DESC
LIMIT 50
</select>
<!-- 查询可合并的单元 -->
<!-- 注意:不能用NOLOCK,结果用于合并后updateHandlingUnit,脏读可能导致重复合并 - rqrq -->
<select id="selectAvailableUnitsForMerge" resultType="com.gaotao.modules.handlingunit.entity.dto.HandlingUnitDto">
SELECT
unit_id,
site,
unit_type,
part_no,
part_desc,
qty,
batch_no,
location_id,
warehouse_id,
status,
status_db,
created_date
FROM handling_unit
WHERE site = #{site}
AND status_db = 'A'
AND parent_unit_id IS NULL
<if test="partNo != null and partNo != ''">
AND part_no = #{partNo}
</if>
<if test="batchNo != null and batchNo != ''">
AND batch_no = #{batchNo}
</if>
<if test="locationId != null and locationId != ''">
AND location_id = #{locationId}
</if>
ORDER BY created_date DESC
LIMIT 50
</select>
<!-- 更新打印信息 -->
<!-- rqrq - 移除ROWLOCK,由数据库自动管理锁粒度 -->
<update id="updatePrintInfo">
UPDATE handling_unit
SET printer_name = #{printerName},
print_count = #{printCount},
last_print_date = GETDATE()
WHERE site = #{site} AND unit_id = #{unitId}
</update>
<!-- 更新HandlingUnit打印信息(打印次数+1,更新最后打印时间和打印人) -->
<!-- rqrq - 移除ROWLOCK,由数据库自动管理锁粒度 -->
<update id="updateHandlingUnitPrintInfo">
UPDATE handling_unit
SET print_count = ISNULL(print_count, 0) + 1,
last_print_date = GETDATE(),
printer_name = #{printerName},
modified_by = #{printedBy},
modified_date = GETDATE()
WHERE site = #{site} AND unit_id = #{unitId}
</update>
<!-- 取消WMS预定 - rqrq -->
<!-- rqrq - 移除ROWLOCK,由数据库自动管理锁粒度 -->
<update id="cancelReserve">
UPDATE handling_unit
SET reserve_flag = 'N',
reserve_order_ref1 = NULL,
reserve_order_ref2 = NULL,
reserve_order_ref3 = NULL,
modified_date = GETDATE()
WHERE site = #{site} AND unit_id = #{unitId}
</update>
<!-- rqrq - 必须查出 expired_date、eng_chg_level,海安移库分组和JSON按这两个字段取值;NOLOCK 避免与上游更新锁等待 -->
<select id="selectByUnitIds" resultType="com.gaotao.modules.handlingunit.entity.HandlingUnit">
SELECT
unit_id,
site,
parent_unit_id,
unit_type,
unit_type_db,
part_no,
part_desc,
qty,
batch_no,
location_id,
warehouse_id,
wdr,
availability_control_id,
expired_date,
eng_chg_level,
status,
status_db,
freeze_flag,
merged_flag,
in_stock_flag,
created_date,
created_by,
remark,
print_count,
last_print_date
FROM handling_unit WITH (NOLOCK)
WHERE site = #{site} AND unit_id IN
<foreach collection="unitIds" item="unitId" open="(" separator="," close=")">
#{unitId}
</foreach>
</select>
<!-- 批量更新HandlingUnit的收货日期和失效日期 -->
<!-- 单条 UPDATE + CASE WHEN,避免多语句被 Druid 拦截 -->
<!-- rqrq - 移除ROWLOCK,由数据库自动管理锁粒度 -->
<update id="updateExpiredDate">
<if test="handlingUnits != null and handlingUnits.size() > 0">
UPDATE handling_unit
SET
receive_date = CASE
<foreach collection="handlingUnits" item="item">
<if test="item.receiveDate != null">
WHEN site = #{item.site} AND unit_id = #{item.unitId} THEN #{item.receiveDate}
</if>
</foreach>
ELSE receive_date
END,
expired_date = CASE
<foreach collection="handlingUnits" item="item">
<if test="item.expiredDate != null">
WHEN site = #{item.site} AND unit_id = #{item.unitId} THEN #{item.expiredDate}
</if>
</foreach>
ELSE expired_date
END,
modified_date = GETDATE()
WHERE
<foreach collection="handlingUnits" item="item" separator=" OR ">
(site = #{item.site} AND unit_id = #{item.unitId})
</foreach>
</if>
</update>
<!-- 跨site移库改标签:WHERE 卡来源site+在库,防止校验后被并发改出库;site/库位/仓库必须一次改完 -->
<update id="updateSiteAndLocationForCrossSiteMove">
UPDATE handling_unit WITH (ROWLOCK)
SET site = #{destSite},
location_id = #{targetLocationId},
warehouse_id = #{warehouseId},
modified_by = #{modifiedBy},
modified_date = GETDATE(),
in_stock_flag = 'Y'
WHERE site = #{fromSite}
AND in_stock_flag = 'Y'
AND (reserve_flag = 'N' OR reserve_flag IS NULL)
AND unit_id IN
<foreach collection="unitIds" item="unitId" open="(" separator="," close=")">
#{unitId}
</foreach>
</update>
</mapper>