Browse Source

1.修复实际借币比例不正确问题

2.系统用户和盈亏记录增加累计盈亏字段
master
shanji 3 years ago
parent
commit
da6d79bc40
  1. 5
      Binance.TradeRobot.Business/Business/OrderPublishBusiness/Spot/D21OrderPublishBusiness.cs
  2. 79
      Binance.TradeRobot.Business/Business/UserBusiness.cs
  3. 20
      Binance.TradeRobot.Model/Binance.TradeRobot.Model.xml
  4. 18
      Binance.TradeRobot.Model/Db/User/User.cs
  5. 4
      Binance.TradeRobot.Model/Db/User/UserAccountProfitLossRecord.cs
  6. 34
      Binance.TradeRobot.Model/Dto/Response/User/UserResponse.cs

5
Binance.TradeRobot.Business/Business/OrderPublishBusiness/Spot/D21OrderPublishBusiness.cs

@ -210,9 +210,8 @@ namespace Binance.TradeRobot.Business
user.ChangeAmount(capitalChangeType, Math.Abs(changeAmount), false);
var updateUser = fsql.Update<User>(user.Id).Set(u => u.TotalAssets, user.TotalAssets);
var updateUser = fsql.Update<User>(user.Id).Set(u => u.CostAmount, user.CostAmount)
.Set(u => u.Profit, user.Profit);
updateUserList.Add(updateUser);
insertUserAccountProfitLossRecordList.Add(new UserAccountProfitLossRecord()
{
@ -226,7 +225,7 @@ namespace Binance.TradeRobot.Business
UserId = user.Id,
RobotId = robot.Id,
DividendRatio = user.DividendRatio,
UserProfit = user.Profit
CumulativeProfitAndLoss = user.CumulativeProfitAndLoss
});
}
}

79
Binance.TradeRobot.Business/Business/UserBusiness.cs

