Pu Zhibing
2024-12-12 8c4bbe846095733960c17905746bc40882a50f88
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
package com.ruoyi.chargingPile.controller;
import java.time.LocalDateTime;
import java.math.BigDecimal;
 
import com.ruoyi.account.api.feignClient.AppUserCarClient;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.model.TAppUser;
import com.ruoyi.account.api.model.TAppUserCar;
import com.ruoyi.chargingPile.api.feignClient.ChargingGunClient;
import com.ruoyi.chargingPile.api.feignClient.SiteClient;
import com.ruoyi.chargingPile.api.model.Partner;
import com.ruoyi.chargingPile.api.model.Site;
import com.ruoyi.chargingPile.api.model.TChargingGun;
import com.ruoyi.common.core.utils.WebUtils;
import com.ruoyi.common.core.web.domain.BasePojo;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.common.security.annotation.Logical;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.integration.api.feignClient.UploadRealTimeMonitoringDataClient;
import com.ruoyi.integration.api.model.UploadRealTimeMonitoringData;
import com.ruoyi.order.api.model.ExportUidDto;
import com.ruoyi.order.api.feignClient.OrderClient;
import com.ruoyi.order.api.model.TChargingOrder;
import com.ruoyi.order.api.model.TSettlementConfirm;
import com.ruoyi.order.api.vo.*;
 
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.chargingPile.api.dto.ApplyChargingQuery;
import com.ruoyi.chargingPile.api.dto.ApplyChargingRemarkDto;
import com.ruoyi.chargingPile.api.dto.TApplyChargingPileDTO;
import com.ruoyi.chargingPile.api.model.TApplyChargingPile;
import com.ruoyi.chargingPile.dto.TApplyChargingPileExportDto;
import com.ruoyi.chargingPile.service.TApplyChargingPileService;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.log.enums.OperatorType;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.api.feignClient.ChargingOrderClient;
import com.ruoyi.order.api.model.ChargingListQuery;
import com.ruoyi.other.api.feignClient.OtherClient;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.CharEncoding;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-07
 */
@RestController
@RequestMapping("/t-apply-charging-pile")
public class TApplyChargingPileController {
 
    private final TApplyChargingPileService applyChargingPileService;
    private final TokenService tokenService;
    private final RedisService redisService;
    @Resource
    private ChargingOrderClient chargingOrderClient;
    @Resource
    private OrderClient orderClient;
    @Resource
    private SiteClient siteClient;
    @Resource
    private ChargingGunClient chargingGunClient;
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private AppUserCarClient appUserCarClient;
 
 
    @Autowired
    public TApplyChargingPileController(TApplyChargingPileService applyChargingPileService, TokenService tokenService, RedisService redisService) {
        this.applyChargingPileService = applyChargingPileService;
        this.tokenService = tokenService;
        this.redisService = redisService;
    }
 
