xuhy
2024-10-12 a208b51bccff584689dc918e799c63febbcae832
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
package com.ruoyi.order.controller;
 
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.chargingPile.api.feignClient.SiteClient;
import com.ruoyi.chargingPile.api.model.Site;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.WebUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.order.api.model.ExportUidDto;
import com.ruoyi.order.api.model.TChargingBill;
import com.ruoyi.order.api.model.TChargingOrder;
import com.ruoyi.order.api.query.TOrderInvoiceQuery;
import com.ruoyi.order.api.vo.AccountListVO;
import com.ruoyi.order.api.vo.ChargingBillListVO;
import com.ruoyi.order.api.vo.ChargingBillVO;
import com.ruoyi.order.api.model.ChargingListQuery;
import com.ruoyi.order.api.vo.TOrderInvoiceVO;
import com.ruoyi.order.export.*;
import com.ruoyi.order.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-07
 */
@Api(tags = "充电算帐单")
@RestController
@RequestMapping("/chargingBill")
public class ChargingBillController {
 
    @Resource
    private TChargingBillService chargingBillService;
    @Resource
    private SiteClient siteClient;
    @Resource
    private TChargingOrderService chargingOrderService;
 
    @PostMapping(value = "/accountBillList")
    @ApiOperation(value = "列表查询", tags = {"管理后台-账户结算账单"})
    public R<AccountListVO> accountBillList(@RequestBody ChargingListQuery dto) {
        AccountListVO accountListVO = new AccountListVO();
        ChargingBillVO res = chargingBillService.chargingBillList1(dto);
 
        ChargingBillVO res1 = chargingBillService.chargingBillList1(dto);
        List<ChargingBillListVO> records = res1.getList().getRecords();
        accountListVO.setBillCount(records.size());
        accountListVO.setTotalAmount(
                res1.getPaymentAmount().subtract(res1.getRefundAmount()==null?BigDecimal.ZERO:res1.getRefundAmount())
                        .subtract(res1.getCommissionAmount()==null?BigDecimal.ZERO:res1.getCommissionAmount())
                        .subtract(res1.getSharingAmount()==null?BigDecimal.ZERO:res1.getSharingAmount()));
        accountListVO.setPaymentAmount(res1.getPaymentAmount());
        accountListVO.setRefundAmount(res1.getRefundAmount());
        accountListVO.setCommissionAmount(res1.getCommissionAmount());
        accountListVO.setSharingAmount(res1.getSharingAmount());
        accountListVO.setList(res.getList());
        return R.ok(accountListVO);
    }
    @PostMapping(value = "/chargingBillList")
    @ApiOperation(value = "充电算帐单列表查询", tags = {"管理后台-充电算账单"})
    public AjaxResult<ChargingBillVO> chargingBillList(@RequestBody ChargingListQuery dto) {
        ChargingBillVO res = chargingBillService.chargingBillList(dto);
        return AjaxResult.success(res);
    }
    @PostMapping(value = "/chargingBillListR")
    public R<ChargingBillVO> chargingBillListR(@RequestBody ChargingListQuery dto) {
        ChargingBillVO res = chargingBillService.chargingBillList(dto);
        return R.ok(res);
    }
    @GetMapping(value = "/chargingBillListExport")
    public R<ChargingBillVO> chargingBillListExport(String uid) {
        return R.ok(null);
    }
    @ApiOperation(value = "导出", tags = {"管理后台-充电算账单"})
    @PutMapping("/export")
    public void export(@RequestBody ChargingListQuery dto)
    {
        ChargingBillVO res = chargingBillService.chargingBillList(dto);
        List<ChargingBillListVO> list = res.getList().getRecords();
        List<TChargingBillExport> tChargingBillExports = new ArrayList<>();
        for (ChargingBillListVO orderInvoiceVO : list) {
            TChargingBillExport tChargingBillExport = new TChargingBillExport();
            tChargingBillExport.setCode(orderInvoiceVO.getCode());
            tChargingBillExport.setBillType("月账单");
            tChargingBillExport.setType(orderInvoiceVO.getType().toString());
            tChargingBillExport.setBillWeek(orderInvoiceVO.getBillWeek());
            tChargingBillExport.setSiteName(orderInvoiceVO.getSiteName());
            tChargingBillExport.setPaymentAmount(orderInvoiceVO.getPaymentAmount());
            tChargingBillExport.setElectrovalence(orderInvoiceVO.getElectrovalence());
            tChargingBillExport.setServiceCharge(orderInvoiceVO.getServiceCharge());
            tChargingBillExport.setChargingCapacity(orderInvoiceVO.getChargingCapacity());
            Integer chargingSecond = orderInvoiceVO.getChargingSecond();
            // 根据秒数 转换为xx小时xx分钟xx秒 如果小时为0不展示 如果分钟为0则不展示
            // 计算小时、分钟和秒
            int hours = chargingSecond / 60 / 60;
            int minutes = chargingSecond /60 % 60;
            int seconds = 0; // 如果没有秒数,则默认是0
            // 构造结果字符串
            StringBuilder result = new StringBuilder();
            if (hours > 0) {
                result.append(hours).append("小时");
            }
            if (minutes > 0) {
                result.append(minutes).append("分钟");
            }
            if (seconds > 0 || result.length() == 0) { // 如果秒数大于0,或者小时和分钟都为0,则显示秒数
                result.append(seconds).append("秒");
            }
            tChargingBillExport.setChargingTime(result.toString());
            tChargingBillExport.setOrderCount(orderInvoiceVO.getOrderCount());
            tChargingBillExport.setBillTime(orderInvoiceVO.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            tChargingBillExports.add(tChargingBillExport);
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TChargingBillExport.class, tChargingBillExports);
        HttpServletResponse response = WebUtils.response();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("月账单-"+res.getCategory()+"-"+res.getBillWeek()+"-"+res.getSiteName()+".xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @ApiOperation(value = "导出", tags = {"管理后台-账户结算账单"})
    @PutMapping("/exportAccount")
    public void exportAccount(@RequestBody ChargingListQuery dto)
    {
        ChargingBillVO res = chargingBillService.chargingBillList1(dto);
        List<ChargingBillListVO> list = res.getList().getRecords();
        List<TChargingBillExport> tChargingBillExports = new ArrayList<>();
        for (ChargingBillListVO orderInvoiceVO : list) {
            TChargingBillExport tChargingBillExport = new TChargingBillExport();
            tChargingBillExport.setCode(orderInvoiceVO.getCode());
            switch (orderInvoiceVO.getOrderState()){
                case 1:
                    tChargingBillExport.setBillType("日账单");
                    break;
                case 2:
                    tChargingBillExport.setBillType("月账单");
                    break;
            }
            tChargingBillExport.setType(orderInvoiceVO.getType().toString());
            tChargingBillExport.setBillWeek(orderInvoiceVO.getBillWeek());
            tChargingBillExport.setSiteName(orderInvoiceVO.getSiteName());
            tChargingBillExport.setPaymentAmount(orderInvoiceVO.getPaymentAmount());
            tChargingBillExport.setElectrovalence(orderInvoiceVO.getElectrovalence());
            tChargingBillExport.setServiceCharge(orderInvoiceVO.getServiceCharge());
            tChargingBillExport.setChargingCapacity(orderInvoiceVO.getChargingCapacity());
            Integer chargingSecond = orderInvoiceVO.getChargingSecond();
            // 根据秒数 转换为xx小时xx分钟xx秒 如果小时为0不展示 如果分钟为0则不展示
            // 计算小时、分钟和秒
            int hours = chargingSecond / 60 / 60;
            int minutes = chargingSecond /60 % 60;
            int seconds = 0; // 如果没有秒数,则默认是0
            // 构造结果字符串
            StringBuilder result = new StringBuilder();
            if (hours > 0) {
                result.append(hours).append("小时");
            }
            if (minutes > 0) {
                result.append(minutes).append("分钟");
            }
            if (seconds > 0 || result.length() == 0) { // 如果秒数大于0,或者小时和分钟都为0,则显示秒数
                result.append(seconds).append("秒");
            }
            tChargingBillExport.setChargingTime(result.toString());
            tChargingBillExport.setOrderCount(orderInvoiceVO.getOrderCount());
            tChargingBillExport.setBillTime(orderInvoiceVO.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            tChargingBillExports.add(tChargingBillExport);
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TChargingBillExport.class, tChargingBillExports);
        HttpServletResponse response = WebUtils.response();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("月账单-"+res.getCategory()+"-"+res.getBillWeek()+"-"+res.getSiteName()+".xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @ApiOperation(value = "下载-未出账", tags = {"管理后台-充电算账单"})
    @PutMapping("/download")
    public void download(@RequestBody ExportUidDto uid)
    {
        List<ChargingBillExport> chargingBillExports = new ArrayList<>();
        List<ChargingBillRefundExport> chargingBillRefundExports = new ArrayList<>();
        List<ChargingBillPayExport> chargingBillPayExports = new ArrayList<>();
        TChargingBill byId = chargingBillService.getById(uid.getUid());
        ChargingBillExport chargingBillExport = new ChargingBillExport();
        chargingBillExport.setCode(byId.getCode());
        // todo 确认商户类型
        chargingBillExport.setAccountType("商户类型");
        chargingBillExport.setType(byId.getType().toString());
        chargingBillExport.setBillType("月结");
        LocalDateTime billTime = byId.getBillTime();
        // 将billTime 减去一个月 转化为yyyy-MM格式字符串
        billTime = billTime.minusMonths(1);
        chargingBillExport.setBillWeek(DateUtils.parseDateToStr("yyyy-MM",DateUtils.toDate(billTime)));
 
        chargingBillExport.setBillTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",DateUtils.toDate(billTime)));
        chargingBillExport.setState("未出账");
        List<Site> data = siteClient.getSiteByIds(Arrays.asList(byId.getSiteId())).getData();
        if (!data.isEmpty()){
            chargingBillExport.setSiteName(data.get(0).getName());
        }
        // 根据账单的出账时间 查询上个月的充电订单
        LocalDateTime localDate = byId.getBillTime().minusMonths(1);
        // 账单周期
        // 获取 LocalDate 对象
        LocalDate date = localDate.toLocalDate();
        // 获取该月份的第一天
        LocalDate firstDayOfMonth = date.withDayOfMonth(1);
        // 获取该月份的最后一天
        LocalDate lastDayOfMonth = date.with(TemporalAdjusters.lastDayOfMonth());
        QueryWrapper<TChargingOrder> eq = new QueryWrapper<TChargingOrder>()
                .between("create_time", firstDayOfMonth, lastDayOfMonth)
                .eq("status", 5)
                .eq("recharge_payment_status", 2);
        if (byId.getType() == 2) {
            eq.eq("site_id", byId.getSiteId());
        }
        BigDecimal paymentAmount = new BigDecimal("0");
        BigDecimal refundAmount = new BigDecimal("0");
        BigDecimal income = new BigDecimal("0");
 
        List<TChargingOrder> tChargingOrders = chargingOrderService.list(eq);
        // 累加支付金额
        int i =1;
        for (TChargingOrder tChargingOrder : tChargingOrders) {
            // 账单信息
            paymentAmount = paymentAmount.add(tChargingOrder.getPaymentAmount());
            if (tChargingOrder.getRefundStatus()!=null && tChargingOrder.getRefundStatus()==2){
                refundAmount = refundAmount.add(tChargingOrder.getRefundAmount());
            }
            // 退款信息
            ChargingBillRefundExport chargingBillRefundExport = new ChargingBillRefundExport();
            ChargingBillPayExport chargingBillPayExport = new ChargingBillPayExport();
 
            chargingBillRefundExport.setId(i+"");
            switch (tChargingOrder.getRechargePaymentType()){
                case 1:
                    chargingBillRefundExport.setPlatform("微信");
                    chargingBillRefundExport.setPlatformPay("微信小程序支付");
                    chargingBillPayExport.setPlatform("微信");
                    chargingBillPayExport.setPlatformPay("微信小程序支付");
                    break;
                case 2:
                    chargingBillRefundExport.setPlatform("支付宝");
                    chargingBillRefundExport.setPlatformPay("支付宝小程序支付");
                    chargingBillPayExport.setPlatform("支付宝");
                    chargingBillPayExport.setPlatformPay("支付宝小程序支付");
 
            }
            chargingBillRefundExport.setRechargeSerialNumber(tChargingOrder.getRechargeSerialNumber());
            chargingBillRefundExport.setCode(tChargingOrder.getCode());
            chargingBillRefundExport.setPayTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",DateUtils.toDate(tChargingOrder.getPayTime())));
            if (tChargingOrder.getRefundTime()!=null){
                chargingBillRefundExport.setRefundTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",DateUtils.toDate(tChargingOrder.getRefundTime())));
            }
            chargingBillRefundExport.setRefundMoney(tChargingOrder.getRefundAmount().toString());
            chargingBillRefundExport.setRefundRemark(tChargingOrder.getRefundReason());
            chargingBillRefundExport.setRefundCode(tChargingOrder.getRefundCode());
            chargingBillRefundExport.setRefundSerialNumber(tChargingOrder.getRefundSerialNumber());
            chargingBillRefundExports.add(chargingBillRefundExport);
            // 支付信息
            chargingBillPayExport.setId(i+"");
            chargingBillPayExport.setRechargeSerialNumber(tChargingOrder.getRechargeSerialNumber());
            chargingBillPayExport.setCode(tChargingOrder.getCode());
            chargingBillPayExport.setPayTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",DateUtils.toDate(tChargingOrder.getPayTime())));
            chargingBillPayExport.setPaymentAmount(tChargingOrder.getPaymentAmount().toString());
            chargingBillPayExport.setTotal("");
            chargingBillPayExports.add(chargingBillPayExport);
            i++;
        }
        chargingBillExport.setPaymentAmount(paymentAmount);
        chargingBillExport.setRefundAmount(refundAmount);
        chargingBillExport.setIncome(paymentAmount.subtract(refundAmount));
        chargingBillExports.add(chargingBillExport);
 
        // 导出
        List<Map<String, Object>> sheetsList = new ArrayList<>();
        AtomicInteger atomicInteger = new AtomicInteger();
 
        for (int i1 = 0; i1 < 3; i1++) {
            String sheetName="";
            switch (i1){
                case 1:
                    sheetName= "账单信息";
                    Map<String, Object> exportMap = Maps.newHashMap();
                    ExportParams exportParams = new ExportParams(null, sheetName, ExcelType.HSSF);
                    // 以下3个参数为API中写死的参数名 分别是sheet配置/导出类(注解定义)/数据集
                    exportMap.put("title", exportParams);
                    exportMap.put("entity", ChargingBillExport.class);
                    exportMap.put("data", chargingBillExports);
                    // 加入多sheet配置列表
                    sheetsList.add(exportMap);
                    break;
                case 2:
                    sheetName= "退款订单";
                    Map<String, Object> exportMap1 = Maps.newHashMap();
                    ExportParams exportParams1 = new ExportParams(null, sheetName, ExcelType.HSSF);
                    // 以下3个参数为API中写死的参数名 分别是sheet配置/导出类(注解定义)/数据集
                    exportMap1.put("title", exportParams1);
                    exportMap1.put("entity", ChargingBillRefundExport.class);
                    exportMap1.put("data", chargingBillRefundExports);
                    // 加入多sheet配置列表
                    sheetsList.add(exportMap1);
                    break;
                case 3:
                    sheetName= "支付记录";
                    Map<String, Object> exportMap2 = Maps.newHashMap();
                    ExportParams exportParams2 = new ExportParams(null, sheetName, ExcelType.HSSF);
                    // 以下3个参数为API中写死的参数名 分别是sheet配置/导出类(注解定义)/数据集
                    exportMap2.put("title", exportParams2);
                    exportMap2.put("entity", ChargingBillPayExport.class);
                    exportMap2.put("data", chargingBillPayExports);
                    // 加入多sheet配置列表
                    sheetsList.add(exportMap2);
                    break;
            }
        }
        if (!chargingBillRefundExports.isEmpty()){
            ChargingBillRefundExport chargingBillRefundExport = chargingBillRefundExports.get(0);
            chargingBillRefundExport.setTotal(refundAmount.toString());
        }
        if (!chargingBillPayExports.isEmpty()){
            ChargingBillPayExport chargingBillPayExport = chargingBillPayExports.get(0);
            chargingBillPayExport.setTotal(refundAmount.toString());
        }
        String type="";
        String type1="";
        switch (byId.getType()){
            case 1:
                type = "全站账单";
                type1 = "全站";
                break;
            case 2:
                type = "各个站点账单";
                List<Site> data1 = siteClient.getSiteByIds(Arrays.asList(byId.getSiteId())).getData();
                if (!data1.isEmpty()){
                    type1 = data.get(0).getName();
                }
                break;
        }
        String s = DateUtils.parseDateToStr("yyyy-MM", DateUtils.toDate(billTime));
 
        Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
        HttpServletResponse response = WebUtils.response();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("账户结算账单-月账单-"+type+"-"+s+"-"+type1+".xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}