添加货币扣除或者增加
This commit is contained in:
parent
1f319a25bb
commit
fc925c8780
|
|
@ -111,7 +111,7 @@ namespace HuanMeng.MiaoYu.Code.Category
|
|||
var data = Mapper.Map<DataListDto>(x);
|
||||
data.ActionType = RecommendActionTypeEnum.Chat.ToString();
|
||||
data.ActionId = data.Id.ToString();
|
||||
data.ImageUrl = x.IconImage;//data.BgImage;
|
||||
data.ImageUrl = x.BgImage;//data.BgImage;
|
||||
|
||||
tuijian.Data.Add(data);
|
||||
xiaoshuo.Data.Add(data);
|
||||
|
|
|
|||
|
|
@ -17,10 +17,10 @@ namespace HuanMeng.MiaoYu.Code.Users.UserAccount.VerificationCodeManager
|
|||
/// </summary>
|
||||
public static class MemoryVerificationCodeExtension
|
||||
{
|
||||
// <summary>
|
||||
/// <summary>
|
||||
/// 验证码扩展
|
||||
/// </summary>
|
||||
/// <param name="serviceCollection"></param>
|
||||
/// <param name="builder"></param>
|
||||
public static void AddMemoryVerificationCode(this IHostApplicationBuilder builder)
|
||||
{
|
||||
//注册一个验证码的服务
|
||||
|
|
|
|||
|
|
@ -1,12 +1,20 @@
|
|||
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>
|
||||
|
|
@ -45,5 +53,135 @@ namespace HuanMeng.MiaoYu.Code.Users
|
|||
var userCurrency = dao.daoDbMiaoYu.context.T_User_Currency.FirstOrDefault(it => it.UserId == user.Id && it.CurrencyType == (int)userCurrencyType);
|
||||
return userCurrency?.CurrencyMoney ?? 0;
|
||||
}
|
||||
|
||||
/// <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="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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
using HuanMeng.MiaoYu.Code.DataAccess;
|
||||
using HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
|
@ -9,11 +12,9 @@ namespace HuanMeng.MiaoYu.Code.Users
|
|||
/// <summary>
|
||||
/// 用户信息
|
||||
/// </summary>
|
||||
public class UserInfoBLL : MiaoYuBase<UserInfoBLL>
|
||||
public class UserInfoBLL(DAO dao, int userId)
|
||||
{
|
||||
public UserInfoBLL(IServiceProvider serviceProvider) : base(serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
private T_User? _user;
|
||||
/// <summary>
|
||||
/// 用户表
|
||||
|
|
@ -25,11 +26,11 @@ namespace HuanMeng.MiaoYu.Code.Users
|
|||
|
||||
if (_user == null)
|
||||
{
|
||||
if (_UserId == 0)
|
||||
if (userId == 0)
|
||||
{
|
||||
throw new Exception("未找到用户");
|
||||
}
|
||||
_user = Dao.daoDbMiaoYu.context.T_User.FirstOrDefault(it => it.Id == _UserId);
|
||||
_user = dao.daoDbMiaoYu.context.T_User.FirstOrDefault(it => it.Id == userId);
|
||||
if (_user == null)
|
||||
{
|
||||
throw new Exception("未找到用户");
|
||||
|
|
@ -38,5 +39,12 @@ namespace HuanMeng.MiaoYu.Code.Users
|
|||
return _user;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static class UserInfoExtend
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -128,6 +128,11 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext
|
|||
/// </summary>
|
||||
public virtual DbSet<T_User_Currency> T_User_Currency { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户金额记录表
|
||||
/// </summary>
|
||||
public virtual DbSet<T_User_Currency_Log> T_User_Currency_Log { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 用户信息表
|
||||
/// </summary>
|
||||
|
|
@ -183,6 +188,12 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext
|
|||
entity.Property(e => e.UpdateTime)
|
||||
.HasComment("更新时间")
|
||||
.HasColumnType("datetime");
|
||||
entity.Property(e => e.UserName)
|
||||
.HasMaxLength(50)
|
||||
.HasComment("对话名字");
|
||||
entity.Property(e => e.UserSex)
|
||||
.HasMaxLength(10)
|
||||
.HasComment("对话性别");
|
||||
entity.Property(e => e.Visibility).HasComment("公开/私密 0公开 1私密");
|
||||
//添加全局筛选器
|
||||
if (this.TenantInfo != null)
|
||||
|
|
@ -555,19 +566,21 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext
|
|||
entity.Property(e => e.CreateTime)
|
||||
.HasComment("创建时间")
|
||||
.HasColumnType("datetime");
|
||||
entity.Property(e => e.ExchangeCurrencyCount).HasComment("兑换道具所需要的金币数量");
|
||||
entity.Property(e => e.IsProductDelisting).HasComment("商品是否下架 0否1是");
|
||||
entity.Property(e => e.LaunchDateTime)
|
||||
.HasComment("商品上架时间")
|
||||
.HasColumnType("datetime");
|
||||
entity.Property(e => e.Price)
|
||||
.HasComment("价格")
|
||||
.HasColumnType("decimal(10, 2)");
|
||||
entity.Property(e => e.PropDesc)
|
||||
.HasMaxLength(1)
|
||||
.HasMaxLength(255)
|
||||
.HasComment("道具描述");
|
||||
entity.Property(e => e.PropImgId).HasComment("道具图片配置 图片id");
|
||||
entity.Property(e => e.PropName)
|
||||
.HasMaxLength(1)
|
||||
.HasMaxLength(100)
|
||||
.HasComment("道具名称");
|
||||
entity.Property(e => e.PropType).HasComment("道具类型");
|
||||
entity.Property(e => e.PropType).HasComment("道具类型,0商城,1商店");
|
||||
entity.Property(e => e.StockCount).HasComment("库存数量");
|
||||
entity.Property(e => e.TenantId).HasComment("租户ID");
|
||||
entity.Property(e => e.UpdateTime)
|
||||
|
|
@ -717,6 +730,35 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext
|
|||
}
|
||||
});
|
||||
|
||||
modelBuilder.Entity<T_User_Currency_Log>(entity =>
|
||||
{
|
||||
entity.HasKey(e => e.Id).HasName("PK__T_User_C__3214EC07DBD26048");
|
||||
|
||||
entity.ToTable(tb => tb.HasComment("用户金额记录表"));
|
||||
|
||||
entity.Property(e => e.Consume)
|
||||
.HasComment("金额")
|
||||
.HasColumnType("decimal(10, 2)");
|
||||
entity.Property(e => e.ConsumeType).HasComment("消耗类型,0消耗,1增加");
|
||||
entity.Property(e => e.CreateTime)
|
||||
.HasComment("创建时间")
|
||||
.HasColumnType("datetime");
|
||||
entity.Property(e => e.CurrencyType).HasComment("金额类型");
|
||||
entity.Property(e => e.Remarks)
|
||||
.HasMaxLength(200)
|
||||
.HasComment("备注");
|
||||
entity.Property(e => e.TenantId).HasComment("租户");
|
||||
entity.Property(e => e.UpdateTime)
|
||||
.HasComment("修改时间")
|
||||
.HasColumnType("datetime");
|
||||
entity.Property(e => e.UserId).HasComment("用户");
|
||||
//添加全局筛选器
|
||||
if (this.TenantInfo != null)
|
||||
{
|
||||
entity.HasQueryFilter(it => it.TenantId == this.TenantInfo.TenantId);
|
||||
}
|
||||
});
|
||||
|
||||
modelBuilder.Entity<T_User_Data>(entity =>
|
||||
{
|
||||
entity.HasKey(e => new { e.Id, e.UserId }).HasName("PK__T_User_D__E36C60C3D959FD89");
|
||||
|
|
|
|||
|
|
@ -67,4 +67,14 @@ public partial class T_Character: MultiTenantEntity
|
|||
/// 角色头像(是id)
|
||||
/// </summary>
|
||||
public int? IconImg { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 对话名字
|
||||
/// </summary>
|
||||
public string? UserName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 对话性别
|
||||
/// </summary>
|
||||
public string? UserSex { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
using System;
|
||||
using System;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ public partial class T_ShoppingMall: MultiTenantEntity
|
|||
/// <summary>
|
||||
/// 道具名称
|
||||
/// </summary>
|
||||
public string? PropName { get; set; }
|
||||
public string PropName { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// 道具描述
|
||||
|
|
@ -25,12 +25,7 @@ public partial class T_ShoppingMall: MultiTenantEntity
|
|||
/// <summary>
|
||||
/// 道具图片配置 图片id
|
||||
/// </summary>
|
||||
public int? PropImgId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 兑换道具所需要的金币数量
|
||||
/// </summary>
|
||||
public int? ExchangeCurrencyCount { get; set; }
|
||||
public int PropImgId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 商品是否下架 0否1是
|
||||
|
|
@ -59,7 +54,12 @@ public partial class T_ShoppingMall: MultiTenantEntity
|
|||
public int? StockCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 道具类型
|
||||
/// 道具类型,0商城,1商店
|
||||
/// </summary>
|
||||
public int? PropType { get; set; }
|
||||
public int PropType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 价格
|
||||
/// </summary>
|
||||
public decimal Price { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
using System;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
|
||||
|
||||
/// <summary>
|
||||
/// 用户金额记录表
|
||||
/// </summary>
|
||||
public partial class T_User_Currency_Log: MultiTenantEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 用户
|
||||
/// </summary>
|
||||
public int UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 金额类型
|
||||
/// </summary>
|
||||
public int CurrencyType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 金额
|
||||
/// </summary>
|
||||
public decimal Consume { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 消耗类型,0消耗,1增加
|
||||
/// </summary>
|
||||
public int ConsumeType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
public string? Remarks { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 创建时间
|
||||
/// </summary>
|
||||
public DateTime CreateTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 修改时间
|
||||
/// </summary>
|
||||
public DateTime UpdateTime { get; set; }
|
||||
}
|
||||
|
|
@ -121,15 +121,19 @@ namespace HuanMeng.MiaoYu.Model.Dto.Character
|
|||
_system = ModelConfig.SystemTemplate ?? "";
|
||||
_system = _system
|
||||
.Replace("{assistant}", this.Name) //ai角色名称
|
||||
.Replace("{user}", "") //用户角色名称
|
||||
.Replace("{user}", this.UserName) //用户角色名称
|
||||
.Replace("{sex}", this.Gender == 0 ? "男" : "女") //角色
|
||||
.Replace("{biography}", this.Biography) //提示词 简介
|
||||
.Replace("{personality}", this.PersonalityStr) //性格
|
||||
.Replace("{prologue}", this.Prologue) //剧情
|
||||
.Replace("{genre}", this.TypeStr) //类型,分类
|
||||
.Replace("{label}", this.LabelStr) //标签
|
||||
.Replace("{user_sex}", this.UserSex)
|
||||
;
|
||||
|
||||
if (_system.Contains("[是一个。]"))
|
||||
{
|
||||
_system = _system .Replace("[是一个。]", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
return _system;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HuanMeng.MiaoYu.Model.EnumModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户消费类型
|
||||
/// </summary>
|
||||
public enum UserCurrencyConsumeType
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
消耗 = 0,
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
充值 = 1
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user