    /**
     * 建桩申请
     */
    @ApiOperation(tags = {"小程序-建桩申请"},value = "建桩申请")
    @PostMapping(value = "/add")
    public AjaxResult<Boolean> add(@RequestBody TApplyChargingPileDTO dto) {
        // 验证验证码
        String code;
        if(dto.getLandlordHandling().equals(1)){
            code = redisService.getCacheObject(dto.getLandlordPhone() + Constants.APPLY_CHARGING);
        }else {
            code = redisService.getCacheObject(dto.getAgentPhone() + Constants.APPLY_CHARGING);
        }
        if(StringUtils.isEmpty(code)){
            return AjaxResult.error("验证码已过期,请重新获取验证码!");
        }
        if(!code.equals(dto.getCode())){
            return AjaxResult.error("验证码错误!");
        }
        // 用户id
        Long userId = tokenService.getLoginUserApplet().getUserId();
        dto.setAppUserId(userId);
        return AjaxResult.ok(applyChargingPileService.save(dto));
    }
    
    
    @RequiresPermissions(value = {"/pileApplication/add"}, logical = Logical.OR)
    @Log(title = "【申请建桩】添加建桩申请", businessType = BusinessType.INSERT)
    @ApiOperation(tags = {"后台-申请表单-申请建桩"},value = "建桩申请")
    @PostMapping(value = "/manage/add")
    public AjaxResult<Boolean> manageAdd(@RequestBody TApplyChargingPile dto) {
 
        return AjaxResult.ok(applyChargingPileService.save(dto));
    }
    
    
    @RequiresPermissions(value = {"/pileApplication"}, logical = Logical.OR)
    @ApiOperation(tags = {"后台-申请表单-申请建桩"},value = "列表")
    @PostMapping(value = "/page")
    public AjaxResult<Page<TApplyChargingPile>> page(@RequestBody ApplyChargingQuery applyChargingQuery) {
        Page<TApplyChargingPile> page = applyChargingPileService.lambdaQuery()
                .like(applyChargingQuery.getLandlordPhone() != null && !applyChargingQuery.getLandlordPhone().equals(""), TApplyChargingPile::getLandlordPhone, applyChargingQuery.getLandlordPhone())
                .orderByDesc(BasePojo::getCreateTime )
                .page(Page.of(applyChargingQuery.getPageCurr(), applyChargingQuery.getPageSize()));
        return AjaxResult.ok(page);
    }
    
    
    @RequiresPermissions(value = {"/pileApplication/export"}, logical = Logical.OR)
    @ApiOperation(tags = {"后台-申请表单-申请建桩"},value = "导出")
    @PutMapping(value = "/export")
    @Log(title = "【申请建桩】导出建桩申请列表", businessType = BusinessType.EXPORT)
    public R export(String landlordPhone,HttpServletResponse response) {
        try {
        response.setCharacterEncoding(Constants.UTF8);
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Access-Control-Expose-Headers", "Content-disposition");
        response.setHeader("Content-Disposition", "attachment;filename=" +
                URLEncoder.encode("申请建桩数据", CharEncoding.UTF_8) + ".xlsx");
        } catch (UnsupportedEncodingException e) {
            return R.fail("excel导出失败!");
        }
        try {
            List<TApplyChargingPile> list = applyChargingPileService.lambdaQuery().like(landlordPhone != null && !landlordPhone.equals(""), TApplyChargingPile::getLandlordPhone, landlordPhone).list();
 
            List<TApplyChargingPileExportDto> exportDtos =new ArrayList<>();
            for (TApplyChargingPile tApplyChargingPile : list) {
                TApplyChargingPileExportDto applyChargingPileExportDto = new TApplyChargingPileExportDto();
                BeanUtils.copyProperties(tApplyChargingPile,applyChargingPileExportDto);
                JSONArray p1 = JSON.parseArray(tApplyChargingPile.getLandlordFrontIdCard());
                if (!p1.isEmpty()) {
                    applyChargingPileExportDto.setLandlordFrontIdCard(new URL((String) p1.get(0)));
                }
                JSONArray p2 = JSON.parseArray(tApplyChargingPile.getLandlordBackIdCard());
                if (!p2.isEmpty()){
                applyChargingPileExportDto.setLandlordBackIdCard(new URL((String)p2.get(0)));}
                JSONArray p3 = JSON.parseArray(tApplyChargingPile.getAgentBackIdCard());
                if (!p3.isEmpty())
                applyChargingPileExportDto.setAgentBackIdCard(new URL((String)p3.get(0)));
                JSONArray p4 = JSON.parseArray(tApplyChargingPile.getAgentFrontIdCard());
                if (!p4.isEmpty()) {
                    applyChargingPileExportDto.setAgentFrontIdCard(new URL((String)p4.get(0)));
                }
 
                if (tApplyChargingPile.getChargingPileUser()==1){
                    JSONArray pics1 = JSON.parseArray(tApplyChargingPile.getFixedParkingPropertyRights());
                    if (!pics1.isEmpty()) {
                        applyChargingPileExportDto.setPics1(new URL((String)pics1.get(0)));
                    }
                    JSONArray pics2 = JSON.parseArray(tApplyChargingPile.getConstructionCertificate());
                    if (!pics1.isEmpty()) {
                        applyChargingPileExportDto.setPics2(new URL((String)pics2.get(0)));
                    }
                }else {
                    JSONArray pics1 = JSON.parseArray(tApplyChargingPile.getTitleCertificate());
                    if (!pics1.isEmpty()) {
                        applyChargingPileExportDto.setPics1(new URL((String)pics1.get(0)));
                    }
                    JSONArray pics2 = JSON.parseArray(tApplyChargingPile.getBusinessLicense());
                    if (!pics1.isEmpty()) {
                        applyChargingPileExportDto.setPics2(new URL((String)pics2.get(0)));
                    }
                }
 
                JSONArray objects1 = JSON.parseArray(tApplyChargingPile.getFurtherInformation());
                for (int i = 0; i < objects1.size(); i++) {
                    JSONObject jsonObject = JSON.parseObject(objects1.get(i).toString());
                    System.err.println("==================="+jsonObject.get("url"));
 
                    boolean isImage = isImageUrl(jsonObject);
                    if (!isImage){
                        continue;
                    }
                    System.err.println("Is image: " + isImage);
                    if (i==0){
                        applyChargingPileExportDto.setUrl1(new URL((String)jsonObject.get("url")));
                    }else if (i==1){
                        applyChargingPileExportDto.setUrl2(new URL((String)jsonObject.get("url")));
                    }else if (i==2){
                        applyChargingPileExportDto.setUrl3(new URL((String)jsonObject.get("url")));
                    }else if (i==3){
                        applyChargingPileExportDto.setUrl4(new URL((String)jsonObject.get("url")));
                    }else if (i==4){
                        applyChargingPileExportDto.setUrl5(new URL((String)jsonObject.get("url")));
                    }else if (i==5){
                        applyChargingPileExportDto.setUrl6(new URL((String)jsonObject.get("url")));
                    }
                }
 
                if (tApplyChargingPile.getLandlordHandling()==1){
                    applyChargingPileExportDto.setIs1("是");
                }else {
                    applyChargingPileExportDto.setIs1("否");
                }
                if (tApplyChargingPile.getChargingPileUser()==1){
                    applyChargingPileExportDto.setIs2("是");
                }else {
                    applyChargingPileExportDto.setIs2("否");
                }
                if (tApplyChargingPile.getChargingPile()==1){
                    applyChargingPileExportDto.setIs3("是");
                }else {
                    applyChargingPileExportDto.setIs3("否");
                }
                exportDtos.add(applyChargingPileExportDto);
            }
            // excel模板封装
            ExcelWriterBuilder excelWriterBuilder = EasyExcelFactory.write(response.getOutputStream());
            InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/" +"充电桩申请记录" + ".xlsx");
            // 自动释放资源
            try (ExcelWriter excelWriter = excelWriterBuilder.withTemplate(stream).build()) {
                WriteSheet writeSheet = EasyExcelFactory.writerSheet().build();
                excelWriter.fill(exportDtos, writeSheet);
                excelWriter.finish();
            } catch (Exception e) {
                return R.fail("excel导出失败!");
            }
        } catch (IOException e) {
            return R.fail("excel导出失败!");
        }
        return R.ok();
    }
    
    
    @RequiresPermissions(value = {"/chargeBill/download"}, logical = Logical.OR)
    @ApiOperation(value = "下载-已出账", tags = {"管理后台-充电算账单"})
    @PutMapping("/downloadBill")
    @Log(title = "【充电算账单】下载算账单", businessType = BusinessType.EXPORT)
    public R downloadBill(@RequestBody ExportUidDto uid, HttpServletResponse response)
    {
        ChargingListQuery chargingListQuery = new ChargingListQuery();
        chargingListQuery.setUid(uid.getUid());
        chargingListQuery.setPageCurr(1);
        chargingListQuery.setPageSize(99999);
        ChargingBillVO data = chargingOrderClient.chargingBillListR(chargingListQuery).getData();
 
        List<ChargingBillVO> chargingBillVOS = new ArrayList<>();
        chargingBillVOS.add(data);
        try {
            response.setCharacterEncoding(Constants.UTF8);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Access-Control-Expose-Headers", "Content-disposition");
            response.setHeader("Content-Disposition", "attachment;filename=" +
//                    URLEncoder.encode("月账单-"+data.getCategory()+"-"+data.getBillWeek()+"-"+data.getSiteName(), CharEncoding.UTF_8) + ".xlsx");
                   URLEncoder.encode("月账单", CharEncoding.UTF_8) + ".xlsx");
        } catch (UnsupportedEncodingException e) {
            return R.fail("excel导出失败!");
        }
        try {
            // excel模板封装
            ExcelWriterBuilder excelWriterBuilder = EasyExcelFactory.write(response.getOutputStream());
            InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/" +"充电算帐单已出账" + ".xlsx");
            // 自动释放资源
            try (ExcelWriter excelWriter = excelWriterBuilder.withTemplate(stream).build()) {
                WriteSheet writeSheet = EasyExcel.writerSheet().build();
                FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
                excelWriter.fill(new FillWrapper("data1", chargingBillVOS), fillConfig, writeSheet);
                excelWriter.fill(new FillWrapper("data2", data.getExportList()), fillConfig, writeSheet);
                excelWriter.finish();
            } catch (Exception e) {
                return R.fail("excel导出失败!");
            }
        } catch (IOException e) {
            return R.fail("excel导出失败!");
        }
        return R.ok();
    }
    
    
    
