luodangjia
2024-08-27 375134f8fead27cc32a880c45cc68e72b69c5ce9
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
package com.ruoyi.order.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.api.feignClient.AppUserCarClient;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.feignClient.AppUserVipDetailClient;
import com.ruoyi.account.api.model.TAppUser;
import com.ruoyi.account.api.model.TAppUserCar;
import com.ruoyi.account.api.model.TAppUserVipDetail;
import com.ruoyi.account.api.vo.GetAppUserVipDetail;
import com.ruoyi.chargingPile.api.feignClient.ChargingGunClient;
import com.ruoyi.chargingPile.api.feignClient.ChargingPileClient;
import com.ruoyi.chargingPile.api.feignClient.SiteClient;
import com.ruoyi.chargingPile.api.model.Site;
import com.ruoyi.chargingPile.api.model.TChargingGun;
import com.ruoyi.chargingPile.api.model.TChargingPile;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.BasePage;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.api.model.TChargingOrder;
import com.ruoyi.order.api.model.TChargingOrderAccountingStrategy;
import com.ruoyi.order.api.model.TChargingOrderRefund;
import com.ruoyi.order.api.query.ChargingOrderQuery;
import com.ruoyi.order.api.vo.ChargingOrderVO;
import com.ruoyi.order.api.vo.TCharingOrderVO;
import com.ruoyi.order.dto.*;
import com.ruoyi.order.mapper.TChargingOrderMapper;
import com.ruoyi.order.service.TChargingOrderAccountingStrategyService;
import com.ruoyi.order.service.TChargingOrderService;
import com.ruoyi.payment.api.feignClient.AliPaymentClient;
import com.ruoyi.payment.api.feignClient.WxPaymentClient;
import com.ruoyi.payment.api.vo.*;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-07
 */
@Service
public class TChargingOrderServiceImpl extends ServiceImpl<TChargingOrderMapper, TChargingOrder> implements TChargingOrderService {
    
    @Resource
    private ChargingGunClient chargingGunClient;
    
    @Resource
    private SiteClient siteClient;
    
    @Resource
    private AppUserCarClient appUserCarClient;
    @Resource
    private AppUserClient appUserClient;
    
    @Resource
    private TChargingOrderAccountingStrategyService chargingOrderAccountingStrategyService;
    
    @Resource
    private TokenService tokenService;
    
    @Resource
    private WxPaymentClient wxPaymentClient;
    
    @Resource
    private AliPaymentClient aliPaymentClient;
    
    @Resource
    private ChargingPileClient chargingPileClient;
    
 
    @Resource
    private AppUserVipDetailClient appUserVipDetailClient;
    
    @Resource
    private RedisService redisService;
 
