kongdeqiang
2024-03-18 ffa5f49a2bcb6311486d00777b3629538eb3e6f0
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
package com.boying.controller.phone;
 
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.boying.common.R;
import com.boying.common.ReturnData;
import com.boying.common.SystemConfigProperties;
import com.boying.entity.*;
import com.boying.service.*;
import com.boying.util.DateUtilOther;
import com.boying.util.HTTPEntityUtil;
import com.boying.util.RedisJsonUtil;
import com.google.gson.Gson;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
/**
 * @author kdq
 * @version 1.0.0
 * @ClassName YCPayController.java
 * @Description 邮储对接支付接口
 * @createTime 2023年03月07日 08:22:00
 */
@RestController
@RequestMapping("/ffzf/api")
@RequiredArgsConstructor
public class YCPayController {
    private final OutParkService outParkService;
    private final PayLogService payLogService;
    private final ParkService parkService;
    private final OrderRecordService orderRecordService;
    private final TicketService ticketService;
    private final TicketBlackService ticketBlackService;
 
    private final SystemConfigProperties systemConfigProperties;
    private final WhiteListService whiteListService;
    private final EnterParkService enterParkService;
    private final WXService wxService;
    private final OutParkLogService outParkLogService;
    private final RedisJsonUtil redisJsonUtil;
 
 
    /**
     * 查询订单接口
     * @param queryId
     * @return
     */
    @GetMapping("/getByQueryId")
    public Object getByQueryId(Long queryId){
        if(queryId == null){
            return R.failed("参数为空");
        }
        OutPark byId = outParkService.getById(queryId);
        if(byId.getTime() > 0){
            byId.setTimeStr(DateUtilOther.millisToDayHrMinSec(new Double(byId.getTime()).longValue()));
        }
        return R.ok(byId,"请求成功");
    }
 