@ -60,16 +60,7 @@ namespace Binance.TradeRobot.Business
public IList<UserResponse> GetUserList(bool multiplyBy100 = true)
{
var userList = fsql.Select<User>().ToList(u => new UserResponse()
{
Id = u.Id,
CostAmount = u.CostAmount,
CreateTime = u.CreateTime,
Profit = u.Profit,
UpdateTime = u.UpdateTime,
UserName = u.UserName,
WithdrawAmount = u.WithdrawAmount
});
var userList = fsql.Select<User>().ToList().Map<IList<UserResponse>>();
userList.CalculateRatio(multiplyBy100);
return userList;
}
@ -95,44 +86,16 @@ namespace Binance.TradeRobot.Business
{
if (changeUser.TotalAssets < changeAmount)
throw new BusinessException("用户资产小于减持资金,不能提现");
//获取交易所账户总额(usdt)
//var apiKeyList = stockExchangeAccountBusiness.GetAPIKeyList(null);
//var totalBalance = apiKeyList.Sum(k => k.Balance);
//if (totalBalance < changeAmount)
// throw new BusinessException("交易所总余额小于减持资金,不能提现");
}
changeUser.ChangeAmount(capitalChangeType, changeAmount, true);
//if (capitalChangeType == Enums.CapitalChangeType.Add)
//{
// changeUser.CostAmount += changeAmount;
//}
//else if (capitalChangeType == Enums.CapitalChangeType.Reduce)
//{
// if (changeUser.Profit > 0)
// {
// if (changeUser.Profit >= changeAmount)
// changeUser.Profit -= changeAmount; //收益足够提现,只扣收益
// else
// {
// var lessChangeAmount = changeAmount; //收益不足提现,先扣收益,不足部分再扣本金
// lessChangeAmount -= changeUser.Profit;
// changeUser.Profit = 0;
// changeUser.CostAmount -= lessChangeAmount;
// }
// }
// else
// {
// changeUser.CostAmount -= changeAmount;
// }
//}
fsql.Transaction(() =>
{
fsql.Update<User>(changeUser.Id).Set(u => u.CostAmount, changeUser.CostAmount)
.Set(u => u.Profit, changeUser.Profit)
.SetIf(capitalChangeType == Enums.CapitalChangeType.Reduce, u => u.WithdrawAmount + changeAmount)
.ExecuteAffrows();
.Set(u => u.TotalAssets, changeUser.TotalAssets)
.Set(u => u.WithdrawAmount, changeUser.WithdrawAmount)
.ExecuteAffrows();
fsql.Insert(new UserAccountFundChangeRecord()
{
@ -177,32 +140,20 @@ namespace Binance.TradeRobot.Business
if (fromUser.TotalAssets < fundsTransferRequest.ChangeAmount)
throw new BusinessException("用户资产小于转移资金,不能转移");
if (fromUser.Profit > 0)
{
if (fromUser.Profit >= fundsTransferRequest.ChangeAmount)
fromUser.Profit -= fundsTransferRequest.ChangeAmount; //收益足够提现,只扣收益
else
{
var lessChangeAmount = fundsTransferRequest.ChangeAmount; //收益不足提现,先扣收益,不足部分再扣本金
lessChangeAmount -= fromUser.Profit;
fromUser.Profit = 0;
fromUser.CostAmount -= lessChangeAmount;
}
}
else
{
fromUser.CostAmount -= fundsTransferRequest.ChangeAmount;
}
fromUser.ChangeAmount(Enums.CapitalChangeType.Reduce, fundsTransferRequest.ChangeAmount, true);
toUser.ChangeAmount(Enums.CapitalChangeType.Add, fundsTransferRequest.ChangeAmount, true);
fsql.Transaction(() =>
{
fsql.Update<User>(fromUser.Id).Set(u => u.CostAmount, fromUser.CostAmount)
.Set(u => u.Profit, fromUser.Profit)
.Set(u => u.WithdrawAmount + fundsTransferRequest.ChangeAmount)
.ExecuteAffrows();
.Set(u => u.TotalAssets, fromUser.TotalAssets)
.Set(u => u.WithdrawAmount, fromUser.WithdrawAmount)
.ExecuteAffrows();
fsql.Update<User>(toUser.Id).Set(u => u.CostAmount + fundsTransferRequest.ChangeAmount)
.ExecuteAffrows();
fsql.Update<User>(toUser.Id).Set(u => u.CostAmount, toUser.CostAmount)
.Set(u => u.TotalAssets, toUser.TotalAssets)
.ExecuteAffrows();
fsql.Insert(new UserAccountFundChangeRecord()
{
@ -283,7 +234,7 @@ namespace Binance.TradeRobot.Business
Symbol = r.Symbol,
UserId = p.UserId,
UserName = u.UserName,
UserProfit = p.UserProfit
CumulativeProfitAndLoss = p.CumulativeProfitAndLoss
});
return new UserAccountProfitLossRecordListResponse()
{

20
Binance.TradeRobot.Model/Binance.TradeRobot.Model.xml

@ -494,19 +494,19 @@
现货持仓均价
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Db.User.CostAmount">
<member name="P:Binance.TradeRobot.Model.Db.User.TotalAssets">
<summary>
投资本金
用户资产
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Db.User.Profit">
<member name="P:Binance.TradeRobot.Model.Db.User.CostAmount">
<summary>
收益
投资本金
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Db.User.WithdrawAmount">
<summary>
金额
金额
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Db.UserAccountFundChangeRecord.ChangeAmount">
@ -549,9 +549,9 @@
订单利润
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Db.UserAccountProfitLossRecord.UserProfit">
<member name="P:Binance.TradeRobot.Model.Db.UserAccountProfitLossRecord.CumulativeProfitAndLoss">
<summary>
用户投资收益
累计盈亏
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Dto.AddExchangeAccountRequest.Id">
@ -785,9 +785,9 @@
对端用户名
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Dto.UserResponse.TotalAssets">
<member name="P:Binance.TradeRobot.Model.Dto.UserResponse.CumulativeProfitAndLoss">
<summary>
总资产(本金+收益)
累计盈亏
</summary>
</member>
<member name="P:Binance.TradeRobot.Model.Dto.UserResponse.CostRatio">
@ -806,7 +806,7 @@
</summary>
<param name="capitalChangeType"></param>
<param name="changeAmount"></param>
<param name="priorityAddCost">true:优先增加本金 false:优先增加利润</param>
<param name="isManualOperation">是否手动操作</param>
</member>
<member name="P:Binance.TradeRobot.Model.RuningInfo.D21RuningInfo.RecentSmallTrendSingal">
<summary>

18
Binance.TradeRobot.Model/Db/User/User.cs

@ -10,6 +10,12 @@ namespace Binance.TradeRobot.Model.Db
[Column(DbType = "bigint", IsPrimary = true)]
public long Id { get; set; }
/// <summary>
/// 用户资产
/// </summary>
[Column(DbType = "decimal(18,8)")]
public decimal TotalAssets { get; set; } = 0.0M;
/// <summary>
/// 投资本金
/// </summary>
@ -19,11 +25,11 @@ namespace Binance.TradeRobot.Model.Db
[Column(DbType = "datetime")]
public DateTime? CreateTime { get; set; }
/// <summary>
/// 收益
/// </summary>
[Column(DbType = "decimal(18,8)")]
public decimal Profit { get; set; } = 0.0M;
///// <summary>
///// 收益
///// </summary>
//[Column(DbType = "decimal(18,8)")]
//public decimal Profit { get; set; } = 0.0M;
[Column(StringLength = 20)]
public string Pwd { get; set; }
@ -35,7 +41,7 @@ namespace Binance.TradeRobot.Model.Db
public string UserName { get; set; }
/// <summary>
/// 提金额
/// 提金额
/// </summary>
[Column(DbType = "decimal(18,8)")]
public decimal WithdrawAmount { get; set; } = 0.0M;

4
Binance.TradeRobot.Model/Db/User/UserAccountProfitLossRecord.cs

@ -49,10 +49,10 @@ namespace Binance.TradeRobot.Model.Db
public long UserId { get; set; }
/// <summary>
/// 用户投资收益
/// 累计盈亏
/// </summary>
[Column(DbType = "decimal(18,8)")]
public decimal UserProfit { get; set; } = 0.0M;
public decimal CumulativeProfitAndLoss { get; set; } = 0.0M;
[Column(MapType = typeof(int), DbType = "int")]
public Enums.Exchange ExchangeId { get; set; }

34
Binance.TradeRobot.Model/Dto/Response/User/UserResponse.cs

@ -5,9 +5,9 @@ namespace Binance.TradeRobot.Model.Dto
public class UserResponse : Db.User
{
/// <summary>
/// 总资产(本金+收益)
/// 累计盈亏
/// </summary>
public decimal TotalAssets { get { return CostAmount + Profit; } }
public decimal CumulativeProfitAndLoss { get { return TotalAssets - CostAmount; } }
/// <summary>
/// 本金比例
@ -24,33 +24,25 @@ namespace Binance.TradeRobot.Model.Dto
/// </summary>
/// <param name="capitalChangeType"></param>
/// <param name="changeAmount"></param>
/// <param name="priorityAddCost">true:优先增加本金 false:优先增加利润</param>
public void ChangeAmount(Enums.CapitalChangeType capitalChangeType, decimal changeAmount, bool priorityAddCost)
/// <param name="isManualOperation">是否手动操作</param>
public void ChangeAmount(Enums.CapitalChangeType capitalChangeType, decimal changeAmount, bool isManualOperation)
{
if (capitalChangeType == Enums.CapitalChangeType.Add)
{
if (priorityAddCost)
TotalAssets += changeAmount;
if (isManualOperation)
CostAmount += changeAmount;
else
Profit += changeAmount;
}
else if (capitalChangeType == Enums.CapitalChangeType.Reduce)
{
if (Profit > 0)
{
if (Profit >= changeAmount)
Profit -= changeAmount; //收益足够提现,只扣收益
else
{
var lessChangeAmount = changeAmount; //收益不足提现,先扣收益,不足部分再扣本金
lessChangeAmount -= Profit;
Profit = 0;
CostAmount -= lessChangeAmount;
}
}
else
var cumulativeProfitAndLoss = CumulativeProfitAndLoss;
TotalAssets -= changeAmount;
if (isManualOperation)
{
CostAmount -= changeAmount;
if (cumulativeProfitAndLoss < changeAmount)
CostAmount -= cumulativeProfitAndLoss > 0 ? changeAmount - cumulativeProfitAndLoss : changeAmount;
WithdrawAmount += changeAmount;
}
}
}

Loading…
Cancel
Save