临时提交

This commit is contained in:
磷叶 2025-05-20 17:12:30 +08:00
parent 6fd01f8529
commit 53184270de
15 changed files with 99 additions and 114 deletions

View File

@ -327,8 +327,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item.id}
</foreach>
and <include refid="checkAmount"/>
and bb.status in ('INVALID', 'VALID', 'WAIT_DIVIDE')
and bb.status = 'INVALID'
</update>
<update id="pay">

View File

@ -131,12 +131,10 @@ public interface BonusService {
* @param bstType 业务类型
* @param bstId 业务ID
* @param amount 退款金额
* @param payAmount 原分成总金额
* @param reason 退款原因
* @return 结果
*/
public boolean refundByBst(BonusBstType bstType, Long bstId, BigDecimal amount, BigDecimal payAmount,
String reason);
public boolean refundByBst(BonusBstType bstType, Long bstId, BigDecimal amount, String reason);
/**
* 预览设备分成信息

View File

@ -66,10 +66,8 @@ public class BonusConverterImpl implements BonusConverter {
// 获取基础分成列表
List<Bonus> result = toBaseBonusListPlatform(bo.getDevice(), bo.getPartners(), bo.getAreaJoin());
// 设置分成金额
// 设置分成信息
Order order = bo.getOrder();
BonusUtil.partAmount(result, order.getPayAmount());
ChannelVO channel = bo.getChannel();
// 设置其他基础数据

View File

@ -306,9 +306,8 @@ public class BonusServiceImpl implements BonusService {
}
@Override
public boolean refundByBst(BonusBstType bstType, Long bstId, BigDecimal refundAmount, BigDecimal payAmount,
String reason) {
if (bstType == null || bstId == null || refundAmount == null || payAmount == null) {
public boolean refundByBst(BonusBstType bstType, Long bstId, BigDecimal refundAmount, String reason) {
if (bstType == null || bstId == null || refundAmount == null) {
return false;
}
// 查询分成列表
@ -321,7 +320,7 @@ public class BonusServiceImpl implements BonusService {
}
// 退款
return this.refund(list, refundAmount, payAmount, reason);
return this.refund(list, refundAmount, reason);
}
/**
@ -329,20 +328,19 @@ public class BonusServiceImpl implements BonusService {
*
* @param bonusList
* @param refundAmount
* @param payAmount
* @param reason
* @return
*/
private boolean refund(List<BonusVO> bonusList, BigDecimal refundAmount, BigDecimal payAmount, String reason) {
private boolean refund(List<BonusVO> bonusList, BigDecimal refundAmount, String reason) {
if (CollectionUtils.isEmptyElement(bonusList)) {
return true;
}
if (refundAmount == null || payAmount == null) {
if (refundAmount == null) {
return false;
}
// 构建退款列表
List<Bonus> refundList = BonusUtil.buildRefundList(bonusList, refundAmount, payAmount);
List<Bonus> refundList = BonusUtil.buildRefundList(bonusList, refundAmount);
ServiceUtil.assertion(CollectionUtils.isEmptyElement(refundList), "退款金额分配出错");
// 执行退款逻辑

View File

@ -10,6 +10,7 @@ import com.ruoyi.bst.bonus.domain.Bonus;
import com.ruoyi.bst.bonus.domain.BonusVO;
import com.ruoyi.bst.bonus.domain.enums.BonusArrivalType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;
@ -60,10 +61,7 @@ public class BonusUtil {
if (remainingDiff.compareTo(BigDecimal.ZERO) > 0) {
// 处理正数误差少退了需要增加退款金额
// 计算当前分成方的剩余可退金额
BigDecimal availableRefund = originalBonus.getAmount()
.subtract(originalBonus.getRefundAmount() == null ? BigDecimal.ZERO
: originalBonus.getRefundAmount())
.subtract(refundBonus.getRefundAmount());
BigDecimal availableRefund = MathUtils.subtractDecimal(originalBonus.getAmount(), originalBonus.getRefundAmount(), refundBonus.getRefundAmount());
// 如果剩余可退金额足够承担误差
if (availableRefund.compareTo(BigDecimal.ZERO) > 0) {
@ -101,14 +99,10 @@ public class BonusUtil {
*
* @param bonusList 原始分成列表
* @param refundAmount 需要退款总金额
* @param payAmount 原支付金额
* @return 退款列表
*/
public static List<Bonus> buildRefundList(List<BonusVO> bonusList, BigDecimal refundAmount, BigDecimal payAmount) {
if (CollectionUtils.isEmptyElement(bonusList) || refundAmount == null || payAmount == null) {
return Collections.emptyList();
}
if (payAmount.compareTo(BigDecimal.ZERO) == 0) {
public static List<Bonus> buildRefundList(List<BonusVO> bonusList, BigDecimal refundAmount) {
if (CollectionUtils.isEmptyElement(bonusList) || refundAmount == null) {
return Collections.emptyList();
}
List<Bonus> refundList = new ArrayList<>();
@ -120,16 +114,20 @@ public class BonusUtil {
}
// 计算该分成记录的可退金额 = 分配金额 - 已退金额
BigDecimal availableRefund = bonus.getAmount()
.subtract(bonus.getRefundAmount() == null ? BigDecimal.ZERO : bonus.getRefundAmount());
BigDecimal availableRefund = MathUtils.subtractDecimal(bonus.getAmount(), bonus.getRefundAmount());
if (availableRefund.compareTo(BigDecimal.ZERO) <= 0) {
continue; // 跳过已无可退金额的记录
}
// 按原始比例计算本次应退金额 = 本次退款总额 * (原始分配金额/订单总额)
BigDecimal proportion = bonus.getAmount().divide(payAmount, 6, RoundingMode.HALF_UP);
// 根据分成比例计算本次应退金额
BigDecimal proportion = bonus.getPoint();
BigDecimal shouldRefund = refundAmount.multiply(proportion).setScale(2, RoundingMode.HALF_UP);
// 如果计算出的应退金额为0但原始分成金额大于0则使用剩余可退金额和退款金额中的较小值
if (shouldRefund.compareTo(BigDecimal.ZERO) == 0 && bonus.getAmount().compareTo(BigDecimal.ZERO) > 0) {
shouldRefund = availableRefund.min(refundAmount);
}
// 实际退款金额取可退金额与应退金额的较小值
BigDecimal actualRefund = shouldRefund.min(availableRefund);
if (actualRefund.compareTo(BigDecimal.ZERO) <= 0) {

View File

@ -529,15 +529,7 @@ public class DeviceIotServiceImpl implements DeviceIotService {
// 更新设备信息
DeviceUtil.setIotSysInfo(device, dto.getSys(), LocalDateTime.now(), true, DeviceLocationType.PHONE);
boolean lock = redisLock.lock(RedisLockKey.DEVICE_UPDATE_IOT_LOCK, device.getMac(), 60L);
if (lock) {
int update = this.updateIot(device);
if (update != 1) {
log.error("更新设备信息失败: {}", device.getMac());
}
} else {
log.info("设备{}更新太频繁,跳过本次更新", device.getMac());
}
this.updateIot(device);
return 1;
}

View File

@ -19,5 +19,8 @@ public class OrderSeatDTO {
@ApiModelProperty("手机纬度")
private BigDecimal lat;
@ApiModelProperty("物联网是否必须成功")
private Boolean requiredIot;
}

View File

@ -17,10 +17,6 @@ public class OrderVerifyDTO implements LogBizParam {
@ApiModelProperty("订单ID")
@NotNull(message = "订单ID不能为空")
private Long id;
@ApiModelProperty("是否通过")
@NotNull(message = "是否通过不能为空")
private Boolean pass;
@ApiModelProperty("审核备注")
@Size(max = 200, message = "审核备注不能超过200个字符")

View File

@ -46,7 +46,7 @@ public enum OrderStatus {
// 可以退款的订单状态
public static List<String> canRefund() {
return CollectionUtils.map(OrderStatus::getCode, FINISHED, REFUNDED, REJECTED, WAIT_VERIFY);
return CollectionUtils.map(OrderStatus::getCode, FINISHED, REFUNDED);
}
// 未支付的订单状态

View File

@ -71,7 +71,7 @@ public interface OrderMapper {
/**
* 根据查询条件更新订单
*
*
* @param data 更新数据
* @param query 查询条件
* @return 结果
@ -80,7 +80,7 @@ public interface OrderMapper {
/**
* 查询订单用户数量
*
*
* @param query 查询条件
* @return 结果
*/
@ -88,7 +88,7 @@ public interface OrderMapper {
/**
* 查询订单数量
*
*
* @param query 查询条件
* @return 结果
*/
@ -96,7 +96,7 @@ public interface OrderMapper {
/**
* 查询订单支付金额
*
*
* @param query 查询条件
* @return 结果
*/
@ -104,7 +104,7 @@ public interface OrderMapper {
/**
* 查询订单退款金额
*
*
* @param query 查询条件
* @return 结果
*/
@ -112,7 +112,7 @@ public interface OrderMapper {
/**
* 查询订单每日统计
*
*
* @param query 查询条件
* @param keys 查询字段
* @return 结果
@ -121,7 +121,7 @@ public interface OrderMapper {
/**
* 查询订单每日退款统计
*
*
* @param query 查询条件
* @param keys 查询字段
* @return 结果
@ -131,7 +131,7 @@ public interface OrderMapper {
/**
* 查询订单排行榜
*
*
* @param query 查询条件
* @return 结果
*/
@ -139,7 +139,7 @@ public interface OrderMapper {
/**
* 查询订单状态数量
*
*
* @param query 查询条件
* @return 结果
*/
@ -147,7 +147,7 @@ public interface OrderMapper {
/**
* 增加车损费
*
*
* @param id
* @param deductionFee
* @return
@ -156,24 +156,15 @@ public interface OrderMapper {
/**
* 根据查询条件查询订单ID列表
*
*
* @param query 查询条件
* @return 订单ID列表
*/
List<Long> selectIdByQuery(@Param("query") OrderQuery query);
/**
* 扣减实收金额
*
* @param id
* @param amount
* @return
*/
int subtractActualAmount(@Param("id") Long id, @Param("amount") BigDecimal amount);
/**
* 查询订单实收金额
*
*
* @param query 查询条件
* @return 结果
*/
@ -181,7 +172,7 @@ public interface OrderMapper {
/**
* 查询订单骑行费
*
*
* @param query 查询条件
* @return 结果
*/
@ -189,7 +180,7 @@ public interface OrderMapper {
/**
* 查询订单车损费
*
*
* @param query 查询条件
* @return 结果
*/
@ -197,7 +188,7 @@ public interface OrderMapper {
/**
* 查询订单调度费
*
*
* @param query 查询条件
* @return 结果
*/
@ -205,7 +196,7 @@ public interface OrderMapper {
/**
* 查询订单管理费
*
*
* @param query 查询条件
* @return 结果
*/
@ -213,7 +204,7 @@ public interface OrderMapper {
/**
* 查询订单总金额
*
*
* @param query 查询条件
* @return 结果
*/

View File

@ -581,27 +581,30 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</where>
</select>
<!-- deductDeposit -->
<!-- deductDeposit -->
<update id="deductDeposit">
update bst_order bo
left join bst_pay bp on bp.id = bo.pay_id
set bo.deposit_deduction_amount = bo.deposit_deduction_amount + #{amount}
where bo.id = #{id}
where bo.id = #{id}
and <include refid="depositCanDeductRemain"/> &gt;= #{amount}
</update>
<!-- 剩余可抵扣押金 = 已支付押金 - 退款中 - 已退款 - 已抵扣 - 车损费 -->
<!-- 剩余可抵扣押金 = 已支付押金 - 退款中 - 已退款 - 已抵扣 -->
<sql id="depositCanDeductRemain">
bp.amount - IFNULL(bp.refunding, 0) - IFNULL(bp.refunded, 0) - IFNULL(bo.deposit_deduction_amount, 0) - IFNULL(bo.actual_deduction_fee, 0)
bp.amount - IFNULL(bp.refunding, 0) - IFNULL(bp.refunded, 0) - IFNULL(bo.deposit_deduction_amount, 0)
</sql>
<!-- addDeductionFee -->
<update id="addDeductionFee">
update bst_order bo
left join bst_pay bp on bp.id = bo.pay_id
set bo.deduction_fee = bo.deduction_fee + #{deductionFee},
bo.actual_deduction_fee = bo.actual_deduction_fee + #{deductionFee},
bo.total_fee = bo.total_fee + #{deductionFee}
where bo.id = #{id}
bo.total_fee = bo.total_fee + #{deductionFee},
bo.actual_amount = bo.actual_amount + #{deductionFee},
bo.deposit_deduction_amount = bo.deposit_deduction_amount + #{deductionFee}
where bo.id = #{id}
and <include refid="depositCanDeductRemain"/> &gt;= #{deductionFee}
</update>

View File

@ -541,12 +541,16 @@ public class OrderServiceImpl implements OrderService {
// 处理订单完成
private void handleFinished(Long orderId) {
// 查询订单数据
OrderVO order = selectOrderById(orderId);
ServiceUtil.assertion(order == null, "ID为%s的订单完成失败", orderId);
// 预分成
boolean bonus = this.prepayBonus(orderId);
boolean bonus = this.prepayBonus(order);
ServiceUtil.assertion(!bonus, "ID为%s的订单预分成失败", orderId);
// 剩余金额退款
int refund = this.refundRemainAmount(orderId);
int refund = this.refundRemainAmount(order);
ServiceUtil.assertion(refund != 1, "ID为%s的订单退还剩余金额失败", orderId);
}
@ -560,27 +564,27 @@ public class OrderServiceImpl implements OrderService {
}
// 设置订单实收金额
private void setActualAmount(Order order, BigDecimal payAmount) {
private void setActualAmount(Order data, OrderVO old, BigDecimal payAmount) {
// 实收金额
BigDecimal remain = payAmount;
data.setActualAmount(payAmount);
// 骑行费实收
order.setActualRidingFee(MathUtils.min(remain, order.getRidingFee()));
remain = MathUtils.subtractDecimal(remain, order.getActualRidingFee());
data.setActualRidingFee(MathUtils.min(remain, old.getRidingFee()));
remain = MathUtils.subtractDecimal(remain, data.getActualRidingFee());
// 调度费实收
order.setActualDispatchFee(MathUtils.min(remain, order.getDispatchFee()));
remain = MathUtils.subtractDecimal(remain, order.getActualDispatchFee());
data.setActualDispatchFee(MathUtils.min(remain, old.getDispatchFee()));
remain = MathUtils.subtractDecimal(remain, data.getActualDispatchFee());
// 管理费实收
order.setActualManageFee(MathUtils.min(remain, order.getManageFee()));
data.setActualManageFee(MathUtils.min(remain, old.getManageFee()));
}
private int refundRemainAmount(Long orderId) {
OrderVO order = selectOrderById(orderId);
private int refundRemainAmount(OrderVO order) {
if (order == null) {
return 0;
}
// 订单剩余金额退款
// 退款金额 = 押金支付金额 - 押金抵扣金额 - 车损费
BigDecimal refund = MathUtils.subtractDecimal(order.getPayedAmount(), order.getDepositDeductionAmount(), order.getDeductionFee());
BigDecimal refund = MathUtils.subtractDecimal(order.getPayedAmount(), order.getDepositDeductionAmount(), order.getActualDeductionFee());
if (refund != null && refund.compareTo(BigDecimal.ZERO) > 0) {
return this.refund(order, refund, null, null, "系统", RefundType.AUTO.getCode());
}
@ -609,23 +613,13 @@ public class OrderServiceImpl implements OrderService {
canRefundAmount);
// 退款原因
String refundReason = null;
if (RefundType.ADMIN.getCode().equals(type)) {
refundReason = String.format("【管理员退款】%s订单%s退款 %s 元", userName, order.getNo(), amount);
} else {
refundReason = String.format("【自动退款】%s订单%s退款 %s 元", userName, order.getNo(), amount);
}
if (StringUtils.isNotBlank(reason)) {
refundReason += "" + reason;
}
String finalRefundReason = refundReason;
String refundReason = this.getRefundReason(reason, type, userName, order.getNo(), amount);
Integer result = transactionTemplate.execute(status -> {
// 分成退款
if (RefundType.ADMIN.getCode().equals(type)) {
boolean bonusRefund = bonusService.refundByBst(BonusBstType.ORDER, order.getId(), amount,
order.getPayAmount(), finalRefundReason);
boolean bonusRefund = bonusService.refundByBst(BonusBstType.ORDER, order.getId(), amount, refundReason);
ServiceUtil.assertion(!bonusRefund, "ID为%s的订单分成退款失败", order.getId());
}
@ -633,7 +627,7 @@ public class OrderServiceImpl implements OrderService {
PayRefundDTO dto = new PayRefundDTO();
dto.setId(order.getPayId());
dto.setRefundAmount(amount);
dto.setRefundReason(finalRefundReason);
dto.setRefundReason(refundReason);
dto.setUserId(userId);
dto.setUserName(userName);
dto.setType(type);
@ -644,6 +638,20 @@ public class OrderServiceImpl implements OrderService {
}
// 获取退款原因
private String getRefundReason(String reason, String type, String userName, String orderNo, BigDecimal amount) {
String refundReason = null;
if (RefundType.ADMIN.getCode().equals(type)) {
refundReason = String.format("【管理员退款】%s订单%s退款 %s 元", userName, orderNo, amount);
} else {
refundReason = String.format("【自动退款】%s订单%s退款 %s 元", userName, orderNo, amount);
}
if (StringUtils.isNotBlank(reason)) {
refundReason += "" + reason;
}
return refundReason;
}
@Override
public OrderFeeVO calcFee(OrderCalcFeeDTO dto) {
// 查询订单
@ -835,7 +843,6 @@ public class OrderServiceImpl implements OrderService {
ServiceUtil.assertion(!OrderStatus.canVerify().contains(order.getStatus()), "ID为%s的订单当前状态不允许审核", dto.getId());
// 更新订单状态
boolean pass = dto.getPass() != null && dto.getPass();
Integer result = transactionTemplate.execute(status -> {
// 更新订单状态
Order data = new Order();
@ -857,9 +864,7 @@ public class OrderServiceImpl implements OrderService {
}
// 订单结束操作
if (pass) {
this.handleFinished(order.getId());
}
this.handleFinished(order.getId());
return rows;
});
@ -868,12 +873,11 @@ public class OrderServiceImpl implements OrderService {
return result == null ? 0 : result;
}
private boolean prepayBonus(Long orderId) {
OrderVO order = selectOrderById(orderId);
private boolean prepayBonus(OrderVO order) {
if (order == null) {
return false;
}
return bonusService.prepayByBst(BonusBstType.ORDER, orderId, order.getActualAmount(), order.getEndTime());
return bonusService.prepayByBst(BonusBstType.ORDER, order.getId(), order.getActualAmount(), order.getEndTime());
}
@Override
@ -911,7 +915,8 @@ public class OrderServiceImpl implements OrderService {
// 异步使用手机定位更新设备定位
this.handleDeviceLocationAsync(device, dto.getLon(), dto.getLat());
return deviceIotService.unlockSeat(device, "订单打开坐垫锁:" + order.getNo(), false);
// 发送命令开锁
return deviceIotService.unlockSeat(device, "订单打开坐垫锁:" + order.getNo(), dto.getRequiredIot());
}
@Override
@ -967,7 +972,7 @@ public class OrderServiceImpl implements OrderService {
data.setId(order.getId());
data.setPayType(payType.getCode());
// 计算实收金额
this.setActualAmount(data, bo.getPayAmount());
this.setActualAmount(data, order, bo.getPayAmount());
// 根据是否需要审核设置订单状态
if (order.getAreaReturnVerify() != null && order.getAreaReturnVerify() && bo.getNeedVerify()) {
data.setStatus(OrderStatus.WAIT_VERIFY.getCode());
@ -996,8 +1001,6 @@ public class OrderServiceImpl implements OrderService {
return rows;
});
return result == null ? 0 : result;
}
}

View File

@ -141,7 +141,7 @@ public class OrderUtil {
vo.setMobileAreaSub(mobileAreaSub);
// 是否在停车点
boolean inParking = deviceAreaSub != null || mobileAreaSub != null;
vo.setInParking(inParking);
vo.setInParking(inParking);
// 实际停车点
if (deviceAreaSub != null) {
@ -177,6 +177,9 @@ public class OrderUtil {
return vo;
}
/**
* 计算可退款的押金金额
*/
public static BigDecimal calcCanRefundAmount(OrderVO order) {
if (order == null) {
return BigDecimal.ZERO;
@ -199,7 +202,7 @@ public class OrderUtil {
}
/**
* 剩余可抵扣押金
* 剩余可抵扣押金
* @param order
* @return
*/

View File

@ -45,7 +45,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
su.nick_name as order_user_name,
mch.nick_name as device_mch_name,
bm.name as device_model_name,
bm.enable_seat as device_model_eanble_seat
bm.enable_seat as device_model_enable_seat
from bst_order_device bod
left join bst_order bo on bo.id = bod.order_id
left join bst_device bd on bd.id = bod.device_id

View File

@ -225,6 +225,9 @@ public class AppOrderController extends BaseController {
OrderVO order = orderService.selectOrderById(dto.getOrderId());
ServiceUtil.assertion(!orderValidator.isUser(order, getUserId()), "您无权操作ID为%s的订单打开坐垫锁", order.getId());
if (dto.getRequiredIot() == null) {
dto.setRequiredIot(false);
}
return success(orderService.seat(dto));
}