2 天以前 7bf754a7835d06f26240c4ca15bc5f83650de377
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?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.ruoyi.sales.mapper.SalesLedgerArrivalMapper">
 
    <!-- attachments 是 JSON 数组列:自定义 select 不会自动套 JacksonTypeHandler,必须显式声明,
         否则返回给前端的是裸 JSON 字符串而不是数组 -->
    <resultMap id="ArrivalDtoMap" type="com.ruoyi.sales.dto.SalesLedgerArrivalDto">
        <id column="id" property="id"/>
        <result column="sales_ledger_id" property="salesLedgerId"/>
        <result column="sales_ledger_product_id" property="salesLedgerProductId"/>
        <result column="stock_out_record_id" property="stockOutRecordId"/>
        <result column="vehicle_id" property="vehicleId"/>
        <result column="truck_plate_no" property="truckPlateNo"/>
        <result column="arrival_quantity" property="arrivalQuantity"/>
        <result column="arrival_date" property="arrivalDate"/>
        <result column="attachments" property="attachments"
                typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
        <result column="remark" property="remark"/>
        <result column="create_time" property="createTime"/>
        <result column="create_user" property="createUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="dept_id" property="deptId"/>
        <result column="sales_contract_no" property="salesContractNo"/>
        <result column="customer_name" property="customerName"/>
        <result column="outbound_batches" property="outboundBatches"/>
        <result column="specification_model" property="specificationModel"/>
        <result column="product_name" property="productName"/>
        <result column="unit" property="unit"/>
        <result column="out_quantity" property="outQuantity"/>
        <result column="arrived_quantity" property="arrivedQuantity"/>
        <result column="create_by" property="createBy"/>
    </resultMap>
 
    <sql id="arrivalColumns">
        sla.*,
        sl.sales_contract_no,
        sl.customer_name,
        sor.outbound_batches,
        pm.model as specification_model,
        p.product_name,
        pm.unit,
        u.nick_name as create_by
    </sql>
 
    <sql id="arrivalJoins">
        from sales_ledger_arrival sla
        left join sales_ledger sl on sl.id = sla.sales_ledger_id
        left join stock_out_record sor on sor.id = sla.stock_out_record_id
        left join sales_ledger_product slp on slp.id = sla.sales_ledger_product_id
        left join product_model pm on pm.id = slp.product_model_id
        left join product p on p.id = pm.product_id
        left join sys_user u on u.user_id = sla.create_user
    </sql>
 
    <!-- 到货记录是发货台账的子行,可见性必须与父行(出库单)一致,否则会出现「有父无子」的错觉,
         故与 StockOutRecordMapper#bindableSalesLedgerPage 一样用 /*data_scope*/ 跳过数据权限过滤 -->
    <select id="listPage" resultMap="ArrivalDtoMap">
        select /*data_scope*/
        <include refid="arrivalColumns"/>
        <include refid="arrivalJoins"/>
        <where>
            <if test="req.salesLedgerId != null">
                and sla.sales_ledger_id = #{req.salesLedgerId}
            </if>
            <if test="req.stockOutRecordId != null">
                and sla.stock_out_record_id = #{req.stockOutRecordId}
            </if>
            <if test="req.truckPlateNo != null and req.truckPlateNo != ''">
                and sla.truck_plate_no like concat('%',#{req.truckPlateNo},'%')
            </if>
            <if test="req.salesContractNo != null and req.salesContractNo != ''">
                and sl.sales_contract_no like concat('%',#{req.salesContractNo},'%')
            </if>
            <if test="req.customerName != null and req.customerName != ''">
                and sl.customer_name like concat('%',#{req.customerName},'%')
            </if>
            <if test="req.arrivalDate != null">
                and sla.arrival_date = #{req.arrivalDate}
            </if>
        </where>
        order by sla.id desc
    </select>
 
    <!-- 单条到货记录:与列表同一口径跳过数据权限(可见性跟随父行出库单) -->
    <select id="selectArrivalById" resultMap="ArrivalDtoMap">
        select /*data_scope*/
        <include refid="arrivalColumns"/>
        <include refid="arrivalJoins"/>
        where sla.id = #{id}
    </select>
 
    <!-- 某台账在某出库单上累计已到货数量:上限校验用。
         必须跳过数据权限,否则漏掉他人录入的到货记录会让「累计到货 ≤ 出库数量」形同虚设 -->
    <select id="sumArrivedQuantity" resultType="java.math.BigDecimal">
        select /*data_scope*/
        ifnull(sum(sla.arrival_quantity), 0)
        from sales_ledger_arrival sla
        where sla.stock_out_record_id = #{stockOutRecordId}
        and sla.sales_ledger_id = #{salesLedgerId}
        <if test="excludeId != null">
            and sla.id != #{excludeId}
        </if>
    </select>
 
    <!-- 出库单下的到货记录条数:删除出库单 / 改绑台账的守卫用,同样要跳过数据权限 -->
    <select id="countByStockOutRecordId" resultType="java.lang.Long">
        select /*data_scope*/
        count(1)
        from sales_ledger_arrival
        where stock_out_record_id = #{stockOutRecordId}
    </select>
 
    <!-- 发货台账组装子行:一次批量取,避免 N+1 -->
    <select id="listByStockOutRecordIds" resultMap="ArrivalDtoMap">
        select /*data_scope*/
        <include refid="arrivalColumns"/>
        <include refid="arrivalJoins"/>
        where sla.stock_out_record_id in
        <foreach collection="stockOutRecordIds" item="recordId" open="(" separator="," close=")">
            #{recordId}
        </foreach>
        order by sla.arrival_date desc, sla.id desc
    </select>
 
    <!-- 可到货的出库单(含车辆)下拉:只能选该台账关联的、审批通过的油品出库。
         卖油走绑定表 —— stock_out_record.sales_ledger_id 只存了首项台账,多台账时不可用;
         代储不产绑定行(绑定行同时是开票/收款候选的数据源,代储不能进),但它「一次出库只能绑一个台账」,
         单值列反而是权威的,出库量取整单 stock_out_num。
         不按 sor.vehicle_id is not null 过滤:代储/客存出库不强制选车,滤掉会让这类出库单永远登记不了到货 -->
    <select id="bindableVehicles" resultMap="ArrivalDtoMap">
        select /*data_scope*/
        sor.id as stock_out_record_id,
        sor.outbound_batches,
        sor.vehicle_id,
        sor.truck_plate_no,
        ifnull(max(b.quantity), max(sor.stock_out_num)) as out_quantity,
        ifnull(max(a.arrived), 0) as arrived_quantity
        from stock_out_record sor
        left join stock_out_record_sales_ledger b
        on b.stock_out_record_id = sor.id and b.sales_ledger_id = #{salesLedgerId}
        left join sales_ledger self_ledger on self_ledger.id = sor.sales_ledger_id
        left join (
        select stock_out_record_id, sales_ledger_id, sum(arrival_quantity) as arrived
        from sales_ledger_arrival
        group by stock_out_record_id, sales_ledger_id
        ) a on a.stock_out_record_id = sor.id and a.sales_ledger_id = #{salesLedgerId}
        where sor.approval_status = 1
        and (sor.type is null or sor.type = '')
        and (b.id is not null
        or (self_ledger.id = #{salesLedgerId} and ifnull(self_ledger.ledger_type, '') = '代储'))
        group by sor.id, sor.outbound_batches, sor.vehicle_id, sor.truck_plate_no
        order by sor.id desc
    </select>
 
</mapper>