liurunyu
2 天以前 6630fbaf54a4c36fb4e7bf6bfd5ee62e6f56e4ed
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
package com.dy.pipIrrTerminal.card;
 
import com.dy.pipIrrGlobal.daoPr.PrWaterPriceMapper;
import com.dy.pipIrrGlobal.daoSe.SeCardOperateMapper;
import com.dy.pipIrrGlobal.daoSe.SeClientCardMapper;
import com.dy.pipIrrGlobal.daoSe.SeClientMapper;
import com.dy.pipIrrGlobal.daoSe.SeRechargeHistoryMapper;
import com.dy.pipIrrGlobal.pojoSe.SeCardOperate;
import com.dy.pipIrrGlobal.pojoSe.SeClientCard;
import com.dy.pipIrrGlobal.pojoSe.SeRechargeHistory;
import com.dy.pipIrrGlobal.voSe.VoTermActiveCard;
import com.dy.pipIrrGlobal.voSe.VoTermRecharge;
import com.dy.pipIrrTerminal.card.dto.ActiveCard;
import com.dy.pipIrrTerminal.card.dto.CardSimple;
import com.dy.pipIrrTerminal.card.dto.DtoRecharge;
import com.dy.pipIrrTerminal.card.dto.DtoReissue;
import com.dy.pipIrrTerminal.card.enums.CardStateENUM;
import com.dy.pipIrrTerminal.card.enums.LastOperateENUM;
import com.dy.pipIrrTerminal.card.enums.OperateTypeENUM;
import com.dy.pipIrrTerminal.card.enums.RechargeTypeENUM;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
 
/**
 * @author ZhuBaoMin
 * @date 2025-05-08 10:15
 * @LastEditTime 2025-05-08 10:15
 * @Description
 */
 
@Slf4j
@Service
public class CardSv {
    @Autowired
    private SeCardOperateMapper seCardOperateMapper;
 
    @Autowired
    private SeClientCardMapper seClientCardMapper;
 
    @Autowired
    private SeClientMapper seClientMapper;
 
    @Autowired
    private PrWaterPriceMapper prWaterPriceMapper;
 
    @Autowired
    private SeRechargeHistoryMapper seRechargeHistoryMapper;
 
    @Value("${project.projectNo}")
    private Integer projectNo;
 
    /**
     * 根据水卡地址判断该卡是否可以开卡
     * @param cardAddr
     * @return true:可以开卡
     */
    public Boolean canActiveCard(String cardAddr) {
        // 指定水卡地址的水卡数量,无效卡片排除在外
        Long cardCount = Optional.ofNullable(seClientCardMapper.getCountByCardAddr(cardAddr)).orElse(0L);
        if (cardCount == 0) {
            return true;
        }
 
        // 指定水卡地址且正常状态或挂失状态的水卡数量
        cardCount = Optional.ofNullable(seClientCardMapper.getCountByCardAddrAndState(cardAddr)).orElse(0L);
        if (cardCount == 0) {
            return true;
        }
 
        return false;
    }
 
    /**
     * 根据水卡编号判断该卡是否可以充值
     * @param po
     * @return true:可以充值
     */
    public Map canRecharge(DtoRecharge po) {
        Map map = new HashMap<>();
        map.put("success", false);
        map.put("content", null);
 
        String stateName = Optional.ofNullable(seClientCardMapper.getCardStateByCardNum(po.getCardNum())).orElse("");
        // 单独充值时卡片必须为正常
        if((po.getRechargeType() == RechargeTypeENUM.RECHARGE.getCode()) && !stateName.equals("正常")) {
            map.put("msg", stateName + ", " + "水卡状态不支持当前操作");
            return map;
        }
 
        map.put("success", true);
        return map;
    }
 
