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.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Internal;
namespace Hncore.Pass.BaseInfo.Service namespace Hncore.Pass.BaseInfo.Service
{ {
public class UserService : ServiceBase<User>, IFindService public class UserService : ServiceBase<User>, IFindService
{ {
private static readonly AsyncLock _mutex1 = new AsyncLock();
private static string _secret = "hncore_yh_lzh_20f_2020_READY"; private static string _secret = "hncore_yh_lzh_20f_2020_READY";
private UserDbContext _dbContext; private UserDbContext _dbContext;
@@ -247,57 +249,62 @@ namespace Hncore.Pass.BaseInfo.Service
public async Task<ApiResult> UpdateAmount(UpdateAmountRequest request) public async Task<ApiResult> UpdateAmount(UpdateAmountRequest request)
{ {
var entity = await this.GetById(request.UserId); using (await _mutex1.LockAsync())
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()
{ {
UserId = request.UserId, var entity = await this.GetById(request.UserId);
UserName = entity.LoginCode, if (entity == null)
ScoreType = request.OpAmountType, return new ApiResult(ResultCode.C_NOT_EXISTS_ERROR, "用户不存在");
ScoreValue = request.Amount, if (request.Amount <= 0)
ScoreTypeName = request.OpAmountType.GetEnumDisplayName(), return new ApiResult(ResultCode.C_INVALID_ERROR, "金额必须大于0");
Remark = request.AttchInfo,
OperateUserName = request.OperateUserName
};
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, "余额不足"); UserId = request.UserId,
} UserName = entity.LoginCode,
userScore.RestAmount1 = entity.RestAmount; ScoreType = request.OpAmountType,
entity.RestAmount -= request.Amount; ScoreValue = request.Amount,
userScore.RestAmount2 = entity.RestAmount; ScoreTypeName = request.OpAmountType.GetEnumDisplayName(),
Remark = request.AttchInfo,
OperateUserName = request.OperateUserName
};
} if (request.OpAmountType == ScoreType.ManagerDeduct || request.OpAmountType == ScoreType.Pay)
else {
{ if (entity.RestAmount < request.Amount)
userScore.RestAmount1 = entity.RestAmount; {
entity.RestAmount += request.Amount; return new ApiResult(ResultCode.C_INVALID_ERROR, "余额不足");
userScore.RestAmount2 = entity.RestAmount; }
} 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(); userScore.RestAmount1 = entity.RestAmount;
LogHelper.Error("UpdateAmount", ex.Message); entity.RestAmount += request.Amount;
return new ApiResult(ResultCode.C_INVALID_ERROR); 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 Hncore.Pass.BaseInfo.Models;
using ScoreType = Hncore.Pass.BaseInfo.Models.ScoreType; using ScoreType = Hncore.Pass.BaseInfo.Models.ScoreType;
using System.Linq.Expressions; using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Internal;
namespace Hncore.Pass.Vpn.Service namespace Hncore.Pass.Vpn.Service
{ {
public partial class ProductOrderService : ServiceBase<ProductOrderEntity>, IFindService public partial class ProductOrderService : ServiceBase<ProductOrderEntity>, IFindService
{ {
private static readonly AsyncLock _mutex1 = new AsyncLock();
CourseContext m_DbContext; CourseContext m_DbContext;
ProductService m_ProductService; ProductService m_ProductService;
ProductPackageService m_ProductPackageService; ProductPackageService m_ProductPackageService;
@@ -269,6 +271,7 @@ FROM product_order where {where} GROUP BY Channel,ProductName order by Channel,
await CheckRequest(order); await CheckRequest(order);
var ret = await CaclePaymentAmount(order, request.UseAccountAmount); var ret = await CaclePaymentAmount(order, request.UseAccountAmount);
if (ret.Code != ResultCode.C_SUCCESS) return ret; if (ret.Code != ResultCode.C_SUCCESS) return ret;
using (var tran = await m_DbContext.Database.BeginTransactionAsync()) 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) public async Task<ApiResult<ProductOrderEntity>> CaclePaymentAmount(ProductOrderEntity order, int useAccountAmount)
{ {
var userEntity = await m_UserService.GetById(order.UserId); using (await _mutex1.LockAsync())
//使用优惠券
var couponAmount = 0m;
if (order.CouponId > 0)
{ {
var currentCoupon = await m_CouponService.GetOneUserAvailableCoupon(userEntity.Id, order.CouponId.Value); var userEntity = await m_UserService.GetById(order.UserId);
if (currentCoupon == null) //使用优惠券
return new ApiResult<ProductOrderEntity>(ResultCode.C_INVALID_ERROR, "没有可用的优惠券"); var couponAmount = 0m;
if (currentCoupon.Coupon.AllowMinAmount > 0 && order.OrderAmount < currentCoupon.Coupon.AllowMinAmount) 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.CouponAmount = couponAmount;
order.PaymentAmount = order.OrderAmount - couponAmount; order.PaymentAmount = order.OrderAmount - couponAmount;
order.PaymentAmount = order.PaymentAmount < 0 ? 0 : order.PaymentAmount; order.PaymentAmount = order.PaymentAmount < 0 ? 0 : order.PaymentAmount;
//使用余额 //使用余额
if (useAccountAmount == 1) if (useAccountAmount == 1)
{
if (userEntity.RestAmount >= order.PaymentAmount)
order.AccountPayAmount = order.PaymentAmount;
else
{ {
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); return new ApiResult<ProductOrderEntity>(order);
} }
@@ -340,7 +346,11 @@ FROM product_order where {where} GROUP BY Channel,ProductName order by Channel,
AttchInfo = order.OrderNo, AttchInfo = order.OrderNo,
OperateUserName = order.UserName, 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) if (order.OtherPayAmount == 0)