    /**
     * 缴款书
     */
    @PostMapping("park")
    public Object park(Long id){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
        OutPark outPark =  outParkService.getById(id);
        if(outPark.getPrice()==0){
            updateOutPark(outPark.getId(),"03");
            return R.failed("本次停车无需支付费用");
        }
 
        //判断是否在白名单
        QueryWrapper<WhiteList> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(WhiteList::getCarNo,outPark.getCarNo());
        List<WhiteList> all = whiteListService.list(wrapper);
        for(WhiteList w : all){
            if(w.getType()==0){
                updateOutPark(outPark.getId(),"03");
                return R.failed("本次停车无需支付费用!");
            }else{
                if(StringUtils.isNotBlank(w.getParkIds())){
                    if(w.getParkIds().contains(outPark.getParkId()+"")){
                        if(w.getEndTime()!=null){
                            if(System.currentTimeMillis()<w.getEndTime().getTime()){
                                updateOutPark(outPark.getId(),"03");
                                return R.failed("本次停车无需支付费用!");
                            }
                        }
                    }
 
                }
            }
        }
        if(StringUtils.isNotBlank(outPark.getPayCode()) && outPark.getStatus() == 0){
            String logs = sdf.format(new Date())+" 车牌号为:"+outPark.getCarNo()+",申请缴费2------》\n";
            writeTxt(logs);
           // "http://wxtest.smeia.cn/h5/static/router.html?rurl=router&router=psbc&appid=CUP16522400671&params={\"payMode\":\"B\",\"payItemNo\":\"41301106100124\",\"userNo\":"+id+"}"
            return R.ok("跳转邮储支付","https://openpayment.psbc.com/h5/static/router.html?rurl=router&router=psbc&appid=CUP14094502391&params={\"payMode\":\"B\",\"payItemNo\":\"41304508000500\",\"userNo\":"+id+"}");
          //  return R.ok("跳转邮储支付","http://wxtest.smeia.cn/h5/static/router.html?rurl=router&router=psbc&appid=CUP16522400671&params={\"payMode\":\"B\",\"payItemNo\":\"41301106100124\",\"userNo\":"+id+"}");
        }else if(StringUtils.isNotBlank(outPark.getPayCode()) && outPark.getStatus() == 1){
            return R.failed("已缴费");
        }
 
        if(StringUtils.isBlank(outPark.getPayCode())){
            String logs = sdf.format(new Date())+" 车牌号为:"+outPark.getCarNo()+",申请缴费------》\n";
            writeTxt(logs);
            List<EnterPark> byCarNo = enterParkService.findByCarNo(outPark.getCarNo(),outPark.getParkId());
            if(byCarNo.size()>0){
                EnterPark enterPark = byCarNo.get(0);
                if(enterPark != null && enterPark.getIsPay() == 1){
                    if(outPark.getPrice()==enterPark.getPrice()){
                        return R.failed("已缴费");
                    }else {
                        outPark.setPrice(outPark.getPrice()-enterPark.getPrice());
                        outParkService.updateById(outPark);
                    }
                }
            }
            addOrderRecord(outPark);
            return R.ok("跳转邮储支付","https://openpayment.psbc.com/h5/static/router.html?rurl=router&router=psbc&appid=CUP14094502391&params={\"payMode\":\"B\",\"payItemNo\":\"41304508000500\",\"userNo\":"+id+"}");
        }else {
            return R.failed("出错");
        }
    }
 
 
    /**
     * 判断是否缴费
     */
    @PostMapping("parkXT")
    public Object parkXT(Long id){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
        OutPark outPark =  outParkService.getById(id);
        System.out.println("id:"+id);
        if(id == null){
            return R.failed("未找到数据,请稍后重新点击");
        }
        if(outPark.getPrice()==0){
            updateOutPark(outPark.getId(),"03");
            return R.failed("本次停车无需支付费用");
        }
 
        //判断是否在白名单
        QueryWrapper<WhiteList> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(WhiteList::getCarNo,outPark.getCarNo());
        List<WhiteList> all = whiteListService.list(wrapper);
        for(WhiteList w : all){
            if(w.getType()==0){
                updateOutPark(outPark.getId(),"03");
                return R.failed("本次停车无需支付费用!");
            }else{
                if(StringUtils.isNotBlank(w.getParkIds())){
                    if(w.getParkIds().contains(outPark.getParkId()+"")){
                        if(w.getEndTime()!=null){
                            if(System.currentTimeMillis()<w.getEndTime().getTime()){
                                updateOutPark(outPark.getId(),"03");
                                return R.failed("本次停车无需支付费用!");
                            }
                        }
                    }
 
                }
            }
        }
        if(StringUtils.isNotBlank(outPark.getPayCode()) && outPark.getStatus() == 0){
            String logs = sdf.format(new Date())+" 车牌号为:"+outPark.getCarNo()+",申请缴费2------》\n";
            writeTxt(logs);
            OutParkLog byOutPark = outParkLogService.getByOutPark(outPark.getCarNo(), outPark.getId());
            byOutPark.setPayStartTime(LocalDateTime.now());
            outParkLogService.saveOrUpdate(byOutPark);
            return R.ok(null,"跳转邢台银行支付");
        }else if(StringUtils.isNotBlank(outPark.getPayCode()) && outPark.getStatus() == 1){
            return R.failed("已缴费");
        }
 
        if(StringUtils.isBlank(outPark.getPayCode())){
            String logs = sdf.format(new Date())+" 车牌号为:"+outPark.getCarNo()+",申请缴费------》\n";
            writeTxt(logs);
            List<EnterPark> byCarNo = enterParkService.findByCarNo(outPark.getCarNo(),outPark.getParkId());
            if(byCarNo.size()>0){
                EnterPark enterPark = byCarNo.get(0);
                if(enterPark != null && enterPark.getIsPay() == 1){
                    if(outPark.getPrice()==enterPark.getPrice()){
                        return R.failed("已缴费");
                    }else {
                        outPark.setPrice(outPark.getPrice()-enterPark.getPrice());
                        outParkService.updateById(outPark);
                        OutParkLog byOutPark = outParkLogService.getByOutPark(outPark.getCarNo(), outPark.getId());
                        byOutPark.setPayStartTime(LocalDateTime.now());
                        outParkLogService.saveOrUpdate(byOutPark);
                    }
                }
            }
            addOrderRecord(outPark);
            return R.ok(null,"跳转邢台银行支付");
        }else {
            return R.failed("出错");
        }
    }
 
