404 lines
16 KiB
C#
404 lines
16 KiB
C#
using HuanMeng.MiaoYu.Code.Base;
|
|
using HuanMeng.MiaoYu.Code.DataAccess;
|
|
using HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
|
|
using HuanMeng.MiaoYu.Model.EnumModel;
|
|
using HuanMeng.MiaoYu.Model.EnumModel.User;
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
namespace HuanMeng.MiaoYu.Code.Users
|
|
{
|
|
/// <summary>
|
|
/// 用户货币记录
|
|
/// </summary>
|
|
public class UserCurrencyBLL(MiaoYuBase miaoYuBase)
|
|
{
|
|
/// <summary>
|
|
/// 获取货币余额
|
|
/// </summary>
|
|
/// <param name="userCurrencyType"></param>
|
|
/// <returns></returns>
|
|
public decimal GetUserCurrency(UserCurrencyType userCurrencyType)
|
|
{
|
|
var userCurrency = miaoYuBase.Dao.daoDbMiaoYu.context.T_User_Currency.FirstOrDefault(it => it.UserId == miaoYuBase._UserId && it.CurrencyType == (int)userCurrencyType);
|
|
return userCurrency?.CurrencyMoney ?? 0;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class UserCurrencyExtend
|
|
{
|
|
/// <summary>
|
|
/// 获取用户货币余额,没有货币的时候添加
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <param name="userCurrencyType"></param>
|
|
/// <param name="dao"></param>
|
|
/// <returns></returns>
|
|
public static decimal GetUserCurrencyMoney(this T_User user, UserCurrencyType userCurrencyType, DAO dao)
|
|
{
|
|
var userCurrency = dao.daoDbMiaoYu.context.T_User_Currency.FirstOrDefault(it => it.UserId == user.Id && it.CurrencyType == (int)userCurrencyType);
|
|
if (userCurrency == null)
|
|
{
|
|
userCurrency = new T_User_Currency()
|
|
{
|
|
CurrencyMoney = 0,
|
|
CreateAt = DateTime.Now,
|
|
CurrencyName = userCurrencyType.ToString(),
|
|
CurrencyType = (int)userCurrencyType,
|
|
TenantId = dao.daoDbMiaoYu.context.TenantInfo.TenantId,
|
|
UpdateAt = DateTime.Now,
|
|
UserId = user.Id,
|
|
};
|
|
dao.daoDbMiaoYu.context.Add(userCurrency);
|
|
dao.daoDbMiaoYu.context.SaveChanges();
|
|
}
|
|
return userCurrency?.CurrencyMoney ?? 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取用户货币余额
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <param name="userCurrencyType"></param>
|
|
/// <param name="dao"></param>
|
|
/// <returns></returns>
|
|
public static T_User_Currency GetUserCurrency(this T_User user, UserCurrencyType userCurrencyType, DAO dao)
|
|
{
|
|
var userCurrency = dao.daoDbMiaoYu.context.T_User_Currency.FirstOrDefault(it => it.UserId == user.Id && it.CurrencyType == (int)userCurrencyType);
|
|
return userCurrency;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取用户所有货币信息
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <param name="userCurrencyType"></param>
|
|
/// <param name="dao"></param>
|
|
/// <returns></returns>
|
|
public static List<T_User_Currency> GetUserCurrencys(this T_User user, DAO dao)
|
|
{
|
|
var userCurrency = dao.daoDbMiaoYu.context.T_User_Currency.Where(it => it.UserId == user.Id).ToList();
|
|
return userCurrency;
|
|
}
|
|
|
|
#region 不带事务
|
|
|
|
/// <summary>
|
|
/// 扣除或者充值货币
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <param name="userCurrencyType">货币类型</param>
|
|
/// <param name="money">扣除金额(负数扣除,正数添加)</param>
|
|
/// <param name="dao">数据库</param>
|
|
/// <param name="_currency"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
/// <exception cref="Exception"></exception>
|
|
public static bool ConsumeMoneyNoWork(this T_User user, UserCurrencyType userCurrencyType, decimal money, DAO dao, T_User_Currency? _currency = null)
|
|
{
|
|
if (user == null || user.Id == 0)
|
|
{
|
|
throw new ArgumentNullException("用户不能为空");
|
|
}
|
|
int userId = user.Id;
|
|
return ConsumeMoneyNoWork(userId, userCurrencyType, money, dao, _currency);
|
|
}
|
|
/// <summary>
|
|
/// 扣除或者充值货币
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <param name="userCurrencyType">货币类型</param>
|
|
/// <param name="money">扣除金额(负数扣除,正数添加)</param>
|
|
/// <param name="dao">数据库</param>
|
|
/// <param name="_currency"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
/// <exception cref="Exception"></exception>
|
|
public static bool ConsumeMoneyNoWork(this T_User_Data user, UserCurrencyType userCurrencyType, decimal money, DAO dao, T_User_Currency? _currency = null)
|
|
{
|
|
if (user == null || user.UserId == 0)
|
|
{
|
|
throw new ArgumentNullException("用户不能为空");
|
|
}
|
|
int userId = user.UserId;
|
|
return ConsumeMoneyNoWork(userId, userCurrencyType, money, dao, _currency);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 扣除或者充值货币
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <param name="userCurrencyType">货币类型</param>
|
|
/// <param name="money">扣除金额(负数扣除,正数添加)</param>
|
|
/// <param name="dao">数据库</param>
|
|
/// <param name="_currency"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
/// <exception cref="Exception"></exception>
|
|
public static bool ConsumeMoneyNoWork(this T_User_Currency user, decimal money, DAO dao)
|
|
{
|
|
if (user == null || user.UserId == 0)
|
|
{
|
|
throw new ArgumentNullException("用户不能为空");
|
|
}
|
|
int userId = user.UserId;
|
|
return ConsumeMoneyNoWork(userId, (UserCurrencyType)user.CurrencyType, money, dao, user);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 扣除或者充值货币,没有事务
|
|
/// </summary>>
|
|
/// <param name="userId">用户Id</param>
|
|
/// <param name="userCurrencyType">货币类型</param>
|
|
/// <param name="money">扣除金额(负数扣除,正数添加)</param>
|
|
/// <param name="dao">数据库</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="Exception"></exception>
|
|
public static bool ConsumeMoneyNoWork(int userId, UserCurrencyType userCurrencyType, decimal money, DAO dao, T_User_Currency? _currency = null)
|
|
{
|
|
var userCurrency = dao.daoDbMiaoYu.context.T_User_Currency.FirstOrDefault(it => it.UserId == userId && it.CurrencyType == (int)userCurrencyType);
|
|
if (userCurrency == null)
|
|
{
|
|
userCurrency = new T_User_Currency()
|
|
{
|
|
CreateAt = DateTime.Now,
|
|
CurrencyMoney = 0,
|
|
CurrencyName = userCurrencyType.ToString(),
|
|
CurrencyType = (int)userCurrencyType,
|
|
TenantId = dao.daoDbMiaoYu.context.TenantInfo.TenantId,
|
|
UpdateAt = DateTime.Now,
|
|
UserId = userId
|
|
};
|
|
dao.daoDbMiaoYu.context.Add(userCurrency);
|
|
dao.daoDbMiaoYu.context.SaveChanges();
|
|
}
|
|
userCurrency.CurrencyMoney += money;
|
|
if (userCurrency.CurrencyMoney < 0)
|
|
{
|
|
//余额不足
|
|
throw new Exception("余额不足");
|
|
}
|
|
T_User_Currency_Log? log = null;
|
|
UserCurrencyConsumeType userCurrencyConsumeType = UserCurrencyConsumeType.消耗;
|
|
if (money >= 0)
|
|
{
|
|
userCurrencyConsumeType = UserCurrencyConsumeType.充值;
|
|
}
|
|
|
|
if (userCurrencyType == UserCurrencyType.聊天次数)
|
|
{
|
|
var mintes = DateTime.Now.AddMinutes(-5);
|
|
log = dao.daoDbMiaoYu.context.T_User_Currency_Log.Where(it => it.CreateTime > mintes).OrderByDescending(it => it.CreateTime).FirstOrDefault();
|
|
}
|
|
var tempMoney = Math.Abs(money);
|
|
//消费
|
|
if (log == null)
|
|
{
|
|
log = new T_User_Currency_Log()
|
|
{
|
|
Consume = tempMoney,
|
|
ConsumeType = (int)userCurrencyConsumeType,
|
|
CreateTime = DateTime.Now,
|
|
CurrencyType = (int)userCurrencyType,
|
|
TenantId = dao.daoDbMiaoYu.context.TenantInfo.TenantId,
|
|
UpdateTime = DateTime.Now,
|
|
UserId = userId,
|
|
Remarks = ""
|
|
};
|
|
dao.daoDbMiaoYu.context.T_User_Currency_Log.Add(log);
|
|
}
|
|
log.Remarks += $"于{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}{userCurrencyConsumeType.ToString()}[{tempMoney}]{userCurrencyType.ToString()};";
|
|
if (log.Remarks.Length > 200)
|
|
{
|
|
log.Remarks = log.Remarks.Substring(log.Remarks.Length - 200);
|
|
}
|
|
dao.daoDbMiaoYu.context.SaveChanges();
|
|
if (_currency != null)
|
|
{
|
|
_currency.CurrencyMoney = userCurrency.CurrencyMoney;
|
|
_currency.UpdateAt = userCurrency.UpdateAt;
|
|
_currency.Id = userCurrency.Id;
|
|
_currency.CurrencyName = userCurrency.CurrencyName;
|
|
_currency.CreateAt = userCurrency.CreateAt;
|
|
_currency.CurrencyType = userCurrency.CurrencyType;
|
|
}
|
|
else
|
|
{
|
|
_currency = userCurrency;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
#endregion
|
|
#region 自带事务
|
|
/// <summary>
|
|
/// 扣除或者充值货币
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <param name="userCurrencyType">货币类型</param>
|
|
/// <param name="money">扣除金额(负数扣除,正数添加)</param>
|
|
/// <param name="dao">数据库</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
public static bool ConsumeMoney(this T_User user, UserCurrencyType userCurrencyType, decimal money, DAO dao)
|
|
{
|
|
if (user == null || user.Id == 0)
|
|
{
|
|
throw new ArgumentNullException("用户不能为空");
|
|
}
|
|
int userId = user.Id;
|
|
return ConsumeMoney(userId, userCurrencyType, money, dao);
|
|
}
|
|
/// <summary>
|
|
/// 扣除或者充值货币
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <param name="userCurrencyType">货币类型</param>
|
|
/// <param name="money">扣除金额(负数扣除,正数添加)</param>
|
|
/// <param name="dao">数据库</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
public static bool ConsumeMoney(this T_User_Data user, UserCurrencyType userCurrencyType, decimal money, DAO dao)
|
|
{
|
|
if (user == null || user.UserId == 0)
|
|
{
|
|
throw new ArgumentNullException("用户不能为空");
|
|
}
|
|
int userId = user.UserId;
|
|
return ConsumeMoney(userId, userCurrencyType, money, dao);
|
|
|
|
}
|
|
/// <summary>
|
|
/// 扣除或者充值货币
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <param name="userCurrencyType">货币类型</param>
|
|
/// <param name="money">扣除金额(负数扣除,正数添加)</param>
|
|
/// <param name="dao">数据库</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
public static bool ConsumeMoney(this T_User_Currency user, UserCurrencyType userCurrencyType, decimal money, DAO dao)
|
|
{
|
|
if (user == null || user.UserId == 0)
|
|
{
|
|
throw new ArgumentNullException("用户不能为空");
|
|
}
|
|
int userId = user.UserId;
|
|
return ConsumeMoney(userId, userCurrencyType, money, dao);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 扣除或者充值货币
|
|
/// </summary>>
|
|
/// <param name="userId">用户Id</param>
|
|
/// <param name="userCurrencyType">货币类型</param>
|
|
/// <param name="money">扣除金额(负数扣除,正数添加)</param>
|
|
/// <param name="dao">数据库</param>
|
|
/// <returns></returns>
|
|
public static bool ConsumeMoney(int userId, UserCurrencyType userCurrencyType, decimal money, DAO dao)
|
|
{
|
|
var userCurrency = dao.daoDbMiaoYu.context.T_User_Currency.FirstOrDefault(it => it.UserId == userId && it.CurrencyType == (int)userCurrencyType);
|
|
if (userCurrency == null)
|
|
{
|
|
userCurrency = new T_User_Currency()
|
|
{
|
|
CreateAt = DateTime.Now,
|
|
CurrencyMoney = 0,
|
|
CurrencyName = userCurrencyType.ToString(),
|
|
CurrencyType = (int)userCurrencyType,
|
|
TenantId = dao.daoDbMiaoYu.context.TenantInfo.TenantId,
|
|
UpdateAt = DateTime.Now,
|
|
UserId = userId
|
|
};
|
|
dao.daoDbMiaoYu.context.Add(userCurrency);
|
|
dao.daoDbMiaoYu.context.SaveChanges();
|
|
}
|
|
var tempCurrencyMoney = userCurrency.CurrencyMoney + money;
|
|
if (tempCurrencyMoney < 0)
|
|
{
|
|
//余额不足
|
|
return false;
|
|
}
|
|
// 开启事务
|
|
using (IDbContextTransaction transaction = dao.daoDbMiaoYu.context.Database.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
userCurrency.CurrencyMoney += money;
|
|
if (userCurrency.CurrencyMoney < 0)
|
|
{
|
|
transaction.Rollback();
|
|
//余额不足
|
|
return false;
|
|
}
|
|
T_User_Currency_Log? log = null;
|
|
UserCurrencyConsumeType userCurrencyConsumeType = UserCurrencyConsumeType.消耗;
|
|
if (money >= 0)
|
|
{
|
|
userCurrencyConsumeType = UserCurrencyConsumeType.充值;
|
|
}
|
|
|
|
if (userCurrencyType == UserCurrencyType.聊天次数)
|
|
{
|
|
var mintes = DateTime.Now.AddMinutes(-5);
|
|
log = dao.daoDbMiaoYu.context.T_User_Currency_Log.Where(it => it.CreateTime > mintes).OrderByDescending(it => it.CreateTime).FirstOrDefault();
|
|
}
|
|
var tempMoney = Math.Abs(money);
|
|
//消费
|
|
if (log == null)
|
|
{
|
|
log = new T_User_Currency_Log()
|
|
{
|
|
Consume = tempMoney,
|
|
ConsumeType = (int)userCurrencyConsumeType,
|
|
CreateTime = DateTime.Now,
|
|
CurrencyType = (int)userCurrencyType,
|
|
TenantId = dao.daoDbMiaoYu.context.TenantInfo.TenantId,
|
|
UpdateTime = DateTime.Now,
|
|
UserId = userId,
|
|
Remarks = ""
|
|
};
|
|
dao.daoDbMiaoYu.context.T_User_Currency_Log.Add(log);
|
|
}
|
|
|
|
log.Remarks += $"于{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}{userCurrencyConsumeType.ToString()}[{tempMoney}]{userCurrencyType.ToString()};";
|
|
if (log.Remarks.Length > 200)
|
|
{
|
|
log.Remarks = log.Remarks.Substring(log.Remarks.Length - 200);
|
|
}
|
|
dao.daoDbMiaoYu.context.SaveChanges();
|
|
transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// 出现异常,回滚事务
|
|
transaction.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
#endregion
|
|
|
|
|
|
}
|
|
}
|