111
This commit is contained in:
@@ -105,22 +105,18 @@ public class AssetController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 资金流水
|
||||
* 每日盈亏日历
|
||||
*/
|
||||
@GetMapping("/flow")
|
||||
public Result<Map<String, Object>> getFlows(
|
||||
@RequestParam(required = false) Integer flowType,
|
||||
@RequestParam(defaultValue = "1") int pageNum,
|
||||
@RequestParam(defaultValue = "20") int pageSize) {
|
||||
@GetMapping("/daily-profit")
|
||||
public Result<Map<String, Object>> getDailyProfit(
|
||||
@RequestParam int year,
|
||||
@RequestParam int month) {
|
||||
Long userId = UserContext.getUserId();
|
||||
if (userId == null) {
|
||||
return Result.unauthorized("请先登录");
|
||||
}
|
||||
|
||||
try {
|
||||
List<?> flows = assetService.getFlows(userId, flowType, pageNum, pageSize);
|
||||
Map<String, Object> result = new java.util.HashMap<>();
|
||||
result.put("list", flows);
|
||||
Map<String, Object> result = assetService.getDailyProfit(userId, year, month);
|
||||
return Result.success(result);
|
||||
} catch (Exception e) {
|
||||
return Result.fail(e.getMessage());
|
||||
|
||||
@@ -5,9 +5,11 @@ import com.it.rattan.monisuo.entity.AccountFlow;
|
||||
import com.it.rattan.monisuo.entity.AccountFund;
|
||||
import com.it.rattan.monisuo.entity.AccountTrade;
|
||||
import com.it.rattan.monisuo.entity.Coin;
|
||||
import com.it.rattan.monisuo.entity.OrderTrade;
|
||||
import com.it.rattan.monisuo.mapper.AccountFlowMapper;
|
||||
import com.it.rattan.monisuo.mapper.AccountFundMapper;
|
||||
import com.it.rattan.monisuo.mapper.AccountTradeMapper;
|
||||
import com.it.rattan.monisuo.mapper.OrderTradeMapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.it.rattan.monisuo.util.OrderNoUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -15,6 +17,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
@@ -30,6 +33,9 @@ public class AssetService {
|
||||
@Autowired
|
||||
private AccountTradeMapper accountTradeMapper;
|
||||
|
||||
@Autowired
|
||||
private OrderTradeMapper orderTradeMapper;
|
||||
|
||||
@Autowired
|
||||
private AccountFlowMapper accountFlowMapper;
|
||||
|
||||
@@ -288,4 +294,64 @@ public class AssetService {
|
||||
public void updateFundAccount(LambdaUpdateWrapper<AccountFund> updateWrapper) {
|
||||
accountFundMapper.update(null, updateWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取每日盈亏数据
|
||||
*/
|
||||
public Map<String, Object> getDailyProfit(Long userId, int year, int month) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
Map<String, BigDecimal> dailyMap = new LinkedHashMap<>();
|
||||
|
||||
LocalDate start = LocalDate.of(year, month, 1);
|
||||
LocalDate end = start.withDayOfMonth(start.lengthOfMonth());
|
||||
|
||||
// 查询该月所有已完成的卖出订单
|
||||
LambdaQueryWrapper<OrderTrade> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(OrderTrade::getUserId, userId)
|
||||
.eq(OrderTrade::getDirection, 2)
|
||||
.eq(OrderTrade::getStatus, 1)
|
||||
.ge(OrderTrade::getCreateTime, start.atStartOfDay())
|
||||
.lt(OrderTrade::getCreateTime, end.plusDays(1).atStartOfDay());
|
||||
List<OrderTrade> sellOrders = orderTradeMapper.selectList(wrapper);
|
||||
|
||||
// 按日期分组计算每日已实现盈亏
|
||||
for (OrderTrade order : sellOrders) {
|
||||
String dateKey = order.getCreateTime().toLocalDate().toString();
|
||||
AccountTrade account = getOrCreateTradeAccount(userId, order.getCoinCode());
|
||||
BigDecimal cost = order.getQuantity().multiply(account.getAvgPrice());
|
||||
BigDecimal profit = order.getAmount().subtract(cost);
|
||||
dailyMap.merge(dateKey, profit, BigDecimal::add);
|
||||
}
|
||||
|
||||
// 今日额外加上未实现盈亏
|
||||
LocalDate today = LocalDate.now();
|
||||
if (!today.isBefore(start) && !today.isAfter(end)) {
|
||||
BigDecimal unrealized = BigDecimal.ZERO;
|
||||
LambdaQueryWrapper<AccountTrade> tradeWrapper = new LambdaQueryWrapper<>();
|
||||
tradeWrapper.eq(AccountTrade::getUserId, userId)
|
||||
.gt(AccountTrade::getQuantity, BigDecimal.ZERO);
|
||||
List<AccountTrade> trades = accountTradeMapper.selectList(tradeWrapper);
|
||||
|
||||
for (AccountTrade trade : trades) {
|
||||
Coin coin = coinService.getCoinByCode(trade.getCoinCode());
|
||||
if (coin != null) {
|
||||
BigDecimal value = trade.getQuantity().multiply(coin.getPrice());
|
||||
BigDecimal cost = trade.getQuantity().multiply(trade.getAvgPrice());
|
||||
unrealized = unrealized.add(value.subtract(cost));
|
||||
}
|
||||
}
|
||||
String todayKey = today.toString();
|
||||
dailyMap.merge(todayKey, unrealized, BigDecimal::add);
|
||||
}
|
||||
|
||||
// 计算月度总盈亏
|
||||
BigDecimal totalProfit = BigDecimal.ZERO;
|
||||
for (BigDecimal profit : dailyMap.values()) {
|
||||
totalProfit = totalProfit.add(profit);
|
||||
}
|
||||
|
||||
result.put("daily", dailyMap);
|
||||
result.put("totalProfit", totalProfit);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user