Files
Kg.SeaTime/Service/Application.Domain/Services/Service/User/UnitUserWeight.cs
2026-07-04 18:07:37 +08:00

230 lines
8.0 KiB
C#

namespace Application.Domain;
public class UnitUserWeight(ISqlSugarClient DbClient, IRedisCache redis) : IUnitUserWeight, ITransient
{
public async Task<unit_user_weight> GetUserWeightInfo(string userId)
{
string key = string.Format(UserCache.BaseCacheKeys, "WeightData", "Weight");
var data = await redis.GetHashAsync<unit_user_weight>(key, userId);
if (data == null)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_weight>();
data = await db.Queryable<unit_user_weight>().Where(it => it.userId == userId).SingleAsync();
await redis.AddHashAsync(key, userId, data);
}
data.maxWeight = await GetUserMaxWeight(data);
return data;
}
private async Task<int> GetUserMaxWeight(unit_user_weight data)
{
int result = 100 + (int)data.maxWeight;
var attrService = App.GetService<IUnitUserAttrService>();
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<bool> UpdateUserWeight(unit_user_weight data)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_weight>();
bool result = await db.Updateable(data).ExecuteCommandAsync() > 0;
if (result)
{
await ClearUserWeightInfo(data.userId);
}
return result;
}
public async Task<bool> UpdateUserWeight(string userId, int op, int weight)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_weight>();
bool result = await db.Updateable<unit_user_weight>()
.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<bool> UpdateUserMaxWeight(string userId, int op, int weight, int count = 1, int goodsId = 0,
string goodsName = "")
{
int sumWeight = weight * count;
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_weight>();
bool result = await db.Updateable<unit_user_weight>()
.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<bool> AddUserWeightLog(string userId, int goodsId, string goodsName, int weight, int count)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_weight_log>();
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<List<unit_user_weight_log>> GetUserWeightLog(string userId)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_weight_log>();
return await db.Queryable<unit_user_weight_log>().Where(it => it.userId == userId)
.ToListAsync();
}
public async Task<bool> 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<unit_user_ship> GetUserShipInfo(string usId)
{
string key = string.Format(UserCache.BaseCacheKeys, "UserShip", "Info");
var data = await redis.GetHashAsync<unit_user_ship>(key, usId);
if (data == null)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_ship>();
data = await db.Queryable<unit_user_ship>().Where(it => it.usId == usId).SingleAsync();
await redis.AddHashAsync(key, usId, data);
}
return data;
}
public async Task<List<unit_user_ship>> GetUserShip(string userId)
{
string key = string.Format(UserCache.BaseCacheKeys, "WeightData", "Ship");
var data = await redis.GetHashAsync<List<unit_user_ship>>(key, userId);
if (data == null)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_ship>();
data = await db.Queryable<unit_user_ship>().Where(it => it.userId == userId).ToListAsync();
await redis.AddHashAsync(key, userId, data);
}
return data;
}
public async Task<int> 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<bool> UpdateUserShipOnWeight(string userId, int op, int weight)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_weight>();
bool result = await db.Updateable<unit_user_weight>()
.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<bool> 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<bool> DeleteUserShip(string usId, string userId)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_ship>();
bool result = await db.Deleteable<unit_user_ship>().Where(it => it.usId == usId).ExecuteCommandAsync() > 0;
if (result)
{
await ClearUserShipData(userId);
}
return result;
}
public async Task<bool> 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<unit_user_ship>();
bool result = await db.Insertable(ship).ExecuteCommandAsync() > 0;
if (result)
{
await ClearUserShipData(userId);
}
return result;
}
#endregion
}