12 小时以前 e731bf5f64d71ed93a570d505c52dc44d491175e
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?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.stock.mapper.StockOutRecordMapper">
 
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ruoyi.stock.pojo.StockOutRecord">
        <id column="id" property="id" />
        <result column="outbound_batches" property="outboundBatches" />
        <result column="stock_out_num" property="stockOutNum" />
        <result column="record_id" property="recordId" />
        <result column="record_type" property="recordType" />
        <result column="product_model_id" property="productModelId" />
        <result column="remark" property="remark" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="create_user" property="createUser" />
        <result column="update_user" property="updateUser" />
    </resultMap>
 
    <select id="listPage" resultType="com.ruoyi.stock.dto.StockOutRecordDto">
        WITH RECURSIVE product_tree AS (
        SELECT id
        FROM product
        WHERE id = #{params.topParentProductId}
 
        UNION ALL
 
        SELECT p.id
        FROM product p
        INNER JOIN product_tree pt ON p.parent_id = pt.id
        )
        SELECT
        sor.*,
        p.product_name as productName,
        pm.model,
        pm.unit,
        u.nick_name as createBy,
        <!-- 油品出库可绑多个销售台账(多客户),sor.sales_ledger_id 只存首项,故从绑定表汇总全部合同号;
             未绑定绑定表的存量行仍退回 sor.sales_ledger_id 关联的单个合同号 -->
        IFNULL(
            (select group_concat(distinct slb.sales_contract_no separator ',')
             from stock_out_record_sales_ledger b
                      join sales_ledger slb on slb.id = b.sales_ledger_id
             where b.stock_out_record_id = sor.id),
            sl.sales_contract_no) as salesContractNo
        FROM stock_out_record as sor
        LEFT JOIN product_model as pm on sor.product_model_id = pm.id
        LEFT JOIN product as p on pm.product_id = p.id
        LEFT JOIN sys_user as u on sor.create_user = u.user_id
        LEFT JOIN sales_ledger as sl on sl.id = sor.sales_ledger_id
        <where>
            <if test="params.timeStr != null and params.timeStr != ''">
                and sor.create_time like concat('%',#{params.timeStr},'%')
            </if>
            <if test="params.productName != null and params.productName != ''">
                and p.product_name like concat('%',#{params.productName},'%')
            </if>
            <if test="params.model != null and params.model != ''">
                and pm.model like concat('%',#{params.model},'%')
            </if>
            <if test="params.batchNo != null and params.batchNo != ''">
                and sor.batch_no like concat('%',#{params.batchNo},'%')
            </if>
            <if test="params.productModelId != null and params.productModelId > 0">
                and sor.product_model_id = #{params.productModelId}
            </if>
            <if test="params.type != null and params.type != ''">
                and sor.type = #{params.type}
            </if>
            <if test="params.recordType != null and params.recordType != ''">
                and sor.record_type = #{params.recordType}
            </if>
            <if test="params.topParentProductId != null and params.topParentProductId > 0">
                and p.id in (select id from product_tree)
            </if>
        </where>
        order by sor.id desc
    </select>
    <!-- 按来源明细查出库记录:加 /*data_scope*/ 跳过数据权限,否则换账号会查不到记录,
         导致委外的「完成校验」被静默绕过、「作废释放占用」失效 -->
    <select id="selectByRecordIds" resultType="com.ruoyi.stock.pojo.StockOutRecord">
        select /*data_scope*/ *
        from stock_out_record
        where record_type = #{recordType}
        and record_id in
        <foreach collection="recordIds" item="recordId" open="(" separator="," close=")">
            #{recordId}
        </foreach>
    </select>
 
    <!-- 油品出库可绑定的销售台账下拉。
         sales_ledger 不在数据权限白名单里,按 create_user/dept_id 过滤会让油库/发货角色选不到销售建的台账,
         所以加 /*data_scope*/ 跳过数据权限;只读查询,不按 ledger_type 过滤(代储台账也要能绑) -->
    <select id="bindableSalesLedgerPage" resultType="com.ruoyi.sales.pojo.SalesLedger">
        select /*data_scope*/ id,
        sales_contract_no,
        customer_contract_no,
        project_name,
        entry_date,
        salesman,
        customer_id,
        customer_name,
        ledger_type,
        oil_depot_id,
        oil_depot_name,
        tank_id,
        tank_no,
        approval_status,
        contract_amount
        from sales_ledger
        where 1=1
        <if test="req.salesContractNo != null and req.salesContractNo != ''">
            and sales_contract_no like concat('%',#{req.salesContractNo},'%')
        </if>
        <if test="req.customerName != null and req.customerName != ''">
            and customer_name like concat('%',#{req.customerName},'%')
        </if>
        <if test="req.projectName != null and req.projectName != ''">
            and project_name like concat('%',#{req.projectName},'%')
        </if>
        order by id desc
    </select>
 
    <select id="listStockOutRecordExportData" resultType="com.ruoyi.stock.execl.StockOutRecordExportData">
        SELECT
        sor.*,
        p.product_name as productName,
        pm.model,
        pm.unit,
        u.nick_name as createBy
        FROM stock_out_record as sor
        LEFT JOIN product_model as pm on sor.product_model_id = pm.id
        LEFT JOIN product as p on pm.product_id = p.id
        LEFT JOIN sys_user as u on sor.create_user = u.user_id
        <where>
            <if test="params.timeStr != null and params.timeStr != ''">
                and sor.create_time like concat('%',#{params.timeStr},'%')
            </if>
            <if test="params.productName != null and params.productName != ''">
                and p.product_name like concat('%',#{params.productName},'%')
            </if>
            <if test="params.type != null and params.type != ''">
                and sor.type = #{params.type}
            </if>
            <if test="params.recordType != null and params.recordType != ''">
                and sor.record_type = #{params.recordType}
            </if>
        </where>
        order by sor.id desc
    </select>
 
    <!-- 财务管理 · 销售出库台账(收款/开票候选)。
         两类来源:① record_type='13' 的手工发货单出库,客户/明细/发货编号都走 shipping_info;
         ② 油品出库(type 为空)按「出库单×客户」拆到绑定行,一个出库单绑几个台账就出几行,
         金额只算该客户分摊的那部分(b.quantity × 单价),不再是整单数量。
         用 LEFT JOIN 而不是 UNION ALL:DataScopeSqlInterceptor 看到 from 后面紧跟 '(' 会整条放行,
         把两条分支包成派生表会让出库单的数据权限静默失效。 -->
    <select id="listPageAccountSales" resultType="com.ruoyi.account.bean.vo.sales.SalesOutboundVo">
    SELECT
        sor.id,
        sor.outbound_batches,
        IFNULL(b.customer_name, sl.customer_name) AS customerName,
        sor.create_time as shippingDate,
        IFNULL(pb.product_name, p.product_name) AS productName,
        IFNULL(pmb.model, pm.model) as specification_model,
        IFNULL(slpb.tax_rate, slp.tax_rate) as tax_rate,
        CASE
            WHEN b.id IS NOT NULL THEN b.quantity * slpb.tax_inclusive_unit_price
            ELSE sor.stock_out_num * slp.tax_inclusive_unit_price
        END as outboundAmount,
        s.shipping_no,
        IFNULL(slb.sales_contract_no, sl.sales_contract_no) as sales_contract_no,
        b.id AS bindingId,
        IFNULL(b.customer_id, sl.customer_id) AS customer_id
        FROM stock_out_record sor
        <!-- 只有油品出库才去绑绑定表;手工发货单出库的 type 也是空,但它 record_type='13' 且没有绑定行,
             b.id 为 NULL 时下面统一回落到 shipping_info 那一套 -->
        LEFT JOIN stock_out_record_sales_ledger b ON b.stock_out_record_id = sor.id
        left join shipping_info s on sor.record_id = s.id
        LEFT JOIN sales_ledger sl ON s.sales_ledger_id = sl.id
        LEFT JOIN sales_ledger_product slp ON s.sales_ledger_product_id = slp.id and slp.type = 1
        LEFT JOIN sales_ledger slb ON slb.id = b.sales_ledger_id
        LEFT JOIN sales_ledger_product slpb ON slpb.id = b.sales_ledger_product_id and slpb.type = 1
        left join product_model pm on slp.product_model_id = pm.id
        left join product p on pm.product_id = p.id
        left join product_model pmb on sor.product_model_id = pmb.id
        left join product pb on pmb.product_id = pb.id
        WHERE sor.approval_status=1
        AND (
            sor.record_type='13'
            OR (b.id IS NOT NULL AND (sor.type IS NULL OR sor.type = ''))
        )
        <if test="req.outboundBatches != null and req.outboundBatches != ''">
            AND sor.outbound_batches LIKE CONCAT('%',#{req.outboundBatches},'%')
        </if>
        <if test="req.customerName != null and req.customerName != ''">
            AND IFNULL(b.customer_name, sl.customer_name) LIKE CONCAT('%',#{req.customerName},'%')
        </if>
        <if test="req.customerId != null ">
            AND IFNULL(b.customer_id, sl.customer_id) = #{req.customerId}
        </if>
        <if test="req.startDate != null and req.endDate != null">
            AND IFNULL(s.shipping_date, sor.create_time) BETWEEN #{req.startDate} AND #{req.endDate}
        </if>
        order by sor.id DESC, IFNULL(b.id, 0)
    </select>
</mapper>