puzhibing
2024-08-31 02382a97294be4bff8b7139ea3ebb663fa48fa7c
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
package com.ruoyi.order.controller;
import com.google.common.collect.Lists;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.account.api.feignClient.AppUserCarClient;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.model.TAppUserCar;
import com.ruoyi.chargingPile.api.feignClient.ChargingGunClient;
import com.ruoyi.chargingPile.api.feignClient.ChargingPileClient;
import com.ruoyi.chargingPile.api.feignClient.ParkingLotClient;
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.chargingPile.api.model.TParkingRecord;
import com.ruoyi.chargingPile.api.vo.SiteVO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.service.TokenService;
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.order.api.model.*;
import com.ruoyi.order.api.query.ChargingOrderQuery;
import com.ruoyi.order.api.query.TChargingCountQuery;
import com.ruoyi.order.api.vo.ChargingOrderInfoVO;
import com.ruoyi.order.api.vo.ChargingOrderVO;
import com.ruoyi.order.api.vo.TCharingOrderVO;
import com.ruoyi.order.dto.GetMyChargingOrderList;
import com.ruoyi.order.dto.GetNoInvoicedOrder;
import com.ruoyi.order.dto.MyChargingOrderInfo;
import com.ruoyi.order.dto.MyChargingOrderList;
import com.ruoyi.order.dto.OrderEvaluateVo;
import com.ruoyi.order.dto.*;
import com.ruoyi.order.service.*;
import com.ruoyi.payment.api.feignClient.AliPaymentClient;
import com.ruoyi.payment.api.feignClient.WxPaymentClient;
import com.ruoyi.payment.api.vo.AliQueryOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.*;
import java.util.List;
 
/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-07
 */
@Api(tags = "充电订单")
@RestController
@RequestMapping("/t-charging-order")
public class TChargingOrderController {
 
    @Resource
    private TChargingOrderService chargingOrderService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private TOrderEvaluateService orderEvaluateService;
 
    @Resource
    private WxPaymentClient wxPaymentClient;
 
    @Resource
    private RedisService redisService;
 
    @Resource
    private AliPaymentClient aliPaymentClient;
    @Resource
    private TShoppingOrderService shoppingOrderService;
 
    @Autowired
    private AppUserClient appUserClient;
 
    @Resource
    private TVipOrderService vipOrderService;
    @Resource
    private ParkingLotClient parkingLotClient;
    @Resource
    private TChargingOrderRefundService chargingOrderRefundService;
 
    @Resource
    private TShoppingOrderRefundService shoppingOrderRefundService;
    @Resource
    private TVipOrderRefundService vipOrderRefundService;
    @Resource
    private SiteClient siteClient;
    @Resource
    private ChargingPileClient chargingPileClient;
    @Resource
    private ChargingGunClient chargingGunClient;
    @Resource
    private AppUserCarClient appUserCarClient;
    @Resource
    private TChargingOrderAccountingStrategyService chargingOrderAccountingStrategyService;
 
    @ResponseBody
    @PostMapping(value = "/pay/order/list")
    @ApiOperation(value = "列表", tags = {"管理后台-支付订单-订单信息"})
    public R<PageInfo<PayOrderDto>> payOrderList(@RequestBody PayOrderQueryDto payOrderQueryDto) {
        return chargingOrderService.payOrderQuery(payOrderQueryDto);
    }
 