    /**
     * 获取小程序充电记录列表数据
     * @param query
     * @return
     */
    @Override
    public Map<String, Object> getMyChargingOrderList(GetMyChargingOrderList query) {
        Long appUserId = tokenService.getLoginUserApplet().getUserId();
        Map<String, Object> map = new HashMap<>();
        int size = this.baseMapper.getMyChargingOrderList(appUserId, 1, null, null).size();
        map.put("number", size);
        List<TChargingOrder> list = this.baseMapper.getMyChargingOrderList(appUserId, query.getType(), query.getPageCurr(), query.getPageSize());
        List<MyChargingOrderList> orderLists = new ArrayList<>();
        for (TChargingOrder tChargingOrder : list) {
            MyChargingOrderList myChargingOrderList = new MyChargingOrderList();
            myChargingOrderList.setId(tChargingOrder.getId().toString());
            myChargingOrderList.setStatus(tChargingOrder.getStatus());
            Site site = siteClient.getSiteByIds(Arrays.asList(tChargingOrder.getSiteId())).getData().get(0);
            myChargingOrderList.setTitle(site.getName());
            myChargingOrderList.setChargingDegree(tChargingOrder.getChargingCapacity());
            String name = chargingGunClient.getAllName(tChargingOrder.getChargingGunId()).getData();
            myChargingOrderList.setChargingGunId(tChargingOrder.getChargingGunId());
            myChargingOrderList.setName(name);
            myChargingOrderList.setEndMode(tChargingOrder.getEndMode());
            BigDecimal payMoney = tChargingOrder.getStatus() < 4 ? tChargingOrder.getRechargeAmount() : tChargingOrder.getPaymentAmount();
            myChargingOrderList.setPayMoney(payMoney);
            myChargingOrderList.setCreateTime(tChargingOrder.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            orderLists.add(myChargingOrderList);
        }
        map.put("list", orderLists);
        return map;
    }
    
    
    /**
     * 获取充电订单明细
     * @param id 订单id
     * @return
     */
    @Override
    public MyChargingOrderInfo getMyChargingOrderInfo(String id) {
        TChargingOrder chargingOrder = this.getById(id);
        MyChargingOrderInfo myChargingOrderInfo = new MyChargingOrderInfo();
        myChargingOrderInfo.setId(id);
        myChargingOrderInfo.setCode(chargingOrder.getCode());
        myChargingOrderInfo.setCreateTime(chargingOrder.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        myChargingOrderInfo.setStatus(chargingOrder.getStatus());
        Site site = siteClient.getSiteByIds(Arrays.asList(chargingOrder.getSiteId())).getData().get(0);
        myChargingOrderInfo.setTitle(site.getName());
        TChargingGun chargingGun = chargingGunClient.getChargingGunById(chargingOrder.getChargingGunId()).getData();
        myChargingOrderInfo.setChargeMode(chargingGun.getChargeMode());
        String name = chargingGunClient.getAllName(chargingOrder.getChargingGunId()).getData();
        myChargingOrderInfo.setName(name);
        if(null != chargingOrder.getAppUserCarId()){
            TAppUserCar tAppUserCar = appUserCarClient.getCarByIds(Arrays.asList(chargingOrder.getAppUserCarId())).getData().get(0);
            myChargingOrderInfo.setLicensePlate(tAppUserCar.getLicensePlate());
        }else{
            myChargingOrderInfo.setLicensePlate("无");
        }
        myChargingOrderInfo.setStartTime(chargingOrder.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        myChargingOrderInfo.setEndTime(chargingOrder.getEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        myChargingOrderInfo.setOrderAmount(chargingOrder.getOrderAmount());
        myChargingOrderInfo.setPaymentAmount(chargingOrder.getPaymentAmount());
        myChargingOrderInfo.setRechargeAmount(chargingOrder.getRechargeAmount());
        myChargingOrderInfo.setCouponDiscountAmount(chargingOrder.getCouponDiscountAmount());
        myChargingOrderInfo.setVipDiscountAmount(chargingOrder.getVipDiscountAmount());
        myChargingOrderInfo.setRefundAmount(chargingOrder.getRefundAmount());
        List<TChargingOrderAccountingStrategy> stageCost = chargingOrderAccountingStrategyService.list(
                new LambdaQueryWrapper<TChargingOrderAccountingStrategy>().eq(TChargingOrderAccountingStrategy::getChargingOrderId, id)
                        .orderByAsc(TChargingOrderAccountingStrategy::getStartTime)
        );
        myChargingOrderInfo.setStageCost(stageCost);
        // todo 待完善
        //再MongoDB中获取数据
        myChargingOrderInfo.setCurrentAndVoltage(null);
        //再MongoDB中获取数据
        myChargingOrderInfo.setPower(null);
        myChargingOrderInfo.setActionable(myChargingOrderInfo.getEndTime() + 604800000L > System.currentTimeMillis() ? 0 : 1);
        return myChargingOrderInfo;
    }
    
    
    /**
     * 获取待开票订单列表
     * @param query
     * @return
     */
    @Override
    public List<MyChargingOrderList> getNoInvoicedOrder(GetNoInvoicedOrder query) {
        Long appUserId = tokenService.getLoginUserApplet().getUserId();
        List<TChargingOrder> orderList = this.baseMapper.getNoInvoicedOrder(appUserId, query.getMonth(), query.getPageCurr(), query.getPageSize());
        List<MyChargingOrderList> list = new ArrayList<>();
        for (TChargingOrder tChargingOrder : orderList) {
            MyChargingOrderList myChargingOrderList = new MyChargingOrderList();
            myChargingOrderList.setId(tChargingOrder.getId().toString());
            myChargingOrderList.setStatus(tChargingOrder.getStatus());
            Site site = siteClient.getSiteByIds(Arrays.asList(tChargingOrder.getSiteId())).getData().get(0);
            myChargingOrderList.setTitle(site.getName());
            myChargingOrderList.setChargingDegree(tChargingOrder.getChargingCapacity());
            String name = chargingGunClient.getAllName(tChargingOrder.getChargingGunId()).getData();
            myChargingOrderList.setName(name);
            myChargingOrderList.setEndMode(tChargingOrder.getEndMode());
            BigDecimal payMoney = tChargingOrder.getStatus() < 4 ? tChargingOrder.getRechargeAmount() : tChargingOrder.getPaymentAmount();
            myChargingOrderList.setPayMoney(payMoney);
            myChargingOrderList.setCreateTime(tChargingOrder.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            list.add(myChargingOrderList);
        }
        return list;
    }
    
    
    /**
     * 充电支付
     * @param addChargingOrder
     * @return
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public AjaxResult paymentChargingOrder(AddChargingOrder addChargingOrder) {
        Long userId = tokenService.getLoginUserApplet().getUserId();
        TAppUser appUser = appUserClient.getUserById(userId).getData();
        //查询待支付状态的订单,没有支付则删除订单
        List<TChargingOrder> list = this.list(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getAppUserId, userId)
                .eq(TChargingOrder::getRechargePaymentStatus, 1).eq(TChargingOrder::getDelFlag, 0));
        for (TChargingOrder tChargingOrder : list) {
            Integer rechargePaymentType = tChargingOrder.getRechargePaymentType();
            if(1 == rechargePaymentType){
                NotifyV3PayDecodeRespBody data = wxPaymentClient.queryOrderInfo(tChargingOrder.getCode()).getData();
                if(null != data){
                    String trade_state = data.getTrade_state();
                    //支付失败,删除无效的订单
                    if(trade_state.equals("REFUND") || trade_state.equals("NOTPAY") || trade_state.equals("REVOKED") || trade_state.equals("PAYERROR")){
                        this.removeById(tChargingOrder.getId());
                    }
                }
            }
            if(2 == rechargePaymentType){
                AliQueryOrder data = aliPaymentClient.query(tChargingOrder.getCode()).getData();
                if(null != data){
                    //支付失败,删除无效的订单
                    String tradeStatus = data.getTradeStatus();
                    if(tradeStatus.equals("TRADE_CLOSED")){
                        this.removeById(tChargingOrder.getId());
                    }
                }
            }
        }
        
        //检查当前枪是否是正在使用中
        TChargingOrder one = this.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getChargingGunId, addChargingOrder.getId())
                .in(TChargingOrder::getStatus, Arrays.asList(1, 2, 3)).eq(TChargingOrder::getDelFlag, 0));
        if(null != one){
            return AjaxResult.error("充电枪正在充电桩中,启动失败");
        }
        //查询当前充电枪是否有正在支付的订单
        one = this.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getChargingGunId, addChargingOrder.getId())
                .eq(TChargingOrder::getAppUserCarId, userId).eq(TChargingOrder::getRechargePaymentStatus, 1).eq(TChargingOrder::getDelFlag, 0));
        if(null != one){
            //查询三方支付数据,支付中直接结束三方订单
            if(1 == one.getRechargePaymentType()){
                NotifyV3PayDecodeRespBody data = wxPaymentClient.queryOrderInfo(one.getCode()).getData();
                if(null != data){
                    String trade_state = data.getTrade_state();
                    //支付失败,删除无效的订单
                    if(trade_state.equals("REFUND") || trade_state.equals("NOTPAY") || trade_state.equals("REVOKED") || trade_state.equals("PAYERROR")){
                        this.removeById(one.getId());
                    }else{
                        //结束第三方支付,删除订单
                        wxPaymentClient.close(one.getCode());
                        this.removeById(one.getId());
                    }
                }
            }
            if(2 == one.getRechargePaymentType()){
                AliQueryOrder data = aliPaymentClient.query(one.getCode()).getData();
                if(null != data){
                    String trade_state = data.getTradeStatus();
                    //支付失败,删除无效的订单
                    if(trade_state.equals("TRADE_CLOSED")){
                        this.removeById(one.getId());
                    }
                    if(trade_state.equals("WAIT_BUYER_PAY")){
                        //结束第三方支付,删除订单
                        aliPaymentClient.close(one.getCode());
                        this.removeById(one.getId());
                    }
                }
            }
        }
        
        
        //构建新的待支付订单
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        TChargingOrder chargingOrder = new TChargingOrder();
        String code = "CD" + Math.random() * 1000 + sdf.format(new Date());
        chargingOrder.setCode(code);
        chargingOrder.setOrderType(1);
        chargingOrder.setOrderClassification(1);
        chargingOrder.setAppUserId(userId);
        chargingOrder.setAppUserCarId(addChargingOrder.getAppUserCarId());
        TChargingGun tChargingGun = chargingGunClient.getChargingGunById(addChargingOrder.getId()).getData();
        chargingOrder.setSiteId(tChargingGun.getSiteId());
        chargingOrder.setChargingPileId(tChargingGun.getChargingPileId());
        chargingOrder.setChargingGunId(addChargingOrder.getId());
        chargingOrder.setRechargePaymentType(addChargingOrder.getPaymentType());
        chargingOrder.setRechargePaymentStatus(1);
        chargingOrder.setRechargeAmount(addChargingOrder.getPaymentAmount());
        chargingOrder.setAppCouponId(addChargingOrder.getAppUserCouponId());
        if(null != appUser.getVipId()){
            GetAppUserVipDetail getAppUserVipDetail = new GetAppUserVipDetail();
            getAppUserVipDetail.setAppUserId(appUser.getId());
            getAppUserVipDetail.setVipId(appUser.getVipId());
            TAppUserVipDetail vipDetail = appUserVipDetailClient.getAppUserVipDetail(getAppUserVipDetail).getData();
            if(null != vipDetail){
                Integer chargeNum = vipDetail.getChargeNum();
                if(0 > chargeNum){
                    chargingOrder.setVipDiscount(vipDetail.getDiscount());
                    BigDecimal discountAmount = addChargingOrder.getPaymentAmount().multiply((new BigDecimal(10)
                            .subtract(vipDetail.getDiscount())).divide(new BigDecimal(10))).setScale(4, BigDecimal.ROUND_HALF_EVEN);
                    chargingOrder.setVipDiscountAmount(discountAmount);
                }
            }
            
        }
        this.save(chargingOrder);
        //会员优惠折扣将其计入增加充电时长(增加总充电金额)
        //如果充电总金额未使用完,则需要退回费用=(原金额/总金额)*(总金额-实际充电金额)
        if(1 == addChargingOrder.getPaymentType()){
            PaymentOrder paymentOrder = new PaymentOrder();
            paymentOrder.setCode(chargingOrder.getCode());
            paymentOrder.setAmount(addChargingOrder.getPaymentAmount());
            paymentOrder.setOpenId(appUser.getWxOpenid());
            paymentOrder.setDescription("充电充值");
            paymentOrder.setNotifyUrl("/order/t-charging-order/chargingOrderWXCallback");
            Map<String, Object> data = wxPaymentClient.orderPay(paymentOrder).getData();
            return AjaxResult.success(data);
        }
        if(2 == addChargingOrder.getPaymentType()){
            AliPaymentReq req = new AliPaymentReq();
            req.setOutTradeNo(chargingOrder.getCode());
            req.setTotalAmount(chargingOrder.getPaymentAmount().toString());
            req.setSubject("充电充值");
            req.setBuyerOpenId(appUser.getAliOpenid());
            req.setBody("充电充值");
            AliPaymentResp data = aliPaymentClient.payment(req).getData();
            if(null != data){
                data.setNotifyUrl(data.getNotifyUrl() + "/t-charging-order/chargingOrderALICallback");
                return AjaxResult.success(data);
            }
        }
        throw new RuntimeException("无效的支付方式");
    }
    
    
    /**
     * 支付成功后的回调处理逻辑
     * @param paymentType 支付方式:1=微信,2=支付宝
     * @param out_trade_no 业务流水号
     * @param transaction_id 三方支付流水号
     * @param attach 附加数据
     * @return
     */
    @Override
    public AjaxResult chargingOrderCallback(Integer paymentType, String out_trade_no, String transaction_id, String attach) {
        TChargingOrder chargingOrder = this.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getCode, out_trade_no));
        if(chargingOrder.getRechargePaymentStatus() == 2){
            return AjaxResult.success();
        }
        chargingOrder.setRechargePaymentStatus(2);
        chargingOrder.setRechargeSerialNumber(transaction_id);
        this.updateById(chargingOrder);
        //添加安全检测数据到缓存中,每步安全检测完成后需要更新缓存数据
        PreChargeCheck preChargeCheck = new PreChargeCheck();
        preChargeCheck.setElectronicLockLock(false);
        preChargeCheck.setInsulationTesting(false);
        preChargeCheck.setSecureConnectionDetection(false);
        String key = "AQJC_" + chargingOrder.getChargingGunId();
        redisService.setCacheObject(key, preChargeCheck);
        //开始检测充电桩状,将检查状态写入缓存。检测完成后开始开启充电
        //充电结束后需要计算退款剩余金额
        // todo 需要完善
        
        return AjaxResult.success();
    }
    
    
    /**
     * 获取充电中的详情
     * @param id
     * @return
     */
    @Override
    public ChargingDetails getChargingDetails(Integer id) {
        Long userId = tokenService.getLoginUserApplet().getUserId();
        TChargingOrder one = this.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getAppUserId, userId).eq(TChargingOrder::getChargingGunId, id)
                .in(TChargingOrder::getStatus, Arrays.asList(1, 2, 3)).eq(TChargingOrder::getRechargePaymentStatus, 2).eq(TChargingOrder::getDelFlag, 0));
        if(null == one){
            return null;
        }
        // todo 需要完善充电实时数据
        ChargingDetails chargingDetails = new ChargingDetails();
        chargingDetails.setId(one.getId().toString());
        TChargingGun chargingGun = chargingGunClient.getChargingGunById(id).getData();
        TChargingPile chargingPile = chargingPileClient.getChargingPileById(chargingGun.getChargingPileId()).getData();
        Site site = siteClient.getSiteByIds(Arrays.asList(chargingPile.getSiteId())).getData().get(0);
        chargingDetails.setName(site.getName() + "-" + chargingPile.getName());
        chargingDetails.setCode(one.getCode());
        chargingDetails.setStatus(one.getStatus());
        return chargingDetails;
    }
    
    
    /**
     * 停止充电操作
     * @param id 订单id
     * @return
     */
    @Override
    public AjaxResult stopCharging(String id) {
        TChargingOrder chargingOrder = this.getById(id);
        Integer status = chargingOrder.getStatus();
        if(status == 4 || status == 5){
            return AjaxResult.error("不能重复操作");
        }
        chargingOrder.setStatus(4);
        chargingOrder.setEndMode(1);
        this.updateById(chargingOrder);
        //调用硬件停止充电,停止成功后开始计算费用退款
        // todo 待完善
        return AjaxResult.success();
    }
 
 
    @Override
    public TCharingOrderVO chargingOrder(ChargingOrderQuery dto) {
        TCharingOrderVO tCharingOrderVO = new TCharingOrderVO();
 
 
        String startTime1 = null;
        String startTime2 = null;
        String endTime1 = null;
        String endTime2 = null;
 
        if (StringUtils.hasLength(dto.getStartTime())){
            String[] split = dto.getStartTime().split(" - ");
            startTime1 = split[0];
            startTime2 = split[1];
        }
        if (StringUtils.hasLength(dto.getEndTime())){
            String[] split = dto.getEndTime().split(" - ");
            endTime1 = split[0];
            endTime2 = split[1];
        }
        PageInfo<ChargingOrderVO> pageInfo = new PageInfo<>(dto.getPageCurr(),dto.getPageSize());
 
        if (StringUtils.hasLength(dto.getPhone())){
            List<Long> data = appUserClient.getUserIdsByPhone(dto.getPhone()).getData();
            if (data.isEmpty()){
                tCharingOrderVO.setList(pageInfo);
                return tCharingOrderVO;
            }
            dto.setUserIds(data);
        }
        List<ChargingOrderVO> list = this.baseMapper.chargingOrder(pageInfo,dto,startTime1,startTime2,endTime1,endTime2);
         BigDecimal total = new BigDecimal("0");
         long time = 0L;
         BigDecimal income = new BigDecimal("0");
         BigDecimal electronicMoney = new BigDecimal("0");
         BigDecimal serviceMoney = new BigDecimal("0");
        for (ChargingOrderVO chargingOrderVO : list) {
            total = total.add(chargingOrderVO.getChargingCapacity());
            // 充电订单 明细记录
            List<TChargingOrderAccountingStrategy> chargingOrderId = chargingOrderAccountingStrategyService.list(new QueryWrapper<TChargingOrderAccountingStrategy>()
                    .eq("charging_order_id", chargingOrderVO.getId()));
            // 单个订单累计电费
            BigDecimal electronicMoney1 = new BigDecimal("0");
            // 单个订单累计服务费
            BigDecimal serviceMoney1 = new BigDecimal("0");
            LocalDateTime startTime = chargingOrderVO.getStartTime();
            LocalDateTime endTime = chargingOrderVO.getEndTime();
            // 计算时间差 秒 充电时长
            long between = ChronoUnit.SECONDS.between(startTime, endTime);
            chargingOrderVO.setChargingSecond(between);
            time += between;
            // 总收入
            if (chargingOrderVO.getRefundStatus() == 2){
                income = income.add(chargingOrderVO.getPaymentAmount().subtract(chargingOrderVO.getRefundAmount()));
            }else{
                income = income.add(chargingOrderVO.getPaymentAmount());
            }
            for (TChargingOrderAccountingStrategy tChargingOrderAccountingStrategy : chargingOrderId) {
                // 累计电费
                electronicMoney1 = electronicMoney1.add(tChargingOrderAccountingStrategy.getElectrovalence()
                        .multiply(tChargingOrderAccountingStrategy.getChargingCapacity()));
                electronicMoney = electronicMoney.add(electronicMoney1);
                // 累计服务费
                serviceMoney1 = serviceMoney1.add(tChargingOrderAccountingStrategy.getServiceCharge());
                serviceMoney = serviceMoney.add(serviceMoney1);
            }
            chargingOrderVO.setServiceCharge(serviceMoney1);
            chargingOrderVO.setElectrovalence(electronicMoney1);
            // 充电时段数
            int size = chargingOrderId.size();
            chargingOrderVO.setCount(size);
            // 用户手机号
            TAppUser data = appUserClient.getUserById(chargingOrderVO.getAppUserId()).getData();
            if (data!=null){
                // 车牌号
                chargingOrderVO.setPhone(data.getPhone());
                List<Long> longs = new ArrayList<>();
                longs.add(chargingOrderVO.getAppUserCarId());
                List<TAppUserCar> data1 = appUserCarClient.getCarByIds(longs).getData();
                if (!data1.isEmpty()){
                    chargingOrderVO.setLicensePlate(data1.get(0).getLicensePlate());
                }
            }
        }
        tCharingOrderVO.setTotal(total);
        tCharingOrderVO.setTime(time);
        tCharingOrderVO.setIncome(income);
        tCharingOrderVO.setElectronicMoney(electronicMoney);
        tCharingOrderVO.setServiceMoney(serviceMoney);
        pageInfo.setRecords(list);
        tCharingOrderVO.setList(pageInfo);
        return tCharingOrderVO;
    }
 
    @Override
    public R<PageInfo<PayOrderDto>> payOrderQuery(PayOrderQueryDto payOrderQueryDto) {
        PageInfo<PayOrderDto> pageInfo = new PageInfo<>(payOrderQueryDto.getPageCurr(),payOrderQueryDto.getPageSize());
        List<PayOrderDto> list = this.baseMapper.payOrderQuery(pageInfo,payOrderQueryDto);
        pageInfo.setRecords(list);
        return R.ok(pageInfo);
    }
 
    @Override
    public R<PageInfo<TChargingOrderRefund>> getRefundList(ChargingRefundDto chargingRefundDto) {
        PageInfo<TChargingOrderRefund> pageInfo = new PageInfo<>(chargingRefundDto.getPageCurr(),chargingRefundDto.getPageSize());
        List<TChargingOrderRefund> list = this.baseMapper.getRefundList(pageInfo,chargingRefundDto);
        pageInfo.setRecords(list);
 
        return R.ok(pageInfo);
    }
}