wlzboy
2026-04-01 b9e9cde65890851f4ca2d7b4809b802b88937ddf
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?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.system.mapper.SysInvoiceMapper">
    
    <resultMap type="SysInvoice" id="SysInvoiceResult">
        <result property="invoiceId"    column="invoice_id"    />
        <result property="serviceOrderId"    column="service_order_id"    />
        <result property="legacyServiceOrderId"    column="legacy_service_order_id"    />
        <result property="invoiceType"    column="invoice_type"    />
        <result property="invoiceName"    column="invoice_name"    />
        <result property="invoiceMoney"    column="invoice_money"    />
        <result property="invoiceRemarks"    column="invoice_remarks"    />
        <result property="companyAddress"    column="company_address"    />
        <result property="companyBank"    column="company_bank"    />
        <result property="companyBankNo"    column="company_bank_no"    />
        <result property="zipCode"    column="zip_code"    />
        <result property="mailAddress"    column="mail_address"    />
        <result property="contactName"    column="contact_name"    />
        <result property="contactPhone"    column="contact_phone"    />
        <result property="contactEmail"    column="contact_email"    />
        <result property="status"    column="status"    />
        <result property="invoiceNo"    column="invoice_no"    />
        <result property="invoiceUrl"    column="invoice_url"    />
        <result property="applyUserId"    column="apply_user_id"    />
        <result property="applyTime"    column="apply_time"    />
        <result property="auditUserId"    column="audit_user_id"    />
        <result property="auditTime"    column="audit_time"    />
        <result property="auditRemarks"    column="audit_remarks"    />
        <result property="syncStatus"    column="sync_status"    />
        <result property="legacyInvoiceId"    column="legacy_invoice_id"    />
        <result property="serviceCode"    column="serviceCode"    />
    </resultMap>
 
    <sql id="selectSysInvoiceVo">
        select invoice_id, service_order_id, legacy_service_order_id, invoice_type, invoice_name, invoice_money, invoice_remarks, company_address, company_bank, company_bank_no, zip_code, mail_address, contact_name, contact_phone, contact_email, status, invoice_no, invoice_url, apply_user_id, apply_time, audit_user_id, audit_time, audit_remarks, sync_status, legacy_invoice_id from sys_invoice
    </sql>
 
    <select id="selectSysInvoiceList" parameterType="SysInvoice" resultMap="SysInvoiceResult">
        select i.invoice_id, i.service_order_id, i.legacy_service_order_id, i.invoice_type, i.invoice_name, 
               i.invoice_money, i.invoice_remarks, i.company_address, i.company_bank, i.company_bank_no, 
               i.zip_code, i.mail_address, i.contact_name, i.contact_phone, i.contact_email, 
               i.status, i.invoice_no, i.invoice_url, i.apply_user_id, i.apply_time, 
               i.audit_user_id, i.audit_time, i.audit_remarks, i.sync_status, i.legacy_invoice_id,
               CONCAT(
                   IFNULL(e.legacy_service_ord_class, ''),
                   DATE_FORMAT(e.legacy_service_ns_time, '%Y%m%d'),
                   '-',
                   LPAD(IFNULL(e.legacy_service_ord_no, ''), 3, '0')
               ) as serviceCode
        from sys_invoice i
        LEFT JOIN sys_task_emergency e ON i.service_order_id = e.task_id
        <where>  
            <if test="serviceOrderId != null "> and i.service_order_id = #{serviceOrderId}</if>
            <if test="legacyServiceOrderId != null "> and i.legacy_service_order_id = #{legacyServiceOrderId}</if>
            <if test="invoiceType != null "> and i.invoice_type = #{invoiceType}</if>
            <if test="invoiceName != null  and invoiceName != ''"> and i.invoice_name like concat('%', #{invoiceName}, '%')</if>
            <if test="status != null "> and i.status = #{status}</if>
            <!-- serviceCode查询 -->
            <if test="params.serviceCode != null and params.serviceCode != ''">
                AND CONCAT(
                    IFNULL(e.legacy_service_ord_class, ''),
                    DATE_FORMAT(e.legacy_service_ns_time, '%Y%m%d'),
                    '-',
                    LPAD(IFNULL(e.legacy_service_ord_no, ''), 3, '0')
                ) LIKE CONCAT('%', #{params.serviceCode}, '%')
            </if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(i.apply_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                AND date_format(i.apply_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
            <!-- 分公司查询逻辑需要关联服务单表 -->
            <if test="params.serviceOrdClass != null and params.serviceOrdClass != ''">
                AND i.service_order_id IN (SELECT ServiceOrdID FROM service_order WHERE ServiceOrdClass = #{params.serviceOrdClass})
            </if>
        </where>
    </select>
    
    <!-- App端查询我的发票列表,返回Map包含serviceCode -->
    <select id="selectMyInvoiceList" parameterType="SysInvoice" resultType="map">
        SELECT 
            i.invoice_id as invoiceId,
            i.service_order_id as serviceOrderId,
            i.legacy_service_order_id as legacyServiceOrderId,
            i.invoice_type as invoiceType,
            i.invoice_name as invoiceName,
            i.invoice_money as invoiceMoney,
            i.invoice_remarks as invoiceRemarks,
            i.company_address as companyAddress,
            i.company_bank as companyBank,
            i.company_bank_no as companyBankNo,
            i.zip_code as zipCode,
            i.mail_address as mailAddress,
            i.contact_name as contactName,
            i.contact_phone as contactPhone,
            i.contact_email as contactEmail,
            i.status,
            i.invoice_no as invoiceNo,
            i.invoice_url as invoiceUrl,
            i.apply_user_id as applyUserId,
            i.apply_time as applyTime,
            i.audit_user_id as auditUserId,
            i.audit_time as auditTime,
            i.audit_remarks as auditRemarks,
            i.sync_status as syncStatus,
            i.legacy_invoice_id as legacyInvoiceId,
            -- 构建 serviceCode
            CONCAT(
                IFNULL(e.legacy_service_ord_class, ''),
                DATE_FORMAT(e.legacy_service_ns_time, '%Y%m%d'),
                '-',
                LPAD(IFNULL(e.legacy_service_ord_no, ''), 3, '0')
            ) as serviceCode
        FROM sys_invoice i
        LEFT JOIN sys_task_emergency e ON i.service_order_id = e.task_id
        <where>  
            <if test="serviceOrderId != null "> and i.service_order_id = #{serviceOrderId}</if>
            <if test="legacyServiceOrderId != null "> and i.legacy_service_order_id = #{legacyServiceOrderId}</if>
            <if test="invoiceType != null "> and i.invoice_type = #{invoiceType}</if>
            <if test="invoiceName != null  and invoiceName != ''"> and i.invoice_name like concat('%', #{invoiceName}, '%')</if>
            <if test="status != null "> and i.status = #{status}</if>
            <!-- 服务单号搜索 -->
            <if test="params.serviceCode != null and params.serviceCode != ''">
                AND CONCAT(
                    IFNULL(e.legacy_service_ord_class, ''),
                    DATE_FORMAT(e.legacy_service_ns_time, '%Y%m%d'),
                    '-',
                    IFNULL(e.legacy_service_ord_no, '')
                ) LIKE CONCAT('%', #{params.serviceCode}, '%')
            </if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(i.apply_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                AND date_format(i.apply_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
            <!-- 分公司查询逻辑需要关联服务单表 -->
            <if test="params.serviceOrdClass != null and params.serviceOrdClass != ''">
                AND i.service_order_id IN (SELECT ServiceOrdID FROM service_order WHERE ServiceOrdClass = #{params.serviceOrdClass})
            </if>
        </where>
    </select>
    
    <select id="selectSysInvoiceByInvoiceId" parameterType="Long" resultMap="SysInvoiceResult">
        <include refid="selectSysInvoiceVo"/>
        where invoice_id = #{invoiceId}
    </select>
 
    <select id="selectSysInvoiceByLegacyId" parameterType="Integer" resultMap="SysInvoiceResult">
        <include refid="selectSysInvoiceVo"/>
        where legacy_invoice_id = #{legacyInvoiceId}
    </select>
 
    <select id="selectSelectableTasks" resultType="Map">
        SELECT 
            t.task_id as taskId, 
            t.task_code as taskCode, 
            e.legacy_service_ord_id as legacyServiceOrderId,
            t.actual_end_time as completionTime,
            t.departure_address as departure,
            t.destination_address as destination,
            e.legacy_service_ord_class as legacyServiceOrdClass,
            e.legacy_service_ns_time as legacyServiceNsTime,
            e.legacy_service_ord_no as legacyServiceOrdNo,
            IFNULL(e.transfer_price, 0) as transferPrice,
            CONCAT(
                IFNULL(e.legacy_service_ord_class, ''),
                DATE_FORMAT(e.legacy_service_ns_time, '%Y%m%d'),
                '-',
                LPAD(IFNULL(e.legacy_service_ord_no, ''), 3, '0')
            ) as serviceCode
        FROM sys_task t
        INNER JOIN sys_task_emergency e ON t.task_id = e.task_id
        WHERE t.task_type = 'EMERGENCY_TRANSFER' 
          AND t.task_status = 'COMPLETED'
          AND (t.creator_id = #{userId} OR t.assignee_id = #{userId})
          AND t.task_id NOT IN (
              SELECT service_order_id 
              FROM sys_invoice 
              WHERE status IN (0, 1)
          )
          <if test="searchKeyword != null and searchKeyword != ''">
              AND (
                  t.task_code LIKE CONCAT('%', #{searchKeyword}, '%')
                  OR e.legacy_service_ord_no LIKE CONCAT('%', #{searchKeyword}, '%')
                  OR CONCAT(
                      IFNULL(e.legacy_service_ord_class, ''),
                      DATE_FORMAT(e.legacy_service_ns_time, '%Y%m%d'),
                      '-',
                    LPAD(IFNULL(e.legacy_service_ord_no, ''), 3, '0')
 
                  ) LIKE CONCAT('%', #{searchKeyword}, '%')
              )
          </if>
          <if test="serviceOrdClass != null and serviceOrdClass != ''">
              AND e.legacy_service_ord_class = #{serviceOrdClass}
          </if>
        ORDER BY t.actual_end_time DESC
        LIMIT 100
    </select>
        
    <insert id="insertSysInvoice" parameterType="SysInvoice" useGeneratedKeys="true" keyProperty="invoiceId">
        insert into sys_invoice
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="serviceOrderId != null">service_order_id,</if>
            <if test="legacyServiceOrderId != null">legacy_service_order_id,</if>
            <if test="invoiceType != null">invoice_type,</if>
            <if test="invoiceName != null and invoiceName != ''">invoice_name,</if>
            <if test="invoiceMoney != null">invoice_money,</if>
            <if test="invoiceRemarks != null">invoice_remarks,</if>
            <if test="companyAddress != null">company_address,</if>
            <if test="companyBank != null">company_bank,</if>
            <if test="companyBankNo != null">company_bank_no,</if>
            <if test="zipCode != null">zip_code,</if>
            <if test="mailAddress != null">mail_address,</if>
            <if test="contactName != null">contact_name,</if>
            <if test="contactPhone != null">contact_phone,</if>
            <if test="contactEmail != null">contact_email,</if>
            <if test="status != null">status,</if>
            <if test="invoiceNo != null">invoice_no,</if>
            <if test="invoiceUrl != null">invoice_url,</if>
            <if test="applyUserId != null">apply_user_id,</if>
            <if test="applyTime != null">apply_time,</if>
            <if test="auditUserId != null">audit_user_id,</if>
            <if test="auditTime != null">audit_time,</if>
            <if test="auditRemarks != null">audit_remarks,</if>
            <if test="syncStatus != null">sync_status,</if>
            <if test="legacyInvoiceId != null">legacy_invoice_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="serviceOrderId != null">#{serviceOrderId},</if>
            <if test="legacyServiceOrderId != null">#{legacyServiceOrderId},</if>
            <if test="invoiceType != null">#{invoiceType},</if>
            <if test="invoiceName != null and invoiceName != ''">#{invoiceName},</if>
            <if test="invoiceMoney != null">#{invoiceMoney},</if>
            <if test="invoiceRemarks != null">#{invoiceRemarks},</if>
            <if test="companyAddress != null">#{companyAddress},</if>
            <if test="companyBank != null">#{companyBank},</if>
            <if test="companyBankNo != null">#{companyBankNo},</if>
            <if test="zipCode != null">#{zipCode},</if>
            <if test="mailAddress != null">#{mailAddress},</if>
            <if test="contactName != null">#{contactName},</if>
            <if test="contactPhone != null">#{contactPhone},</if>
            <if test="contactEmail != null">#{contactEmail},</if>
            <if test="status != null">#{status},</if>
            <if test="invoiceNo != null">#{invoiceNo},</if>
            <if test="invoiceUrl != null">#{invoiceUrl},</if>
            <if test="applyUserId != null">#{applyUserId},</if>
            <if test="applyTime != null">#{applyTime},</if>
            <if test="auditUserId != null">#{auditUserId},</if>
            <if test="auditTime != null">#{auditTime},</if>
            <if test="auditRemarks != null">#{auditRemarks},</if>
            <if test="syncStatus != null">#{syncStatus},</if>
            <if test="legacyInvoiceId != null">#{legacyInvoiceId},</if>
         </trim>
    </insert>
 
    <update id="updateSysInvoice" parameterType="SysInvoice">
        update sys_invoice
        <trim prefix="SET" suffixOverrides=",">
            <if test="serviceOrderId != null">service_order_id = #{serviceOrderId},</if>
            <if test="legacyServiceOrderId != null">legacy_service_order_id = #{legacyServiceOrderId},</if>
            <if test="invoiceType != null">invoice_type = #{invoiceType},</if>
            <if test="invoiceName != null and invoiceName != ''">invoice_name = #{invoiceName},</if>
            <if test="invoiceMoney != null">invoice_money = #{invoiceMoney},</if>
            <if test="invoiceRemarks != null">invoice_remarks = #{invoiceRemarks},</if>
            <if test="companyAddress != null">company_address = #{companyAddress},</if>
            <if test="companyBank != null">company_bank = #{companyBank},</if>
            <if test="companyBankNo != null">company_bank_no = #{companyBankNo},</if>
            <if test="zipCode != null">zip_code = #{zipCode},</if>
            <if test="mailAddress != null">mail_address = #{mailAddress},</if>
            <if test="contactName != null">contact_name = #{contactName},</if>
            <if test="contactPhone != null">contact_phone = #{contactPhone},</if>
            <if test="contactEmail != null">contact_email = #{contactEmail},</if>
            <if test="status != null">status = #{status},</if>
            <if test="invoiceNo != null">invoice_no = #{invoiceNo},</if>
            <if test="invoiceUrl != null">invoice_url = #{invoiceUrl},</if>
            <if test="applyUserId != null">apply_user_id = #{applyUserId},</if>
            <if test="applyTime != null">apply_time = #{applyTime},</if>
            <if test="auditUserId != null">audit_user_id = #{auditUserId},</if>
            <if test="auditTime != null">audit_time = #{auditTime},</if>
            <if test="auditRemarks != null">audit_remarks = #{auditRemarks},</if>
            <if test="syncStatus != null">sync_status = #{syncStatus},</if>
            <if test="legacyInvoiceId != null">legacy_invoice_id = #{legacyInvoiceId},</if>
        </trim>
        where invoice_id = #{invoiceId}
    </update>
 
    <delete id="deleteSysInvoiceByInvoiceId" parameterType="Long">
        delete from sys_invoice where invoice_id = #{invoiceId}
    </delete>
 
    <delete id="deleteSysInvoiceByInvoiceIds" parameterType="String">
        delete from sys_invoice where invoice_id in 
        <foreach item="invoiceId" collection="array" open="(" separator="," close=")">
            #{invoiceId}
        </foreach>
    </delete>
</mapper>