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); 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); updateUserList.Add(updateUser);
insertUserAccountProfitLossRecordList.Add(new UserAccountProfitLossRecord() insertUserAccountProfitLossRecordList.Add(new UserAccountProfitLossRecord()
{ {
@ -226,7 +225,7 @@ namespace Binance.TradeRobot.Business
UserId = user.Id, UserId = user.Id,
RobotId = robot.Id, RobotId = robot.Id,
DividendRatio = user.DividendRatio, 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) public IList<UserResponse> GetUserList(bool multiplyBy100 = true)
{ {
var userList = fsql.Select<User>().ToList(u => new UserResponse() var userList = fsql.Select<User>().ToList().Map<IList<UserResponse>>();
{
Id = u.Id,
CostAmount = u.CostAmount,
CreateTime = u.CreateTime,
Profit = u.Profit,
UpdateTime = u.UpdateTime,
UserName = u.UserName,
WithdrawAmount = u.WithdrawAmount
});
userList.CalculateRatio(multiplyBy100); userList.CalculateRatio(multiplyBy100);
return userList; return userList;
} }
@ -95,44 +86,16 @@ namespace Binance.TradeRobot.Business
{ {
if (changeUser.TotalAssets < changeAmount) if (changeUser.TotalAssets < changeAmount)
throw new BusinessException("用户资产小于减持资金,不能提现"); 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); 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.Transaction(() =>
{ {
fsql.Update<User>(changeUser.Id).Set(u => u.CostAmount, changeUser.CostAmount) fsql.Update<User>(changeUser.Id).Set(u => u.CostAmount, changeUser.CostAmount)
.Set(u => u.Profit, changeUser.Profit) .Set(u => u.TotalAssets, changeUser.TotalAssets)
.SetIf(capitalChangeType == Enums.CapitalChangeType.Reduce, u => u.WithdrawAmount + changeAmount) .Set(u => u.WithdrawAmount, changeUser.WithdrawAmount)
.ExecuteAffrows(); .ExecuteAffrows();
fsql.Insert(new UserAccountFundChangeRecord() fsql.Insert(new UserAccountFundChangeRecord()
{ {
@ -177,32 +140,20 @@ namespace Binance.TradeRobot.Business
if (fromUser.TotalAssets < fundsTransferRequest.ChangeAmount) if (fromUser.TotalAssets < fundsTransferRequest.ChangeAmount)
throw new BusinessException("用户资产小于转移资金,不能转移"); throw new BusinessException("用户资产小于转移资金,不能转移");
if (fromUser.Profit > 0) fromUser.ChangeAmount(Enums.CapitalChangeType.Reduce, fundsTransferRequest.ChangeAmount, true);
{ toUser.ChangeAmount(Enums.CapitalChangeType.Add, fundsTransferRequest.ChangeAmount, true);
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;
}
fsql.Transaction(() => fsql.Transaction(() =>
{ {
fsql.Update<User>(fromUser.Id).Set(u => u.CostAmount, fromUser.CostAmount) fsql.Update<User>(fromUser.Id).Set(u => u.CostAmount, fromUser.CostAmount)
.Set(u => u.Profit, fromUser.Profit) .Set(u => u.TotalAssets, fromUser.TotalAssets)
.Set(u => u.WithdrawAmount + fundsTransferRequest.ChangeAmount) .Set(u => u.WithdrawAmount, fromUser.WithdrawAmount)
.ExecuteAffrows(); .ExecuteAffrows();
fsql.Update<User>(toUser.Id).Set(u => u.CostAmount + fundsTransferRequest.ChangeAmount) fsql.Update<User>(toUser.Id).Set(u => u.CostAmount, toUser.CostAmount)
.ExecuteAffrows(); .Set(u => u.TotalAssets, toUser.TotalAssets)
.ExecuteAffrows();
fsql.Insert(new UserAccountFundChangeRecord() fsql.Insert(new UserAccountFundChangeRecord()
{ {
@ -283,7 +234,7 @@ namespace Binance.TradeRobot.Business
Symbol = r.Symbol, Symbol = r.Symbol,
UserId = p.UserId, UserId = p.UserId,
UserName = u.UserName, UserName = u.UserName,
UserProfit = p.UserProfit CumulativeProfitAndLoss = p.CumulativeProfitAndLoss
}); });
return new UserAccountProfitLossRecordListResponse() return new UserAccountProfitLossRecordListResponse()
{ {

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

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

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

@ -10,6 +10,12 @@ namespace Binance.TradeRobot.Model.Db
[Column(DbType = "bigint", IsPrimary = true)] [Column(DbType = "bigint", IsPrimary = true)]
public long Id { get; set; } public long Id { get; set; }
/// <summary>
/// 用户资产
/// </summary>
[Column(DbType = "decimal(18,8)")]
public decimal TotalAssets { get; set; } = 0.0M;
/// <summary> /// <summary>
/// 投资本金 /// 投资本金
/// </summary> /// </summary>
@ -19,11 +25,11 @@ namespace Binance.TradeRobot.Model.Db
[Column(DbType = "datetime")] [Column(DbType = "datetime")]
public DateTime? CreateTime { get; set; } public DateTime? CreateTime { get; set; }
/// <summary> ///// <summary>
/// 收益 ///// 收益
/// </summary> ///// </summary>
[Column(DbType = "decimal(18,8)")] //[Column(DbType = "decimal(18,8)")]
public decimal Profit { get; set; } = 0.0M; //public decimal Profit { get; set; } = 0.0M;
[Column(StringLength = 20)] [Column(StringLength = 20)]
public string Pwd { get; set; } public string Pwd { get; set; }
@ -35,7 +41,7 @@ namespace Binance.TradeRobot.Model.Db
public string UserName { get; set; } public string UserName { get; set; }
/// <summary> /// <summary>
/// 提金额 /// 提金额
/// </summary> /// </summary>
[Column(DbType = "decimal(18,8)")] [Column(DbType = "decimal(18,8)")]
public decimal WithdrawAmount { get; set; } = 0.0M; 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; } public long UserId { get; set; }
/// <summary> /// <summary>
/// 用户投资收益 /// 累计盈亏
/// </summary> /// </summary>
[Column(DbType = "decimal(18,8)")] [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")] [Column(MapType = typeof(int), DbType = "int")]
public Enums.Exchange ExchangeId { get; set; } 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 public class UserResponse : Db.User
{ {
/// <summary> /// <summary>
/// 总资产(本金+收益) /// 累计盈亏
/// </summary> /// </summary>
public decimal TotalAssets { get { return CostAmount + Profit; } } public decimal CumulativeProfitAndLoss { get { return TotalAssets - CostAmount; } }
/// <summary> /// <summary>
/// 本金比例 /// 本金比例
@ -24,33 +24,25 @@ namespace Binance.TradeRobot.Model.Dto
/// </summary> /// </summary>
/// <param name="capitalChangeType"></param> /// <param name="capitalChangeType"></param>
/// <param name="changeAmount"></param> /// <param name="changeAmount"></param>
/// <param name="priorityAddCost">true:优先增加本金 false:优先增加利润</param> /// <param name="isManualOperation">是否手动操作</param>
public void ChangeAmount(Enums.CapitalChangeType capitalChangeType, decimal changeAmount, bool priorityAddCost) public void ChangeAmount(Enums.CapitalChangeType capitalChangeType, decimal changeAmount, bool isManualOperation)
{ {
if (capitalChangeType == Enums.CapitalChangeType.Add) if (capitalChangeType == Enums.CapitalChangeType.Add)
{ {
if (priorityAddCost) TotalAssets += changeAmount;
if (isManualOperation)
CostAmount += changeAmount; CostAmount += changeAmount;
else
Profit += changeAmount;
} }
else if (capitalChangeType == Enums.CapitalChangeType.Reduce) else if (capitalChangeType == Enums.CapitalChangeType.Reduce)
{ {
if (Profit > 0) var cumulativeProfitAndLoss = CumulativeProfitAndLoss;
{ TotalAssets -= changeAmount;
if (Profit >= changeAmount)
Profit -= changeAmount; //收益足够提现,只扣收益 if (isManualOperation)
else
{
var lessChangeAmount = changeAmount; //收益不足提现,先扣收益,不足部分再扣本金
lessChangeAmount -= Profit;
Profit = 0;
CostAmount -= lessChangeAmount;
}
}
else
{ {
CostAmount -= changeAmount; if (cumulativeProfitAndLoss < changeAmount)
CostAmount -= cumulativeProfitAndLoss > 0 ? changeAmount - cumulativeProfitAndLoss : changeAmount;
WithdrawAmount += changeAmount;
} }
} }
} }

Loading…
Cancel
Save