kongdeqiang
2022-09-19 a9862e81851bbe037edc6bb1c7f562c1e55c0d7f
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
package com.boying.controller.phone;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.boying.common.BaseController;
import com.boying.common.SystemConfigProperties;
import com.boying.common.util.HttpUtil;
import com.boying.entity.*;
import com.boying.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
 
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static com.boying.common.util.MyX509TrustManager.httpsRequest;
 
@RestController
@RequestMapping("ffPay")
public class FFPayController extends BaseController {
 
    @Autowired
    private TicketService ticketService;
    @Autowired
    private SystemConfigProperties systemConfigProperties;
    @Autowired
    private OutParkService outParkService;
    @Autowired
    private OrderRecordService orderRecordService;
    @Autowired
    private ParkService parkService;
    @Autowired
    private BarrierService barrierService;
    @Autowired
    private EnterParkService enterParkService;
    @Autowired
    private TicketBlackService ticketBlackService;
    @Autowired
    private WhiteListService whiteListService;
 
    static String payKey = "df937eda27cb4a59a12347966678e232";
    static String paySecret = "9e3b2f387b5b4232a9c782affc9c36ed";
    static String payerTypeCode = "1304062100000001";
    static String payerTypeCode2 = "1304062100000002";
    static String ip = "https://pay.hebcz.cn";
    static String ip2 = "http://nontax.hebcz.cn";
 
 
    //生成电子缴款码
    @PostMapping("zhiFa")
    public Object zhiFa(Long id){
        Ticket ticket = (Ticket) ticketService.findById(id);
        if(ticket.getPayStatus()==1){
            return error("此罚单已支付");
        }
        if(ticket.getStatus()==3){
            return error("此罚单已撤销");
        }
 
//        StringBuffer sbf = new StringBuffer();
////        StringBuffer s = new StringBuffer();
////        if(ticket.getMoney()==0){
////            s.append("amt=0.01");
////        }else{
////            DecimalFormat df = new DecimalFormat("#.00");
////            s.append("amt="+df.format(ticket.getMoney()));
////        }
////        s.append("&notifyUrl="+systemConfigProperties.getIp()+"ffPay/result");//回调的url
////        s.append("&payKey="+payKey);//财政给的
////        s.append("&payerName="+ticket.getCarNo());
////        s.append("&payerNum="+ ticket.getCode());//订单号
////        s.append("&payerTypeCode="+payerTypeCode);////财政给的
////        s.append("&reserve1=");
////        s.append("&reserve2=");
////        s.append("&reserve3=");
////        s.append("&returnUrl=");
////        sbf.append(s.toString());
////        s.append("&paySecret="+paySecret);////财政给的
////
////        String encode = DigestUtils.md5DigestAsHex(s.toString().getBytes());
////        sbf.append("&sign="+encode);
////        sbf.append("&signType=MD5");
////        System.out.println(sbf.toString());
////        String s1 = httpsRequest(ip+"/pay/inpay","POST",sbf.toString());
        StringBuffer sbf = new StringBuffer();
        StringBuffer s = new StringBuffer();
        if(ticket.getMoney()==0){
            s.append("amt=0.01");
        }else{
            DecimalFormat df = new DecimalFormat("#0.00");
            s.append("amt="+df.format(ticket.getMoney()).toString());
        }
        s.append("&notifyUrl="+systemConfigProperties.getIp()+"ffPay/result");
        s.append("&payKey=df937eda27cb4a59a12347966678e232");
        s.append("&payerName="+ticket.getCarNo());
        s.append("&payerNum="+ticket.getCode());
        s.append("&payerTypeCode=1304062100000001");
        s.append("&reserve1=");
        s.append("&reserve2=");
        s.append("&reserve3=");
        s.append("&returnUrl=");
        sbf.append(s.toString());
        s.append("&paySecret=9e3b2f387b5b4232a9c782affc9c36ed");
 
        String encode = null;
        try {
            encode = DigestUtils.md5DigestAsHex(s.toString().getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        sbf.append("&sign="+encode);
        sbf.append("&signType=MD5");
        System.out.println(sbf.toString());
        String s1 = httpsRequest(ip + "/pay/inpay", "POST", sbf.toString());
        System.out.println(s1);
        if(s1.equals("error")){
            return error("生成电子缴款码失败,请联系管理员");
        }
        JSONObject jsonObject = JSON.parseObject(s1);
        String payCode = "";
        if(jsonObject.get("payCode")==null){
            String eInfo = jsonObject.get("errorInfo").toString();
            if(eInfo.equals("此订单已过期")){
                ticket.setCode(System.currentTimeMillis()+"");
                String payCode2 = zhiFa2(ticket);
                ticket.setPayCode(payCode2);
                ticketService.save(ticket);
                addOrderRecord2(ticket);
                return success("生成执法电子缴款码","https://hbfs.govpay.ccb.com/online/fssm?fee=130000000001&PyfScnCgycd=01&pyfTpcd=3&BNo="+payCode);
            }
            return error(jsonObject.get("errorInfo").toString());
        }
        payCode = jsonObject.get("payCode").toString();
        ticket.setPayCode(payCode);
        ticketService.save(ticket);
 
        addOrderRecord2(ticket);
        return success("生成执法电子缴款码","https://hbfs.govpay.ccb.com/online/fssm?fee=130000000001&PyfScnCgycd=01&pyfTpcd=3&BNo="+payCode);
    }
 
    public String zhiFa2(Ticket ticket){
        StringBuffer sbf = new StringBuffer();
        StringBuffer s = new StringBuffer();
        if(ticket.getMoney()==0){
            s.append("amt=0.01");
        }else{
            DecimalFormat df = new DecimalFormat("#0.00");
            s.append("amt="+df.format(ticket.getMoney()).toString());
        }
        s.append("&notifyUrl="+systemConfigProperties.getIp()+"ffPay/result");
        s.append("&payKey=df937eda27cb4a59a12347966678e232");
        s.append("&payerName="+ticket.getCarNo());
        s.append("&payerNum="+ticket.getCode());
        s.append("&payerTypeCode=1304062100000001");
        s.append("&reserve1=");
        s.append("&reserve2=");
        s.append("&reserve3=");
        s.append("&returnUrl=");
        sbf.append(s.toString());
        s.append("&paySecret=9e3b2f387b5b4232a9c782affc9c36ed");
 
        String encode = null;
        try {
            encode = DigestUtils.md5DigestAsHex(s.toString().getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        sbf.append("&sign="+encode);
        sbf.append("&signType=MD5");
        String s1 = httpsRequest(ip + "/pay/inpay", "POST", sbf.toString());
        JSONObject jsonObject = JSON.parseObject(s1);
        String payCode = jsonObject.get("payCode").toString();
        return payCode;
    }
 
 
    //支付接口通知
    @PostMapping("result")
    public String result(HttpServletRequest request,String payKey,String payerTypeCode,String payerNum,String payCode,Double amt,String status,String errorInfo,String sign,String signType) throws Exception{
        System.out.println(payCode+"-------"+status);
        String s = "payCode===="+payCode+",\n";
        try {
            s += "开始走回调接口------>";
            writeTxt(s);
            updateOrderRecord(payCode,status);
        }catch (Exception e){
            s += "更新订单记录出异常------>";
            writeTxt(s);
        }
        return "success";
    }
 
    //更新订单记录
    public void updateOrderRecord(String payCode,String status){
        String s = "更新订单记录:payCode===="+payCode+",status==="+status+",\n";
        Specification<OrderRecord> specification = new Specification<OrderRecord>() {
            @Override
            public Predicate toPredicate(Root<OrderRecord> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                list.add(cb.equal(root.get("payCode").as(String.class), payCode));
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        };
        List<OrderRecord> all = orderRecordService.findAll(specification);
        s += "查询到订单记录为:"+all.size()+"条,\n";
        if(all.size()==1){
            OrderRecord orderRecord = all.get(0);
            s += "查询到订单为:"+orderRecord.getId()+",\n";
            if(status.equals("01")){
                orderRecord.setStatus(0);
            }
            if(status.equals("03")){
                orderRecord.setStatus(1);
            }
            if(status.equals("04")||status.equals("05")||status.equals("06")){
                orderRecord.setStatus(2);
            }
            orderRecordService.save(orderRecord);
            if(orderRecord.getType()==0){
                updateTicket(orderRecord.getQueryId(),status);
            }else{
                s += "修改出场表:"+orderRecord.getQueryId()+",status==="+status+",\n";
                writeTxt(s);
                updateOutPark(orderRecord.getQueryId(),status);
            }
        }
        writeTxt(s);
    }
 
    //更新罚单的状态
    public void updateTicket(Long ticketId,String status){
        Ticket ticket = (Ticket) ticketService.findById(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.save(ticket);
        }
    }
 
    //更新缴费状态
    private void dealCar(String carNo){
        Specification<Ticket> specification2 = new Specification<Ticket>() {
            @Override
            public Predicate toPredicate(Root<Ticket> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                list.add(cb.equal(root.get("carNo").as(String.class), carNo));
                list.add(cb.notEqual(root.get("payStatus"), 1));
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        };
        List<Ticket> tickets = ticketService.findAll(specification2);
        if(tickets.size()==0){
            //更新缴费状态
            Specification<TicketBlack> specification = new Specification<TicketBlack>() {
                @Override
                public Predicate toPredicate(Root<TicketBlack> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                    List<Predicate> list = new ArrayList<Predicate>();
                    list.add(cb.equal(root.get("carNo").as(String.class), carNo));
                    Predicate[] arr = new Predicate[list.size()];
                    cq.where(list.toArray(arr));
                    return null;
                }
            };
            List<TicketBlack> all = ticketBlackService.findAll(specification);
            for(TicketBlack ticketBlack:all){
                ticketBlack.setStatus(1);
                ticketBlack.setBlackType(0);
                ticketBlackService.save(ticketBlack);
            }
        }
    }
    //抬杆
//    public void updateOutPark(Long outParkId,String status){
//        OutPark outPark = (OutPark) outParkService.findById(outParkId);
//        if(outPark!=null){
//            if(status.equals("03")){
//                outPark.setStatus(1);
//                outParkService.save(outPark);
//                Barrier b= (Barrier) barrierService.findById(outPark.getBarrierId());
//                //调用抬杆接口 返回值:{"result":"true","msg":"操作完成"}
//                String res = HttpUtil.get(systemConfigProperties.getIp3()+"?barrierCode="+b.getCode());
//                JSONObject jsonObject = JSON.parseObject(res);
//                if(jsonObject.get("result").equals("true")){
//                    System.out.println("抬杆成功");
//
//                    deleteEnterPark(outPark.getCarNo());
//                }else{
//                    System.out.println("抬杆接口返回失败");
//                }
//            }
//        }
//    }
 
    public void updateOutPark(Long outParkId,String status){
        String s = "开始修改出场表-----》,\n";
        s+= "outparkId==="+outParkId+",status==="+status+",\n";
        writeTxt(s);
        OutPark outPark = (OutPark) outParkService.findById(outParkId);
        if(outPark!=null){
            s += "找到出场数据:"+outPark.getId()+",\n";
            writeTxt(s);
            if(status.equals("03")){
                s += "出场数据修改----》";
                writeTxt(s);
                outPark.setStatus(1);
                outParkService.save(outPark);
            }
        }
        s += "未找到出场数据-----》";
        writeTxt(s);
    }
 
    //删除进停车场的数据
    public void deleteEnterPark(String carNo){
        Specification<EnterPark> specification = new Specification<EnterPark>() {
            @Override
            public Predicate toPredicate(Root<EnterPark> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                list.add(cb.equal(root.get("carNo").as(String.class), carNo));
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        };
        List<EnterPark> all = enterParkService.findAll(specification);
        for(EnterPark ep:all){
            enterParkService.delete(ep.getId());
        }
    }
 
    //生成电子缴款码
    @PostMapping("park")
    public Object park(Long id){
        String logs = "开始缴费流程------》";
        logs += "出场id为:"+id+",\n";
        OutPark outPark = (OutPark) outParkService.findById(id);
        if(outPark.getPrice()==0){
            updateOutPark(outPark.getId(),"03");
            logs += "支付金额为0,\n";
            writeTxt(logs);
            return error("本次停车无需支付费用");
        }
 
        //判断是否在白名单
        Specification<WhiteList> specification = new Specification<WhiteList>() {
            @Override
            public Predicate toPredicate(Root<WhiteList> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                list.add(cb.equal(root.get("carNo").as(String.class), outPark.getCarNo()));
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        };
        List<WhiteList> all = whiteListService.findAll(specification);
        for(WhiteList w : all){
            if(w.getType()==0){
                logs+=outPark.getCarNo()+"在白名单,\n";
                updateOutPark(outPark.getId(),"03");
                writeTxt(logs);
                return error("本次停车无需支付费用!");
            }else{
                if(w.getParkId()!=null){
                    if(w.getEndTime()!=null){
                        if(w.getParkId()==outPark.getParkId()&&new Date().getTime()<w.getEndTime().getTime()){
                            logs+=outPark.getCarNo()+"在特殊规则白名单,\n";
                            updateOutPark(outPark.getId(),"03");
                            writeTxt(logs);
                            return error("本次停车无需支付费用!");
                        }
                    }
                }
            }
        }
 
        StringBuffer sbf = new StringBuffer();
        StringBuffer s = new StringBuffer();
        DecimalFormat df = new DecimalFormat("#0.00");
        s.append("amt="+df.format(outPark.getPrice()).toString());
        s.append("&notifyUrl="+systemConfigProperties.getIp()+"ffPay/result");//回调的url
        s.append("&payKey=df937eda27cb4a59a12347966678e232");//财政给的
        s.append("&payerName="+outPark.getCarNo());
        s.append("&payerNum="+ outPark.getCode());//订单号
        s.append("&payerTypeCode=1304062100000001");////财政给的
        s.append("&reserve1=");
        s.append("&reserve2=");
        s.append("&reserve3=");
        s.append("&returnUrl=");
        sbf.append(s.toString());
        s.append("&paySecret=9e3b2f387b5b4232a9c782affc9c36ed");////财政给的
 
        String encode = null;
        try {
            encode = DigestUtils.md5DigestAsHex(s.toString().getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        sbf.append("&sign="+encode);
        sbf.append("&signType=MD5");
        String s1 = httpsRequest(ip+"/pay/inpay","POST",sbf.toString());
        if(s1.equals("error")){
            logs += "生成执法电子缴款码失败,请联系管理员,\n";
            writeTxt(logs);
            return error("生成执法电子缴款码失败,请联系管理员");
        }
        JSONObject jsonObject = JSON.parseObject(s1);
        if(jsonObject.get("payCode")==null){
            String eInfo = jsonObject.get("errorInfo").toString();
            if(eInfo.equals("此订单已过期")){
                outPark.setCode(System.currentTimeMillis()+"");
                outParkService.save(outPark);
                logs += "此订单已过期,\n";
                writeTxt(logs);
                return park(id);
            }else{
                logs += "此订单有异常错误,\n";
                writeTxt(logs);
                return error(eInfo);
            }
        }else{
            String payCode = jsonObject.get("payCode").toString();
            logs += "payCode: "+payCode+",\n";
            outPark.setPayCode(payCode);
            outParkService.save(outPark);
            writeTxt(logs);
            addOrderRecord(outPark);
            return success("生成停车电子缴款码","https://hbfs.govpay.ccb.com/online/fssm?fee=130000000001&PyfScnCgycd=01&pyfTpcd=3&BNo="+payCode);
        }
    }
 
 
    //插入停车订单记录
    public void addOrderRecord(OutPark outPark){
        Park park = (Park) parkService.findById(outPark.getParkId());
        OrderRecord orderRecord = new OrderRecord();
        orderRecord.setAddress(park.getName());
        orderRecord.setCarNo(outPark.getCarNo());
        orderRecord.setCode(outPark.getCode());
        orderRecord.setMoney(outPark.getPrice());
        orderRecord.setQueryId(outPark.getId());
        orderRecord.setType(1);
        orderRecord.setPayCode(outPark.getPayCode());
        orderRecordService.save(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.save(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("打印错误");
        }
    }
 
 
 
 
}