zhubaomin
2024-10-12 a180931acef6562560faec9fb01a46b9d4337532
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
<?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.dy.pipIrrGlobal.daoSe.SeClientCardMapper">
    <resultMap id="BaseResultMap" type="com.dy.pipIrrGlobal.pojoSe.SeClientCard">
        <!--@mbg.generated-->
        <!--@Table se_client_card-->
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="protocol" jdbcType="VARCHAR" property="protocol"/>
        <result column="cardAddr" jdbcType="VARCHAR" property="cardaddr"/>
        <result column="cardNum" jdbcType="BIGINT" property="cardnum"/>
        <result column="clientId" jdbcType="BIGINT" property="clientid"/>
        <result column="money" jdbcType="FLOAT" property="money"/>
        <result column="state" jdbcType="TINYINT" property="state"/>
        <result column="original_card_id" jdbcType="BIGINT" property="originalCardId"/>
        <result column="createDt" jdbcType="TIMESTAMP" property="createdt"/>
        <result column="replaceDt" jdbcType="TIMESTAMP" property="replacedt"/>
        <result column="rechargeDt" jdbcType="TIMESTAMP" property="rechargedt"/>
        <result column="lossDtDt" jdbcType="TIMESTAMP" property="lossdtdt"/>
        <result column="cancelDt" jdbcType="TIMESTAMP" property="canceldt"/>
        <result column="unlockDt" jdbcType="TIMESTAMP" property="unlockdt"/>
        <result column="reversalDt" jdbcType="TIMESTAMP" property="reversaldt"/>
        <result column="refundDt" jdbcType="TIMESTAMP" property="refunddt"/>
        <result column="consumeDt" jdbcType="TIMESTAMP" property="consumedt"/>
        <result column="lastOper" jdbcType="TINYINT" property="lastoper"/>
        <result column="remarks" jdbcType="VARCHAR" property="remarks"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id,
        protocol,
        cardAddr,
        cardNum,
        clientId,
        money,
        `state`,
        original_card_id,
        createDt,
        replaceDt,
        rechargeDt,
        lossDtDt,
        cancelDt,
        unlockDt,
        reversalDt,
        refundDt,
        consumeDt,
        lastOper,
        remarks
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List"/>
        from se_client_card
        where id = #{id,jdbcType=BIGINT}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        <!--@mbg.generated-->
        delete
        from se_client_card
        where id = #{id,jdbcType=BIGINT}
    </delete>
 
    <!--依据水卡地址获取水卡编号(12月19日废弃)-->
    <!--2024-06-30取消废弃,恢复使用-->
    <select id="getCardIdByAddr" resultType="java.lang.Long">
        SELECT id AS cardId
        FROM se_client_card
        WHERE cardAddr = #{cardAddr}
        ORDER BY id DESC
        LIMIT 1
    </select>
 
    <!--根据水卡编号获取水卡表主键(12月19日添加后废弃)-->
    <select id="getCardIdByNum" resultType="java.lang.Long">
        SELECT id AS cardId
        FROM se_client_card
        WHERE cardNum = #{cardNum}
    </select>
 
    <!--根据水卡编号获取水卡表主键及农户编号-->
    <select id="getCardIdAndClientNum" resultType="java.util.Map">
        <!--    SELECT id AS cardId, clientNum FROM se_client_card WHERE cardNum = #{cardNum}-->
        SELECT card.id AS cardId,
               cli.clientNum,
               cli.id  AS clientId,
               protocol
        FROM se_client_card card
                 INNER JOIN se_client cli ON card.clientId = cli.id
        WHERE card.cardNum = #{cardNum}
    </select>
 
    <!-- 根据水卡编号获取水卡对应的农户id和姓名 -->
    <select id="getClientIdAndNameByCardAddrAndCardNo" resultType="java.util.Map">
        SELECT cli.id   AS clientId,
               cli.name AS clientName
        FROM se_client_card card
                 INNER JOIN se_client cli ON card.clientId = cli.id
        WHERE card.cardAddr = #{cardAddr}
          and card.cardNum = #{cardNum}
    </select>
 
 
    <!-- 根据水卡编号获取水卡 -->
    <select id="getCardsByAddrAndNum" resultType="com.dy.pipIrrGlobal.voSe.VoCardInfo1">
        SELECT id,
               money
        FROM se_client_card
        WHERE cardAddr = #{cardAddr}
          and cardNum = #{cardNum}
    </select>
 
    <!--根据行政区划串模糊查询水卡编号-->
    <select id="getCardNumOfMax" resultType="java.lang.String">
        SELECT cardNum
        FROM se_client_card
        WHERE cardNum LIKE CONCAT(#{areaCode}, '%')
        ORDER BY cardNum desc
        LIMIT 0,1
    </select>
 
    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.dy.pipIrrGlobal.pojoSe.SeClientCard">
        <!--@mbg.generated-->
        insert into se_client_card (id, protocol, cardAddr, cardNum, clientId,
                                    money, `state`, original_card_id, createDt,
                                    replaceDt, rechargeDt, lossDtDt,
                                    cancelDt, unlockDt, reversalDt, refundDt,
                                    consumeDt, lastOper, remarks)
        values (#{id,jdbcType=BIGINT}, #{protocol,jdbcType=VARCHAR}, #{cardaddr,jdbcType=VARCHAR},
                #{cardnum,jdbcType=BIGINT}, #{clientid,jdbcType=BIGINT},
                #{money,jdbcType=FLOAT}, #{state,jdbcType=TINYINT}, #{originalCardId,jdbcType=BIGINT},
                #{createdt,jdbcType=TIMESTAMP},
                #{replacedt,jdbcType=TIMESTAMP}, #{rechargedt,jdbcType=TIMESTAMP}, #{lossdtdt,jdbcType=TIMESTAMP},
                #{canceldt,jdbcType=TIMESTAMP}, #{unlockdt,jdbcType=TIMESTAMP}, #{reversaldt,jdbcType=TIMESTAMP},
                #{refunddt,jdbcType=TIMESTAMP}, #{consumedt,jdbcType=TIMESTAMP}, #{lastoper,jdbcType=TINYINT},
                #{remarks,jdbcType=VARCHAR})
    </insert>
    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.dy.pipIrrGlobal.pojoSe.SeClientCard"
            useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into se_client_card
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="protocol != null">
                protocol,
            </if>
            <if test="cardaddr != null">
                cardAddr,
            </if>
            <if test="cardnum != null">
                cardNum,
            </if>
            <if test="clientid != null">
                clientId,
            </if>
            <if test="money != null">
                money,
            </if>
            <if test="state != null">
                `state`,
            </if>
            <if test="originalCardId != null">
                original_card_id,
            </if>
            <if test="createdt != null">
                createDt,
            </if>
            <if test="replacedt != null">
                replaceDt,
            </if>
            <if test="rechargedt != null">
                rechargeDt,
            </if>
            <if test="lossdtdt != null">
                lossDtDt,
            </if>
            <if test="canceldt != null">
                cancelDt,
            </if>
            <if test="unlockdt != null">
                unlockDt,
            </if>
            <if test="reversaldt != null">
                reversalDt,
            </if>
            <if test="refunddt != null">
                refundDt,
            </if>
            <if test="consumedt != null">
                consumeDt,
            </if>
            <if test="lastoper != null">
                lastOper,
            </if>
            <if test="remarks != null">
                remarks,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="protocol != null">
                #{protocol,jdbcType=VARCHAR},
            </if>
            <if test="cardaddr != null">
                #{cardaddr,jdbcType=VARCHAR},
            </if>
            <if test="cardnum != null">
                #{cardnum,jdbcType=BIGINT},
            </if>
            <if test="clientid != null">
                #{clientid,jdbcType=BIGINT},
            </if>
            <if test="money != null">
                #{money,jdbcType=FLOAT},
            </if>
            <if test="state != null">
                #{state,jdbcType=TINYINT},
            </if>
            <if test="originalCardId != null">
                #{originalCardId,jdbcType=BIGINT},
            </if>
            <if test="createdt != null">
                #{createdt,jdbcType=TIMESTAMP},
            </if>
            <if test="replacedt != null">
                #{replacedt,jdbcType=TIMESTAMP},
            </if>
            <if test="rechargedt != null">
                #{rechargedt,jdbcType=TIMESTAMP},
            </if>
            <if test="lossdtdt != null">
                #{lossdtdt,jdbcType=TIMESTAMP},
            </if>
            <if test="canceldt != null">
                #{canceldt,jdbcType=TIMESTAMP},
            </if>
            <if test="unlockdt != null">
                #{unlockdt,jdbcType=TIMESTAMP},
            </if>
            <if test="reversaldt != null">
                #{reversaldt,jdbcType=TIMESTAMP},
            </if>
            <if test="refunddt != null">
                #{refunddt,jdbcType=TIMESTAMP},
            </if>
            <if test="consumedt != null">
                #{consumedt,jdbcType=TIMESTAMP},
            </if>
            <if test="lastoper != null">
                #{lastoper,jdbcType=TINYINT},
            </if>
            <if test="remarks != null">
                #{remarks,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.dy.pipIrrGlobal.pojoSe.SeClientCard">
        <!--@mbg.generated-->
        update se_client_card
        <set>
            <if test="protocol != null">
                protocol = #{protocol,jdbcType=VARCHAR},
            </if>
            <if test="cardaddr != null">
                cardAddr = #{cardaddr,jdbcType=VARCHAR},
            </if>
            <if test="cardnum != null">
                cardNum = #{cardnum,jdbcType=BIGINT},
            </if>
            <if test="clientid != null">
                clientId = #{clientid,jdbcType=BIGINT},
            </if>
            <if test="money != null">
                money = #{money,jdbcType=FLOAT},
            </if>
            <if test="state != null">
                `state` = #{state,jdbcType=TINYINT},
            </if>
            <if test="originalCardId != null">
                `original_card_id` = #{originalCardId,jdbcType=BIGINT},
            </if>
            <if test="createdt != null">
                createDt = #{createdt,jdbcType=TIMESTAMP},
            </if>
            <if test="replacedt != null">
                replaceDt = #{replacedt,jdbcType=TIMESTAMP},
            </if>
            <if test="rechargedt != null">
                rechargeDt = #{rechargedt,jdbcType=TIMESTAMP},
            </if>
            <if test="lossdtdt != null">
                lossDtDt = #{lossdtdt,jdbcType=TIMESTAMP},
            </if>
            <if test="canceldt != null">
                cancelDt = #{canceldt,jdbcType=TIMESTAMP},
            </if>
            <if test="unlockdt != null">
                unlockDt = #{unlockdt,jdbcType=TIMESTAMP},
            </if>
            <if test="reversaldt != null">
                reversalDt = #{reversaldt,jdbcType=TIMESTAMP},
            </if>
            <if test="refunddt != null">
                refundDt = #{refunddt,jdbcType=TIMESTAMP},
            </if>
            <if test="consumedt != null">
                consumeDt = #{consumedt,jdbcType=TIMESTAMP},
            </if>
            <if test="lastoper != null">
                lastOper = #{lastoper,jdbcType=TINYINT},
            </if>
            <if test="remarks != null">
                remarks = #{remarks,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.dy.pipIrrGlobal.pojoSe.SeClientCard">
        <!--@mbg.generated-->
        update se_client_card
        set protocol         = #{protocol,jdbcType=VARCHAR},
            cardAddr         = #{cardaddr,jdbcType=VARCHAR},
            cardNum          = #{cardnum,jdbcType=BIGINT},
            clientId         = #{clientid,jdbcType=BIGINT},
            money            = #{money,jdbcType=FLOAT},
            `state`          = #{state,jdbcType=TINYINT},
            original_card_id = #{originalCardId,jdbcType=BIGINT},
            createDt         = #{createdt,jdbcType=TIMESTAMP},
            replaceDt        = #{replacedt,jdbcType=TIMESTAMP},
            rechargeDt       = #{rechargedt,jdbcType=TIMESTAMP},
            lossDtDt         = #{lossdtdt,jdbcType=TIMESTAMP},
            cancelDt         = #{canceldt,jdbcType=TIMESTAMP},
            unlockDt         = #{unlockdt,jdbcType=TIMESTAMP},
            reversalDt       = #{reversaldt,jdbcType=TIMESTAMP},
            refundDt         = #{refunddt,jdbcType=TIMESTAMP},
            consumeDt        = #{consumedt,jdbcType=TIMESTAMP},
            lastOper         = #{lastoper,jdbcType=TINYINT},
            remarks          = #{remarks,jdbcType=VARCHAR}
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateMoney">
        update se_client_card
        set money = #{money,jdbcType=FLOAT}
        where id = #{id,jdbcType=BIGINT}
    </update>
    <!--根据农户主键获取水卡列表(物理卡+虚拟卡)-->
    <select id="getCardInfoByClientId" resultType="com.dy.pipIrrGlobal.voSe.VoCardInfo">
        SELECT *
        FROM (SELECT '物理卡'        AS cardType,
                     card.cardNum AS cardNum,
                     card.money   AS money,
                     (CASE
                          WHEN card.state = 1 THEN '正常'
                          WHEN card.state = 2 THEN '已注销'
                          ELSE '已挂失'
                         END)     AS state
              FROM se_client_card card
                       INNER JOIN se_client cli ON cli.id = card.clientId
              WHERE state != 4 AND card.clientId = #{clientId,jdbcType=BIGINT}
              UNION ALL
              SELECT '虚拟卡'     AS cardType,
                     vc.vc_num AS cardNum,
                     vc.money  AS money,
                     (CASE
                          WHEN vc.in_use = 0 THEN '未使用'
                          WHEN vc.in_use = 1 THEN '使用中'
                         END)  AS state
              FROM se_virtual_card vc
                       INNER JOIN se_client cli ON cli.id = vc.client_id
              WHERE vc.client_id = #{clientId,jdbcType=BIGINT}
        ) card
        ORDER BY card.Money DESC, card.State DESC
    </select>
 
    <!--根据水卡编号获取操作记录列表-->
    <select id="getOperateRecordsByCardNum" resultType="java.util.HashMap">
        SELECT
            ope.id AS orderNumber,
            cli.villageId,
            cli.districtTitle,
            cli.clientNum,
            cli.`name`,
            card.cardNum,
            cli.idCard,
            cli.phone,
            IFNULL(ope.money,0) AS money,
            IFNULL(ope.gift,0) AS gift,
            IFNULL(ope.card_cost,0) AS cardCost,
            IFNULL((ope.money + ope.trade_amount + ope.gift),0) AS afterRecharge,
            ope.payment_id AS paymentId,
            pay.name AS paymentMethod,
            CASE
            WHEN ope.operate_type = 1 THEN '开卡'
            WHEN ope.operate_type = 2 THEN '充值'
            WHEN ope.operate_type = 3 THEN '销卡'
            END AS operateType,
            ope.operate_dt AS operateDt,
            user.name AS operatorName
        FROM se_card_operate ope
            LEFT JOIN se_client cli ON cli.id = ope.client_id
            LEFT JOIN se_client_card card ON card.id = ope.card_id
            LEFT JOIN se_payment_method pay ON pay.id = ope.payment_id
            LEFT JOIN ba_user user ON user.id = ope.operator
        WHERE ope.operate_valid = 2 AND ope.operate_type in (1,2,3) AND cardNum = #{cardNum,jdbcType=BIGINT}
<!--        SELECT *-->
<!--        FROM v_operate-->
<!--        WHERE cardNum = #{cardNum,jdbcType=BIGINT}-->
    </select>
 
    <!--根据水卡编号获取余额-->
    <select id="getMoneyByCardNum" resultType="java.lang.Float">
        SELECT money
        FROM se_client_card
        WHERE cardNum = #{cardNum,jdbcType=BIGINT}
    </select>
 
    <!--根据水卡编号获取充值总额-->
    <select id="sumRechargeByCardNum" resultType="java.lang.Float">
        SELECT SUM(his.amount) AS amount
        FROM se_recharge_history his
                 INNER JOIN se_client_card card ON his.cardId = card.id
        WHERE card.cardNum = #{cardNum,jdbcType=BIGINT}
    </select>
 
    <!--根据水卡编号获取卡片状态:1-开卡,2-补卡,3-充值,4-挂失,5-注销,6-解锁,7-冲正,8-消费-->
    <select id="getCardStateByCardNum" resultType="java.lang.String">
        <!--    SELECT-->
        <!--      (CASE-->
        <!--         WHEN lastOper = 1 THEN "开卡"-->
        <!--         WHEN lastOper = 2 THEN "补卡"-->
        <!--         WHEN lastOper = 3 THEN "充值"-->
        <!--         WHEN lastOper = 4 THEN "挂失"-->
        <!--         WHEN lastOper = 5 THEN "注销"-->
        <!--         WHEN lastOper = 6 THEN "解锁"-->
        <!--         WHEN lastOper = 7 THEN "冲正"-->
        <!--         WHEN lastOper = 8 THEN "消费"-->
        <!--        END) AS stateName-->
        <!--    FROM se_client_card-->
        <!--    WHERE cardNum = ${cardNum}-->
        SELECT (CASE
                    WHEN state = 1 THEN '正常'
                    WHEN state = 2 THEN '已注销'
                    WHEN state = 3 THEN '已挂失'
                    WHEN state = 4 THEN '无效卡片'
            END) AS stateName
        FROM se_client_card
        WHERE cardNum = #{cardNum,jdbcType=BIGINT}
    </select>
 
    <!--根据指定条件获取水卡列表记录数,应用程序使用-->
    <select id="getCardsCount" parameterType="java.util.Map" resultType="java.lang.Long">
        SELECT COUNT(*) AS recordCount
        FROM se_client_card card
            INNER JOIN se_client cli ON card.clientId = cli.id
        <where>
            AND card.state = 1
            <if test="clientNum != null and clientNum != ''">
                AND cli.clientNum like CONCAT('%', #{clientNum}, '%')
            </if>
 
            <if test="clientName != null and clientName != ''">
                AND cli.name like CONCAT('%', #{clientName}, '%')
            </if>
 
            <if test="cardNum != null and cardNum != ''">
                AND card.cardNum like CONCAT('%', #{cardNum}, '%')
            </if>
        </where>
    </select>
 
    <!--根据指定条件获取水卡列表,应用程序使用-->
    <select id="getCards" resultType="com.dy.pipIrrGlobal.voSe.VoCards">
        SELECT cli.clientNum,
            cli.name AS clientName,
            cardNum,
            cli.phone,
            cli.idCard,
            card.state AS cardState,
            '正常' AS stateName,
            '农户卡' AS cardType,
            FORMAT(card.money, 2) AS money
        FROM se_client_card card
            INNER JOIN se_client cli ON card.clientId = cli.id
        <where>
            AND card.state = 1
            <if test="clientNum != null and clientNum != ''">
                AND cli.clientNum like CONCAT('%', #{clientNum}, '%')
            </if>
 
            <if test="clientName != null and clientName != ''">
                AND cli.name like CONCAT('%', #{clientName}, '%')
            </if>
 
            <if test="cardNum != null and cardNum != ''">
                AND card.cardNum like CONCAT('%', #{cardNum}, '%')
            </if>
        </where>
        ORDER BY card.id
        <trim prefix="limit ">
            <if test="start != null and count != null">
                #{start,javaType=Integer,jdbcType=INTEGER}, #{count,javaType=Integer,jdbcType=INTEGER}
            </if>
        </trim>
    </select>
 
    <!--获取已挂失未补卡的记录数量-->
    <select id="getUnreplacedRecordCount" resultType="java.lang.Long">
        SELECT COUNT(*) AS recordCount
        FROM se_client_card card
                 INNER JOIN se_client cli ON card.clientId = cli.id
        WHERE card.state = 3
          AND NOT EXISTS(SELECT * FROM se_client_card card2 WHERE card2.original_card_id = card.cardNum)
    </select>
 
    <!--获取已挂失未补卡的记录-->
    <select id="getUnreplaced" resultType="com.dy.pipIrrGlobal.voSe.VoCards">
        SELECT
            cli.clientNum,
            cli.name AS clientName,
            cardNum,
            cli.phone,
            cli.idCard,
            card.state AS cardState,
            '已挂失' AS stateName,
            '农户卡' AS cardType,
            FORMAT(card.money, 2) AS money
        FROM se_client_card card
            INNER JOIN se_client cli ON card.clientId = cli.id
        WHERE card.state = 3
            AND NOT EXISTS(SELECT * FROM se_client_card card2 WHERE card2.original_card_id = card.id)
        ORDER BY card.id
        <trim prefix="limit ">
            <if test="start != null and count != null">
                #{start,javaType=Integer,jdbcType=INTEGER}, #{count,javaType=Integer,jdbcType=INTEGER}
            </if>
        </trim>
    </select>
 
    <!--根据指定水卡编号获取挂失记录数量(补卡、解锁使用)-->
    <select id="getLostCount" resultType="java.lang.Integer">
        SELECT COUNT(*) AS recordCount
        FROM se_client_card card
        <where>
            AND card.state = 3
            <if test="cardNum != null and cardNum > 0">
                AND card.cardNum = #{cardNum}
            </if>
        </where>
    </select>
 
    <!--根据指定水卡编号获取已补卡数量(补卡、解锁使用)-->
    <select id="getReplacedCount" resultType="java.lang.Integer">
        SELECT COUNT(*) AS recordCount
        FROM se_client_card card
                 INNER JOIN se_client_card card2 ON card2.original_card_id = card.id
        <where>
            <if test="cardNum != null and cardNum > 0">
                AND card.cardNum = #{cardNum}
            </if>
        </where>
    </select>
    <!--根据农户姓名和手机号获取水卡列表记录数,应用程序使用-->
    <select id="getCardsByClientNameAndPhoneCount" resultType="java.lang.Long">
        SELECT COUNT(*) AS recordCount
        FROM se_client_card card
            INNER JOIN se_client cli ON card.clientId = cli.id
        <where>
            card.state = 1
            <if test="clientName != null and clientName != ''">
                AND cli.name like CONCAT('%', #{clientName}, '%')
            </if>
 
            <if test="phone != null and phone != ''">
                AND cli.phone like CONCAT('%', #{phone}, '%')
            </if>
        </where>
    </select>
    <!--根据农户姓名和手机号获取水卡列表,应用程序使用-->
    <select id="getCardsByClientNameAndPhone" resultType="com.dy.pipIrrGlobal.voSe.VoCards2">
        SELECT card.id               AS clientCardId,
               cli.id                AS clientId,
               cli.clientNum,
               cli.name              AS clientName,
               card.cardNum,
               cli.phone,
               cli.idCard,
               card.state            AS cardState,
               (CASE
                    WHEN card.state = 1 THEN '正常'
                    WHEN card.state = 2 THEN '已注销'
                    WHEN card.state = 3 THEN '已挂失'
                   End)              AS stateName,
               '农户卡'                 AS cardType,
               FORMAT(card.money, 2) AS money
        FROM se_client_card card
            LEFT JOIN se_client cli ON card.clientId = cli.id
        <where>
            card.state = 1
            <if test="clientName != null and clientName != ''">
                AND cli.name like CONCAT('%', #{clientName}, '%')
            </if>
 
            <if test="phone != null and phone != ''">
                AND cli.phone like CONCAT('%', #{phone}, '%')
            </if>
        </where>
        ORDER BY card.id
        <trim prefix="limit ">
            <if test="start != null and count != null">
                #{start,javaType=Integer,jdbcType=INTEGER}, #{count,javaType=Integer,jdbcType=INTEGER}
            </if>
        </trim>
    </select>
    <!--根据农户ID查询正常状态的水卡列表,小程序使用-->
    <select id="getCardsByClientID" resultType="com.dy.pipIrrGlobal.voWe.VoCards3">
        SELECT card.id               AS clientCardId,
               cli.id                AS clientId,
               cli.clientNum,
               cli.name              AS clientName,
               card.cardNum,
               cli.phone,
               cli.idCard,
               card.state            AS cardState,
               (CASE
                    WHEN card.state = 1 THEN '正常'
                    WHEN card.state = 2 THEN '已注销'
                    WHEN card.state = 3 THEN '已挂失'
                   End)              AS stateName,
               '农户卡'                 AS cardType,
               FORMAT(card.money, 2) AS money
        FROM se_client_card card
                 INNER JOIN se_client cli ON card.clientId = cli.id
        where card.state = 1
          AND cli.id like CONCAT('%', #{clientId}, '%')
 
 
        ORDER BY card.id
    </select>
    <!--当前余额总量(物理卡)-->
    <select id="getTotalMoneyIcCards" resultType="java.lang.Double">
        SELECT IFNULL(SUM(money), 0) AS totalMoney
        FROM `se_client_card`
        WHERE state = 1
    </select>
 
    <!--根据水卡地址获取水卡数量,无效卡片排除在外-->
    <select id="getCountByCardAddr" resultType="java.lang.Long">
        SELECT COUNT(*) AS recordCount
        FROM se_client_card
        WHERE cardAddr = #{cardAddr} AND se_client_card.state != 4
    </select>
 
    <!--根据水卡地址获取指定状态的水卡数量-->
    <select id="getCountByCardAddrAndState" resultType="java.lang.Long">
        SELECT COUNT(*) AS recordCount
        FROM se_client_card
        WHERE cardAddr = #{cardAddr}
          AND state IN (1, 3)
    </select>
 
    <!--获取指定时间段水卡使用情况记录数量-->
    <select id="getCardUsagesCount" resultType="com.dy.pipIrrGlobal.voSt.VoCardUsage">
        SELECT
            cli.name AS clientName,
            card.cardNum,
            (SELECT ROUND(SUM(rch.amount),2) FROM se_recharge_history rch WHERE rch.cardId = card.id AND rch.operateDt BETWEEN #{timeStart} AND #{timeStop}) AS totalRecharge,
            (SELECT ROUND(SUM(his.cl_this_money),2) FROM rm_open_close_valve_history his WHERE his.cl_ic_card_no = card.cardNum AND his.cl_dt BETWEEN #{timeStart} AND #{timeStop}) AS totalConsumption,
            ROUND(card.money,2) AS balance
        FROM se_client_card card
            INNER JOIN se_client cli ON cli.id = card.clientId
            INNER JOIN se_recharge_history rch ON rch.cardId = card.id
        <where>
            <if test="clientName != null and clientName != ''">
                AND cli.name like CONCAT('%', #{clientName}, '%')
            </if>
 
            <if test="cardNum != null and cardNum != ''">
                AND card.cardNum like CONCAT('%', #{cardNum}, '%')
            </if>
 
            <if test="clientNum != null and clientNum != ''">
                AND cli.clientNum like CONCAT('%', #{clientNum}, '%')
            </if>
        </where>
    </select>
 
    <!--获取指定时间段水卡使用情况:充值合计、消费合计、余额-->
    <select id="getCardUsages" resultType="com.dy.pipIrrGlobal.voSt.VoCardUsage">
        SELECT
            cli.name AS clientName,
            card.cardNum,
            (SELECT ROUND(SUM(rch.amount),2) FROM se_recharge_history rch WHERE rch.cardId = card.id AND rch.operateDt BETWEEN #{timeStart} AND #{timeStop}) AS totalRecharge,
            (SELECT ROUND(SUM(his.cl_this_money),2) FROM rm_open_close_valve_history his WHERE his.cl_ic_card_no = card.cardNum AND his.cl_dt BETWEEN #{timeStart} AND #{timeStop}) AS totalConsumption,
            ROUND(card.money,2) AS balance
        FROM se_client_card card
            INNER JOIN se_client cli ON cli.id = card.clientId
            INNER JOIN se_recharge_history rch ON rch.cardId = card.id
        <where>
            <if test="clientName != null and clientName != ''">
                AND cli.name like CONCAT('%', #{clientName}, '%')
            </if>
 
            <if test="cardNum != null and cardNum != ''">
                AND card.cardNum like CONCAT('%', #{cardNum}, '%')
            </if>
 
            <if test="clientNum != null and clientNum != ''">
                AND cli.clientNum like CONCAT('%', #{clientNum}, '%')
            </if>
        </where>
        ORDER BY cli.name, card.cardNum
        <trim prefix="limit ">
            <if test="start != null and count != null">
                #{start,javaType=Integer,jdbcType=INTEGER}, #{count,javaType=Integer,jdbcType=INTEGER}
            </if>
        </trim>
    </select>
 
    <!--获取指定时间段内水卡充值总计-->
    <select id="getTotalRecharge" resultType="java.lang.Float">
        SELECT ROUND(SUM(amount),2) AS totalRecharge
        FROM se_recharge_history
        WHERE operateDt BETWEEN #{timeStart} AND #{timeStop}
    </select>
 
    <!--获取指定时间段内水卡消费总计-->
    <select id="getTotalConsumption" resultType="java.lang.Float">
        SELECT ROUND(SUM(cl_this_money),2) AS totalConsumption
        FROM rm_open_close_valve_history
        WHERE cl_dt BETWEEN #{timeStart} AND #{timeStop}
    </select>
 
    <!--依据水卡地址将最后一条无效状态的指定操作记录改为有效-->
    <update id="turnCardValidByAddr">
        UPDATE se_client_card
        SET state = 1
        WHERE id = (
            SELECT id FROM (
                SELECT id
                FROM se_client_card
                WHERE state = 4 AND cardAddr = #{cardAddr} AND lastOper  = #{operateType}
                ORDER BY id DESC
                LIMIT 1) AS subquery
        );
    </update>
 
    <!--根据水卡ID获取原水卡ID,补卡通知中使用-->
    <select id="getOriginalCardIdByCardId" resultType="java.lang.Long">
        SELECT original_card_id AS originalCardId FROM se_client_card WHERE id = #{cardId};
    </select>
</mapper>