    @ResponseBody
    @GetMapping(value = "/pay/order/pay/detail")
    @ApiOperation(value = "支付信息", tags = {"管理后台-支付订单-订单信息"})
    public R<PayOrderInfoDto> payOrderList(Long orderId, Integer type) {
        switch (type) {
            case 1:
                TChargingOrder byId = chargingOrderService.getById(orderId);
                PayOrderInfoDto payOrderInfoDto = new PayOrderInfoDto();
                payOrderInfoDto.setOrderId(byId.getId().toString());
                payOrderInfoDto.setCode(byId.getCode());
                payOrderInfoDto.setTradeNo(byId.getRechargeSerialNumber());
                payOrderInfoDto.setPayType(byId.getRechargePaymentType());
                payOrderInfoDto.setPayAmount(byId.getPaymentAmount());
                payOrderInfoDto.setPayTime(byId.getPayTime());
                payOrderInfoDto.setRefundAmount(byId.getRefundAmount());
                return R.ok(payOrderInfoDto);
            case 2:
                TShoppingOrder byId1 = shoppingOrderService.getById(orderId);
                PayOrderInfoDto payOrderInfoDto1 = new PayOrderInfoDto();
                payOrderInfoDto1.setOrderId(byId1.getId().toString());
                payOrderInfoDto1.setCode(byId1.getCode());
                payOrderInfoDto1.setTradeNo(byId1.getSerialNumber());
                payOrderInfoDto1.setPayType(byId1.getPaymentType());
                payOrderInfoDto1.setPayAmount(byId1.getPaymentAmount());
                payOrderInfoDto1.setPayTime(byId1.getPayTime());
                payOrderInfoDto1.setRefundAmount(byId1.getRefundAmount());
                return R.ok(payOrderInfoDto1);
            case 3:
                TVipOrder byId2 = vipOrderService.getById(orderId);
                PayOrderInfoDto payOrderInfoDto2 = new PayOrderInfoDto();
                payOrderInfoDto2.setOrderId(byId2.getId().toString());
                payOrderInfoDto2.setCode(byId2.getCode());
                payOrderInfoDto2.setTradeNo(byId2.getSerialNumber());
                payOrderInfoDto2.setPayType(byId2.getPaymentType());
                payOrderInfoDto2.setPayAmount(byId2.getPaymentAmount());
                payOrderInfoDto2.setPayTime(byId2.getPayTime());
                payOrderInfoDto2.setRefundAmount(byId2.getRefundAmount());
                return R.ok(payOrderInfoDto2);
            //todo luo 停车场订单
//                case 4:
//                    TParkingRecord byId3 = parkingLotClient.getRecordById(orderId).getData();
//                    PayOrderInfoDto payOrderInfoDto3 = new PayOrderInfoDto();
//                    payOrderInfoDto3.setOrderId(byId3.getId().toString());
//                    payOrderInfoDto3.setCode(byId3.getCode());
//                    payOrderInfoDto3.setTradeNo(byId3);
//                    payOrderInfoDto3.setPayType(0);
//                    payOrderInfoDto3.setPayAmount(new BigDecimal("0"));
//                    payOrderInfoDto3.setPayTime(LocalDateTime.now());
//                    payOrderInfoDto3.setRefundAmount(new BigDecimal("0"));
 
 
        }
        return R.ok();
 
    }
 
    @ResponseBody
    @GetMapping(value = "/pay/order/refund/detail")
    @ApiOperation(value = "退款信息", tags = {"管理后台-支付订单-订单信息"})
    public R refundDetail(Long orderId, Integer type) {
        switch (type) {
            case 1:
                List<TChargingOrderRefund> byId = chargingOrderRefundService.lambdaQuery().eq(TChargingOrderRefund::getChargingOrderId, orderId).orderByDesc(TChargingOrderRefund::getRefundTime).list();
                return R.ok(byId);
            case 2:
                List<TShoppingOrderRefund> list = shoppingOrderRefundService.lambdaQuery().eq(TShoppingOrderRefund::getShoppingOrderId, orderId).orderByDesc(TShoppingOrderRefund::getRefundTime).list();
                return R.ok(list);
            case 3:
                List<TVipOrderRefund> list1 = vipOrderRefundService.lambdaQuery().eq(TVipOrderRefund::getVipOrderId, orderId).orderByDesc(TVipOrderRefund::getRefundTime).list();
                return R.ok(list1);
 
        }
        return R.ok();
 
    }
 
    @ResponseBody
    @GetMapping(value = "/pay/order/charging")
    @ApiOperation(value = "充电信息", tags = {"管理后台-支付订单-订单信息"})
    public R refundDetail(Long orderId) {
        PayOrderChargingInfo payOrderChargingInfo = new PayOrderChargingInfo();
        TChargingOrder byId = chargingOrderService.getById(orderId);
        List<Site> data = siteClient.getSiteByIds(Collections.singletonList(byId.getSiteId())).getData();
        payOrderChargingInfo.setSiteName(data.get(0).getName());
        TChargingPile data1 = chargingPileClient.getChargingPileById(byId.getChargingPileId()).getData();
        payOrderChargingInfo.setChargingName(data1.getName());
        TChargingGun data2 = chargingGunClient.getChargingGunById(byId.getChargingGunId()).getData();
        payOrderChargingInfo.setGunName(data2.getName());
        List<TAppUserCar> data3 = appUserCarClient.getCarByIds(Collections.singletonList(byId.getAppUserCarId())).getData();
        payOrderChargingInfo.setCarNum(data3.get(0).getLicensePlate());
        return R.ok();
    }
 
