This commit is contained in:
@@ -20,11 +20,13 @@ using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
|
||||
namespace Hncore.Pass.BaseInfo.Service
|
||||
{
|
||||
public class UserService : ServiceBase<User>, IFindService
|
||||
{
|
||||
private static readonly AsyncLock _mutex1 = new AsyncLock();
|
||||
private static string _secret = "hncore_yh_lzh_20f_2020_READY";
|
||||
|
||||
private UserDbContext _dbContext;
|
||||
@@ -247,57 +249,62 @@ namespace Hncore.Pass.BaseInfo.Service
|
||||
|
||||
public async Task<ApiResult> UpdateAmount(UpdateAmountRequest request)
|
||||
{
|
||||
var entity = await this.GetById(request.UserId);
|
||||
if (entity == null)
|
||||
return new ApiResult(ResultCode.C_NOT_EXISTS_ERROR, "用户不存在");
|
||||
if (request.Amount <= 0)
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "金额必须大于0");
|
||||
|
||||
|
||||
var userScore = new UserScore()
|
||||
using (await _mutex1.LockAsync())
|
||||
{
|
||||
UserId = request.UserId,
|
||||
UserName = entity.LoginCode,
|
||||
ScoreType = request.OpAmountType,
|
||||
ScoreValue = request.Amount,
|
||||
ScoreTypeName = request.OpAmountType.GetEnumDisplayName(),
|
||||
Remark = request.AttchInfo,
|
||||
OperateUserName = request.OperateUserName
|
||||
};
|
||||
var entity = await this.GetById(request.UserId);
|
||||
if (entity == null)
|
||||
return new ApiResult(ResultCode.C_NOT_EXISTS_ERROR, "用户不存在");
|
||||
if (request.Amount <= 0)
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "金额必须大于0");
|
||||
|
||||
|
||||
|
||||
if (request.OpAmountType == ScoreType.ManagerDeduct || request.OpAmountType == ScoreType.Pay)
|
||||
{
|
||||
if (entity.RestAmount < request.Amount)
|
||||
var userScore = new UserScore()
|
||||
{
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "余额不足");
|
||||
}
|
||||
userScore.RestAmount1 = entity.RestAmount;
|
||||
entity.RestAmount -= request.Amount;
|
||||
userScore.RestAmount2 = entity.RestAmount;
|
||||
UserId = request.UserId,
|
||||
UserName = entity.LoginCode,
|
||||
ScoreType = request.OpAmountType,
|
||||
ScoreValue = request.Amount,
|
||||
ScoreTypeName = request.OpAmountType.GetEnumDisplayName(),
|
||||
Remark = request.AttchInfo,
|
||||
OperateUserName = request.OperateUserName
|
||||
};
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
userScore.RestAmount1 = entity.RestAmount;
|
||||
entity.RestAmount += request.Amount;
|
||||
userScore.RestAmount2 = entity.RestAmount;
|
||||
}
|
||||
if (request.OpAmountType == ScoreType.ManagerDeduct || request.OpAmountType == ScoreType.Pay)
|
||||
{
|
||||
if (entity.RestAmount < request.Amount)
|
||||
{
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR, "余额不足");
|
||||
}
|
||||
userScore.RestAmount1 = entity.RestAmount;
|
||||
entity.RestAmount -= request.Amount;
|
||||
userScore.RestAmount2 = entity.RestAmount;
|
||||
|
||||
using (var tran = await m_DbContextBase.Database.BeginTransactionAsync())
|
||||
{
|
||||
try
|
||||
{
|
||||
await m_UserScoreService.Add(userScore);
|
||||
await this.Update(entity);
|
||||
tran.Commit();
|
||||
return new ApiResult(ResultCode.C_SUCCESS);
|
||||
}
|
||||
catch (Exception ex)
|
||||
else
|
||||
{
|
||||
tran.Rollback();
|
||||
LogHelper.Error("UpdateAmount", ex.Message);
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR);
|
||||
userScore.RestAmount1 = entity.RestAmount;
|
||||
entity.RestAmount += request.Amount;
|
||||
userScore.RestAmount2 = entity.RestAmount;
|
||||
}
|
||||
|
||||
using (var tran = await m_DbContextBase.Database.BeginTransactionAsync())
|
||||
{
|
||||
try
|
||||
{
|
||||
await m_UserScoreService.Add(userScore);
|
||||
await this.Update(entity);
|
||||
tran.Commit();
|
||||
return new ApiResult(ResultCode.C_SUCCESS);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tran.Rollback();
|
||||
LogHelper.Error("UpdateAmount", ex.Message);
|
||||
return new ApiResult(ResultCode.C_INVALID_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user