namespace Application.Domain; public class UnitUserWeight(ISqlSugarClient DbClient, IRedisCache redis) : IUnitUserWeight, ITransient { public async Task GetUserWeightInfo(string userId) { string key = string.Format(UserCache.BaseCacheKeys, "WeightData", "Weight"); var data = await redis.GetHashAsync(key, userId); if (data == null) { var db = DbClient.AsTenant().GetConnectionWithAttr(); data = await db.Queryable().Where(it => it.userId == userId).SingleAsync(); await redis.AddHashAsync(key, userId, data); } data.maxWeight = await GetUserMaxWeight(data); return data; } private async Task GetUserMaxWeight(unit_user_weight data) { int result = 100 + (int)data.maxWeight; var attrService = App.GetService(); var userAttr = await attrService.GetUserAttrModel(data.userId); result += userAttr.lev * GameConfig.UpLevAddWeight; result += userAttr.weight; return result; } private async Task ClearUserWeightInfo(string userId) { string key = string.Format(UserCache.BaseCacheKeys, "WeightData", "Weight"); await redis.DelHashAsync(key, userId); } public async Task UpdateUserWeight(unit_user_weight data) { var db = DbClient.AsTenant().GetConnectionWithAttr(); bool result = await db.Updateable(data).ExecuteCommandAsync() > 0; if (result) { await ClearUserWeightInfo(data.userId); } return result; } public async Task UpdateUserWeight(string userId, int op, int weight) { var db = DbClient.AsTenant().GetConnectionWithAttr(); bool result = await db.Updateable() .SetColumnsIF(op == 0, it => it.onWeight == it.onWeight - weight) .SetColumnsIF(op == 1, it => it.onWeight == it.onWeight + weight) .SetColumnsIF(op == 2, it => it.onWeight == weight) .Where(it => it.userId == userId).ExecuteCommandAsync() > 0; if (result) { await ClearUserWeightInfo(userId); } return result; } public async Task UpdateUserMaxWeight(string userId, int op, int weight, int count = 1, int goodsId = 0, string goodsName = "") { int sumWeight = weight * count; var db = DbClient.AsTenant().GetConnectionWithAttr(); bool result = await db.Updateable() .SetColumnsIF(op == 0, it => it.maxWeight == it.maxWeight - sumWeight) .SetColumnsIF(op == 1, it => it.maxWeight == it.maxWeight + sumWeight) .Where(it => it.userId == userId).ExecuteCommandAsync() > 0; if (result) { if (op == 1) { await AddUserWeightLog(userId, goodsId, goodsName, weight, count); } else { await AddUserWeightLog(userId, goodsId, goodsName, (0 - weight), count); } await ClearUserWeightInfo(userId); } return result; } private async Task AddUserWeightLog(string userId, int goodsId, string goodsName, int weight, int count) { var db = DbClient.AsTenant().GetConnectionWithAttr(); unit_user_weight_log log = new unit_user_weight_log(); log.logId = StringAssist.NewGuid; log.userId = userId; log.goodsId = goodsId; log.goodsName = goodsName; log.weight = weight; log.count = count; log.sum = weight * count; log.addTime = DateTime.Now; return await db.Insertable(log).ExecuteCommandAsync() > 0; } public async Task> GetUserWeightLog(string userId) { var db = DbClient.AsTenant().GetConnectionWithAttr(); return await db.Queryable().Where(it => it.userId == userId) .ToListAsync(); } public async Task CheckUserWeight(string userId, int useWeight) { bool result = false; var weightInfo = await GetUserWeightInfo(userId); if (weightInfo != null) { result = (useWeight + weightInfo.onWeight) <= weightInfo.maxWeight; } return result; } #region 船只相关 public async Task GetUserShipInfo(string usId) { string key = string.Format(UserCache.BaseCacheKeys, "UserShip", "Info"); var data = await redis.GetHashAsync(key, usId); if (data == null) { var db = DbClient.AsTenant().GetConnectionWithAttr(); data = await db.Queryable().Where(it => it.usId == usId).SingleAsync(); await redis.AddHashAsync(key, usId, data); } return data; } public async Task> GetUserShip(string userId) { string key = string.Format(UserCache.BaseCacheKeys, "WeightData", "Ship"); var data = await redis.GetHashAsync>(key, userId); if (data == null) { var db = DbClient.AsTenant().GetConnectionWithAttr(); data = await db.Queryable().Where(it => it.userId == userId).ToListAsync(); await redis.AddHashAsync(key, userId, data); } return data; } public async Task GetUserShipMaxWeight(string userId) { var data = await GetUserShip(userId); return data.Sum(it => (int)it.weight); } private async Task ClearUserShipData(string userId) { string key = string.Format(UserCache.BaseCacheKeys, "WeightData", "Ship"); await redis.DelHashAsync(key, userId); } public async Task UpdateUserShipOnWeight(string userId, int op, int weight) { var db = DbClient.AsTenant().GetConnectionWithAttr(); bool result = await db.Updateable() .SetColumnsIF(op == 0, it => it.shipOnWeight == it.shipOnWeight - weight) .SetColumnsIF(op == 1, it => it.shipOnWeight == it.shipOnWeight + weight) .Where(it => it.userId == userId).ExecuteCommandAsync() > 0; if (result) { await ClearUserWeightInfo(userId); } return result; } public async Task CheakUserShipWeight(string userId, int useWeight) { bool result = false; var weightInfo = await GetUserWeightInfo(userId); var maxShipWeight = await GetUserShipMaxWeight(userId); if (weightInfo != null) { result = (useWeight + weightInfo.onWeight) <= maxShipWeight; } return result; } public async Task DeleteUserShip(string usId, string userId) { var db = DbClient.AsTenant().GetConnectionWithAttr(); bool result = await db.Deleteable().Where(it => it.usId == usId).ExecuteCommandAsync() > 0; if (result) { await ClearUserShipData(userId); } return result; } public async Task AddUserShip(string userId, string name, int goodsId, int speed, int weight,int copper,long sale,string remark) { unit_user_ship ship = new unit_user_ship(); ship.usId = StringAssist.NewGuid; ship.userId = userId; ship.goodsId = goodsId; ship.name = name; ship.speed = speed; ship.weight = weight; ship.copper = copper; ship.sale = sale; ship.remark = remark; var db = DbClient.AsTenant().GetConnectionWithAttr(); bool result = await db.Insertable(ship).ExecuteCommandAsync() > 0; if (result) { await ClearUserShipData(userId); } return result; } #endregion }