    /**
     * 根据水卡编号判断该卡是否可以补卡
     * @param po
     * @return true:可以补卡
     */
    public Map canReissue(DtoReissue po) {
        Map map = new HashMap<>();
        map.put("success", false);
        map.put("content", null);
 
        Long cardNum = po.getCardNum();
        Integer lostCount = seClientCardMapper.getLostCount(cardNum);
        Integer replacedCount = seClientCardMapper.getReplacedCount(cardNum);
        if(lostCount == 0 || replacedCount > 0) {
            map.put("msg", "水卡未挂失或已补卡,不能补卡");
            return map;
        }
 
        Float reissueAmount = po.getReissueAmount();
        if (reissueAmount != null && reissueAmount > 0) {
            Double tradeAmount = seCardOperateMapper.getTradeAmountByCardNo(cardNum);
            if (tradeAmount != null && tradeAmount > 0) {
                map.put("msg", "原卡挂失时已退款,补卡时不能补费用");
                return map;
            }
        }
 
        Map map_card = Optional.ofNullable(seClientCardMapper.getCardIdAndClientNum(cardNum)).orElse(new HashMap());
        if (map_card == null || map_card.size() <= 0) {
            map.put("msg", "卡号错误,该卡不存在");
            return map;
        }
 
        CardSimple card = new CardSimple();
        card.setCardId(Long.parseLong(map_card.get("cardId").toString()));
        card.setClientId(Long.parseLong(map_card.get("clientId").toString()));
        card.setProtocol(map_card.get("protocol").toString());
 
        map.put("success", true);
        map.put("content", card);
        return map;
    }
 
    /**
     * 根据农户编号获取5级行政区划串areaCode,补卡过程中开新卡使用
     * @param clientId
     * @return
     */
    public Long getAreaCodeById(Long clientId) {
        return seClientMapper.getAreaCodeById(clientId);
    }
 
    public String getCardNumOfMax(String areaCode) {
        return seClientCardMapper.getCardNumOfMax(areaCode);
    }
 
    public Map generateCardNum(Long clientId) {
        Map map_cardNum = new HashMap<>();
        map_cardNum.put("success", false);
        map_cardNum.put("content", null);
 
        // 获取5级行政区划串areaCode
        Long areaCodeL = getAreaCodeById(clientId);
        if (areaCodeL == null) {
            map_cardNum.put("msg", "该农户行政区划异常");
            return map_cardNum;
        }
 
        String areaCode = String.valueOf(areaCodeL);
        String cardNum = Optional.ofNullable(getCardNumOfMax(areaCode)).orElse("");
        if (cardNum != null && cardNum.trim().length() > 0) {
            Integer number = Integer.parseInt(cardNum.substring(12));
            number = number + 1;
            if (number > 65535) {
                map_cardNum.put("msg", "水卡编号已满");
                return map_cardNum;
            }
            cardNum = cardNum.substring(0, 12) + String.format("%05d", number);
        } else {
            cardNum = areaCode + "00001";
        }
 
        map_cardNum.put("success", true);
        map_cardNum.put("content", cardNum);
        return map_cardNum;
    }
 
