156 lines
5.4 KiB
C#
156 lines
5.4 KiB
C#
using System.Reflection.Metadata;
|
|
|
|
namespace Application.Domain;
|
|
|
|
public class GameGoodsService(ISqlSugarClient DbClient, IRedisCache redis) : IGameEquService, ITransient
|
|
{
|
|
#region 道具资源
|
|
|
|
public async Task<game_goods> GetGoodsInfo(int goodsId)
|
|
{
|
|
string key = string.Format(BaseCache.BaseCacheKey, "GoodsData");
|
|
var data = await redis.GetHashAsync<game_goods>(key, goodsId.ToString());
|
|
if (data == null)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<game_goods>();
|
|
data = await db.Queryable<game_goods>().Where(it => it.goodsId == goodsId).SingleAsync();
|
|
await redis.AddHashAsync(key, goodsId.ToString(), data);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<string> GetGoodsContent(int goodsId)
|
|
{
|
|
var goodsInfo = await GetGoodsInfo(goodsId);
|
|
if (goodsInfo == null)
|
|
{
|
|
return "";
|
|
}
|
|
else
|
|
{
|
|
return goodsInfo.content;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 个人道具
|
|
|
|
public async Task<unit_user_goods> GetUserGoodsInfo(string ugId)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_goods>();
|
|
return await db.Queryable<unit_user_goods>().Where(it => it.ugId == ugId).SingleAsync();
|
|
}
|
|
|
|
public async Task<unit_user_goods> GetUserGoodsInfo(string userId, int goodsId)
|
|
{
|
|
string ugId = GetUserGoodsKey(userId, goodsId);
|
|
return await GetUserGoodsInfo(ugId);
|
|
}
|
|
|
|
public async Task<int> GetUserGoodsCount(string userId, int goodsId)
|
|
{
|
|
var info = await GetUserGoodsInfo(userId, goodsId);
|
|
return info == null ? 0 : (int)info.count;
|
|
}
|
|
|
|
public async Task<List<unit_user_goods>> GetUserGoodsData(string userId, List<string> code, string search, int page,
|
|
int limit, RefAsync<int> total)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_goods>();
|
|
return await db.Queryable<unit_user_goods>().Where(it => it.userId == userId && it.count > 0)
|
|
.WhereIF(code.Count > 0, it => code.Contains(it.code))
|
|
.WhereIF(!string.IsNullOrEmpty(search), it => it.goodsName.Contains(search))
|
|
.OrderBy(it => it.lev, OrderByType.Desc)
|
|
.OrderBy(it => it.count, OrderByType.Desc)
|
|
.ToPageListAsync(page, limit, total);
|
|
}
|
|
|
|
|
|
public async Task<bool> UpdateUserGoods(string userId, int op, int goodsId, int count, string remark = "")
|
|
{
|
|
bool isOk = false;
|
|
unit_user_goods UserGoods = new unit_user_goods();
|
|
string ugId = GetUserGoodsKey(userId, goodsId);
|
|
try
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_goods>();
|
|
UserGoods = await db.Queryable<unit_user_goods>().Where(it => it.ugId == ugId).SingleAsync();
|
|
await DbClient.AsTenant().BeginTranAsync();
|
|
if (op == 1) //增加
|
|
{
|
|
if (UserGoods == null)
|
|
{
|
|
var goodsInfo = await GetGoodsInfo(goodsId);
|
|
UserGoods = new unit_user_goods();
|
|
UserGoods.ugId = ugId;
|
|
UserGoods.userId = userId;
|
|
UserGoods.goodsId = goodsId;
|
|
UserGoods.count = count;
|
|
UserGoods.lev = goodsInfo.lev;
|
|
UserGoods.code = goodsInfo.code;
|
|
UserGoods.goodsName = goodsInfo.goodsName;
|
|
UserGoods.weight = goodsInfo.weight;
|
|
UserGoods.sysPrice = goodsInfo.sysPrice;
|
|
UserGoods.isDeal = goodsInfo.isDeal;
|
|
UserGoods.isGive = goodsInfo.isGive;
|
|
isOk = await db.Insertable(UserGoods).ExecuteCommandAsync() > 0;
|
|
}
|
|
else
|
|
{
|
|
isOk = await db.Updateable<unit_user_goods>().SetColumns(it => it.count == it.count + count)
|
|
.Where(it => it.ugId == ugId).ExecuteCommandAsync() > 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
isOk = await db.Updateable<unit_user_goods>().SetColumns(it => it.count == it.count - count)
|
|
.Where(it => it.ugId == ugId).ExecuteCommandAsync() > 0;
|
|
}
|
|
|
|
await DbClient.AsTenant().CommitTranAsync();
|
|
}
|
|
catch
|
|
{
|
|
await DbClient.AsTenant().RollbackTranAsync();
|
|
isOk = false;
|
|
}
|
|
|
|
if (isOk)
|
|
{
|
|
//更新负重
|
|
|
|
//添加日志
|
|
|
|
await AddGoodsLog(userId, goodsId, op, count, remark);
|
|
}
|
|
|
|
return isOk;
|
|
}
|
|
|
|
private async Task<bool> AddGoodsLog(string userId, int goodsId, int code, int count, string remark)
|
|
{
|
|
unit_user_goods_log bagLog = new unit_user_goods_log();
|
|
bagLog.logId = StringAssist.NewGuid;
|
|
bagLog.userId = userId;
|
|
bagLog.goodsId = goodsId;
|
|
bagLog.code = code == 0 ? bagLog.code = nameof(GameEnum.LogCode.减少) : bagLog.code = nameof(GameEnum.LogCode.增加);
|
|
bagLog.count = count;
|
|
bagLog.addTime = DateTime.Now;
|
|
bagLog.remark = remark;
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_goods_log>();
|
|
return await db.Insertable(bagLog).SplitTable().ExecuteCommandAsync() > 0;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 辅助
|
|
|
|
private string GetUserGoodsKey(string userId, int goodsId)
|
|
{
|
|
return $"{userId}_{goodsId}";
|
|
}
|
|
|
|
#endregion
|
|
} |