xuhy
2025-01-09 712f70b2936079a131ecb1e63c6d337171618cad
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.account.controller.AppOrderController;
import com.stylefeng.guns.modular.account.model.TEnterpriseWithdrawal;
import com.stylefeng.guns.modular.account.service.ITEnterpriseWithdrawalService;
import com.stylefeng.guns.modular.account.service.UserWithdrawalService;
import com.stylefeng.guns.modular.account.unionpay.GetOpenBodySig;
import com.stylefeng.guns.modular.account.unionpay.QrCodeConfiguration;
import com.stylefeng.guns.modular.account.unionpay.Util;
import com.stylefeng.guns.modular.account.util.Base64Util;
import com.stylefeng.guns.modular.cloudPayment.example.AllocationExample;
import com.stylefeng.guns.modular.cloudPayment.req.AllocationReq;
import com.stylefeng.guns.modular.crossCity.model.OrderCrossCity;
import com.stylefeng.guns.modular.crossCity.server.IOrderCrossCityService;
import com.stylefeng.guns.modular.smallLogistics.model.OrderLogistics;
import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsService;
import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsSpreadService;
import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar;
import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService;
import com.stylefeng.guns.modular.system.dao.DispatchMapper;
import com.stylefeng.guns.modular.system.dao.ReassignMapper;
import com.stylefeng.guns.modular.system.dao.SensitiveWordsMapper;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.*;
import com.stylefeng.guns.modular.taxi.model.OrderTaxi;
import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService;
import com.unionpay.upyzt.resp.AllocationResp;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
 
 
@Service
@Transactional(isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ReassignServiceImpl extends ServiceImpl<ReassignMapper, Reassign> implements IReassignService {
 
    @Resource
    private ReassignMapper reassignMapper;
 
    @Resource
    private SensitiveWordsMapper sensitiveWordsMapper;
 
    @Autowired
    private IOrderService orderService;
 
    @Autowired
    private IDriverService driverService;
 
    @Autowired
    private ITransactionDetailsService transactionDetailsService;
 
    @Autowired
    private IPaymentRecordService paymentRecordService;
 
    @Autowired
    private PayMoneyUtil payMoneyUtil;
 
    @Autowired
    private IOrderTaxiService orderTaxiService;
 
    @Autowired
    private IIncomeService incomeService;
 
    @Autowired
    private ISystemNoticeService systemNoticeService;
 
    @Autowired
    private IOrderPrivateCarService orderPrivateCarService;
 
    @Autowired
    private IOrderCrossCityService orderCrossCityService;
 
    @Autowired
    private JGPushUtil jgPushUtil;
    @Autowired
    private QrCodeConfiguration qrCodeConfiguration;
    @Resource
    private DispatchMapper dispatchMapper;
 
    @Autowired
    private IOrderLogisticsService orderLogisticsService;
 
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ITEnterpriseWithdrawalService enterpriseWithdrawalService;
    @Autowired
    private UserWithdrawalService userWithdrawalService;
 
 
    @Value("${callbackPath}")
    private String callbackPath;
 
 
 
    /**
     * 提交改派申请
     * @param uid           申请司机id
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil saveData(Reassign reassign, Integer uid, Integer type) throws Exception {
        type = null == type ? 2 : type;//现目前微信只支持小程序支付
        String content = reassign.getRemark();
        if(ToolUtil.isNotEmpty(content)){
            List<SensitiveWords> sensitiveWords = sensitiveWordsMapper.selectList(null);
            for(SensitiveWords s : sensitiveWords){
                content = content.replaceAll(s.getContent(), "***");
            }
        }
        reassign.setRemark(content);
        ResultUtil resultUtil = ResultUtil.success();
        Driver driver = driverService.selectById(uid);
        // TODO: 2020/11/12 司机端不控制重复提交(如果重复提交且未支付,直接将之前的数据删除)
//        Reassign query = reassignMapper.query(uid, null, null, null, 1);
//        if(null != query){
//            return ResultUtil.error("有未完成支付的改派申请");
//        }
        Reassign query = reassignMapper.query(uid, null, reassign.getOrderId(), reassign.getOrderType(), 1);
        if(null != query){
//            return ResultUtil.error("不能重复提交申请");
            reassignMapper.deleteById(query.getId());
        }
        query = reassignMapper.query(uid, null, reassign.getOrderId(), reassign.getOrderType(), 2);
        if(null != query){
            return ResultUtil.error("您提交的申请正在处理中");
        }
 
        //获取支付金额
        Double aDouble = orderService.queryReassignMoney(reassign.getOrderId(), reassign.getOrderType());
        reassign.setMoney(aDouble);
        reassign.setOriginalDriverId(uid);
        reassign.setOriginalCarId(driver.getCarId());
        reassign.setInsertTime(new Date());
        this.insert(reassign);
 
        systemNoticeService.addSystemNotice(2, "您的改派申请已成功提交,我们会尽快为你处理!", uid, 1);
 
        //开始支付
        if(null != reassign.getPayType()){
            if(reassign.getPayType() == 1){//微信支付
                reassign.setState(1);
 
                resultUtil = payMoneyUtil.weixinpay("改派订单", "", reassign.getId() + "_" + reassign.getOrderType() + "_" + UUIDUtil.getRandomCode(5), aDouble.toString(), "/base/wxReassign", "APP", "");
                paymentRecordService.saveData(1, uid, 2, reassign.getOrderId(), reassign.getOrderType(), 1, aDouble, "", 1);//添加预支付数据
            }
            if(reassign.getPayType() == 2){//支付宝
                reassign.setState(1);
                resultUtil = payMoneyUtil.alipay("改派订单", "改派订单", "", reassign.getId() + "_" + reassign.getOrderType() + "_" + UUIDUtil.getRandomCode(5), aDouble.toString(), "/base/aliReassign");
                paymentRecordService.saveData(1, uid, 2, reassign.getOrderId(), reassign.getOrderType(), 2, aDouble, "", 1);//添加预支付数据
            }
            if(reassign.getPayType() == 3){//余额
                reassign.setState(2);
                reassign.setPayTime(new Date());
                reassignMapper.updateById(reassign);
 
                Double balance = driver.getBalance();
                Double laveBusinessMoney = driver.getLaveBusinessMoney();
                Double laveActivityMoney = driver.getLaveActivityMoney();
                if(null == balance || balance < aDouble){
                    throw new SystemException("账户余额不足");
                }
                if(null != laveBusinessMoney && laveBusinessMoney.compareTo(aDouble) >= 0){
                    driver.setLaveBusinessMoney(new BigDecimal(laveBusinessMoney).subtract(new BigDecimal(aDouble)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                }else if(null != laveActivityMoney && laveActivityMoney.compareTo(aDouble) >= 0){
                    driver.setLaveActivityMoney(new BigDecimal(laveActivityMoney).subtract(new BigDecimal(aDouble)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                }
                driver.setBalance(new BigDecimal(balance).subtract(new BigDecimal(aDouble)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                driverService.updateById(driver);
                //添加交易明细
                transactionDetailsService.saveData(uid, "司机改派支付", aDouble, 2, 1, 2, reassign.getOrderType(), reassign.getOrderId());
 
 
                //修改改派通知状态
                switch (reassign.getOrderType()){
                    case 1:
                        OrderPrivateCar orderPrivateCar = orderPrivateCarService.selectById(reassign.getOrderId());
                        orderPrivateCar.setOldState(orderPrivateCar.getState());
                        orderPrivateCar.setState(11);
                        orderPrivateCar.setReassignNotice(2);
                        orderPrivateCarService.updateById(orderPrivateCar);
 
                        //添加已收入明细
                        incomeService.saveData(1, orderPrivateCar.getCompanyId(), 4, orderPrivateCar.getId(), 1, reassign.getMoney());
                        break;
                    case 2:
                        OrderTaxi orderTaxi = orderTaxiService.selectById(reassign.getOrderId());
                        orderTaxi.setOldState(orderTaxi.getState());
                        orderTaxi.setState(11);
                        orderTaxi.setReassignNotice(2);
                        orderTaxiService.updateById(orderTaxi);
 
                        //添加已收入明细
                        incomeService.saveData(1, orderTaxi.getCompanyId(), 4, orderTaxi.getId(), 2, reassign.getMoney());
                        break;
                    case 4:
                        OrderLogistics orderLogistics = orderLogisticsService.selectById(reassign.getOrderId());
                        orderLogistics.setOldState(orderLogistics.getState());
                        orderLogistics.setState(11);
                        orderLogistics.setReassignNotice(2);
                        orderLogisticsService.updateById(orderLogistics);
 
                        //添加已收入明细
                        incomeService.saveData(1, orderLogistics.getCompanyId(), 4, orderLogistics.getId(), orderLogistics.getType(), reassign.getMoney());
                        break;
                    case 5:
                        OrderLogistics orderLogistics1 = orderLogisticsService.selectById(reassign.getOrderId());
                        orderLogistics1.setOldState(orderLogistics1.getState());
                        orderLogistics1.setState(11);
                        orderLogistics1.setReassignNotice(2);
                        orderLogisticsService.updateById(orderLogistics1);
 
                        //添加已收入明细
                        incomeService.saveData(1, orderLogistics1.getCompanyId(), 4, orderLogistics1.getId(), orderLogistics1.getType(), reassign.getMoney());
                        break;
                }
 
                systemNoticeService.addSystemNotice(2, "您已使用余额成功支付改派申请费用!", uid, 1);
                //开始推送调度单
                Map<String, String> map = new HashMap<>();
                map.put("orderId", reassign.getOrderId().toString());
                map.put("orderType", reassign.getOrderType().toString());
                List<Dispatch> dispatches = dispatchMapper.queryDispatchs(driver.getFranchiseeId() == null ? driver.getCompanyId() : driver.getFranchiseeId(), 1);
                for(Dispatch dispatch : dispatches){
                    jgPushUtil.push(2,"有新的改派订单需要处理,请及时处理!", "订单改派", map, "DISPATCH" + dispatch.getId());
                }
 
                // TODO 司机云闪付 改派余额支付,司机电子账簿转账到平台
                if(BigDecimal.ZERO.compareTo(new BigDecimal(aDouble)) < 0){
                    cloudPay(reassign.getOriginalDriverId(),aDouble,Util.getMerOrderId(qrCodeConfiguration.getMsgSrcId()));
                }
 
            }
            if(reassign.getPayType() == 4){
                // TODO 司机云闪付 改派调起云闪付
                Double payMoney = reassign.getMoney();
                paymentRecordService.saveData(1, uid, 2, reassign.getOrderId(), reassign.getOrderType(), 4, aDouble, "", 1);//添加预支付数据
                resultUtil = placeAnOrder(new BigDecimal(payMoney), 4,1,reassign.getId());
            }
        }
        return resultUtil;
    }
 
    private void cloudPay(Integer uid,Double total_fee,String transaction_id) {
        System.out.println(uid+"======"+total_fee+"====="+transaction_id);
        Driver driver = driverService.selectById(uid);
        if(Objects.isNull(driver)){
            throw new RuntimeException("该司机不存在!"+uid);
        }
        // 查询平台账户
        Company company = companyService.selectOne(new EntityWrapper<Company>()
                .eq("type", 1)
                .last("LIMIT 1"));
        System.out.println("查询平台账户==============="+company);
        // 查询平台开户信息
        TEnterpriseWithdrawal enterpriseWithdrawal = enterpriseWithdrawalService.selectOne(new EntityWrapper<TEnterpriseWithdrawal>()
                .eq("companyId", company.getId())); //  替换为公司id
        System.out.println("查询平台开户信息==============="+enterpriseWithdrawal);
        // TODO 司机进件信息
        UserWithdrawal driverWith = userWithdrawalService.selectOne(new EntityWrapper<UserWithdrawal>()
                .eq("phone", driver.getPhone())
                .last("LIMIT 1"));
        try {
            AllocationReq allocationReq = new AllocationReq();
            allocationReq.setOutOrderNo(transaction_id);
            allocationReq.setPayBalanceAcctId(driverWith.getBalanceAcctId()); // 发送方
            allocationReq.setRecvBalanceAcctId(enterpriseWithdrawal.getBalanceAcctId()); // 接收方
            BigDecimal multiply = new BigDecimal(100).multiply(new BigDecimal(total_fee));
            allocationReq.setAmount(multiply.intValue()); // 金额
            allocationReq.setPassword(Base64Util.decode(enterpriseWithdrawal.getTransactionAuthorizationCode())); // 密码
            allocationReq.setOrderNo(transaction_id);
            allocationReq.setOrderAmount(multiply.longValue());
            allocationReq.setProductName("订单改派");
            allocationReq.setProductCount(1);
            AllocationResp allocationResp = AllocationExample.create(allocationReq);
            System.err.println("司机分账信息:"+allocationResp);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
    private ResultUtil placeAnOrder(BigDecimal amount, Integer type,Integer orderType,Integer reassignId) throws Exception {
        Reassign reassign = reassignMapper.selectById(reassignId);
        String merOrderId = Util.getMerOrderId(qrCodeConfiguration.getMsgSrcId());
        reassign.setBillNo(merOrderId+"0");
        reassignMapper.updateById(reassign);
        JSONObject json = new JSONObject();
        json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));    // 报文请求时间
        json.put("merOrderId", merOrderId); // 商户订单号
        json.put("mid", qrCodeConfiguration.getMid()); // 商户号
        json.put("tid", qrCodeConfiguration.getTid());    // 终端号
        json.put("instMid", qrCodeConfiguration.getInstMid()); // 业务类型
        json.put("totalAmount", amount.multiply(new BigDecimal("100")).intValue());      // 支付总金额
        json.put("orderDesc", "APP下单"); // 订单描述
        json.put("srcReserve", reassignId+"_"+orderType+"_"+UUIDUtil.getRandomCode(5) + "-" +orderType+"-"+type); // 预留字段
        json.put("subAppId", "wxasdasdasdasdads");
        json.put("notifyUrl", "https://zhentonggongsi.com:443/driver/api/placeOrder/notifyUrl");
        json.put("tradeType", "APP");  // 交易类型,微信必传
        //  json.put("divisionFlag", true); // 分账标记
        //  json.put("platformAmount", 0); // 平台商户分账金额
//        ArrayList<JSONObject> subOrders = new ArrayList<>();
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("mid", "988460101800201");
//        jsonObject.put("totalAmount", 1);
//        subOrders.add(jsonObject);
        //    json.put("subOrders", subOrders);
 
        System.err.println("请求报文json:\n" + json);
        String url;
        if(1 == type){
//            url = "https://test-api-open.chinaums.com/v1/netpay/wx/app-pre-order";// 测试
            url = "https://api-mop.chinaums.com/v1/netpay/wx/app-pre-order";
        }else if (2 == type){
//            url = "https://test-api-open.chinaums.com/v1/netpay/trade/precreate";// 测试
            url = "https://api-mop.chinaums.com/v1/netpay/trade/precreate";
        }else {
//            url = "https://test-api-open.chinaums.com/v1/netpay/uac/app-order";// 测试
            url = "https://api-mop.chinaums.com/v1/netpay/uac/app-order";
        }
        //OPEN-BODY-SIG 方式
        String result = GetOpenBodySig.sendOpenBodySig(qrCodeConfiguration.getAppId(),qrCodeConfiguration.getAppKey(),url, json.toString());
        System.err.println("响应报文json:\n" + result);
        JSONObject jsonObject = JSONObject.parseObject(result);
        Object appPayRequest = jsonObject.get("appPayRequest");
        System.out.println(appPayRequest);
        return ResultUtil.success(appPayRequest);
    }
 
    /**
     * 跨城订单改派
     * @param amount
     * @param type
     * @param orderType
     * @param reassignId
     * @return
     * @throws Exception
     */
    private ResultUtil placeAnOrder1(BigDecimal amount, Integer type,Integer orderType,Integer reassignId) throws Exception {
        Reassign reassign = reassignMapper.selectById(reassignId);
        String merOrderId = Util.getMerOrderId(qrCodeConfiguration.getMsgSrcId());
        reassign.setBillNo(merOrderId+"0");
        reassignMapper.updateById(reassign);
        JSONObject json = new JSONObject();
        json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));    // 报文请求时间
        json.put("merOrderId", merOrderId); // 商户订单号
        json.put("mid", qrCodeConfiguration.getMid()); // 商户号
        json.put("tid", qrCodeConfiguration.getTid());    // 终端号
        json.put("instMid", qrCodeConfiguration.getInstMid()); // 业务类型
        json.put("totalAmount", amount.multiply(new BigDecimal("100")).intValue());      // 支付总金额
        json.put("orderDesc", "APP下单"); // 订单描述
        json.put("srcReserve", reassignId+"_"+orderType+"_"+UUIDUtil.getRandomCode(5) + "-" +1+"-"+type); // 预留字段
        json.put("subAppId", "wxasdasdasdasdads");
        json.put("notifyUrl", "https://zhentonggongsi.com:443/driver/api/placeOrder/notifyUrl");
        json.put("tradeType", "APP");  // 交易类型,微信必传
        //  json.put("divisionFlag", true); // 分账标记
        //  json.put("platformAmount", 0); // 平台商户分账金额
//        ArrayList<JSONObject> subOrders = new ArrayList<>();
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("mid", "988460101800201");
//        jsonObject.put("totalAmount", 1);
//        subOrders.add(jsonObject);
        //    json.put("subOrders", subOrders);
 
        System.err.println("请求报文json:\n" + json);
        String url;
        if(1 == type){
//            url = "https://test-api-open.chinaums.com/v1/netpay/wx/app-pre-order";// 测试
            url = "https://api-mop.chinaums.com/v1/netpay/wx/app-pre-order";
        }else if (2 == type){
//            url = "https://test-api-open.chinaums.com/v1/netpay/trade/precreate";// 测试
            url = "https://api-mop.chinaums.com/v1/netpay/trade/precreate";
        }else {
//            url = "https://test-api-open.chinaums.com/v1/netpay/uac/app-order";// 测试
            url = "https://api-mop.chinaums.com/v1/netpay/uac/app-order";
        }
        //OPEN-BODY-SIG 方式
        String result = GetOpenBodySig.sendOpenBodySig(qrCodeConfiguration.getAppId(),qrCodeConfiguration.getAppKey(),url, json.toString());
        System.err.println("响应报文json:\n" + result);
        JSONObject jsonObject = JSONObject.parseObject(result);
        Object appPayRequest = jsonObject.get("appPayRequest");
        System.out.println(appPayRequest);
        return ResultUtil.success(appPayRequest);
    }
 
 
    /**
     * 提交改派申请(跨城专用)
     * @param uid           申请司机id
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil saveData_(Reassign reassign, String orderIds, Integer uid, Integer type) throws Exception {
        type = null == type ? 2 : type;//现目前微信只支持小程序支付
        String content = reassign.getRemark();
        if(ToolUtil.isNotEmpty(content)){
            List<SensitiveWords> sensitiveWords = sensitiveWordsMapper.selectList(null);
            for(SensitiveWords s : sensitiveWords){
                content = content.replaceAll(s.getContent(), "***");
            }
        }
        reassign.setRemark(content);
        ResultUtil resultUtil = ResultUtil.success();
        Driver driver = driverService.selectById(uid);
        // TODO: 2020/11/12 司机端不控制重复提交(如果重复提交且未支付,直接将之前的数据删除)
//        Reassign query = reassignMapper.query(uid, null, null, null, 1);
//        if(null != query){
//            return ResultUtil.error("有未完成支付的改派申请");
//        }
        String[] split = orderIds.split(",");//获取支付金额
        Double aDouble = orderService.queryReassignMoney(Integer.valueOf(split[0]), 3);
        Double totalMoney = aDouble * split.length;
        String ids = "";
        for(String id : split){
            Reassign query = reassignMapper.query(uid, null, Integer.valueOf(id), 3, 1);
            if(null != query){
//                return ResultUtil.error("不能重复提交申请");
                reassignMapper.deleteById(query.getId());
            }
            query = reassignMapper.query(uid, null, Integer.valueOf(id), 3, 2);
            if(null != query){
                return ResultUtil.error("您提交的申请正在处理中");
            }
 
            reassign.setMoney(aDouble);
            reassign.setOrderId(Integer.valueOf(id));
            reassign.setOrderType(3);
            reassign.setOriginalDriverId(uid);
            reassign.setOriginalCarId(driver.getCarId());
            reassign.setInsertTime(new Date());
            reassign.setState(1);
            this.insert(reassign);
            ids += reassign.getId() + ",";
        }
        ids = ids.substring(0, ids.length() - 1);
        systemNoticeService.addSystemNotice(2, "您的改派申请已成功提交,我们会尽快为你处理!", uid, 1);
 
        //开始支付
        if(null != reassign.getPayType()){
            if(reassign.getPayType() == 1){//微信支付
                String[] split1 = ids.split(",");
 
                resultUtil = payMoneyUtil.weixinpay("改派订单", "", ids + "_3_" + UUIDUtil.getRandomCode(5), totalMoney.toString(), "/base/wxReassign", "APP", "");
                for(String id : split1){
                    paymentRecordService.saveData(1, uid, 2, Integer.valueOf(id), 3, 1, aDouble, "", 1);//添加预支付数据
                }
            }
            if(reassign.getPayType() == 2){//支付宝
                String[] split1 = ids.split(",");
                resultUtil = payMoneyUtil.alipay("改派订单", "改派订单", "", ids + "_3_" + UUIDUtil.getRandomCode(5), totalMoney.toString(), "/base/aliReassign");
                for(String id : split1){
                    paymentRecordService.saveData(1, uid, 2, Integer.valueOf(id), 3, 2, aDouble, "", 1);//添加预支付数据
                }
            }
            if(reassign.getPayType() == 3){//余额
                for(String id : split){
                    Reassign query = reassignMapper.query(uid, null, Integer.valueOf(id), 3, 1);
                    query.setState(2);
                    query.setPayTime(new Date());
                    reassignMapper.updateById(query);
                    //添加交易明细
                    transactionDetailsService.saveData(uid, "司机改派支付", aDouble, 2, 1, 2, 3, Integer.valueOf(id));
                }
                Double balance = driver.getBalance();
                Double laveBusinessMoney = driver.getLaveBusinessMoney();
                Double laveActivityMoney = driver.getLaveActivityMoney();
                if(null == balance || balance < totalMoney){
                    throw new SystemException("账户余额不足");
                }
                if(null != laveBusinessMoney && laveBusinessMoney.compareTo(totalMoney) >= 0){
                    driver.setLaveBusinessMoney(new BigDecimal(laveBusinessMoney).subtract(new BigDecimal(totalMoney)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                }else if(null != laveActivityMoney && laveActivityMoney.compareTo(totalMoney) >= 0){
                    driver.setLaveActivityMoney(new BigDecimal(laveActivityMoney).subtract(new BigDecimal(totalMoney)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                }
                driver.setBalance(new BigDecimal(balance).subtract(new BigDecimal(totalMoney)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
                driverService.updateById(driver);
 
 
                //修改改派通知状态
                for(String id : split){
                    OrderCrossCity orderCrossCity = orderCrossCityService.selectById(id);
                    orderCrossCity.setOldState(orderCrossCity.getState());
                    orderCrossCity.setState(11);
                    orderCrossCity.setReassignNotice(2);
                    orderCrossCityService.updateById(orderCrossCity);
 
                    //添加已收入明细
                    incomeService.saveData(1, orderCrossCity.getCompanyId(), 4, orderCrossCity.getId(), 3, reassign.getMoney());
                }
                systemNoticeService.addSystemNotice(2, "您已使用余额成功支付改派申请费用!", uid, 1);
                //开始推送调度单
                Map<String, String> map = new HashMap<>();
                map.put("orderId", reassign.getOrderId().toString());
                map.put("orderType", reassign.getOrderType().toString());
                List<Dispatch> dispatches = dispatchMapper.queryDispatchs(driver.getFranchiseeId() == null ? driver.getCompanyId() : driver.getFranchiseeId(), 1);
                for(Dispatch dispatch : dispatches){
                    jgPushUtil.push(2,"有新的改派订单需要处理,请及时处理!", "订单改派", map, "DISPATCH" + dispatch.getId());
                }
            }
            if(reassign.getPayType() == 4){
 
                String[] split1 = ids.split(",");
                resultUtil = placeAnOrder1(new BigDecimal(aDouble), 4,3,Integer.valueOf(split1[0]));
                for(String id : split1){
                    // TODO 司机云闪付 跨城改派调起云闪付
                    paymentRecordService.saveData(1, uid, 2, Integer.valueOf(id), 3, 4, aDouble, "", 1);//添加预支付数据
                }
            }
        }
        return resultUtil;
    }
 
 
 
 
    /**
     * 获取列表数据
     * @param uid
     * @param pageNum
     * @param size
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryList(Integer uid, Integer pageNum, Integer size) throws Exception {
        pageNum = (pageNum - 1) * size;
        return reassignMapper.queryList(uid, pageNum, size);
    }
 
 
    /**
     * 改派支付完成后的操作
     * @param id            改派单id
     * @param order_id      工行支付订单
     * @param type          1=微信,2=支付宝,4=云闪付
     * @throws Exception
     */
    @Override
    public void payReassign(String id, String order_id, Integer type) throws Exception {
        System.out.println("改派支付完成后的操作");
        String[] s = id.split("_");
        if(Integer.valueOf(s[1]) == 3){//跨城订单
            System.out.println("跨城订单");
            String[] split = s[0].split(",");
            List<Reassign> reassigns = this.selectBatchIds(Arrays.asList(split));
            Driver driver = driverService.selectById(reassigns.get(0).getOriginalDriverId());
            for(String key : split){
                PaymentRecord query = paymentRecordService.query(1, driver.getId(), 2, Integer.valueOf(key), 3, type, 1);
                if(null != query){
                    System.out.println("跨城订单开始");
                    //添加交易明细
                    transactionDetailsService.saveData(reassigns.get(0).getOriginalDriverId(), "司机改派支付", reassigns.get(0).getMoney(), 2, 1, 2, 3, Integer.valueOf(key));
                    System.out.println("==============添加交易明细完毕===================");
                    for(Reassign reassign : reassigns){
                        reassign.setState(2);
                        reassign.setPayOrder(order_id);
                        reassign.setPayTime(new Date());
                        this.updateById(reassign);
                        //开始推送调度单
                        Map<String, String> map = new HashMap<>();
                        map.put("orderId", reassign.getOrderId().toString());
                        map.put("orderType", reassign.getOrderType().toString());
                        List<Dispatch> dispatches = dispatchMapper.queryDispatchs(driver.getFranchiseeId() == null ? driver.getCompanyId() : driver.getFranchiseeId(), 1);
                        for(Dispatch dispatch : dispatches){
                            jgPushUtil.push(2,"有新的改派订单需要处理,请及时处理!", "订单改派", map, "DISPATCH" + dispatch.getId());
                        }
                    }
                    query.setState(2);
                    query.setCode(order_id);
                    paymentRecordService.updateById(query);
                    System.out.println("==============paymentRecord完毕==================="+query);
                    //修改改派通知状态
                    for(Reassign reassign : reassigns){
                        OrderCrossCity orderCrossCity1 = orderCrossCityService.selectById(reassign.getOrderId());
                        orderCrossCity1.setOldState(orderCrossCity1.getState());
                        orderCrossCity1.setState(11);
                        orderCrossCity1.setReassignNotice(2);
                        orderCrossCityService.updateById(orderCrossCity1);
                        System.out.println("==============跨城修改改派通知状态完毕==================="+orderCrossCity1);
 
                        //添加已收入明细
                        incomeService.saveData(1, orderCrossCity1.getCompanyId(), 4, orderCrossCity1.getId(), 3, reassign.getMoney());
                    }
                    systemNoticeService.addSystemNotice(2, "您已使用" + (type == 1 ? "微信" : (type == 2?"支付宝":"云闪付")) + "成功支付改派申请费用!", reassigns.get(0).getOriginalDriverId(), 1);
                    System.out.println("==============跨城修改改派通知状态完毕===================");
                }else{
                    System.err.println("预支付数据异常(orderId = "  + id + ")");
                }
            }
 
 
        }else{//其他订单
            System.out.println("其他订单");
            Reassign reassign = this.selectById(s[0]);
            Driver driver = driverService.selectById(reassign.getOriginalDriverId());
            PaymentRecord query = paymentRecordService.query(1, driver.getId(), 2, reassign.getOrderId(), reassign.getOrderType(), type, 1);
            System.out.println("==============paymentRecord查询==================="+query);
            if(null != query){
                //添加交易明细
                transactionDetailsService.saveData(reassign.getOriginalDriverId(), "司机改派支付", reassign.getMoney(), 2, 1, 2, reassign.getOrderType(), reassign.getOrderId());
                reassign.setState(2);
                reassign.setPayOrder(order_id);
                reassign.setPayTime(new Date());
                this.updateById(reassign);
 
                query.setState(2);
                query.setCode(order_id);
                paymentRecordService.updateById(query);
                System.out.println("==============paymentRecord完毕==================="+query);
 
                //修改改派通知状态
                switch (reassign.getOrderType()){
                    case 1:
                        OrderPrivateCar orderPrivateCar = orderPrivateCarService.selectById(reassign.getOrderId());
                        orderPrivateCar.setOldState(orderPrivateCar.getState());
                        orderPrivateCar.setState(11);
                        orderPrivateCar.setReassignNotice(2);
                        orderPrivateCarService.updateById(orderPrivateCar);
 
                        //添加已收入明细
                        incomeService.saveData(1, orderPrivateCar.getCompanyId(), 4, orderPrivateCar.getId(), 1, reassign.getMoney());
                        System.out.println("==============快车完毕==================="+orderPrivateCar);
                        break;
                    case 2:
                        OrderTaxi orderTaxi = orderTaxiService.selectById(reassign.getOrderId());
                        orderTaxi.setOldState(orderTaxi.getState());
                        orderTaxi.setState(11);
                        orderTaxi.setReassignNotice(2);
                        orderTaxiService.updateById(orderTaxi);
 
                        //添加已收入明细
                        incomeService.saveData(1, orderTaxi.getCompanyId(), 4, orderTaxi.getId(), 2, reassign.getMoney());
                        System.out.println("==============出租车完毕==================="+orderTaxi);
                        break;
                    case 4:
                        OrderLogistics orderLogistics = orderLogisticsService.selectById(reassign.getOrderId());
                        orderLogistics.setOldState(orderLogistics.getState());
                        orderLogistics.setState(11);
                        orderLogistics.setReassignNotice(2);
                        orderLogisticsService.updateById(orderLogistics);
 
                        //添加已收入明细
                        incomeService.saveData(1, orderLogistics.getCompanyId(), 4, orderLogistics.getId(), 4, reassign.getMoney());
                        System.out.println("==============物流订单完毕1==================="+orderLogistics);
                        break;
                    case 5:
                        OrderLogistics orderLogistics1 = orderLogisticsService.selectById(reassign.getOrderId());
                        orderLogistics1.setOldState(orderLogistics1.getState());
                        orderLogistics1.setState(11);
                        orderLogistics1.setReassignNotice(2);
                        orderLogisticsService.updateById(orderLogistics1);
 
                        //添加已收入明细
                        incomeService.saveData(1, orderLogistics1.getCompanyId(), 4, orderLogistics1.getId(), 5, reassign.getMoney());
                        System.out.println("==============物流订单完毕2==================="+orderLogistics1);
                        break;
                }
                systemNoticeService.addSystemNotice(2, "您已使用" + (type == 1 ? "微信" :(type == 2?"支付宝":"云闪付") ) + "成功支付改派申请费用!", reassign.getOriginalDriverId(), 1);
                //开始推送调度单
                Map<String, String> map = new HashMap<>();
                map.put("orderId", reassign.getOrderId().toString());
                map.put("orderType", reassign.getOrderType().toString());
                List<Dispatch> dispatches = dispatchMapper.queryDispatchs(driver.getFranchiseeId() == null ? driver.getCompanyId() : driver.getFranchiseeId(), 1);
                for(Dispatch dispatch : dispatches){
                    jgPushUtil.push(2,"有新的改派订单需要处理,请及时处理!", "订单改派", map, "DISPATCH" + dispatch.getId());
                }
                System.out.println("==============其他订单完毕===================");
            }else{
                System.err.println("预支付数据异常(orderId = "  + id + ")");
            }
        }
 
 
    }
}