apps流水逻辑
This commit is contained in:
1 parent
0ff174e872
commit
15cc537639
10 files changed
+256
-232
No files matched your search
+11
-86
@@ -5,11 +5,9 @@ import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.enums.BusinessType;
|
||||
import com.ruoyi.common.select.Ids;
|
||||
import com.ruoyi.common.select.SelectItem;
|
||||
import com.ruoyi.common.utils.poi.ExcelUtil;
|
||||
import com.ruoyi.system.domain.AppsBalanceLedger;
|
||||
import com.ruoyi.system.domain.AppsBalanceLedgerPlus;
|
||||
import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery;
|
||||
import com.ruoyi.system.service.IAppsBalanceLedgerService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
@@ -29,34 +27,6 @@ public class AppsBalanceLedgerController extends BaseController
|
||||
{
|
||||
@Autowired
|
||||
private IAppsBalanceLedgerService appsBalanceLedgerService;
|
||||
@PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:getByIds')")
|
||||
@PostMapping("/getByIds")
|
||||
public AjaxResult getByIds(@RequestBody Ids ids)
|
||||
{
|
||||
List<AppsBalanceLedgerPlus> list = appsBalanceLedgerService.getByIds(ids.getLongIds(),false,AppsBalanceLedgerPlus::getAppId,AppsBalanceLedgerPlus::getId);
|
||||
return AjaxResult.success(list);
|
||||
}
|
||||
@PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:user-getByIds')")
|
||||
@PostMapping("/user/getByIds")
|
||||
public AjaxResult userGetByIds(@RequestBody Ids ids)
|
||||
{
|
||||
List<AppsBalanceLedgerPlus> list = appsBalanceLedgerService.getByIds(ids.getLongIds(),true,AppsBalanceLedgerPlus::getUserId,AppsBalanceLedgerPlus::getAppId,AppsBalanceLedgerPlus::getId);
|
||||
return AjaxResult.success(list);
|
||||
}
|
||||
@PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:select')")
|
||||
@GetMapping("/select")
|
||||
public AjaxResult select(String keyword)
|
||||
{
|
||||
List<SelectItem> list = appsBalanceLedgerService.select(keyword,false);
|
||||
return AjaxResult.success(list);
|
||||
}
|
||||
@PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:user-select')")
|
||||
@GetMapping("/user/select")
|
||||
public AjaxResult userSelect(String keyword)
|
||||
{
|
||||
List<SelectItem> list = appsBalanceLedgerService.select(keyword,true);
|
||||
return AjaxResult.success(list);
|
||||
}
|
||||
/**
|
||||
* 查询应用账面余额流水列表
|
||||
*/
|
||||
@@ -110,61 +80,16 @@ public class AppsBalanceLedgerController extends BaseController
|
||||
{
|
||||
return AjaxResult.success(appsBalanceLedgerService.selectAppsBalanceLedgerById(id,true));
|
||||
}
|
||||
/**
|
||||
* 新增应用账面余额流水
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:add')")
|
||||
@Log(title = "应用账面余额流水", businessType = BusinessType.INSERT)
|
||||
@PostMapping
|
||||
public AjaxResult add(@RequestBody AppsBalanceLedger appsBalanceLedger)
|
||||
{
|
||||
return toAjax(appsBalanceLedgerService.insertAppsBalanceLedger(appsBalanceLedger,false));
|
||||
|
||||
@PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:list')")
|
||||
@GetMapping("/statistics")
|
||||
public AjaxResult statistics(AppsLedgerStatisticsQuery query) {
|
||||
return AjaxResult.success(appsBalanceLedgerService.statistics(query, false));
|
||||
}
|
||||
/**
|
||||
* 新增应用账面余额流水
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:user-add')")
|
||||
@Log(title = "应用账面余额流水", businessType = BusinessType.INSERT)
|
||||
@PostMapping("/user")
|
||||
public AjaxResult userAdd(@RequestBody AppsBalanceLedger appsBalanceLedger)
|
||||
{
|
||||
return toAjax(appsBalanceLedgerService.insertAppsBalanceLedger(appsBalanceLedger,true));
|
||||
}
|
||||
/**
|
||||
* 修改应用账面余额流水
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:edit')")
|
||||
@Log(title = "应用账面余额流水", businessType = BusinessType.UPDATE)
|
||||
@PutMapping
|
||||
public AjaxResult edit(@RequestBody AppsBalanceLedger appsBalanceLedger)
|
||||
{
|
||||
return toAjax(appsBalanceLedgerService.updateAppsBalanceLedger(appsBalanceLedger,false));
|
||||
}
|
||||
@PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:user-edit')")
|
||||
@Log(title = "应用账面余额流水", businessType = BusinessType.UPDATE)
|
||||
@PutMapping("/user")
|
||||
public AjaxResult userEdit(@RequestBody AppsBalanceLedger appsBalanceLedger)
|
||||
{
|
||||
return toAjax(appsBalanceLedgerService.updateAppsBalanceLedger(appsBalanceLedger,true));
|
||||
}
|
||||
/**
|
||||
* 删除应用账面余额流水
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:remove')")
|
||||
@Log(title = "应用账面余额流水", businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/{ids}")
|
||||
public AjaxResult remove(@PathVariable Long[] ids)
|
||||
{
|
||||
return toAjax(appsBalanceLedgerService.deleteAppsBalanceLedgerByIds(ids,false));
|
||||
}
|
||||
/**
|
||||
* 删除应用账面余额流水
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:user-remove')")
|
||||
@Log(title = "应用账面余额流水", businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/user/{ids}")
|
||||
public AjaxResult userRemove(@PathVariable Long[] ids)
|
||||
{
|
||||
return toAjax(appsBalanceLedgerService.deleteAppsBalanceLedgerByIds(ids,true));
|
||||
|
||||
@PreAuthorize("@ss.hasPermi('system:appsBalanceLedger:user-list')")
|
||||
@GetMapping("/user/statistics")
|
||||
public AjaxResult userStatistics(AppsLedgerStatisticsQuery query) {
|
||||
return AjaxResult.success(appsBalanceLedgerService.statistics(query, true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.ruoyi.system.domain.vo;
|
||||
|
||||
import java.util.Date;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
/** 应用余额流水统计查询条件。 */
|
||||
public class AppsLedgerStatisticsQuery {
|
||||
private Long appId;
|
||||
private Long userId;
|
||||
private String currency;
|
||||
private String businessType;
|
||||
private String granularity;
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date beginTime;
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date endTime;
|
||||
|
||||
public Long getAppId() { return appId; }
|
||||
public void setAppId(Long appId) { this.appId = appId; }
|
||||
public Long getUserId() { return userId; }
|
||||
public void setUserId(Long userId) { this.userId = userId; }
|
||||
public String getCurrency() { return currency; }
|
||||
public void setCurrency(String currency) { this.currency = currency; }
|
||||
public String getBusinessType() { return businessType; }
|
||||
public void setBusinessType(String businessType) { this.businessType = businessType; }
|
||||
public String getGranularity() { return granularity; }
|
||||
public void setGranularity(String granularity) { this.granularity = granularity; }
|
||||
public Date getBeginTime() { return beginTime; }
|
||||
public void setBeginTime(Date beginTime) { this.beginTime = beginTime; }
|
||||
public Date getEndTime() { return endTime; }
|
||||
public void setEndTime(Date endTime) { this.endTime = endTime; }
|
||||
}
|
||||
@@ -6,6 +6,9 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import com.ruoyi.system.domain.AppsBalanceLedger;
|
||||
import com.ruoyi.system.domain.AppsBalanceLedgerPlus;
|
||||
import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery;
|
||||
import com.ruoyi.system.domain.vo.WalletLedgerSummaryVo;
|
||||
import com.ruoyi.system.domain.vo.WalletLedgerTrendVo;
|
||||
|
||||
/**
|
||||
* 应用账面余额流水Mapper接口
|
||||
@@ -31,6 +34,13 @@ public interface AppsBalanceLedgerMapper extends BaseMapper<AppsBalanceLedgerPlu
|
||||
*/
|
||||
public List<AppsBalanceLedger> selectAppsBalanceLedgerList(AppsBalanceLedger appsBalanceLedger);
|
||||
|
||||
/** 系统业务幂等写入,重复幂等键不生成新流水。 */
|
||||
int insertSystemLedgerIgnoreDuplicate(AppsBalanceLedger ledger);
|
||||
|
||||
List<WalletLedgerSummaryVo> selectStatisticsSummary(AppsLedgerStatisticsQuery query);
|
||||
|
||||
List<WalletLedgerTrendVo> selectStatisticsTrend(AppsLedgerStatisticsQuery query);
|
||||
|
||||
/**
|
||||
* 新增应用账面余额流水
|
||||
*
|
||||
|
||||
@@ -24,6 +24,12 @@ public interface AppsMapper extends BaseMapper<AppsPlus>
|
||||
/** 锁定应用,串行化同一应用的钱包领取。 */
|
||||
Apps selectAppsByIdForUpdate(@Param("id") Long id);
|
||||
|
||||
/** 订单结算时仅更新应用可用余额,冻结余额不参与变动。 */
|
||||
int updateAvailableBalances(@Param("id") Long id,
|
||||
@Param("usdtBalance") java.math.BigDecimal usdtBalance,
|
||||
@Param("trxBalance") java.math.BigDecimal trxBalance,
|
||||
@Param("updatedAt") java.util.Date updatedAt);
|
||||
|
||||
int updateKeyPair(@Param("id") Long id, @Param("publicKey") String publicKey,
|
||||
@Param("privateKey") String privateKey, @Param("updatedAt") java.util.Date updatedAt);
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.ruoyi.common.select.SelectItem;
|
||||
import com.ruoyi.system.domain.AppsBalanceLedger;
|
||||
import com.ruoyi.system.domain.AppsBalanceLedgerPlus;
|
||||
import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery;
|
||||
import com.ruoyi.system.domain.vo.WalletLedgerStatisticsVo;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -40,31 +42,7 @@ public interface IAppsBalanceLedgerService extends IService<AppsBalanceLedgerPlu
|
||||
* @param appsBalanceLedger 应用账面余额流水
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertAppsBalanceLedger(AppsBalanceLedger appsBalanceLedger,boolean isUser);
|
||||
|
||||
/**
|
||||
* 修改应用账面余额流水
|
||||
*
|
||||
* @param appsBalanceLedger 应用账面余额流水
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateAppsBalanceLedger(AppsBalanceLedger appsBalanceLedger,boolean isUser);
|
||||
|
||||
/**
|
||||
* 批量删除应用账面余额流水
|
||||
*
|
||||
* @param ids 需要删除的应用账面余额流水ID
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteAppsBalanceLedgerByIds(Long[] ids,boolean isUser);
|
||||
|
||||
/**
|
||||
* 删除应用账面余额流水信息
|
||||
*
|
||||
* @param id 应用账面余额流水ID
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteAppsBalanceLedgerById(Long id,boolean isUser);
|
||||
WalletLedgerStatisticsVo statistics(AppsLedgerStatisticsQuery query, boolean isUser);
|
||||
|
||||
List<AppsBalanceLedgerPlus> getByIds(List<Long> ids,boolean isUser, SFunction<AppsBalanceLedgerPlus, ?>... columns);
|
||||
|
||||
@@ -76,16 +54,4 @@ public interface IAppsBalanceLedgerService extends IService<AppsBalanceLedgerPlu
|
||||
|
||||
Map<Long, AppsBalanceLedgerPlus> getMapByIds(List<Long> ids);
|
||||
|
||||
/**
|
||||
* @param field sum 字段
|
||||
* @param where 条件
|
||||
* @param tClass 返回类型
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
<T> T sum(String field, String where, Class<T> tClass);
|
||||
|
||||
Long count(String where);
|
||||
|
||||
<T> List<T> countByGroupBy(String select, String where, String groupby, Class<T> tClass);
|
||||
}
|
||||
+88
-1
@@ -3,15 +3,22 @@ package com.ruoyi.system.service;
|
||||
import com.ruoyi.common.exception.CustomException;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.system.domain.UsdtAggregationTaskItemPlus;
|
||||
import com.ruoyi.system.domain.Apps;
|
||||
import com.ruoyi.system.domain.AppsBalanceLedger;
|
||||
import com.ruoyi.system.domain.UsdtConfig;
|
||||
import com.ruoyi.system.domain.UsdtOrdersPlus;
|
||||
import com.ruoyi.system.domain.WalletAddressLedger;
|
||||
import com.ruoyi.system.domain.WalletAddressPlus;
|
||||
import com.ruoyi.system.mapper.UsdtOrdersMapper;
|
||||
import com.ruoyi.system.mapper.AppsBalanceLedgerMapper;
|
||||
import com.ruoyi.system.mapper.AppsMapper;
|
||||
import com.ruoyi.system.mapper.UsdtConfigMapper;
|
||||
import com.ruoyi.system.mapper.WalletAddressLedgerMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Date;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 系统业务流水入账服务。
|
||||
@@ -26,15 +33,24 @@ public class WalletLedgerPostingService {
|
||||
private final WalletAddressLedgerMapper ledgerMapper;
|
||||
private final IWalletAddressService walletService;
|
||||
private final IUsdtAggregationTaskItemService aggregationItemService;
|
||||
private final AppsMapper appsMapper;
|
||||
private final AppsBalanceLedgerMapper appsLedgerMapper;
|
||||
private final UsdtConfigMapper configMapper;
|
||||
|
||||
public WalletLedgerPostingService(UsdtOrdersMapper ordersMapper,
|
||||
WalletAddressLedgerMapper ledgerMapper,
|
||||
IWalletAddressService walletService,
|
||||
IUsdtAggregationTaskItemService aggregationItemService) {
|
||||
IUsdtAggregationTaskItemService aggregationItemService,
|
||||
AppsMapper appsMapper,
|
||||
AppsBalanceLedgerMapper appsLedgerMapper,
|
||||
UsdtConfigMapper configMapper) {
|
||||
this.ordersMapper = ordersMapper;
|
||||
this.ledgerMapper = ledgerMapper;
|
||||
this.walletService = walletService;
|
||||
this.aggregationItemService = aggregationItemService;
|
||||
this.appsMapper = appsMapper;
|
||||
this.appsLedgerMapper = appsLedgerMapper;
|
||||
this.configMapper = configMapper;
|
||||
}
|
||||
|
||||
/** 将待支付订单结算为成功,同一订单重复扫描不会重复产生流水。 */
|
||||
@@ -74,9 +90,80 @@ public class WalletLedgerPostingService {
|
||||
ledger.setSourceType("ORDER");
|
||||
ledger.setRemark("订单支付成功");
|
||||
ledgerMapper.insertSystemLedgerIgnoreDuplicate(ledger);
|
||||
|
||||
postOrderToAppBalance(order, transactionId, now);
|
||||
return ordersMapper.markOrderPaid(order.getId(), transactionId, now) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 代收订单增加应用 USDT 可用余额,并扣减配置的 TRX 手续费。
|
||||
* 冻结余额仅写入流水快照,不参与本次余额计算。
|
||||
*/
|
||||
private void postOrderToAppBalance(UsdtOrdersPlus order, String transactionId, Date now) {
|
||||
Apps app = appsMapper.selectAppsByIdForUpdate(order.getAppId());
|
||||
if (app == null) {
|
||||
throw new CustomException("订单所属应用不存在");
|
||||
}
|
||||
UsdtConfig config = configMapper.selectFirstForUpdate();
|
||||
if (config == null || config.getReceiveUsdtNum() == null) {
|
||||
throw new CustomException("全局配置未设置订单代收手续费");
|
||||
}
|
||||
BigDecimal fee = config.getReceiveUsdtNum();
|
||||
if (fee.compareTo(BigDecimal.ZERO) < 0) {
|
||||
throw new CustomException("订单代收手续费不能小于0");
|
||||
}
|
||||
|
||||
BigDecimal usdtBefore = zeroIfNull(app.getUsdtBalance());
|
||||
BigDecimal trxBefore = zeroIfNull(app.getTrxBalance());
|
||||
BigDecimal usdtAfter = usdtBefore.add(order.getActualAmount());
|
||||
BigDecimal trxAfter = trxBefore.subtract(fee);
|
||||
BigDecimal usdtFrozen = zeroIfNull(app.getUsdtFrozenBalance());
|
||||
BigDecimal trxFrozen = zeroIfNull(app.getTrxFrozenBalance());
|
||||
|
||||
appsLedgerMapper.insertSystemLedgerIgnoreDuplicate(buildAppLedger(order, transactionId,
|
||||
"ORDER_RECEIVE", "USDT", 1, order.getActualAmount(), usdtBefore, usdtAfter,
|
||||
usdtFrozen, "订单代收到账", now, app.getUserId()));
|
||||
appsLedgerMapper.insertSystemLedgerIgnoreDuplicate(buildAppLedger(order, transactionId,
|
||||
"ORDER_FEE", "TRX", 2, fee, trxBefore, trxAfter,
|
||||
trxFrozen, "订单代收手续费", now, app.getUserId()));
|
||||
|
||||
if (appsMapper.updateAvailableBalances(app.getId(), usdtAfter, trxAfter, now) != 1) {
|
||||
throw new CustomException("应用余额更新失败");
|
||||
}
|
||||
}
|
||||
|
||||
private AppsBalanceLedger buildAppLedger(UsdtOrdersPlus order, String transactionId,
|
||||
String businessType, String currency, int direction, BigDecimal amount,
|
||||
BigDecimal before, BigDecimal after, BigDecimal frozen,
|
||||
String remark, Date now, Long userId) {
|
||||
AppsBalanceLedger ledger = new AppsBalanceLedger();
|
||||
ledger.setTradeNo(order.getTradeId());
|
||||
ledger.setIdempotencyKey(businessType + ":" + order.getId() + ":" + currency);
|
||||
ledger.setAppId(order.getAppId());
|
||||
ledger.setUserId(userId);
|
||||
ledger.setCurrency(currency);
|
||||
ledger.setBalanceType(1);
|
||||
ledger.setBusinessType(businessType);
|
||||
ledger.setDirection(direction);
|
||||
ledger.setAmount(amount);
|
||||
ledger.setBalanceBefore(before);
|
||||
ledger.setBalanceAfter(after);
|
||||
ledger.setAvailableBefore(before);
|
||||
ledger.setAvailableAfter(after);
|
||||
ledger.setFrozenBefore(frozen);
|
||||
ledger.setFrozenAfter(frozen);
|
||||
ledger.setOrderId(order.getId());
|
||||
ledger.setMerchantOrderId(order.getOrderId());
|
||||
ledger.setTransactionId(transactionId);
|
||||
ledger.setRemark(remark);
|
||||
ledger.setCreatedAt(now);
|
||||
return ledger;
|
||||
}
|
||||
|
||||
private BigDecimal zeroIfNull(BigDecimal value) {
|
||||
return value == null ? BigDecimal.ZERO : value;
|
||||
}
|
||||
|
||||
/** 将已广播成功的归集明细与对应支出流水原子落库。 */
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void completeAggregationItem(UsdtAggregationTaskItemPlus item,
|
||||
|
||||
+29
-107
@@ -1,19 +1,18 @@
|
||||
package com.ruoyi.system.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.ruoyi.common.select.SelectItem;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.common.exception.CustomException;
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.system.domain.AppsBalanceLedger;
|
||||
import com.ruoyi.system.domain.AppsBalanceLedgerPlus;
|
||||
import com.ruoyi.system.domain.vo.AppsLedgerStatisticsQuery;
|
||||
import com.ruoyi.system.domain.vo.WalletLedgerStatisticsVo;
|
||||
import com.ruoyi.system.mapper.AppsBalanceLedgerMapper;
|
||||
import com.ruoyi.system.service.IAppsBalanceLedgerService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
@@ -28,8 +27,6 @@ public class AppsBalanceLedgerServiceImpl extends ServiceImpl<AppsBalanceLedgerM
|
||||
{
|
||||
@Autowired
|
||||
private AppsBalanceLedgerMapper appsBalanceLedgerMapper;
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Override
|
||||
public List<AppsBalanceLedgerPlus> getByIds(List<Long> ids ,boolean isUser, SFunction<AppsBalanceLedgerPlus, ?>... columns) {
|
||||
@@ -122,48 +119,32 @@ public class AppsBalanceLedgerServiceImpl extends ServiceImpl<AppsBalanceLedgerM
|
||||
return this.list(lambdaQueryWrapper);
|
||||
}
|
||||
|
||||
public <T> T sum(String field, String where, Class<T> tClass) {
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
stringBuffer.append("select ");
|
||||
stringBuffer.append(" sum(" + field + ") as total ");
|
||||
stringBuffer.append(" from apps_balance_ledger ");
|
||||
if (StringUtils.isNotBlank(where)) {
|
||||
stringBuffer.append(" where ");
|
||||
stringBuffer.append(where);
|
||||
@Override
|
||||
public WalletLedgerStatisticsVo statistics(AppsLedgerStatisticsQuery query, boolean isUser) {
|
||||
AppsLedgerStatisticsQuery safeQuery = query == null ? new AppsLedgerStatisticsQuery() : query;
|
||||
String granularity = safeQuery.getGranularity();
|
||||
if (granularity == null || granularity.trim().isEmpty()) {
|
||||
safeQuery.setGranularity("DAY");
|
||||
} else {
|
||||
granularity = granularity.trim().toUpperCase(Locale.ENGLISH);
|
||||
if (!Arrays.asList("DAY", "MONTH", "YEAR").contains(granularity)) {
|
||||
throw new CustomException("统计粒度仅支持 DAY、MONTH、YEAR");
|
||||
}
|
||||
safeQuery.setGranularity(granularity);
|
||||
}
|
||||
T result = jdbcTemplate.queryForObject(stringBuffer.toString(), tClass);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Long count(String where) {
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
stringBuffer.append("select ");
|
||||
stringBuffer.append(" count(1)");
|
||||
stringBuffer.append(" from ");
|
||||
stringBuffer.append(" apps_balance_ledger ");
|
||||
if (StringUtils.isNotBlank(where)) {
|
||||
stringBuffer.append(" where ");
|
||||
stringBuffer.append(where);
|
||||
if (safeQuery.getCurrency() != null && !safeQuery.getCurrency().trim().isEmpty()) {
|
||||
String currency = safeQuery.getCurrency().trim().toUpperCase(Locale.ENGLISH);
|
||||
if (!Arrays.asList("USDT", "TRX").contains(currency)) {
|
||||
throw new CustomException("币种仅支持 USDT、TRX");
|
||||
}
|
||||
safeQuery.setCurrency(currency);
|
||||
}
|
||||
Long result = jdbcTemplate.queryForObject(stringBuffer.toString(), Long.class);
|
||||
return result;
|
||||
}
|
||||
|
||||
public <T> List<T> countByGroupBy(String select, String where, String groupby, Class<T> tClass) {
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
stringBuffer.append("select ");
|
||||
stringBuffer.append(select);
|
||||
stringBuffer.append(" from ");
|
||||
stringBuffer.append(" apps_balance_ledger ");
|
||||
if (StringUtils.isNotBlank(where)) {
|
||||
stringBuffer.append(" where ");
|
||||
stringBuffer.append(where);
|
||||
if (isUser) {
|
||||
safeQuery.setUserId(SecurityUtils.getUserId());
|
||||
}
|
||||
stringBuffer.append(" group by ");
|
||||
stringBuffer.append(groupby);
|
||||
List result = jdbcTemplate.queryForList(stringBuffer.toString());
|
||||
List<T> tList = JSONArray.parseArray(JSONArray.toJSONString(result), tClass);
|
||||
return tList;
|
||||
return new WalletLedgerStatisticsVo(
|
||||
appsBalanceLedgerMapper.selectStatisticsSummary(safeQuery),
|
||||
appsBalanceLedgerMapper.selectStatisticsTrend(safeQuery));
|
||||
}
|
||||
/**
|
||||
* 查询应用账面余额流水
|
||||
@@ -175,6 +156,9 @@ public class AppsBalanceLedgerServiceImpl extends ServiceImpl<AppsBalanceLedgerM
|
||||
public AppsBalanceLedger selectAppsBalanceLedgerById(Long id,boolean isUser)
|
||||
{
|
||||
AppsBalanceLedger vo = appsBalanceLedgerMapper.selectAppsBalanceLedgerById(id);
|
||||
if (vo == null) {
|
||||
throw new CustomException("应用流水不存在");
|
||||
}
|
||||
if(isUser){
|
||||
checkUser(vo);
|
||||
}
|
||||
@@ -203,66 +187,4 @@ public class AppsBalanceLedgerServiceImpl extends ServiceImpl<AppsBalanceLedgerM
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增应用账面余额流水
|
||||
*
|
||||
* @param appsBalanceLedger 应用账面余额流水
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int insertAppsBalanceLedger(AppsBalanceLedger appsBalanceLedger,boolean isUser)
|
||||
{
|
||||
appsBalanceLedger.setCreatedAt(DateUtils.getNowDate());
|
||||
if(isUser){
|
||||
appsBalanceLedger.setUserId(SecurityUtils.getUserId());
|
||||
}
|
||||
return appsBalanceLedgerMapper.insertAppsBalanceLedger(appsBalanceLedger);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改应用账面余额流水
|
||||
*
|
||||
* @param appsBalanceLedger 应用账面余额流水
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int updateAppsBalanceLedger(AppsBalanceLedger appsBalanceLedger,boolean isUser)
|
||||
{
|
||||
if(isUser){
|
||||
checkUser(appsBalanceLedger);
|
||||
}
|
||||
return appsBalanceLedgerMapper.updateAppsBalanceLedger(appsBalanceLedger);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除应用账面余额流水
|
||||
*
|
||||
* @param ids 需要删除的应用账面余额流水ID
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteAppsBalanceLedgerByIds(Long[] ids,boolean isUser)
|
||||
{
|
||||
if(isUser){
|
||||
List<AppsBalanceLedgerPlus> list= getByIds(Arrays.asList(ids),false,AppsBalanceLedgerPlus::getUserId,AppsBalanceLedgerPlus::getId);
|
||||
checkUserPlus(list);
|
||||
}
|
||||
return appsBalanceLedgerMapper.deleteAppsBalanceLedgerByIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除应用账面余额流水信息
|
||||
*
|
||||
* @param id 应用账面余额流水ID
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteAppsBalanceLedgerById(Long id,boolean isUser)
|
||||
{
|
||||
if(isUser){
|
||||
AppsBalanceLedgerPlus vo = getById(id,AppsBalanceLedgerPlus::getUserId,AppsBalanceLedgerPlus::getId);
|
||||
checkUser(vo);
|
||||
}
|
||||
return appsBalanceLedgerMapper.deleteAppsBalanceLedgerById(id);
|
||||
}
|
||||
}
|
||||
@@ -62,7 +62,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
<if test="operatorId != null "> and operator_id = #{operatorId}</if>
|
||||
<if test="requestId != null and requestId != ''"> and request_id = #{requestId}</if>
|
||||
<if test="createdAt != null "> and created_at = #{createdAt}</if>
|
||||
<if test="params.beginTime != null and params.beginTime != ''">and created_at >= #{params.beginTime}</if>
|
||||
<if test="params.endTime != null and params.endTime != ''">and created_at <= #{params.endTime}</if>
|
||||
</where>
|
||||
order by created_at desc, id desc
|
||||
</select>
|
||||
|
||||
<select id="selectAppsBalanceLedgerById" parameterType="Long" resultMap="AppsBalanceLedgerResult">
|
||||
@@ -126,6 +129,60 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<insert id="insertSystemLedgerIgnoreDuplicate" parameterType="AppsBalanceLedger">
|
||||
insert into apps_balance_ledger
|
||||
(trade_no, idempotency_key, app_id, user_id, currency, balance_type,
|
||||
business_type, direction, amount, balance_before, balance_after,
|
||||
available_before, available_after, frozen_before, frozen_after,
|
||||
order_id, merchant_order_id, transaction_id, remark, created_at)
|
||||
values
|
||||
(#{tradeNo}, #{idempotencyKey}, #{appId}, #{userId}, #{currency}, #{balanceType},
|
||||
#{businessType}, #{direction}, #{amount}, #{balanceBefore}, #{balanceAfter},
|
||||
#{availableBefore}, #{availableAfter}, #{frozenBefore}, #{frozenAfter},
|
||||
#{orderId}, #{merchantOrderId}, #{transactionId}, #{remark}, #{createdAt})
|
||||
on duplicate key update id = id
|
||||
</insert>
|
||||
|
||||
<sql id="statisticsConditions">
|
||||
1 = 1
|
||||
<if test="appId != null">and app_id = #{appId}</if>
|
||||
<if test="userId != null">and user_id = #{userId}</if>
|
||||
<if test="currency != null and currency != ''">and currency = #{currency}</if>
|
||||
<if test="businessType != null and businessType != ''">and business_type = #{businessType}</if>
|
||||
<if test="beginTime != null">and created_at >= #{beginTime}</if>
|
||||
<if test="endTime != null">and created_at <= #{endTime}</if>
|
||||
</sql>
|
||||
|
||||
<select id="selectStatisticsSummary" resultType="com.ruoyi.system.domain.vo.WalletLedgerSummaryVo">
|
||||
select currency,
|
||||
coalesce(sum(case when direction = 1 then amount else 0 end), 0) as incomeAmount,
|
||||
coalesce(sum(case when direction = 2 then amount else 0 end), 0) as expenseAmount,
|
||||
coalesce(sum(case when direction = 1 then amount else -amount end), 0) as netAmount,
|
||||
count(1) as count
|
||||
from apps_balance_ledger
|
||||
where <include refid="statisticsConditions"/>
|
||||
group by currency
|
||||
order by currency
|
||||
</select>
|
||||
|
||||
<select id="selectStatisticsTrend" resultType="com.ruoyi.system.domain.vo.WalletLedgerTrendVo">
|
||||
select
|
||||
<choose>
|
||||
<when test="granularity == 'YEAR'">date_format(created_at, '%Y')</when>
|
||||
<when test="granularity == 'MONTH'">date_format(created_at, '%Y-%m')</when>
|
||||
<otherwise>date_format(created_at, '%Y-%m-%d')</otherwise>
|
||||
</choose> as period,
|
||||
currency,
|
||||
coalesce(sum(case when direction = 1 then amount else 0 end), 0) as incomeAmount,
|
||||
coalesce(sum(case when direction = 2 then amount else 0 end), 0) as expenseAmount,
|
||||
coalesce(sum(case when direction = 1 then amount else -amount end), 0) as netAmount,
|
||||
count(1) as count
|
||||
from apps_balance_ledger
|
||||
where <include refid="statisticsConditions"/>
|
||||
group by period, currency
|
||||
order by period, currency
|
||||
</select>
|
||||
|
||||
<update id="updateAppsBalanceLedger" parameterType="AppsBalanceLedger">
|
||||
update apps_balance_ledger
|
||||
<trim prefix="SET" suffixOverrides=",">
|
||||
@@ -167,4 +224,4 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
#{id}
|
||||
</foreach>
|
||||
</delete>
|
||||
</mapper>
|
||||
</mapper>
|
||||
@@ -74,6 +74,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
where id = #{id} and deleted_at is null for update
|
||||
</select>
|
||||
|
||||
<update id="updateAvailableBalances">
|
||||
update apps
|
||||
set usdt_balance = #{usdtBalance}, trx_balance = #{trxBalance}, updated_at = #{updatedAt}
|
||||
where id = #{id} and deleted_at is null
|
||||
</update>
|
||||
|
||||
<update id="updateKeyPair">
|
||||
update apps
|
||||
set public_key = #{publicKey}, private_key = #{privateKey}, updated_at = #{updatedAt}
|
||||
|
||||
Reference in new issue
Block a user