    @ResponseBody
    @GetMapping(value = "/pay/order/charging/details")
    @ApiOperation(value = "充电明细", tags = {"管理后台-支付订单-订单信息"})
    public R<List<TChargingOrderAccountingStrategy>> chargingDetail(Long orderId) {
        List<TChargingOrderAccountingStrategy> list = chargingOrderAccountingStrategyService.lambdaQuery().eq(TChargingOrderAccountingStrategy::getChargingOrderId, orderId).orderByDesc(TChargingOrderAccountingStrategy::getStartTime).list();
        return R.ok(list);
    }
 
 
    @ResponseBody
    @PostMapping(value = "/pay/order/refund/list")
    @ApiOperation(value = "充电信息", tags = {"管理后台-支付订单-退款订单"})
    public R<PageInfo<TChargingOrderRefund>> refundList(@RequestBody ChargingRefundDto chargingRefundDto) {
        return chargingOrderService.getRefundList(chargingRefundDto);
 
    }
 
 
    @ResponseBody
    @PostMapping(value = "/chargingOrder")
    @ApiOperation(value = "充电桩订单列表", tags = {"管理后台-订单管理"})
    public AjaxResult<TCharingOrderVO> chargingOrder(@RequestBody ChargingOrderQuery dto) {
        List<Long> data = appUserClient.getUserIdsByPhone(dto.getPhone()).getData();
        dto.setUserIds(data);
        TCharingOrderVO res = chargingOrderService.chargingOrder(dto);
        return AjaxResult.success(res);
    }
 
    @ResponseBody
    @PostMapping(value = "/chargingOrderInfo")
    @ApiOperation(value = "充电桩订单列表查看详情", tags = {"管理后台-订单管理"})
    public AjaxResult<ChargingOrderInfoVO> chargingOrderInfo(String orderId) {
        ChargingOrderInfoVO chargingOrderInfoVO = new ChargingOrderInfoVO();
        chargingOrderInfoVO.setCdElectronic("");
        chargingOrderInfoVO.setCdVoltage("");
        chargingOrderInfoVO.setSurplus("");
        chargingOrderInfoVO.setTotalPower("");
        chargingOrderInfoVO.setLicensePlate("");
        chargingOrderInfoVO.setVehicleBrand("");
        chargingOrderInfoVO.setVehicleModel("");
        chargingOrderInfoVO.setVehicleUse("");
        List<TChargingOrderAccountingStrategy> tChargingOrderAccountingStrategies = new ArrayList<>();
        chargingOrderInfoVO.setList(tChargingOrderAccountingStrategies);
        return AjaxResult.success(chargingOrderInfoVO);
    }
 
    @ResponseBody
    @PostMapping(value = "/addEvaluate")
    @ApiOperation(value = "添加评价", tags = {"小程序-扫一扫"})
    public AjaxResult getMyChargingOrderList(@RequestBody OrderEvaluateVo dto) {
        dto.setAppUserId(tokenService.getLoginUserApplet().getUserId());
        orderEvaluateService.addOrderEvaluate(dto);
        return AjaxResult.success();
    }
 
    /**
     * 查询用户最近一次充电记录使用的车辆
     *
     * @param
     * @return
     */
    @GetMapping(value = "/getCar")
    public R<Long> getCar() {
        List<TChargingOrder> list = chargingOrderService.list(new LambdaQueryWrapper<TChargingOrder>()
                .eq(TChargingOrder::getAppUserId, tokenService.getLoginUserApplet().getUserId())
                .isNotNull(TChargingOrder::getAppUserCarId));
        if (!list.isEmpty()) {
            // 最近使用的车辆id
            Long size = list.get(0).getAppUserCarId();
            return R.ok(size);
        } else {
            return R.ok(-1L);
        }
    }
 
    /**
     * 查询会员在本月有多少次享受了充电折扣
     *
     * @param req
     * @return
     */
    @PostMapping(value = "/getChargingCount")
    public R<Integer> getChargingCount(@RequestBody TChargingCountQuery req) {
        int size = chargingOrderService.list(new LambdaQueryWrapper<TChargingOrder>()
                .eq(TChargingOrder::getAppUserId, req.getUserId())
                .eq(TChargingOrder::getRechargePaymentStatus, 2)
                .between(TChargingOrder::getStartTime, req.getStartTime(), req.getEndTime())).size();
        return R.ok(size);
    }
 
    //用户订单数量
    @PostMapping(value = "/useOrderCount")
    public R<Long> useOrderCount(@RequestParam("userId") Long userId) {
        Long count = chargingOrderService.lambdaQuery().eq(TChargingOrder::getAppUserId, userId).count();
 
        return R.ok(count);
    }
 
    //订单详情
    @PostMapping(value = "/detail")
    public R<TChargingOrder> detail(@RequestParam("orderId") Long orderId) {
        return R.ok(chargingOrderService.getById(orderId));
    }
 