    @RequiresPermissions(value = {"/accountSettlementStatement/download"}, logical = Logical.OR)
    @ApiOperation(value = "下载", tags = {"管理后台-账户结算账单"})
    @PutMapping("/downloadAccount")
    @Log(title = "【账户结算账单】下载账单", businessType = BusinessType.EXPORT)
    public R downloadAccount(@RequestBody ChargingListQuery dto,HttpServletResponse response)
    {   dto.setPageCurr(1);
        dto.setPageSize(99999999);
        AccountListVO data = orderClient.accountBillList(dto).getData();
        List<TChargingOrder> collect = data.getOrderList().stream().distinct().collect(Collectors.toList());
        int i = 1;
        BigDecimal bigDecimal = new BigDecimal("0");
 
        for (TChargingOrder chargingOrder : collect) {
            bigDecimal = bigDecimal.add(chargingOrder.getRefundAmount());
            if (chargingOrder.getRechargePaymentType()!=null){
                switch (chargingOrder.getRechargePaymentType()){
                    case 1:
                        chargingOrder.setPayPlatform("微信");
                        chargingOrder.setRechargePaymentType1("微信小程序支付");
                        break;
                    case 2:
                        chargingOrder.setPayPlatform("支付宝");
                        chargingOrder.setRechargePaymentType1("支付宝小程序支付");
                        break;
                }
            }
            chargingOrder.setXuhao(i);
            i++;
        }
        collect.get(0).setTotal(bigDecimal);
        try {
            response.setCharacterEncoding(Constants.UTF8);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Access-Control-Expose-Headers", "Content-disposition");
            response.setHeader("Content-Disposition", "attachment;filename=" +
                    URLEncoder.encode("账户结算账单", CharEncoding.UTF_8) + ".xlsx");
        } catch (UnsupportedEncodingException e) {
            return R.fail("excel导出失败!");
        }
        try {
            // excel模板封装
            ExcelWriterBuilder excelWriterBuilder = EasyExcelFactory.write(response.getOutputStream());
            InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/" +"账户结算账单" + ".xlsx");
            // 自动释放资源
            try (ExcelWriter excelWriter = excelWriterBuilder.withTemplate(stream).build()) {
                WriteSheet writeSheet = EasyExcel.writerSheet().build();
                FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.VERTICAL).build();
                excelWriter.fill(new FillWrapper("data1", collect), fillConfig, writeSheet);
                excelWriter.finish();
            } catch (Exception e) {
                return R.fail("excel导出失败!");
            }
        } catch (IOException e) {
            return R.fail("excel导出失败!");
        }
        return R.ok();
    }
    
    
    
    @RequiresPermissions(value = {"/summarySettlement/export", "/balanceSheetRecord/download"}, logical = Logical.OR)
    @ApiOperation(value = "导出", tags = {"管理后台-结算汇总表"})
    @PutMapping("/downloadSettlementTotal")
    @Log(title = "【结算汇总表】下载汇总表", businessType = BusinessType.EXPORT)
    public R downloadSettlementTotal(@RequestBody ExportUidDto uid,HttpServletResponse response)
    {
        Long userId = tokenService.getLoginUser().getUserid();
        SettlementTotalVO data = chargingOrderClient.settlementTotalR(uid.getTime(),userId).getData();
        try {
            response.setCharacterEncoding(Constants.UTF8);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Access-Control-Expose-Headers", "Content-disposition");
            response.setHeader("Content-Disposition", "attachment;filename=" +
                    URLEncoder.encode("结算确认单", CharEncoding.UTF_8) + ".xlsx");
        } catch (UnsupportedEncodingException e) {
            return R.fail("excel导出失败!");
        }
        try {
            // excel模板封装
            ExcelWriterBuilder excelWriterBuilder = EasyExcelFactory.write(response.getOutputStream());
            InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/" +"结算汇总" + ".xlsx");
            // 自动释放资源
            try (ExcelWriter excelWriter = excelWriterBuilder.withTemplate(stream).build()) {
                WriteSheet writeSheet = EasyExcel.writerSheet().build();
                FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.VERTICAL).build();
                excelWriter.fill(new FillWrapper("data1", data.getList1()), fillConfig, writeSheet);
                excelWriter.fill(new FillWrapper("data2", data.getList2()), fillConfig, writeSheet);
                excelWriter.finish();
            } catch (Exception e) {
                return R.fail("excel导出失败!");
            }
        } catch (IOException e) {
            return R.fail("excel导出失败!");
        }
        return R.ok();
    }
    
    
    
    @Resource
    private OtherClient otherClient;
    @Resource
    private UploadRealTimeMonitoringDataClient uploadRealTimeMonitoringDataClient;
    @ApiOperation(value = "下载", tags = {"管理后台-结算表记录"})
    @PutMapping("/downloadSettlement")
    @Log(title = "【结算表记录】下载结算表", businessType = BusinessType.EXPORT)
    public R downloadSettlement(@RequestBody ExportUidDto uid,HttpServletResponse response)
    {
        TSettlementConfirm data = chargingOrderClient.downloadSettlement(uid.getUid()).getData();
        for (TChargingOrder chargingOrder : data.getList()) {
            SettlementExportVO settlementExportVO = new SettlementExportVO();
            BeanUtils.copyProperties(data, settlementExportVO);
            List<Site> data3 = siteClient.getSiteByIds(Arrays.asList(data.getSiteId())).getData();
            if (data3!=null && (!data3.isEmpty())){
                TAppUser data4 = appUserClient.getUserById(chargingOrder.getAppUserId()).getData();
                if (data4.getVipId()== null ){
                    settlementExportVO.setVipType("非会员");
                }else{
                    List<Integer> vipIds = new ArrayList<>();
                    //获取会员map
                    R<Map<Integer, String>> vipMap = otherClient.getVipMap(vipIds);
                    settlementExportVO.setVipType(vipMap.getData().get(data4.getVipId()));
                }
                settlementExportVO.setPhone(data4.getPhone());
                settlementExportVO.setRealName(data4.getName());
                if (chargingOrder.getAppUserCarId()!=null){
                    List<TAppUserCar> data5 = appUserCarClient.getCarByIds(Arrays.asList(chargingOrder.getAppUserCarId())).getData();
                    if (!data5.isEmpty()){
                        settlementExportVO.setLicensePlate(data5.get(0).getLicensePlate());
                        settlementExportVO.setVehicleModel(data5.get(0).getVehicleModel());
                        settlementExportVO.setVehicleBrand(data5.get(0).getVehicleBrand());
                    }
 
                }
                settlementExportVO.setSiteCode(data3.get(0).getCode());
                settlementExportVO.setSiteName(data3.get(0).getName());
                settlementExportVO.setCity(data3.get(0).getCity());
                settlementExportVO.setDistricts(data3.get(0).getDistricts());
                Partner data1 = siteClient.getPartnerR(data3.get(0).getPartnerId()).getData();
                if (data1!=null){
                    settlementExportVO.setPartnerName(data1.getName());
                }
                switch (data3.get(0).getSiteType()){
                    case 0:
                        settlementExportVO.setSiteType("其他");
                        break;
                    case 1:
                        settlementExportVO.setSiteType("公共");
                        break;
                    case 2:
                        settlementExportVO.setSiteType("个人");
                        break;
                    case 3:
                        settlementExportVO.setSiteType("公交(专业)");
                        break;
                    case 4:
                        settlementExportVO.setSiteType("环卫(专用)");
                        break;
                    case 5:
                        settlementExportVO.setSiteType("物流(专用)");
                        break;
                    case 6:
                        settlementExportVO.setSiteType("出租车(专用)");
                        break;
                }
                switch (data3.get(0).getStatus()){
                    case 1:
                        settlementExportVO.setSiteStatus("正常使用");
                        break;
                    case 2:
                        settlementExportVO.setSiteStatus("维修中");
                        break;
                    case 3:
                        settlementExportVO.setSiteStatus("关闭下线");
                        break;
                }
                switch (data3.get(0).getBusinessCategory()){
                    case 1:
                        settlementExportVO.setBusinessCategory("直营");
                        break;
                    case 2:
                        settlementExportVO.setBusinessCategory("非直营");
                        break;
                }
                // todo 查询站点计费策略名称
//            settlementExportVO.setAccountingStrategy();
 
 
            }
            TChargingGun data4 = chargingGunClient.getChargingGunById(settlementExportVO.getChargingGunId())
                    .getData();
            if (data4!=null){
                settlementExportVO.setName(data4.getName());
                settlementExportVO.setDeviceCode(data4.getCode());
            }
 
            settlementExportVO.setElectronicType("转售");
            settlementExportVO.setPayTypeName("线上结算");
            LocalDateTime createTime = settlementExportVO.getCreateTime();
            LocalDateTime startTime = settlementExportVO.getStartTime();
            LocalDateTime endTime = settlementExportVO.getEndTime();
            settlementExportVO.setCreateTime1(createTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            settlementExportVO.setStartTime1(startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            settlementExportVO.setEndTime1(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            if (chargingOrder.getStatus()!=null){
                switch (chargingOrder.getStatus()){
                    case 0:
                        settlementExportVO.setStatusName("未知");
                        break;
                    case 1:
                        settlementExportVO.setStatusName("等待中/已插枪");
                        break;
                    case 2:
                        settlementExportVO.setStatusName("启动中");
                        break;
                    case 3:
                        settlementExportVO.setStatusName("充电中");
                        break;
                    case 4:
                        settlementExportVO.setStatusName("停止中");
                        break;
                    case 5:
                        settlementExportVO.setStatusName("已结束");
                        break;
 
                }
            }
            settlementExportVO.setOrderTypeName("充电订单");
            settlementExportVO.setChargingType("单桩双充");
            settlementExportVO.setIsFree("否");
            settlementExportVO.setAverageElectronic("");
            settlementExportVO.setAverageServiceChargeReal("");
            settlementExportVO.setAverageServiceCharge("");
            // 获取充电时间
            UploadRealTimeMonitoringData data5 = uploadRealTimeMonitoringDataClient.chargingOrderInfo(chargingOrder.getCode()).getData();
            if (data5!=null){
                if (data5.getCumulative_charging_time()!=null){
                    settlementExportVO.setChargingSecond(data5.getCumulative_charging_time()+"");
 
                }
            }
            // 获取开始SOC 结束soc
            if (chargingOrder.getCode()!=null){
                List<UploadRealTimeMonitoringData> data6 = uploadRealTimeMonitoringDataClient.getDataByOrderCode(chargingOrder.getCode()).getData();
                if (data6!=null && !data6.isEmpty()){
                    int min = 100;
                    int max = 0;
                    for (UploadRealTimeMonitoringData uploadRealTimeMonitoringData : data6) {
                        if (uploadRealTimeMonitoringData.getSoc()>max){
                            max = uploadRealTimeMonitoringData.getSoc();
                        }
                        if (uploadRealTimeMonitoringData.getSoc()!=0 &&uploadRealTimeMonitoringData.getSoc()<min){
                            min = uploadRealTimeMonitoringData.getSoc();
                        }
                    }
                    settlementExportVO.setEndSoc(max+"");
                    settlementExportVO.setStartSoc(min+"");
                }
            }
            settlementExportVO.setIsSoc("否");
            settlementExportVO.setSocType("");
            settlementExportVO.setSocValue("");
            settlementExportVO.setUserType("普通个人用户");
 
            if (chargingOrder.getVipDiscountAmount()!=null && chargingOrder.getVipDiscountAmount().compareTo(BigDecimal.ZERO)>0){
                settlementExportVO.setIsPlus("是");
 
            }else {
                settlementExportVO.setIsPlus("否");
 
            }
            settlementExportVO.setUserCode("");
 
 
            settlementExportVO.setConfirmCode(data.getConfirmCode());
            settlementExportVO.setCompanyName("四川明星新能源有限科技公司");
            settlementExportVO.setMoneyType("线上计费");
            settlementExportVO.setDeviceType("设备直连(自由集控)");
            settlementExportVO.setBeforePayType("先付后退");
            settlementExportVO.setIsSafe("是");
            // 顶部合计数据
            data.setServiceChargeTotal(data.getServiceCharge().add(data.getVenue())
                    .add(data.getVenue()).add(data.getMaintain()).add(data.getClean())+"");
 
            data.setServicePartnerString(data.getServicePartner()+"%");
            BigDecimal bigDecimal = new BigDecimal("100");
            data.setPlatformString(bigDecimal.subtract(data.getServicePartner())+"%");
        }
 
        List<Site> data1 = siteClient.getSiteByIds(Arrays.asList(data.getSiteId())).getData();
        if (!data1.isEmpty()){
            data.setSiteName(data1.get(0).getName());
        }
        Partner data2 = siteClient.getPartnerR(data.getPartnerId()).getData();
        if (data2!=null){
            data.setPartnerName(data2.getName());
        }
        List<TSettlementConfirm> chargingBillVOS = new ArrayList<>();
        chargingBillVOS.add(data);
        try {
            response.setCharacterEncoding(Constants.UTF8);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Access-Control-Expose-Headers", "Content-disposition");
            response.setHeader("Content-Disposition", "attachment;filename=" +
                    URLEncoder.encode("结算确认单", CharEncoding.UTF_8) + ".xlsx");
        } catch (UnsupportedEncodingException e) {
            return R.fail("excel导出失败!");
        }
        try {
            // excel模板封装
            ExcelWriterBuilder excelWriterBuilder = EasyExcelFactory.write(response.getOutputStream());
            InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/" +"运营商名称1-充电桩名称1-结算对账单明细-结算月份1" + ".xlsx");
            // 自动释放资源
            try (ExcelWriter excelWriter = excelWriterBuilder.withTemplate(stream).build()) {
                WriteSheet writeSheet = EasyExcel.writerSheet().build();
                WriteSheet writeSheet2 = EasyExcel.writerSheet().build();
                WriteSheet writeSheet3 = EasyExcel.writerSheet().build();
                FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.VERTICAL).build();
                excelWriter.fill(new FillWrapper("data1", chargingBillVOS), fillConfig, writeSheet);
                excelWriter.fill(new FillWrapper("data2", chargingBillVOS), fillConfig, writeSheet2);
                excelWriter.fill(new FillWrapper("data3", data.getList()), fillConfig, writeSheet3);
                excelWriter.finish();
            } catch (Exception e) {
                return R.fail("excel导出失败!");
            }
        } catch (IOException e) {
            return R.fail("excel导出失败!");
        }
        return R.ok();
    }
 
 
 
 
 
 
    @ApiOperation(value = "下载-未出账", tags = {"管理后台-充电算账单"})
    @PutMapping("/download")
    @Log(title = "【充电算账单】下载算账单", businessType = BusinessType.EXPORT)
    public R download(@RequestBody ExportUidDto uid,HttpServletResponse response)
    {
        ChargingListQuery chargingListQuery = new ChargingListQuery();
        chargingListQuery.setUid(uid.getUid());
        chargingListQuery.setPageCurr(1);
        chargingListQuery.setPageSize(99999);
        ChargingBillVO data = chargingOrderClient.chargingBillListR(chargingListQuery).getData();
 
        List<ChargingBillVO> chargingBillVOS = new ArrayList<>();
        chargingBillVOS.add(data);
        try {
            response.setCharacterEncoding(Constants.UTF8);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Access-Control-Expose-Headers", "Content-disposition");
            response.setHeader("Content-Disposition", "attachment;filename=" +
//                    URLEncoder.encode("月账单-"+data.getCategory()+data.getBillWeek()+data.getSiteName(), CharEncoding.UTF_8) + ".xlsx");
                    URLEncoder.encode("月账单", CharEncoding.UTF_8) + ".xlsx");
        } catch (UnsupportedEncodingException e) {
            return R.fail("excel导出失败!");
        }
        try {
            // excel模板封装
            ExcelWriterBuilder excelWriterBuilder = EasyExcelFactory.write(response.getOutputStream());
            InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/" +"充电算帐单已出账" + ".xlsx");
            // 自动释放资源
            try (ExcelWriter excelWriter = excelWriterBuilder.withTemplate(stream).build()) {
                WriteSheet writeSheet = EasyExcel.writerSheet().build();
                FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
                excelWriter.fill(new FillWrapper("data1", chargingBillVOS), fillConfig, writeSheet);
                excelWriter.fill(new FillWrapper("data2", data.getExportList()), fillConfig, writeSheet);
                excelWriter.finish();
            } catch (Exception e) {
                return R.fail("excel导出失败!");
            }
        } catch (IOException e) {
            return R.fail("excel导出失败!");
        }
        return R.ok();
    }
 
    public static boolean isImageUrl(JSONObject jsonObject) {
        String url = jsonObject.getString("url");
        Pattern pattern = Pattern.compile(
                "^.*\\.(?i)(jpg|jpeg|png|gif|bmp|webp)$"
        );
        return pattern.matcher(url).matches();
    }
    
    @RequiresPermissions(value = {"/pileApplication/select"}, logical = Logical.OR)
    @ApiOperation(tags = {"后台-申请表单-申请建桩"},value = "详情")
    @PostMapping(value = "/detail/{id}")
    public AjaxResult<TApplyChargingPile> detail(@PathVariable Integer id) {
 
        return AjaxResult.ok(applyChargingPileService.getById(id));
    }
    
    @RequiresPermissions(value = {"/pileApplication/remark"}, logical = Logical.OR)
    @ApiOperation(tags = {"后台-申请表单-申请建桩"},value = "修改备注")
    @PostMapping(value = "/remark")
    @Log(title = "【申请建桩】修改备注", businessType = BusinessType.UPDATE)
    public AjaxResult<TApplyChargingPile> remark(@RequestBody ApplyChargingRemarkDto applyChargingRemarkDt) {
        TApplyChargingPile byId = applyChargingPileService.getById(applyChargingRemarkDt.getId());
        byId.setRemark(applyChargingRemarkDt.getRemark());
        applyChargingPileService.updateById(byId);
        return AjaxResult.success();
    }
    
    @RequiresPermissions(value = {"/pileApplication/del"}, logical = Logical.OR)
    @ApiOperation(tags = {"后台-申请表单-申请建桩"},value = "删除")
    @DeleteMapping(value = "/remark")
    @Log(title = "【申请建桩】删除建桩申请", businessType = BusinessType.DELETE)
    public AjaxResult remark(String ids) {
 
        String[] split = ids.split(",");
        for (String s : split) {
            applyChargingPileService.removeById(s);
 
        }
        return AjaxResult.success();
 
    }
 
 
 
 
    
    
}