    public String generateOrderNo() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyMMddHHmmss");
        LocalDateTime dateTime = LocalDateTime.now();
        Random random = new Random();
        String CHARACTERS = "0123456789";
        StringBuilder sb = new StringBuilder(4);
        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(index));
        }
        return dtf.format(dateTime) + sb.toString();
    }
 
    public Map addCardAndOperate(ActiveCard po, Long cardNum, String orderNo) {
        Map map = new HashMap<>();
        map.put("success", false);
        map.put("content", null);
 
        // amount:充值接口为输入参数,补卡接口为原卡退还金额
        SeClientCard seClientCard = new SeClientCard();
        seClientCard.setProtocol(po.getProtocol());
        seClientCard.setCardaddr(po.getCardAddr());
        seClientCard.setCardnum(String.valueOf(cardNum));
        seClientCard.setClientid(po.getClientId());
        seClientCard.setMoney(po.getAmount());
        seClientCard.setState(CardStateENUM.INVALID.getCode());
        if (po.getOriginalCardId() != null) {
            // 补卡
            seClientCard.setOriginalCardId(po.getOriginalCardId());
            seClientCard.setLastoper(LastOperateENUM.REPLACE.getCode());
            seClientCard.setReplacedt(new Date());
        } else {
            // 开新卡
            seClientCard.setLastoper(LastOperateENUM.ACTIVE.getCode());
            seClientCard.setCreatedt(new Date());
        }
 
        seClientCardMapper.insert(seClientCard);
        Long cardId = Optional.ofNullable(seClientCard.getId()).orElse(0L);
        if (cardId == 0) {
            map.put("msg", "开卡失败-农户卡写入异常");
            return map;
        }
 
        SeCardOperate seCardOperate = new SeCardOperate();
        seCardOperate.setCardId(cardId);
        seCardOperate.setClientId(po.getClientId());
        seCardOperate.setMoney(0f);
        seCardOperate.setCardCost(po.getCardCost());
        seCardOperate.setPaymentId(po.getPaymentId());
        if (po.getOriginalCardId() != null) {
            // 补卡
            seCardOperate.setOperateType(OperateTypeENUM.REISSUE.getCode());
            seCardOperate.setNoTradeAmount(po.getAmount());
        } else {
            // 开新卡
            seCardOperate.setOperateType(OperateTypeENUM.ACTIVE.getCode());
        }
        seCardOperate.setRemarks(po.getRemarks());
        seCardOperate.setOperator(po.getOperator());
        seCardOperate.setOperateDt(new Date());
        seCardOperate.setOrderNo(orderNo);
        seCardOperate.setOperateValid((byte) 1);
 
        seCardOperateMapper.insert(seCardOperate);
        Long rec = Optional.ofNullable(seCardOperate.getId()).orElse(0L);
        if (rec == 0) {
            map.put("msg", "开卡失败-开卡记录写入异常");
            return map;
        }
 
        map.put("success", true);
        return map;
    }
 
    /**
     * 开卡附加充值
     * @param po
     * @param cardNum
     * @return
     */
    public Map plusRecharge(ActiveCard po, Long cardNum) {
        Map map = new HashMap<>();
        map.put("success", false);
        map.put("content", null);
 
        /**
         * 添加水卡操作记录
         */
        DtoRecharge dtoRecharge = new DtoRecharge();
        dtoRecharge.setCardNum(cardNum);
        dtoRecharge.setAmount(po.getAmount());
        dtoRecharge.setPaymentId(po.getPaymentId());
        dtoRecharge.setRemarks(po.getRemarks());
        dtoRecharge.setOperator(po.getOperator());
        dtoRecharge.setMoney(0f);
        dtoRecharge.setGift(0f);
        dtoRecharge.setPrice(0f);
        Map map_plusRecharge = addRecharge(dtoRecharge);
        if (!map_plusRecharge.get("success").equals(true)) {
            map.put("msg", "开卡失败-充值异常");
            return map;
        }
        map.put("success", true);
        return map;
    }
 
    /**
     * 激活或补卡
     * @param po
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Map activeOrReissueTermCard(ActiveCard po) {
        Map map = new HashMap<>();
        map.put("success", false);
        map.put("content", null);
        Float amount = po.getAmount();
        Long originalCardId = po.getOriginalCardId();
 
        if (!canActiveCard(po.getCardAddr())) {
            map.put("msg", "开卡失败-此卡已存在");
            return map;
        }
 
        Map map_cardNum = generateCardNum(po.getClientId());
        if(map_cardNum.get("success").equals(false)) {
            map.put("msg", map_cardNum.get("msg").toString());
            return map;
        }
        Long cardNum = Long.parseLong(map_cardNum.get("content").toString());
 
        String orderNo = generateOrderNo();
        Map map_addCardAndOperate = addCardAndOperate(po, cardNum, orderNo);
        if(map_addCardAndOperate.get("success").equals(false)) {
            map.put("msg", map_addCardAndOperate.get("msg").toString());
            return map;
        }
 
        if (amount != null && amount > 0 && originalCardId == null) {
            Map map_plusRecharge = plusRecharge(po, cardNum);
            if(map_plusRecharge.get("success").equals(false)) {
                map.put("msg", map_plusRecharge.get("msg").toString());
                return map;
            }
        }
 
        Float balance = Optional.ofNullable(seClientCardMapper.getMoneyByCardNum(cardNum)).orElse(0f);
        Double waterPrice = prWaterPriceMapper.getPrice();
 
        VoTermActiveCard voTermActiveCard = new VoTermActiveCard();
        voTermActiveCard.setProjectNo(projectNo);
        voTermActiveCard.setCardNum(cardNum);
        voTermActiveCard.setBalance(balance);
        voTermActiveCard.setWaterPrice(waterPrice);
        voTermActiveCard.setTime(new Date());
        voTermActiveCard.setOrderNo(orderNo);
 
        map.put("success", true);
        map.put("msg", "操作成功");
        map.put("content", voTermActiveCard);
        return map;
    }
 
    /**
     * 充值
     * @param po
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Map addRecharge(DtoRecharge po) {
        Map map = new HashMap<>();
        map.put("success", false);
        map.put("content", null);
 
        Map map_canRecharge = canRecharge(po);
        if(map_canRecharge.get("success").equals(false)) {
            map.put("msg", map_canRecharge.get("msg").toString());
            return map;
        }
 
        /**
         * 依据水卡编号获取水卡表主键及农户编号
         */
        Map map_cardAndClient = seClientCardMapper.getCardIdAndClientNum(po.getCardNum());
        if (map_cardAndClient == null || map_cardAndClient.size() <= 0) {
            map.put("msg", "卡号错误,该卡不存在");
            return map;
        }
        Long cardId = Long.parseLong(map_cardAndClient.get("cardId").toString());
        Long clientId = Long.parseLong(map_cardAndClient.get("clientId").toString());
        String orderNo = generateOrderNo();
 
        /**
         * 添加水卡充值操作记录
         */
        SeCardOperate seCardOperate = new SeCardOperate();
        seCardOperate.setCardId(cardId);
        seCardOperate.setClientId(clientId);
        seCardOperate.setMoney(po.getMoney());
        seCardOperate.setTradeAmount(po.getAmount());
        seCardOperate.setGift(po.getGift());
        seCardOperate.setOperateType(OperateTypeENUM.RECHARGE.getCode());
        seCardOperate.setPaymentId(po.getPaymentId());
        seCardOperate.setPrice(po.getPrice());
        seCardOperate.setRemarks(po.getRemarks());
        seCardOperate.setOperator(po.getOperator());
        seCardOperate.setOperateDt(new Date());
        seCardOperate.setOrderNo(orderNo);
        seCardOperate.setOperateValid((byte) 1);
        seCardOperateMapper.insert(seCardOperate);
        Long rec = Optional.ofNullable(seCardOperate.getId()).orElse(0L);
        if (rec == 0) {
            map.put("msg", "充值失败-充值记录写入异常");
            return map;
        }
 
        /**
         * 添加充值历史记录
         */
        SeRechargeHistory seRechargeHistory = new SeRechargeHistory();
        seRechargeHistory.setCardid(cardId);
        seRechargeHistory.setClientid(clientId);
        seRechargeHistory.setMoney(po.getMoney());
        seRechargeHistory.setAmount(po.getAmount());
        seRechargeHistory.setGift(po.getGift());
        Float afterRecharge = po.getMoney() + po.getAmount() + po.getGift();
        seRechargeHistory.setAfterrecharge(afterRecharge);
        seRechargeHistory.setPaymentid(po.getPaymentId());
        seRechargeHistory.setPrice(po.getPrice());
        seRechargeHistory.setRemarks(po.getRemarks());
        seRechargeHistory.setOperator(po.getOperator());
        seRechargeHistory.setOperatedt(new Date());
        seRechargeHistory.setOperateValid((byte) 1);
        seRechargeHistoryMapper.insert(seRechargeHistory);
        Long rec_seRechargeHistory = Optional.ofNullable(seRechargeHistory.getId()).orElse(0L);
        if (rec_seRechargeHistory == 0) {
            map.put("msg", "充值失败-充值历史记录写入异常");
            return map;
        }
 
        VoTermRecharge voTermRecharge = new VoTermRecharge();
        voTermRecharge.setProjectNo(projectNo);
        voTermRecharge.setCardNum(po.getCardNum());
        voTermRecharge.setOrderNo(orderNo);
 
        map.put("success", true);
        map.put("msg", "操作成功");
        map.put("content", voTermRecharge);
        return map;
    }
 
    /**
     * 补卡
     * @param po
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Map reissue(DtoReissue po) {
        Map map = new HashMap<>();
        map.put("success", false);
        map.put("content", null);
 
        Map map_canReissue = canReissue(po);
        if(map_canReissue.get("success").equals(false)) {
            map.put("msg", map_canReissue.get("msg").toString());
            return map;
        }
        CardSimple card = (CardSimple) map_canReissue.get("content");
        Long cardId = card.getCardId();
        Long clientId = card.getClientId();
        String protocol = card.getProtocol();
 
        /**
         * 添加开卡记录,退还金额冲到新卡中
         */
        ActiveCard activeCard = new ActiveCard();
        activeCard.setProtocol(protocol);
        activeCard.setCardAddr(po.getCardAddr());
        activeCard.setClientId(clientId);
        activeCard.setOriginalCardId(cardId);
        activeCard.setCardCost(po.getCardCost());
        activeCard.setAmount(po.getReissueAmount());
        activeCard.setPaymentId(po.getPaymentId());
        activeCard.setRemarks(po.getRemarks());
        activeCard.setOperator(po.getOperator());
 
        Map map_newCard = activeOrReissueTermCard(activeCard);
        VoTermActiveCard voTermActiveCard = (VoTermActiveCard) map_newCard.get("content");
 
        map.put("success", true);
        map.put("msg", "操作成功");
        map.put("content", voTermActiveCard);
        return map;
    }
}