    /**
     * 根据充电枪id获取正在进行中的订单
     *
     * @param chargingGunId 充电枪id
     * @return
     */
    @PostMapping(value = "/getOrderDetailByGunId")
    public R<TChargingOrder> getOrderDetailByGunId(@RequestParam("chargingGunId") Integer chargingGunId) {
        TChargingOrder one = chargingOrderService.getOne(new LambdaQueryWrapper<TChargingOrder>().eq(TChargingOrder::getChargingGunId, chargingGunId)
                .eq(TChargingOrder::getDelFlag, 0).eq(TChargingOrder::getStatus, 3));
        return R.ok(one);
    }
 
 
    @ResponseBody
    @GetMapping(value = "/getMyChargingOrderList")
    @ApiOperation(value = "获取充电记录列表", tags = {"小程序-充电记录"})
    public AjaxResult<Map<String, Object>> getMyChargingOrderList(GetMyChargingOrderList query) {
        Map<String, Object> orderList = chargingOrderService.getMyChargingOrderList(query);
        return AjaxResult.success(orderList);
    }
 
 
    @ResponseBody
    @GetMapping(value = "/getMyChargingOrderInfo")
    @ApiOperation(value = "获取充电记订单明细", tags = {"小程序-充电记录"})
    public AjaxResult<MyChargingOrderInfo> getMyChargingOrderInfo(String id) {
        MyChargingOrderInfo myChargingOrderInfo = chargingOrderService.getMyChargingOrderInfo(id);
        return AjaxResult.success(myChargingOrderInfo);
    }
 
 
    @ResponseBody
    @GetMapping(value = "/getNoInvoicedOrder")
    @ApiOperation(value = "获取未开票的订单数据", tags = {"小程序-充电发票"})
    public AjaxResult<List<MyChargingOrderList>> getNoInvoicedOrder(GetNoInvoicedOrder query) {
        List<MyChargingOrderList> list = chargingOrderService.getNoInvoicedOrder(query);
        return AjaxResult.success(list);
    }
 
 
    @ResponseBody
    @PostMapping(value = "/paymentChargingOrder")
    @ApiOperation(value = "支付充电充值费用", tags = {"小程序-扫一扫"})
    public AjaxResult paymentChargingOrder(@RequestBody AddChargingOrder addChargingOrder) {
        return chargingOrderService.paymentChargingOrder(addChargingOrder);
    }
 
    /**
     * 充电充值支付回调
     *
     * @param request
     */
    @ResponseBody
    @PostMapping(value = "/chargingOrderWXCallback")
    public void chargingOrderWXCallback(HttpServletRequest request) {
        Map<String, Object> data = wxPaymentClient.payNotify(request).getData();
        if (null != data) {
            String out_trade_no = data.get("out_trade_no").toString();
            String transaction_id = data.get("transaction_id").toString();
            String attach = data.get("attach").toString();
            AjaxResult ajaxResult = chargingOrderService.chargingOrderCallback(1, out_trade_no, transaction_id, attach);
            if (ajaxResult.isSuccess()) {
                wxPaymentClient.ack();
            }
        }
    }
 
 
    /**
     * 支付宝支付成功后的回调
     */
    @ResponseBody
    @PostMapping(value = "/chargingOrderALICallback")
    public void chargingOrderALICallback(@RequestBody AliQueryOrder aliQueryOrder, HttpServletResponse response) {
        try {
            String out_trade_no = aliQueryOrder.getOutTradeNo();
            String transaction_id = aliQueryOrder.getTradeNo();
            String attach = aliQueryOrder.getPassbackParams();
            AjaxResult ajaxResult = chargingOrderService.chargingOrderCallback(2, out_trade_no, transaction_id, attach);
            if (ajaxResult.isSuccess()) {
                PrintWriter writer = response.getWriter();
                writer.println("success");
                writer.flush();
                writer.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    @ResponseBody
    @GetMapping(value = "/preChargeCheck/{id}")
    @ApiOperation(value = "获取安全检测数据", tags = {"小程序-扫一扫"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "充电枪id", required = true)
    })
    public AjaxResult<PreChargeCheck> preChargeCheck(@PathVariable Integer id) {
        String key = "AQJC_" + id;
        Object cacheObject = redisService.getCacheObject(key);
        return AjaxResult.success(cacheObject);
    }
 
 
    @ResponseBody
    @GetMapping(value = "/getChargingDetails/{id}")
    @ApiOperation(value = "获取充电中页面数据", tags = {"小程序-扫一扫"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "充电枪id", required = true)
    })
    public AjaxResult<ChargingDetails> getChargingDetails(@PathVariable Integer id) {
        ChargingDetails chargingDetails = chargingOrderService.getChargingDetails(id);
        return AjaxResult.success(chargingDetails);
    }
 
 
    @ResponseBody
    @PutMapping(value = "/stopCharging/{id}")
    @ApiOperation(value = "手动停止充电", tags = {"小程序-扫一扫"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", required = true)
    })
    public AjaxResult stopCharging(@PathVariable String id) {
        return chargingOrderService.stopCharging(id);
    }
}