2
0

fix: 流量计费调整

This commit is contained in:
caiyuchao
2025-01-16 15:34:49 +08:00
parent 1550b78912
commit 6c5a4d0805
5 changed files with 93 additions and 7 deletions

View File

@@ -129,7 +129,7 @@ CREATE TABLE `u_bill_rule` (
-- ---------------------------- -- ----------------------------
-- Records of u_bill_rule -- Records of u_bill_rule
-- ---------------------------- -- ----------------------------
INSERT INTO `u_bill_rule` VALUES (1, 1.0000, 1, 1, 1, 0, NULL, NULL, 2, '2025-01-07 17:11:30'); INSERT INTO `u_bill_rule` VALUES (1, 1.0000, 1, 2, 1, 0, NULL, NULL, 2, '2025-01-07 17:11:30');
-- ---------------------------- -- ----------------------------
-- Table structure for u_cdr -- Table structure for u_cdr

View File

@@ -0,0 +1,36 @@
package org.wfc.user.domain.constant;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @description: 流量单位枚举
* @author: cyc
* @since: 2025-01-16
*/
@Getter
@AllArgsConstructor
public enum TratticUnitEnum {
B(0, "B"),
KB(1, "KB"),
MB(3, "MB"),
GB(4, "GB"),
TB(5, "TB"),
;
private final Integer code;
private final String desc;
public static TratticUnitEnum getByCode(Integer code) {
if (code == null) {
return null;
}
for (TratticUnitEnum tratticUnitEnum : TratticUnitEnum.values()) {
if (tratticUnitEnum.getCode().equals(code)) {
return tratticUnitEnum;
}
}
return null;
}
}

View File

@@ -3,7 +3,6 @@ package org.wfc.user.service.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -32,9 +31,9 @@ import org.wfc.user.mapper.UCdrMapper;
import org.wfc.user.mapper.UClientMapper; import org.wfc.user.mapper.UClientMapper;
import org.wfc.user.service.IUAccountService; import org.wfc.user.service.IUAccountService;
import org.wfc.user.util.AccountUtil; import org.wfc.user.util.AccountUtil;
import org.wfc.user.util.BillRuleUtil;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@@ -103,9 +102,8 @@ public class UAccountServiceImpl extends ServiceImpl<UAccountMapper, UAccount> i
if (ObjectUtil.isNull(currentTraffic)) { if (ObjectUtil.isNull(currentTraffic)) {
continue; continue;
} }
BigDecimal traffic = BigDecimal.valueOf(currentTraffic).divide(BigDecimal.valueOf(1048576), 4, RoundingMode.HALF_UP);
UAccount account = this.getOne(Wrappers.<UAccount>lambdaQuery().eq(UAccount::getUserId, balanceUserId), false); UAccount account = this.getOne(Wrappers.<UAccount>lambdaQuery().eq(UAccount::getUserId, balanceUserId), false);
account.setBalanceUsed(NumberUtil.mul(traffic, billRule.getPrice())); account.setBalanceUsed(BillRuleUtil.calc(billRule, currentTraffic));
if (ObjectUtil.isNull(account.getBalance())) { if (ObjectUtil.isNull(account.getBalance())) {
account.setBalance(BigDecimal.ZERO); account.setBalance(BigDecimal.ZERO);
} }
@@ -122,8 +120,7 @@ public class UAccountServiceImpl extends ServiceImpl<UAccountMapper, UAccount> i
bill.setUserId(historyVo.getUserId()); bill.setUserId(historyVo.getUserId());
bill.setCdrHistoryId(historyVo.getId()); bill.setCdrHistoryId(historyVo.getId());
bill.setStatus(1); bill.setStatus(1);
BigDecimal total = NumberUtil.div(NumberUtil.add(historyVo.getTrafficDown(), historyVo.getTrafficUp()), 1048576); bill.setAmount(BillRuleUtil.calc(billRule, historyVo.getTrafficDown() + historyVo.getTrafficUp()));
bill.setAmount(NumberUtil.mul(total, billRule.getPrice()));
billMapper.insert(bill); billMapper.insert(bill);
UAccount account = this.getOne(Wrappers.<UAccount>lambdaQuery().eq(UAccount::getUserId, historyVo.getUserId()), false); UAccount account = this.getOne(Wrappers.<UAccount>lambdaQuery().eq(UAccount::getUserId, historyVo.getUserId()), false);

View File

@@ -10,11 +10,13 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wfc.common.security.utils.SecurityUtils; import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.user.api.IWifiApi; import org.wfc.user.api.IWifiApi;
import org.wfc.user.api.omada.domain.convert.OmadaConvert; import org.wfc.user.api.omada.domain.convert.OmadaConvert;
import org.wfc.user.api.omada.domain.dto.ClientRateLimitSettingDto; import org.wfc.user.api.omada.domain.dto.ClientRateLimitSettingDto;
import org.wfc.user.domain.UAccount; import org.wfc.user.domain.UAccount;
import org.wfc.user.domain.UBill;
import org.wfc.user.domain.UBillRule; import org.wfc.user.domain.UBillRule;
import org.wfc.user.domain.UClient; import org.wfc.user.domain.UClient;
import org.wfc.user.domain.UOrder; import org.wfc.user.domain.UOrder;
@@ -23,6 +25,7 @@ import org.wfc.user.domain.URateLimit;
import org.wfc.user.domain.constant.OrderStatusEnum; import org.wfc.user.domain.constant.OrderStatusEnum;
import org.wfc.user.domain.constant.OrderTypeEnum; import org.wfc.user.domain.constant.OrderTypeEnum;
import org.wfc.user.domain.constant.PeriodTypeEnum; import org.wfc.user.domain.constant.PeriodTypeEnum;
import org.wfc.user.mapper.UBillMapper;
import org.wfc.user.mapper.UBillRuleMapper; import org.wfc.user.mapper.UBillRuleMapper;
import org.wfc.user.mapper.UOrderMapper; import org.wfc.user.mapper.UOrderMapper;
import org.wfc.user.service.IUAccountService; import org.wfc.user.service.IUAccountService;
@@ -60,6 +63,9 @@ public class UOrderServiceImpl extends ServiceImpl<UOrderMapper, UOrder> impleme
@Autowired @Autowired
private IUClientService clientService; private IUClientService clientService;
@Autowired
private UBillMapper billMapper;
@Autowired @Autowired
private UBillRuleMapper billRuleMapper; private UBillRuleMapper billRuleMapper;
@@ -67,6 +73,7 @@ public class UOrderServiceImpl extends ServiceImpl<UOrderMapper, UOrder> impleme
private IWifiApi wifiApi; private IWifiApi wifiApi;
@Override @Override
@Transactional(rollbackFor = Exception.class)
public boolean paySuccess(Long orderId) { public boolean paySuccess(Long orderId) {
// 支付成功回调(预留) // 支付成功回调(预留)
@@ -78,6 +85,14 @@ public class UOrderServiceImpl extends ServiceImpl<UOrderMapper, UOrder> impleme
order.setStatus(OrderStatusEnum.PAID.getCode()); order.setStatus(OrderStatusEnum.PAID.getCode());
this.updateById(order); this.updateById(order);
// 保存账单信息
UBill bill = new UBill();
bill.setAmount(order.getOrderAmount());
bill.setUserId(order.getUserId());
bill.setType(order.getType());
bill.setStatus(OrderStatusEnum.PAID.getCode());
billMapper.insert(bill);
// 保存或更新账户信息 // 保存或更新账户信息
UAccount account = accountService.getOne(Wrappers.<UAccount>lambdaQuery().eq(UAccount::getUserId, order.getUserId()), false); UAccount account = accountService.getOne(Wrappers.<UAccount>lambdaQuery().eq(UAccount::getUserId, order.getUserId()), false);
Long accountId = null; Long accountId = null;

View File

@@ -0,0 +1,38 @@
package org.wfc.user.util;
import org.wfc.user.domain.UBillRule;
import org.wfc.user.domain.constant.TratticUnitEnum;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* @description: 计费规则工具
* @author: cyc
* @since: 2025-01-16
*/
public class BillRuleUtil {
public static BigDecimal calc(UBillRule billRule, Long currentTraffic) {
long exchange = 1L;
switch (TratticUnitEnum.getByCode(billRule.getUnit())) {
case KB:
exchange = 1024L;
break;
case MB:
exchange = 1024*1024L;
break;
case GB:
exchange = 1024*1024*1024L;
break;
case TB:
exchange = 1024*1024*1024*1024L;
break;
default:
}
// 计算公式:金额 = 已用流量B * 价格 / 流量 / 换算值
return BigDecimal.valueOf(currentTraffic).multiply(billRule.getPrice())
.divide(BigDecimal.valueOf(billRule.getTraffic()), 8, RoundingMode.HALF_UP)
.divide(BigDecimal.valueOf(exchange), 4, RoundingMode.HALF_UP);
}
}