This commit is contained in:
wanyongkang
2020-10-30 10:22:38 +08:00
parent 377ad1a3ae
commit 04c2ae94f7
2 changed files with 86 additions and 69 deletions

View File

@@ -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);
}
}
}
}

View File

@@ -18,11 +18,13 @@ using Hncore.Pass.BaseInfo.Request.User;
using Hncore.Pass.BaseInfo.Models;
using ScoreType = Hncore.Pass.BaseInfo.Models.ScoreType;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Internal;
namespace Hncore.Pass.Vpn.Service
{
public partial class ProductOrderService : ServiceBase<ProductOrderEntity>, IFindService
{
private static readonly AsyncLock _mutex1 = new AsyncLock();
CourseContext m_DbContext;
ProductService m_ProductService;
ProductPackageService m_ProductPackageService;
@@ -269,6 +271,7 @@ FROM product_order where {where} GROUP BY Channel,ProductName order by Channel,
await CheckRequest(order);
var ret = await CaclePaymentAmount(order, request.UseAccountAmount);
if (ret.Code != ResultCode.C_SUCCESS) return ret;
using (var tran = await m_DbContext.Database.BeginTransactionAsync())
{
@@ -292,38 +295,41 @@ FROM product_order where {where} GROUP BY Channel,ProductName order by Channel,
public async Task<ApiResult<ProductOrderEntity>> CaclePaymentAmount(ProductOrderEntity order, int useAccountAmount)
{
var userEntity = await m_UserService.GetById(order.UserId);
//使用优惠券
var couponAmount = 0m;
if (order.CouponId > 0)
using (await _mutex1.LockAsync())
{
var currentCoupon = await m_CouponService.GetOneUserAvailableCoupon(userEntity.Id, order.CouponId.Value);
if (currentCoupon == null)
return new ApiResult<ProductOrderEntity>(ResultCode.C_INVALID_ERROR, "没有可用的优惠券");
if (currentCoupon.Coupon.AllowMinAmount > 0 && order.OrderAmount < currentCoupon.Coupon.AllowMinAmount)
var userEntity = await m_UserService.GetById(order.UserId);
//使用优惠券
var couponAmount = 0m;
if (order.CouponId > 0)
{
return new ApiResult<ProductOrderEntity>(ResultCode.C_INVALID_ERROR, $"满{currentCoupon.Coupon.AllowMinAmount}元才能使用");
var currentCoupon = await m_CouponService.GetOneUserAvailableCoupon(userEntity.Id, order.CouponId.Value);
if (currentCoupon == null)
return new ApiResult<ProductOrderEntity>(ResultCode.C_INVALID_ERROR, "没有可用的优惠券");
if (currentCoupon.Coupon.AllowMinAmount > 0 && order.OrderAmount < currentCoupon.Coupon.AllowMinAmount)
{
return new ApiResult<ProductOrderEntity>(ResultCode.C_INVALID_ERROR, $"满{currentCoupon.Coupon.AllowMinAmount}元才能使用");
}
if (currentCoupon.Coupon.CouponType == Sells.Model.ECouponType.Minus)
couponAmount = currentCoupon.Coupon.CouponValue;
else
couponAmount = order.OrderAmount * (currentCoupon.Coupon.CouponValue * 0.1m);
}
if (currentCoupon.Coupon.CouponType == Sells.Model.ECouponType.Minus)
couponAmount = currentCoupon.Coupon.CouponValue;
else
couponAmount = order.OrderAmount * (currentCoupon.Coupon.CouponValue * 0.1m);
}
order.CouponAmount = couponAmount;
order.PaymentAmount = order.OrderAmount - couponAmount;
order.PaymentAmount = order.PaymentAmount < 0 ? 0 : order.PaymentAmount;
//使用余额
if (useAccountAmount == 1)
{
if (userEntity.RestAmount >= order.PaymentAmount)
order.AccountPayAmount = order.PaymentAmount;
else
order.CouponAmount = couponAmount;
order.PaymentAmount = order.OrderAmount - couponAmount;
order.PaymentAmount = order.PaymentAmount < 0 ? 0 : order.PaymentAmount;
//使用余额
if (useAccountAmount == 1)
{
return new ApiResult<ProductOrderEntity>(ResultCode.C_UNKNOWN_ERROR, "余额不足");
if (userEntity.RestAmount >= order.PaymentAmount)
order.AccountPayAmount = order.PaymentAmount;
else
{
return new ApiResult<ProductOrderEntity>(ResultCode.C_UNKNOWN_ERROR, "余额不足");
}
}
order.OtherPayAmount = order.PaymentAmount - order.AccountPayAmount;
}
order.OtherPayAmount = order.PaymentAmount - order.AccountPayAmount;
return new ApiResult<ProductOrderEntity>(order);
}
@@ -340,7 +346,11 @@ FROM product_order where {where} GROUP BY Channel,ProductName order by Channel,
AttchInfo = order.OrderNo,
OperateUserName = order.UserName,
};
await m_BaseUserService.UpdateAmount(amountInfo);
var ret = await m_BaseUserService.UpdateAmount(amountInfo);
if (ret.Code != ResultCode.C_SUCCESS)
{
order.OtherPayAmount = order.PaymentAmount;
}
}
//全是余额支付
if (order.OtherPayAmount == 0)