    /**
     * 阿里缴费
     */
    @PostMapping("payByAli")
    public Object payByAli(Long id){
        OutPark outPark =  outParkService.getById(id);
        String xtyhpay = systemConfigProperties.getXTYHPAY();
        LocalDateTime date3 = LocalDateTime.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdf.format(new Date());
        Map<String,Object> map = new HashMap<>();
        map.put("encoding","UTF-8");
        map.put("signMethod","01");
        map.put("sdkAppId",systemConfigProperties.getXTYHMERID());
        map.put("txnType","1007");
        map.put("txnSubType","100702");
        map.put("txnAccType","03");
        map.put("aesWay","01");
        map.put("merId",systemConfigProperties.getXTYHMERID());
        map.put("merName",systemConfigProperties.getXTYHMERNAME());
        map.put("backEndUrl",systemConfigProperties.getXTYHRESULT());
        map.put("txnOrderId",outPark.getId()+""+format);
        map.put("txnOrderTime",format);
        map.put("txnOrderBody",outPark.getCarNo()+"泊车缴费");
        map.put("txnAmt",Math.round(outPark.getPrice() * 100)+"");
        map.put("txnCcyType","156");
        if(outPark.getPayTime() != null){
            LocalDateTime date4 = outPark.getPayTime();
            Duration duration = Duration.between(date3, date4);
            long l = duration.toMinutes();
            if(l<3){
                map.put("preTxnSsn",outPark.getCode2());
            }
        }
 
        Map<String, String> stringStringMap = wxService.addSign(map);
        map.put("certId",stringStringMap.get("certId"));
        map.put("signAture",stringStringMap.get("signAture"));
        Gson gson =new Gson();
        String str = gson.toJson(map);
        System.out.println("请求报文:"+str);
        writeTxt2(outPark.getId()+"ALipay请求: "+str);
 
        String body = HttpRequest.post(xtyhpay)
                .header(Header.CONTENT_TYPE, "application/json")
                .body(str)
                .timeout(15000)
                .execute()
                .body();
        System.out.println(outPark.getId()+"响应报文:"+body);
 
        writeTxt2(outPark.getId()+"ALipay响应: "+body);
        JSONObject map1 = JSON.parseObject(body);
        String respCode = (String)map1.get("respCode");
        if(respCode.equals("0000")){
            String respTxnSsn = (String)map1.get("respTxnSsn");
            String respData = (String)map1.get("respData");
            String respTxnTime = (String)map1.get("respTxnTime");
            Map map2 = gson.fromJson(respData, Map.class);
            String qrCode = (String)map2.get("qrCode");
            PayLog payLog = new PayLog();
            payLog.setOutParkId(outPark.getId());
            payLog.setCode2(respTxnSsn);
            payLog.setQrCode(qrCode);
            payLog.setPayTime(LocalDateTime.now());
            payLog.setTxnOrderId(outPark.getId()+""+format);
            payLog.setTxnOrderTime(format);
            payLog.setRespTxnTime(respTxnTime);
            payLogService.save(payLog);
            return R.ok(null,qrCode);
        }
        String respMsg = (String)map1.get("respMsg");
        return R.failed(respCode+":"+respMsg);
    }
 
    /**
     * 获取微信openid
     */
    @PostMapping("getOpenId")
    public Object park(Long id,String code){
        OutPark outPark =  outParkService.getById(id);
        String openIdByCode = wxService.getOpenIdByCode(code);
        writeTxt2(outPark.getId()+"获取openid为:"+openIdByCode);
        return payByWX(id,openIdByCode);
    }
 
