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);
|
}
|
|
}
|
|
}
|