huliguo
2025-04-23 f2070facdb5715e7349df69cfe257289c680d292
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
package com.ruoyi.order.service.impl;
 
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.feignClient.UserPointClient;
import com.ruoyi.account.api.model.AppUser;
import com.ruoyi.account.api.model.UserPoint;
import com.ruoyi.common.core.constant.ExpressCompanyMap;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.order.dto.GetImportOrderDTO;
import com.ruoyi.order.mapper.ChargeOrderMapper;
import com.ruoyi.order.mapper.OrderMapper;
import com.ruoyi.order.model.ChargeOrder;
import com.ruoyi.order.model.Order;
import com.ruoyi.order.service.ChargeOrderService;
import com.ruoyi.order.service.OrderService;
import com.ruoyi.order.vo.ConfirmDelivery;
import com.ruoyi.other.api.domain.Region;
import com.ruoyi.system.api.domain.SysConfig;
import com.ruoyi.system.api.feignClient.SysConfigClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
 
@Slf4j
@Service
public class ChargeOrderServiceImpl extends ServiceImpl<ChargeOrderMapper, ChargeOrder> implements ChargeOrderService {
 
    @Resource
    private OrderService orderService;
    @Resource
    private SysConfigClient sysConfigClient;
 
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private UserPointClient userPointClient;
    @Resource
    private ChargeOrderService chargeOrderService;
    private static final String DEFAULT_AVATAR_URL = "http://qijishenghuiyuan.obs.cn-southwest-2.myhuaweicloud.com/admin/aedfbbb41280471f8d9fa7905298b65f.png";
 
 
    @Override
    public void importExpress(String url) {
        URL url1 = null;
        try {
            url1 = new URL(url);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        List<String> orderNumberList=new ArrayList<>();
 
        try (InputStream fileInputStream = url1.openStream()) {
            Workbook workbook = new XSSFWorkbook(fileInputStream);
            Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet
            int lastRowNum = sheet.getLastRowNum();
            for (int i = 1; i <= lastRowNum; i++) {//第二行开始
                Row row = sheet.getRow(i);
                // 订单编号
                if (row.getCell(0) == null){
                    throw new ServiceException("第" + i + "行订单编号为空", 500);
                }
                row.getCell(0).setCellType(CellType.STRING);
                String orderNum = row.getCell(0).getStringCellValue();
 
                // 订单类型
                if (row.getCell(1) == null){
                    throw new ServiceException("第" + i + "行订单类型为空", 500);
                }
                row.getCell(1).setCellType(CellType.STRING);
                String orderType = row.getCell(1).getStringCellValue();
 
                // 充电方式
                if (row.getCell(2) == null){
                    throw new ServiceException("第" + i + "行充电方式为空", 500);
                }
                row.getCell(2).setCellType(CellType.STRING);
                String chargeType = row.getCell(2).getStringCellValue();
 
 
                // 充电用户
                if (row.getCell(3) == null){
                    throw new ServiceException("第" + i + "行充电用户为空", 500);
                }
                row.getCell(3).setCellType(CellType.STRING);
                String phone = row.getCell(3).getStringCellValue();
 
 
                // 开始时间
                Cell beginTimeCell = row.getCell(6);
                if (beginTimeCell == null) {
                    throw new ServiceException("第" + (i + 1) + "行开始时间为空", 500);
                }
 
                LocalDateTime beginTime;
                if (beginTimeCell.getCellType() == CellType.NUMERIC || DateUtil.isCellDateFormatted(beginTimeCell)) {
                    // 处理数值型日期(包括Excel内置日期格式)
                    beginTime = beginTimeCell.getDateCellValue().toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalDateTime();
                } else {
                    // 处理文本型日期(如 "2025/4/3 23:25:38")
                    String dateStr = beginTimeCell.getStringCellValue().trim();
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:mm:ss");
                    beginTime = LocalDateTime.parse(dateStr, formatter);
                }
 
                // 结束时间
                Cell endTimeCell = row.getCell(6);
                if (endTimeCell == null) {
                    throw new ServiceException("第" + (i + 1) + "行开始时间为空", 500);
                }
 
                LocalDateTime endTime;
                if (endTimeCell.getCellType() == CellType.NUMERIC || DateUtil.isCellDateFormatted(endTimeCell)) {
                    // 处理数值型日期(包括Excel内置日期格式)
                    endTime = endTimeCell.getDateCellValue().toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalDateTime();
                } else {
                    // 处理文本型日期(如 "2025/4/3 23:25:38")
                    String dateStr = beginTimeCell.getStringCellValue().trim();
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:mm:ss");
                    endTime = LocalDateTime.parse(dateStr, formatter);
                }
 
 
                // 充电时长
                if (row.getCell(8 )== null){
                    throw new ServiceException("第" + i + "行充电时长为空", 500);
                }
                row.getCell(8).setCellType(CellType.STRING);
                String chargingDuration = row.getCell(8).getStringCellValue();
 
 
                // 充电电量
                if (row.getCell(9 )== null){
                    throw new ServiceException("第" + i + "行充电电量为空", 500);
                }
                row.getCell(9).setCellType(CellType.STRING);
                String chargingCapacity = row.getCell(9).getStringCellValue();
 
                // 电站运营商
                if (row.getCell(15 )== null){
                    throw new ServiceException("第" + i + "行电站运营商为空", 500);
                }
                row.getCell(15).setCellType(CellType.STRING);
                String powerStationOperator = row.getCell(15).getStringCellValue();
 
                // 城市名称
                if (row.getCell(16 )== null){
                    throw new ServiceException("第" + i + "行城市名称为空", 500);
                }
                row.getCell(16).setCellType(CellType.STRING);
                String city = row.getCell(16).getStringCellValue();
 
                // 充电场站
                if (row.getCell(17 )== null){
                    throw new ServiceException("第" + i + "行充电场站为空", 500);
                }
                row.getCell(17).setCellType(CellType.STRING);
                String chargingStation = row.getCell(17).getStringCellValue();
 
                // 电站id
                if (row.getCell(18 )== null){
                    throw new ServiceException("第" + i + "行电站id为空", 500);
                }
                row.getCell(18).setCellType(CellType.STRING);
                String powerStationId = row.getCell(18).getStringCellValue();
 
                // 终端编码
                if (row.getCell(20 )== null){
                    throw new ServiceException("第" + i + "行电站id为空", 500);
                }
                row.getCell(20).setCellType(CellType.STRING);
                String terminalCode = row.getCell(20).getStringCellValue();
 
                // 车牌号
                if (row.getCell(26 )== null){
                    throw new ServiceException("第" + i + "行车牌号为空", 500);
                }
                row.getCell(26).setCellType(CellType.STRING);
                String plateNumber = row.getCell(26).getStringCellValue();
 
                // 电站价电费金额
                if (row.getCell(73 )== null){
                    throw new ServiceException("第" + i + "行电站价电费金额为空", 500);
                }
                row.getCell(73).setCellType(CellType.STRING);
                String electricityAmount = row.getCell(73).getStringCellValue();
 
                // 电站价服务费金额
                if (row.getCell(74 )== null){
                    throw new ServiceException("第" + i + "行电站价服务费金额为空", 500);
                }
                row.getCell(74).setCellType(CellType.STRING);
                String serviceAmount = row.getCell(74).getStringCellValue();
 
                // 电站价总金额
                if (row.getCell(75 )== null){
                    throw new ServiceException("第" + i + "行电站价服务费金额为空", 500);
                }
                row.getCell(75).setCellType(CellType.STRING);
                String totalAmount = row.getCell(75).getStringCellValue();
 
 
                //检查订单号是否重复
                if (orderNumberList.contains(orderNum)) {
                    throw new ServiceException("订单号重复:" + orderNum, 500);
                }
                //数据库中是否存在
                ChargeOrder order = chargeOrderService.getOne(new LambdaQueryWrapper<ChargeOrder>()
                        .eq(ChargeOrder::getOrderNumber, orderNum)
                );
                if (order != null) {
                    throw new ServiceException("数据库中订单已存在:" + orderNum, 500);
                }
                orderNumberList.add(orderNum);//添加到订单集合中
 
 
                ChargeOrder chargeOrder = new ChargeOrder();
                chargeOrder.setOrderNumber(orderNum);
                chargeOrder.setOrderType(orderType);
                chargeOrder.setChargeType(chargeType);
                chargeOrder.setPhone(phone);
                chargeOrder.setBeginTime(beginTime );
                chargeOrder.setEndTime(endTime);
                chargeOrder.setChargingDuration(chargingDuration);
                chargeOrder.setChargingCapacity( new BigDecimal(chargingCapacity));
                chargeOrder.setPowerStationOperator(powerStationOperator);
                chargeOrder.setCity(city);
                chargeOrder.setChargingStation(chargingStation);
                chargeOrder.setPowerStationId(Integer.valueOf(powerStationId));
                chargeOrder.setTerminalCode(terminalCode);
                chargeOrder.setPlateNumber(plateNumber);
                chargeOrder.setElectricityAmount(new BigDecimal(electricityAmount));
                chargeOrder.setServiceAmount(new BigDecimal(serviceAmount));
                chargeOrder.setTotalAmount(new BigDecimal(totalAmount));
 
                R r = importData(chargeOrder);
                if (R.isError(r)) {
                    throw new ServiceException(r.getMsg(), 500);
                }
            }
 
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
    }
 
    @Override
    public PageInfo<ChargeOrder> getUserPointPageList(GetImportOrderDTO dto) {
        PageInfo<ChargeOrder> pageInfo = new PageInfo<>(dto.getPageCurr(), dto.getPageSize());
        List<ChargeOrder> list= baseMapper.getUserPointPageList(pageInfo,dto);
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    private R importData(ChargeOrder chargeOrder) {
        //先计算出积分数
        Integer point = getPoint(chargeOrder.getTotalAmount());
        //获取用户,更改积分数
        AppUser appuser = appUserClient.getAppUserByPhone1(chargeOrder.getPhone()).getData();
        if (null==appuser){
            //创建用户
            appuser = new AppUser();
//            Long userId = UUID.randomUUID().getMostSignificantBits() & Long.MAX_VALUE;
//            appuser.setId(userId);
            appuser.setPhone(chargeOrder.getPhone());
            appuser.setStatus(1);
            appuser.setAvatar( DEFAULT_AVATAR_URL);
            appuser.setCreateTime(LocalDateTime.now());
            appuser.setDelFlag(false);
        }
        Integer chargePoint=appuser.getRechargePoint();//充值积分
        Integer availablePoint=appuser.getAvailablePoint();//可用积分
        Integer totalPoint=appuser.getTotalPoint();//总积分
        appuser.setRechargePoint(chargePoint==null?0:chargePoint +point);//充值积分增加
        appuser.setAvailablePoint(availablePoint==null?0:availablePoint+point);//可用积分增加
        appuser.setTotalPoint(totalPoint==null?0:totalPoint+point);//总积分增加
 
        Long userId = appUserClient.saveOrUpdateAppUser(appuser);
        if (userId==null){
            return R.fail("导入订单-保存用户失败");
        }
 
        //添加用户积分流水
        //转入用户
        UserPoint userPoint = new UserPoint();
        userPoint.setType(17);
        userPoint.setHistoricalPoint(availablePoint);
        userPoint.setVariablePoint(point);
        userPoint.setBalance(appuser.getAvailablePoint());
        userPoint.setCreateTime(LocalDateTime.now());
        userPoint.setAppUserId(userId);
        userPoint.setExtention(chargeOrder.getOrderNumber());
        R r = userPointClient.save(userPoint);
        if (r.isError(r)) {
            return R.fail("导入订单-保存用户积分流水失败");
        }
        //保存到数据库
        chargeOrder.setPoint(point);
        chargeOrderService.save(chargeOrder);
 
        return R.ok();
    }
 
    /**
     * 获取现金兑换的积分数
     * @param cash
     * @return
     */
    public Integer getPoint(BigDecimal cash){
        if (cash == null || cash.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("金额不能为null或负数");
        }
        // 获取积分兑换比例配置
        R<SysConfig> info = sysConfigClient.getInfo(8L);
        if (info == null || info.getData() == null) {
            throw new RuntimeException("获取积分兑换比例配置失败");
        }
        String configValue = info.getData().getConfigValue();
        if (StringUtils.isBlank(configValue)) {
            throw new RuntimeException("积分兑换比例配置值为空");
        }
        try {
            // 使用BigDecimal处理比例,避免精度问题
            BigDecimal ratio = new BigDecimal(configValue.trim());
            if (ratio.compareTo(BigDecimal.ZERO) <= 0) {
                throw new RuntimeException("积分兑换比例必须大于0");
            }
 
            // 计算积分并向下取整(Floor)
            BigDecimal points = cash.multiply(ratio);
            return points.setScale(0, RoundingMode.FLOOR).intValue();
 
        } catch (NumberFormatException e) {
            throw new RuntimeException("积分兑换比例配置值格式错误", e);
        } catch (ArithmeticException e) {
            throw new RuntimeException("积分计算结果溢出", e);
        }
 
    }
 
}