    /**
     * 微信缴费
     */
    @PostMapping("payByWX")
    public Object payByWX(Long id,String openId){
        System.out.println("开始支付");
        OutPark outPark =  outParkService.getById(id);
        String xtyhpay = systemConfigProperties.getXTYHPAY();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdf.format(new Date());
        LocalDateTime date3 = LocalDateTime.now();
        Map<String,Object> map = new HashMap<>();
        map.put("encoding","UTF-8");
        map.put("signMethod","01");
        map.put("sdkAppId",systemConfigProperties.getXTYHMERID());
        map.put("txnType","1006");
        map.put("txnSubType","100603");
        map.put("txnAccType","02");
        map.put("secMerAppId",systemConfigProperties.getXTYHAPPID());
        map.put("txnSubOpenid",openId);
        map.put("txnProductId",outPark.getId()+"");
        map.put("aesWay","01");
        map.put("merId",systemConfigProperties.getXTYHMERID());
        map.put("merName",systemConfigProperties.getXTYHMERNAME());
        map.put("backEndUrl",systemConfigProperties.getXTYHRESULT());
        map.put("txnOrderId",outPark.getId()+""+format);
        map.put("txnOrderTime",format);
        map.put("txnOrderBody",outPark.getCarNo()+"泊车缴费");
        map.put("txnAmt",Math.round(outPark.getPrice() * 100)+"");
        map.put("txnCcyType","156");
        if(outPark.getPayTime() != null){
            LocalDateTime date4 = outPark.getPayTime();
            Duration duration = Duration.between(date3, date4);
            long l = duration.toMinutes();
            if(l<3){
                map.put("preTxnSsn",outPark.getCode2());
            }
        }
        Map<String, String> stringStringMap = wxService.addSign(map);
        map.put("certId",stringStringMap.get("certId"));
        map.put("signAture",stringStringMap.get("signAture"));
        Gson gson =new Gson();
        String str = gson.toJson(map);
        System.out.println("请求报文:"+str);
        writeTxt2(outPark.getId()+"WXpay请求: "+str);
 
        String body = HttpRequest.post(xtyhpay)
                .header(Header.CONTENT_TYPE, "application/json")
                .body(str)
                .timeout(15000)
                .execute()
                .body();
        System.out.println(outPark.getId()+"响应报文:"+body);
 
        writeTxt2(outPark.getId()+"WXpay响应: "+body);
        JSONObject map1 = JSON.parseObject(body);
        String respCode = (String)map1.get("respCode");
        if(respCode.equals("0000")){
            String respTxnSsn = (String)map1.get("respTxnSsn");
            String respTxnTime = (String)map1.get("respTxnTime");
            String respData = (String)map1.get("respData");
            Map map2 = gson.fromJson(respData, Map.class);
            String appId = (String)map2.get("appId");
            String timeStamp = (String)map2.get("timeStamp");
            String nonceStr = (String)map2.get("nonceStr");
            String signType = (String)map2.get("signType");
            String package1 = (String)map2.get("package");
            String paySign = (String)map2.get("paySign");
            Map<String,String> resultMap = new HashMap<>();
            resultMap.put("appId",appId);
            resultMap.put("timeStamp",timeStamp);
            resultMap.put("nonceStr",nonceStr);
            resultMap.put("signType",signType);
            resultMap.put("package",package1);
            resultMap.put("paySign",paySign);
            PayLog payLog = new PayLog();
            payLog.setOutParkId(outPark.getId());
            payLog.setCode2(respTxnSsn);
            payLog.setPayTime(LocalDateTime.now());
            payLog.setTxnOrderId(outPark.getId()+""+format);
            payLog.setTxnOrderTime(format);
            payLog.setRespTxnTime(respTxnTime);
            payLogService.save(payLog);
            return R.ok(resultMap,null);
        }
        String respMsg = (String)map1.get("respMsg");
        return R.failed(respCode+":"+respMsg);
    }
 
 
    /**
     * 支付回调接口
     * @param queryId
     * @return
     */
    @GetMapping("/result")
    public Object result(Long queryId,int success,int msg){
        OutPark byId = outParkService.getById(queryId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
        if(byId == null){
            return R.failed("未查询到订单");
        }else {
            //修改订单状态 0成功
            if(success==0){
                updateOrderRecord(queryId,"03");
                String logs = sdf.format(new Date())+" 车牌号为:"+byId.getCarNo()+",缴费成功------》\n";
                writeTxt(logs);
                List<EnterPark> byCarNo = enterParkService.findByCarNo(byId.getCarNo(),byId.getParkId());
                if(byCarNo.size()>0){
                    EnterPark enterPark = byCarNo.get(0);
                    enterPark.setIsPay(1);
                    enterParkService.updateById(enterPark);
                }
                return R.ok(null,"回调成功");
            }else {
                String logs = sdf.format(new Date())+" 出场id为:"+queryId+",缴费失败------》\n";
                logs += "回执原因:"+msg+",\n";
                writeTxt(logs);
            }
        }
        return R.ok(byId,"回调成功");
    }
 
 
    /**
     * 邢台银行支付回调接口
     * @param
     * @return
     */
    @PostMapping("/getResult")
    public Object getResult(HttpServletRequest request){
        System.out.println("getResult收到访问");
        String respCode = request.getParameter("respCode");
        String respTxnSsn = request.getParameter("respTxnSsn");
//        String respCode = map.get("respCode");
//        String respTxnSsn = map.get("respTxnSsn");
        System.out.println(respCode+":"+respTxnSsn);
        if(respCode != null){
            if(respCode.equals("0000")){
                //交易成功,根据流水号查询出场记录
                PayLog payLog = payLogService.findByPayCode(respTxnSsn);
                if(payLog == null){
                    return R.failed("未查询到流水记录");
                }else {
                    OutPark byPayCode = outParkService.getById(payLog.getOutParkId());
                    writeTxt2(byPayCode.getId()+"收到回执\n");
                    if(byPayCode == null){
                        return R.failed("未查询到订单");
                    }else {
                        if(byPayCode.getStatus() == 1){
                            return R.ok(null,"订单已支付");
                        }else {
                            OutParkLog byOutPark = outParkLogService.getByOutPark(byPayCode.getCarNo(), byPayCode.getId());
                            byOutPark.setPayEndTime(LocalDateTime.now());
                            outParkLogService.saveOrUpdate(byOutPark);
                            updateOrderRecord((long)byPayCode.getId(),"03");
                            List<EnterPark> byCarNo = enterParkService.findByCarNo(byPayCode.getCarNo(),byPayCode.getParkId());
                            if(byCarNo.size()>0){
                                EnterPark enterPark = byCarNo.get(0);
                                enterPark.setIsPay(1);
                                enterParkService.updateById(enterPark);
                            }
                            return R.ok(null,"回调成功");
                        }
                    }
                }
            }else if(respCode.equals("0002") || respCode.equals("0003") || respCode.equals("0003") || respCode.equals("0012")){
                //交易成功,超时,未明,稍后发起查询
            }else if(respCode.equals("0011")){
                //交易
            }
        }else {
            return R.failed("未接收到入参");
        }
        return R.failed(null,"请求失败");
    }
 
 
    /**
     * 对账接口
     * @param
     * @return
     */
    @PostMapping ("/checkbill")
    public Object checkbill(@RequestBody Map params){
        String checkDate = (String) params.get("checkDate");
        String tranDate = (String) params.get("tranDate");
        String fileName = (String) params.get("fileName");
        checkDate=checkDate.replaceAll("-","");
        String name = "E:\\ycCheckBill\\Bank_PSBC_"+checkDate+".txt";
        File file = new File(name);
        Boolean aBoolean = judeFileExists(file);
        if(aBoolean){
            return R.ok(null,"查询文件成功");
        }else {
            return R.failed(null,"文件不存在");
        }
    }
 
    /**
     * 邢台银行对账接口
     * @param
     * @return
     */
    @PostMapping ("/checkbillXTYH")
    public Object checkbillXTYH(String date){
        String xtyhpay = systemConfigProperties.getXTYHPAY();
        Map<String,Object> map = new HashMap<>();
        map.put("encoding","UTF-8");
        map.put("signMethod","01");
        map.put("txnType","4001");
        map.put("txnSubType","400102");
        map.put("billType","00");
        map.put("billDate",date);
        map.put("merId","8202309110000001");
 
        Map<String, String> stringStringMap = wxService.addSign(map);
        map.put("certId",stringStringMap.get("certId"));
        map.put("signAture",stringStringMap.get("signAture"));
        Gson gson =new Gson();
        String str = gson.toJson(map);
        System.out.println("请求报文:"+str);
        writeTxt2(date+"对账请求: "+str);
 
        String body = HttpRequest.post(xtyhpay)
                .header(Header.CONTENT_TYPE, "application/json")
                .body(str)
                .timeout(15000)
                .execute()
                .body();
 
        writeTxt2(date+"对账响应: "+body);
        JSONObject map1 = JSON.parseObject(body);
        String respCode = (String)map1.get("respCode");
        if(respCode.equals("0000")){
            String billDownloadUrl = (String)map1.get("billDownloadUrl");
            String respData = (String)map1.get("respData");
            return R.ok(null,billDownloadUrl);
        }
        String respMsg = (String)map1.get("respMsg");
        return R.failed(respCode+":"+respMsg);
    }
 
    /**
     * 交易状态查询
     * @param
     * @return
     */
    @PostMapping ("/searchPayStatus")
    public Object searchPayStatus(String txnOrderId,String txnOrderTime){
        OutPark outPark = outParkService.findByOrderId(txnOrderId);
        if(outPark == null){
            return R.failed("未找到该订单");
        }
        String xtyhpay = systemConfigProperties.getXTYHPAY();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Map<String,Object> map = new HashMap<>();
        map.put("encoding","UTF-8");
        map.put("signMethod","01");
        map.put("sdkAppId","8202309110000001");
        map.put("txnType","1009");
        map.put("txnSubType","100900");
        map.put("merId","8202309110000001");
        map.put("origTxnOrderId",txnOrderId);
        map.put("origTxnOrderTime",txnOrderTime);
 
        Map<String, String> stringStringMap = wxService.addSign(map);
        map.put("certId",stringStringMap.get("certId"));
        map.put("signAture",stringStringMap.get("signAture"));
        Gson gson =new Gson();
        String str = gson.toJson(map);
        System.out.println("请求报文:"+str);
        writeTxt2(txnOrderId+"状态查询请求: "+str);
 
        String body = HttpRequest.post(xtyhpay)
                .header(Header.CONTENT_TYPE, "application/json")
                .body(str)
                .timeout(15000)
                .execute()
                .body();
        System.out.println(txnOrderId+"状态查询响应报文:"+body);
 
        writeTxt2(txnOrderId+"状态查询响应: "+body);
        JSONObject map1 = JSON.parseObject(body);
        String respCode = (String)map1.get("respCode");
        if(respCode.equals("0000")){
            result((long)outPark.getId(),0,0);
            return R.ok(null,"查询成功");
        }
        String respMsg = (String)map1.get("respMsg");
        return R.failed(respCode+":"+respMsg);
    }
 
 
 
 
 
    //更新订单记录
    public void updateOrderRecord(Long queryId,String status){
        QueryWrapper<OrderRecord> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(OrderRecord::getQueryId,queryId)
                .orderByDesc(OrderRecord::getCreateTime);
        List<OrderRecord> all = orderRecordService.list(wrapper);
        if(all.size()>=1){
            OrderRecord orderRecord = all.get(0);
            if(status.equals("01")){
                orderRecord.setStatus(0);
            }
            if(status.equals("03")){
                orderRecord.setStatus(1);
                //测试环境,暂时把开票设置为2
                orderRecord.setTicketStatus(2);
            }
            if(status.equals("04")||status.equals("05")||status.equals("06")){
                orderRecord.setStatus(2);
            }
            orderRecordService.saveOrUpdate(orderRecord);
            if(orderRecord.getType()==0){
                updateTicket(orderRecord.getQueryId(),status);
            }else{
               updateOutPark(orderRecord.getQueryId(),status);
            }
        }
    }
 
    //更新罚单的状态
    public void updateTicket(Integer ticketId,String status){
        Ticket ticket = ticketService.getById(ticketId);
        if(ticket!=null){
            if(status.equals("01")){
                ticket.setPayStatus(0);
            }
            if(status.equals("03")){
                ticket.setPayStatus(1);
                dealCar(ticket.getCarNo());
            }
            if(status.equals("04")||status.equals("05")||status.equals("06")){
                ticket.setPayStatus(2);
            }
            ticketService.saveOrUpdate(ticket);
        }
    }
 
    //更新罚单缴费状态
    private void dealCar(String carNo){
        QueryWrapper<Ticket> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Ticket::getCarNo,carNo)
                .eq(Ticket::getPayStatus,1);
        List<Ticket> tickets = ticketService.list(wrapper);
        if(tickets.size()==0){
            //更新缴费状态
            QueryWrapper<TicketBlack> wrapper1 = new QueryWrapper<>();
            wrapper1.lambda()
                    .eq(TicketBlack::getCarNo,carNo);
            List<TicketBlack> all = ticketBlackService.list(wrapper1);
            for(TicketBlack ticketBlack:all){
                ticketBlack.setStatus(1);
                ticketBlack.setBlackType(0);
                ticketBlackService.saveOrUpdate(ticketBlack);
            }
        }
    }
 
 
    //更新出场
    public void updateOutPark(Integer outParkId, String status){
        OutPark outPark = outParkService.getById(outParkId);
        if(outPark!=null && outPark.getEnterTime() !=null){
            if(status.equals("03")){
                outPark.setStatus(1);
                outParkService.saveOrUpdate(outPark);
                //缓存在redis里
                try {
                    redisJsonUtil.set("outPark-"+outPark.getBarrierId(), outPark);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
    //判断文件是否存在
    public Boolean judeFileExists(File file){
        if(file.exists()){
            return true;
        }else {
            return false;
        }
    }
 
    //插入停车订单记录
    public void addOrderRecord(OutPark outPark){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
        String logs = sdf.format(new Date())+" 车牌号为:"+outPark.getCarNo()+",创建订单------》\n";
        writeTxt(logs);
        outPark.setPayCode(outPark.getId()+"");
        List<EnterPark> byCarNo = enterParkService.findByCarNo(outPark.getCarNo(),outPark.getParkId());
        outParkService.updateById(outPark);
        Park park = parkService.getById(outPark.getParkId());
        OrderRecord orderRecord = new OrderRecord();
        if(byCarNo.size() >0){
            EnterPark enterPark = byCarNo.get(0);
            if(enterPark != null && enterPark.getImgId() != null){
                orderRecord.setImgInId(enterPark.getImgId());
            }
        }
        orderRecord.setAddress(park.getName());
        orderRecord.setCarNo(outPark.getCarNo());
        orderRecord.setCode(outPark.getCode());
        orderRecord.setCode2(outPark.getCode2());
        orderRecord.setMoney(outPark.getPrice());
        orderRecord.setQueryId(outPark.getId());
        orderRecord.setType(1);
        orderRecord.setTicketStatus(0);
        orderRecord.setPayCode(outPark.getPayCode());
        if(outPark.getImgId() != null){
            orderRecord.setImgOutId(outPark.getImgId());
        }
        orderRecordService.saveOrUpdate(orderRecord);
    }
 
    //插入执法订单记录
    public void addOrderRecord2(Ticket ticket){
        OrderRecord orderRecord = new OrderRecord();
        orderRecord.setAddress(ticket.getAddress());
        orderRecord.setCarNo(ticket.getCarNo());
        orderRecord.setCode(ticket.getCode());
        orderRecord.setMoney(ticket.getMoney());
        orderRecord.setQueryId(ticket.getId());
        orderRecord.setType(0);
        orderRecord.setPayCode(ticket.getPayCode());
        orderRecordService.saveOrUpdate(orderRecord);
    }
 
 
 
    private void writeTxt( String txt)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try
        {
            FileWriter f = new FileWriter(systemConfigProperties.getLogPath()+"支付日志"+sdf.format(new Date())+".txt",true);
            BufferedWriter bw=new BufferedWriter(f);
            bw.write(txt);
            bw.newLine();
            bw.close();
        }
        catch(Exception e)
        {
            System.out.println("打印错误");
        }
    }
 
    private void writeTxt2( String txt)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try
        {
            FileWriter f = new FileWriter(systemConfigProperties.getLogPath()+"邢台银行支付日志"+sdf.format(new Date())+".txt",true);
            BufferedWriter bw=new BufferedWriter(f);
            bw.write(txt);
            bw.newLine();
            bw.close();
        }
        catch(Exception e)
        {
            System.out.println("打印错误");
        }
    }
 
    public static void main(String[] args) {
        Path path = Paths.get("E:\\ycCheckBill\\Bank_PSBC_20350421.txt");
        try {
            List<String>  lines = Files.readAllLines(path);
            String s = lines.get(0);
            String[] split = s.split("\\|");
 
 
            for (String line : lines) {
                System.out.println(line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}