代码优化

This commit is contained in:
yanglei 2026-02-27 09:34:55 +08:00
parent bf49850b41
commit d68b914b92
3 changed files with 157 additions and 168 deletions

View File

@ -10,6 +10,7 @@ import com.cdzy.common.model.response.CommonStaffInfo;
import com.cdzy.common.model.response.JsonResult; import com.cdzy.common.model.response.JsonResult;
import com.cdzy.operations.model.dto.EbikeCloseOrderDto; import com.cdzy.operations.model.dto.EbikeCloseOrderDto;
import com.cdzy.operations.model.entity.EbikeEcuInfo; import com.cdzy.operations.model.entity.EbikeEcuInfo;
import com.cdzy.operations.model.vo.EbikeIsSuperAdminVo;
import com.cdzy.operations.service.EbikeEcuInfoService; import com.cdzy.operations.service.EbikeEcuInfoService;
import com.cdzy.operations.utils.RedisUtil; import com.cdzy.operations.utils.RedisUtil;
import com.ebike.feign.clients.UserFeignClient; import com.ebike.feign.clients.UserFeignClient;
@ -56,18 +57,10 @@ public class EbikeStatisticsController {
*/ */
@PostMapping("/getOrderStatistics") @PostMapping("/getOrderStatistics")
public JsonResult<?> getOrderStatistics(@RequestBody @Validated FeignEbikeOrderStatisticsDto dto) { public JsonResult<?> getOrderStatistics(@RequestBody @Validated FeignEbikeOrderStatisticsDto dto) {
long staffId = StpUtil.getLoginIdAsLong(); EbikeIsSuperAdminVo superAdmin = checkUserIsSuperAdmin();
CommonStaffInfo staffInfo = StpUtil.getSession().getModel(String.valueOf(staffId), CommonStaffInfo.class);
if (staffInfo == null) {
throw new EbikeException("当前用户信息不存在");
}
boolean isSuperAdmin = Optional.ofNullable(staffInfo.getRoles())
.orElse(Collections.emptyList())
.stream()
.anyMatch(CommonEbikeRole::getSysAdmin);
// 判断是否超级管理员 // 判断是否超级管理员
if (!isSuperAdmin) { if (!superAdmin.isSuperAdmin()) {
dto.setOperatorId(staffInfo.getOperatorId()); dto.setOperatorId(superAdmin.getOperatorId());
} }
JsonResult<?> jsonResult = userFeignClient.getOrderStatistics(dto); JsonResult<?> jsonResult = userFeignClient.getOrderStatistics(dto);
if (jsonResult.getCode() != Code.SUCCESS) { if (jsonResult.getCode() != Code.SUCCESS) {
@ -126,18 +119,10 @@ public class EbikeStatisticsController {
@GetMapping("getDiffOperatorOrderList") @GetMapping("getDiffOperatorOrderList")
public JsonResult<?> getDiffOperatorOrderList(String bikeCode, PageParam pageParam) { public JsonResult<?> getDiffOperatorOrderList(String bikeCode, PageParam pageParam) {
FeignDiffOperatorOrderList dto = new FeignDiffOperatorOrderList(); FeignDiffOperatorOrderList dto = new FeignDiffOperatorOrderList();
String staffId = StpUtil.getLoginIdAsString(); EbikeIsSuperAdminVo superAdmin = checkUserIsSuperAdmin();
CommonStaffInfo staffInfo = StpUtil.getSession().getModel(staffId, CommonStaffInfo.class);
if (staffInfo == null) {
throw new EbikeException("当前用户信息不存在");
}
boolean isSuperAdmin = Optional.ofNullable(staffInfo.getRoles())
.orElse(Collections.emptyList())
.stream()
.anyMatch(CommonEbikeRole::getSysAdmin);
// 判断是否超级管理员 // 判断是否超级管理员
if (!isSuperAdmin) { if (!superAdmin.isSuperAdmin()) {
dto.setOperatorId(staffInfo.getOperatorId()); dto.setOperatorId(superAdmin.getOperatorId());
} }
dto.setBikeCode(bikeCode); dto.setBikeCode(bikeCode);
dto.setPageParam(pageParam); dto.setPageParam(pageParam);
@ -147,4 +132,25 @@ public class EbikeStatisticsController {
} }
return JsonResult.success(jsonResult.getData()); return JsonResult.success(jsonResult.getData());
} }
/**
* 判断当前用户是否包含超级管理员角色
*
* @return ebikeIsSuperAdminVo
*/
private EbikeIsSuperAdminVo checkUserIsSuperAdmin() {
String staffId = StpUtil.getLoginIdAsString();
CommonStaffInfo staffInfo = StpUtil.getSession().getModel(staffId, CommonStaffInfo.class);
if (staffInfo == null) {
throw new EbikeException("当前用户信息不存在");
}
boolean isSuperAdmin = Optional.ofNullable(staffInfo.getRoles())
.orElse(Collections.emptyList())
.stream()
.anyMatch(CommonEbikeRole::getSysAdmin);
EbikeIsSuperAdminVo isSuperAdminVo = new EbikeIsSuperAdminVo();
isSuperAdminVo.setSuperAdmin(isSuperAdmin);
isSuperAdminVo.setOperatorId(staffInfo.getOperatorId());
return isSuperAdminVo;
}
} }

View File

@ -0,0 +1,29 @@
package com.cdzy.operations.model.vo;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
/**
* 用户是否是超级管理员参数
*
* @author yanglei
* @since 2026-02-27 09:17
*/
@Data
public class EbikeIsSuperAdminVo implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* 是否超级管理员
*/
private boolean isSuperAdmin;
/**
* 运营商id
*/
private Long operatorId;
}

View File

@ -168,34 +168,7 @@ public class EbikeOrderServiceImpl extends ServiceImpl<EbikeOrderMapper, EbikeOr
ebikeOrderDetailVo.setUserHelmetFee(BigDecimal.ZERO); ebikeOrderDetailVo.setUserHelmetFee(BigDecimal.ZERO);
//获取费用详情 //获取费用详情
List<EbikePaymentCostDetailVo> details = ebikeOrderDetailService.getOrderDetailsByOrderId(ebikeOrderDetailVo.getOrderId()); List<EbikePaymentCostDetailVo> details = ebikeOrderDetailService.getOrderDetailsByOrderId(ebikeOrderDetailVo.getOrderId());
if (!CollectionUtils.isEmpty(details)) { return getEbikeOrderDetailVo(ebikeOrderDetailVo, details);
for (EbikePaymentCostDetailVo payDetailVo : details) {
//1-时长费用 2-起步费用 3-运营区调度费用 4-停车区外调度费用 5-禁停区调度费用 6-头盔使用费用
switch (payDetailVo.getDetailType()) {
case 1 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setUserDurationFee((current != null ? current : BigDecimal.ZERO));
}
case 2 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setUserBaseFee((current != null ? current : BigDecimal.ZERO));
}
case 3, 4, 5 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setUserDispatchFee((current != null ? current : BigDecimal.ZERO));
}
case 6 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setUserHelmetFee((current != null ? current : BigDecimal.ZERO));
}
case 7 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setDiscountAmount((current != null ? current : BigDecimal.ZERO));
}
}
}
}
return ebikeOrderDetailVo;
} }
@ -276,50 +249,7 @@ public class EbikeOrderServiceImpl extends ServiceImpl<EbikeOrderMapper, EbikeOr
order.setEndTime(LocalDateTime.now()); order.setEndTime(LocalDateTime.now());
order.setEndLocation(endDto.getEndPoint()); order.setEndLocation(endDto.getEndPoint());
// 关锁 // 关锁
EbikeLockVo ebikeLockVo = new EbikeLockVo(); return processEndOrder(order, endDto.getBikeCode(), endDto.getEndPoint());
ebikeLockVo.setBikeCode(endDto.getBikeCode());
ebikeLockVo.setPoint(endDto.getEndPoint());
JsonResult<FeignEbikeUserLockDto> jsonResult = operationsFeignClient.lock(ebikeLockVo);
if (jsonResult.getCode() != Code.SUCCESS) {
throw new EbikeException("关锁失败:" + jsonResult.getMessage());
}
FeignEbikeUserLockDto data = jsonResult.getData();
// 订单费用计算
EbikeCostDetailDto ebikeCostDetail = costCalculation(order.getEndTime(), order, data);
// 获取总费用
BigDecimal totalAmount = ebikeCostDetail.getTotalCost();
// 费用为0不生成支付订单直接订单已支付返回订单id
if (totalAmount.compareTo(BigDecimal.ZERO) == 0) {
order.setPaymentTime(LocalDateTime.now());
order.setTotalAmount(BigDecimal.ZERO);
order.setActualAmount(BigDecimal.ZERO);
order.setOrderStatus(OrderStatus.PAID);
order.setIsFreeOrder(Boolean.TRUE);
updateById(order);
return order.getOrderId();
}
order.setOrderStatus(OrderStatus.PENDING_PAYMENT);
order.setTotalAmount(totalAmount);
order.setActualAmount(totalAmount);
order.setIsFreeOrder(Boolean.FALSE);
updateById(order);
//生成支付订单
EbikePayment payment = EbikePayment.builder()
.orderId(order.getOrderId())
.costPrice(order.getTotalAmount())
.paymentMethod(EbikePaymentMethod.WECHAT)
.tradeId(StringUtils.generateLongSnowflakeId("tradeId"))
.currency("CNY")
.userId(endDto.getUserId())
.createBy(endDto.getUserId())
.operatorId(order.getOperatorId())
.tradeStatus(EbikePaymentTradeStatus.NO_PAYMENT)
.build();
ebikePaymentService.save(payment);
// 保存计算订单详情
List<EbikeOrderDetail> orderDetails = buildOrderDetails(order, ebikeCostDetail);
ebikeOrderDetailService.saveBatch(orderDetails);
return order.getOrderId();
} }
@Override @Override
@ -379,36 +309,10 @@ public class EbikeOrderServiceImpl extends ServiceImpl<EbikeOrderMapper, EbikeOr
ebikeOrderDetailVo.setUserHelmetFee(BigDecimal.ZERO); ebikeOrderDetailVo.setUserHelmetFee(BigDecimal.ZERO);
//获取费用详情 //获取费用详情
List<EbikePaymentCostDetailVo> details = ebikeOrderDetailService.getOrderDetailsByOrderId(orderId); List<EbikePaymentCostDetailVo> details = ebikeOrderDetailService.getOrderDetailsByOrderId(orderId);
if (!CollectionUtils.isEmpty(details)) { return getEbikeOrderDetailVo(ebikeOrderDetailVo, details);
for (EbikePaymentCostDetailVo payDetailVo : details) {
//1-时长费用 2-起步费用 3-运营区调度费用 4-停车区外调度费用 5-禁停区调度费用 6-头盔使用费用
switch (payDetailVo.getDetailType()) {
case 1 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setUserDurationFee((current != null ? current : BigDecimal.ZERO));
}
case 2 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setUserBaseFee((current != null ? current : BigDecimal.ZERO));
}
case 3, 4, 5 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setUserDispatchFee((current != null ? current : BigDecimal.ZERO));
}
case 6 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setUserHelmetFee((current != null ? current : BigDecimal.ZERO));
}
case 7 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setDiscountAmount((current != null ? current : BigDecimal.ZERO));
}
}
}
}
return ebikeOrderDetailVo;
} }
@Override @Override
public FeignEbikeOrderStatisticsVo getOrderStatistics(FeignEbikeOrderStatisticsDto dto) { public FeignEbikeOrderStatisticsVo getOrderStatistics(FeignEbikeOrderStatisticsDto dto) {
TimeRangeEnum timeRange = TimeRangeEnum.fromCode(dto.getTimeRange()); TimeRangeEnum timeRange = TimeRangeEnum.fromCode(dto.getTimeRange());
@ -508,52 +412,7 @@ public class EbikeOrderServiceImpl extends ServiceImpl<EbikeOrderMapper, EbikeOr
order.setEndTime(LocalDateTime.now()); order.setEndTime(LocalDateTime.now());
order.setEndLocation(location); order.setEndLocation(location);
// 关锁 return processEndOrder(order, dto.getBikeCode(), location);
EbikeLockVo ebikeLockVo = new EbikeLockVo();
ebikeLockVo.setBikeCode(dto.getBikeCode());
ebikeLockVo.setPoint(location);
JsonResult<FeignEbikeUserLockDto> jsonResult = operationsFeignClient.lock(ebikeLockVo);
if (jsonResult.getCode() != Code.SUCCESS) {
throw new EbikeException("关锁失败:" + jsonResult.getMessage());
}
FeignEbikeUserLockDto data = jsonResult.getData();
// 订单费用计算
EbikeCostDetailDto ebikeCostDetail = costCalculation(order.getEndTime(), order, data);
// 获取总费用
BigDecimal totalAmount = ebikeCostDetail.getTotalCost();
// 费用为0不生成支付订单直接订单已支付返回订单id
if (totalAmount.compareTo(BigDecimal.ZERO) == 0) {
order.setPaymentTime(LocalDateTime.now());
order.setTotalAmount(BigDecimal.ZERO);
order.setActualAmount(BigDecimal.ZERO);
order.setOrderStatus(OrderStatus.PAID);
order.setIsFreeOrder(Boolean.TRUE);
updateById(order);
return order.getOrderId();
}
order.setOrderStatus(OrderStatus.PENDING_PAYMENT);
order.setTotalAmount(totalAmount);
order.setActualAmount(totalAmount);
order.setIsFreeOrder(Boolean.FALSE);
updateById(order);
//生成支付订单
EbikePayment payment = EbikePayment.builder()
.orderId(order.getOrderId())
.costPrice(order.getTotalAmount())
.paymentMethod(EbikePaymentMethod.WECHAT)
.tradeId(StringUtils.generateLongSnowflakeId("tradeId"))
.currency("CNY")
.userId(order.getUserId())
.createBy(order.getUserId())
.operatorId(order.getOperatorId())
.tradeStatus(EbikePaymentTradeStatus.NO_PAYMENT)
.build();
ebikePaymentService.save(payment);
// 保存计算订单详情
List<EbikeOrderDetail> orderDetails = buildOrderDetails(order, ebikeCostDetail);
ebikeOrderDetailService.saveBatch(orderDetails);
return order.getOrderId();
} }
@Override @Override
@ -757,4 +616,99 @@ public class EbikeOrderServiceImpl extends ServiceImpl<EbikeOrderMapper, EbikeOr
.where(EBIKE_ORDER.ORDER_STATUS.eq(OrderStatus.IN_PROGRESS).or(EBIKE_ORDER.ORDER_STATUS.eq(OrderStatus.PENDING_PAYMENT))); .where(EBIKE_ORDER.ORDER_STATUS.eq(OrderStatus.IN_PROGRESS).or(EBIKE_ORDER.ORDER_STATUS.eq(OrderStatus.PENDING_PAYMENT)));
return ebikeOrderTransactionMapper.selectOneByQuery(queryWrapper); return ebikeOrderTransactionMapper.selectOneByQuery(queryWrapper);
} }
/**
* 生成订单明细
*
* @param ebikeOrderDetailVo 用户订单参数
* @param details 费用明细
* @return 订单明细
*/
private EbikeOrderDetailVo getEbikeOrderDetailVo(EbikeOrderDetailVo ebikeOrderDetailVo, List<EbikePaymentCostDetailVo> details) {
if (!CollectionUtils.isEmpty(details)) {
for (EbikePaymentCostDetailVo payDetailVo : details) {
//1-时长费用 2-起步费用 3-运营区调度费用 4-停车区外调度费用 5-禁停区调度费用 6-头盔使用费用
switch (payDetailVo.getDetailType()) {
case 1 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setUserDurationFee((current != null ? current : BigDecimal.ZERO));
}
case 2 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setUserBaseFee((current != null ? current : BigDecimal.ZERO));
}
case 3, 4, 5 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setUserDispatchFee((current != null ? current : BigDecimal.ZERO));
}
case 6 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setUserHelmetFee((current != null ? current : BigDecimal.ZERO));
}
case 7 -> {
BigDecimal current = payDetailVo.getDetailAmount();
ebikeOrderDetailVo.setDiscountAmount((current != null ? current : BigDecimal.ZERO));
}
}
}
}
return ebikeOrderDetailVo;
}
/**
* 结束订单
*
* @param order 订单信息
* @param bikeCode 车辆编号
* @param endPoint 结束位置
* @return 订单id
*/
private Long processEndOrder(EbikeOrder order, String bikeCode, Point endPoint) {
// 关锁
EbikeLockVo ebikeLockVo = new EbikeLockVo();
ebikeLockVo.setBikeCode(bikeCode);
ebikeLockVo.setPoint(endPoint);
JsonResult<FeignEbikeUserLockDto> jsonResult = operationsFeignClient.lock(ebikeLockVo);
if (jsonResult.getCode() != Code.SUCCESS) {
throw new EbikeException("关锁失败:" + jsonResult.getMessage());
}
FeignEbikeUserLockDto data = jsonResult.getData();
// 订单费用计算
EbikeCostDetailDto ebikeCostDetail = costCalculation(order.getEndTime(), order, data);
// 获取总费用
BigDecimal totalAmount = ebikeCostDetail.getTotalCost();
// 费用为0不生成支付订单直接订单已支付返回订单id
if (totalAmount.compareTo(BigDecimal.ZERO) == 0) {
order.setPaymentTime(LocalDateTime.now());
order.setTotalAmount(BigDecimal.ZERO);
order.setActualAmount(BigDecimal.ZERO);
order.setOrderStatus(OrderStatus.PAID);
order.setIsFreeOrder(Boolean.TRUE);
updateById(order);
return order.getOrderId();
}
order.setOrderStatus(OrderStatus.PENDING_PAYMENT);
order.setTotalAmount(totalAmount);
order.setActualAmount(totalAmount);
order.setIsFreeOrder(Boolean.FALSE);
updateById(order);
//生成支付订单
EbikePayment payment = EbikePayment.builder()
.orderId(order.getOrderId())
.costPrice(order.getTotalAmount())
.paymentMethod(EbikePaymentMethod.WECHAT)
.tradeId(StringUtils.generateLongSnowflakeId("tradeId"))
.currency("CNY")
.userId(order.getUserId())
.createBy(order.getUserId())
.operatorId(order.getOperatorId())
.tradeStatus(EbikePaymentTradeStatus.NO_PAYMENT)
.build();
ebikePaymentService.save(payment);
// 保存计算订单详情
List<EbikeOrderDetail> orderDetails = buildOrderDetails(order, ebikeCostDetail);
ebikeOrderDetailService.saveBatch(orderDetails);
return order.getOrderId();
}
} }