371 lines
14 KiB
C#
371 lines
14 KiB
C#
using System.Reflection.Metadata;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Application.Domain;
|
|
|
|
public class GameGoodsService(ISqlSugarClient DbClient, IRedisCache redis) : IGameGoodsService, 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, List<string> noCode,
|
|
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(noCode.Count > 0, it => !noCode.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<List<unit_user_goods>> GetUserGoodsData(string userId, List<string> code, List<string> noCode,
|
|
string search, int page,
|
|
int limit, RefAsync<int> total, int DealType)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_goods>();
|
|
return await db.Queryable<unit_user_goods>().Where(it =>
|
|
it.userId == userId && it.count > 0)
|
|
.WhereIF(DealType == 0, it => it.isDeal == 1)
|
|
.WhereIF(DealType == 1, it => it.isDeal == 1 && it.isGive == 1)
|
|
.WhereIF(code.Count > 0, it => code.Contains(it.code))
|
|
.WhereIF(noCode.Count > 0, it => !noCode.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<List<unit_user_goods>> GetUserGoodsData(string userId, string code)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_goods>();
|
|
return await db.Queryable<unit_user_goods>().Where(it => it.userId == userId && it.count > 0 && it.code == code)
|
|
.OrderBy(it => it.lev, OrderByType.Desc)
|
|
.OrderBy(it => it.count, OrderByType.Desc)
|
|
.ToListAsync();
|
|
}
|
|
|
|
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)
|
|
{
|
|
//更新负重
|
|
int weightCount = Convert.ToInt32(UserGoods.weight) * count;
|
|
if (weightCount > 0)
|
|
{
|
|
var weightService = App.GetService<IUnitUserWeight>();
|
|
if (UserGoods.code == nameof(GoodsEnum.Code.Cargo)) //跑商物品处理
|
|
{
|
|
await weightService.UpdateUserShipOnWeight(userId, op, weightCount);
|
|
}
|
|
else
|
|
{
|
|
await weightService.UpdateUserWeight(userId, op, weightCount);
|
|
}
|
|
}
|
|
|
|
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}";
|
|
}
|
|
|
|
public async Task<int> GetUserGoodWeightSum(string userId, int type)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_goods>();
|
|
string code = nameof(GoodsEnum.Code.Cargo);
|
|
var data = await db.Queryable<unit_user_goods>().Where(it => it.userId == userId)
|
|
.WhereIF(type == 0, it => it.code != code)
|
|
.WhereIF(type == 1, it => it.code == code)
|
|
.ToListAsync();
|
|
return data.Sum(it => (int)it.count * (int)it.weight);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 药品
|
|
|
|
private async Task<long> GetUserDrugLockTime(string userId, int goodsId)
|
|
{
|
|
long result = 0;
|
|
string key = string.Format(UserCache.BaseCacheKeys, "UserDrug", $"Lock:{userId}_{goodsId}");
|
|
if (await redis.ExistsAsync(key))
|
|
{
|
|
long end = await redis.GetAsync<long>(key);
|
|
result = end - TimeExtend.GetTimeStampSeconds;
|
|
result = result < 0 ? 0 : result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private async Task AddUserDrugLockTime(string userId, int goodsId, int time)
|
|
{
|
|
if (time > 0)
|
|
{
|
|
long end = TimeExtend.GetTimeStampSeconds + time;
|
|
string key = string.Format(UserCache.BaseCacheKeys, "UserDrug", $"Lock:{userId}_{goodsId}");
|
|
await redis.SetAsync(key, end, time);
|
|
}
|
|
}
|
|
|
|
private async Task<int> GetUserDrugLockCount(string userId, int goodsId, string scene = "")
|
|
{
|
|
int result = 0;
|
|
if (!string.IsNullOrEmpty(scene))
|
|
{
|
|
string maiId = $"Count:{scene}:{userId}_{goodsId}";
|
|
string key = string.Format(UserCache.BaseCacheKeys, "UserDrug", maiId);
|
|
if (await redis.ExistsAsync(key))
|
|
{
|
|
result = await redis.GetAsync<int>(key);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private async Task AddUserDrugLockCount(string userId, int goodsId, string scene = "")
|
|
{
|
|
if (!string.IsNullOrEmpty(scene))
|
|
{
|
|
var count = await GetUserDrugLockCount(userId, goodsId, scene);
|
|
count += 1;
|
|
string maiId = $"Count:{scene}:{userId}_{goodsId}";
|
|
string key = string.Format(UserCache.BaseCacheKeys, "UserDrug",maiId);
|
|
await redis.SetAsync(key, count, 43200);
|
|
}
|
|
}
|
|
|
|
|
|
public async Task<string> CheckUseDrug(string userId, int goodsId, string drugConfig, string scene = "")
|
|
{
|
|
string result = string.Empty;
|
|
dynamic _drugConfig = JsonConvert.DeserializeObject<dynamic>(drugConfig);
|
|
if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.Blood))
|
|
{
|
|
//判断所定
|
|
long lockTime = await GetUserDrugLockTime(userId, goodsId);
|
|
if (lockTime > 0)
|
|
{
|
|
result = $"药品冷却中,{lockTime}秒后可使用!";
|
|
}
|
|
}
|
|
else if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.BloodStock))
|
|
{
|
|
if (_drugConfig.type == nameof(GameEnum.GameStockType.Number))
|
|
{
|
|
var attrService = App.GetService<IUnitUserAttrService>();
|
|
var userStock = await attrService.GetUserStock(userId);
|
|
if (userStock.Count(it => it.goodsId == goodsId.ToString()) > 0)
|
|
{
|
|
result = "您已经使用过该药品啦!";
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(scene))
|
|
{
|
|
//要判断使用个数
|
|
int useCount = Convert.ToInt32(_drugConfig.use);
|
|
if (useCount > 0)
|
|
{
|
|
var count = await GetUserDrugLockCount(userId, goodsId, scene);
|
|
if (count >= Convert.ToInt32(useCount))
|
|
{
|
|
result = $"当前最多可用{useCount}个该物品!";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<string> CheckUseDrug(string userId, int goodsId, string scene = "")
|
|
{
|
|
string result = string.Empty;
|
|
var goodsInfo = await GetGoodsContent(goodsId);
|
|
if (!string.IsNullOrEmpty(goodsInfo))
|
|
{
|
|
result = await CheckUseDrug(userId, goodsId, goodsInfo, scene);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> UseDrug(string userId, int goodsId, string drugConfig, string scene = "")
|
|
{
|
|
bool result = false;
|
|
var attrService = App.GetService<IUnitUserAttrService>();
|
|
dynamic _drugConfig = JsonConvert.DeserializeObject<dynamic>(drugConfig);
|
|
if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.Blood))
|
|
{
|
|
result = await attrService.UpdateUserBlood(userId, 1, Convert.ToInt32(_drugConfig.num), true);
|
|
if (result)
|
|
{
|
|
await AddUserDrugLockTime(userId, goodsId, Convert.ToInt32(_drugConfig.time));
|
|
}
|
|
}
|
|
else if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.Morale))
|
|
{
|
|
result = await attrService.UpdateUserMorale(userId, 1, Convert.ToInt32(_drugConfig.num));
|
|
}
|
|
else if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.Vigour))
|
|
{
|
|
result = await attrService.UpdateUserVigour(userId, 1, Convert.ToInt32(_drugConfig.num));
|
|
}
|
|
else if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.LoadBuff))
|
|
{
|
|
result = await attrService.RandomRemoveUserLoadState(userId, Convert.ToInt32(_drugConfig.num));
|
|
}
|
|
else if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.BloodStock))
|
|
{
|
|
result = await attrService.AddUserStock(userId, goodsId.ToString(), Convert.ToString(_drugConfig.name),
|
|
Convert.ToString(_drugConfig.type),
|
|
Convert.ToString(_drugConfig.code),
|
|
Convert.ToInt64(_drugConfig.num), Convert.ToString(_drugConfig.par),
|
|
Convert.ToInt32(_drugConfig.minute));
|
|
if (result)
|
|
{
|
|
await AddUserDrugLockCount(userId, goodsId, scene);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> UseDrug(string userId, int goodsId, string scene = "")
|
|
{
|
|
bool result = false;
|
|
var goodsInfo = await GetGoodsContent(goodsId);
|
|
if (!string.IsNullOrEmpty(goodsInfo))
|
|
{
|
|
result = await UseDrug(userId, goodsId, goodsInfo, scene);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
} |