refactor,feat: 批量代码重构与新增业务模块
1. 重命名枚举项与实体字段,修正类型引用 2. 新增IsNull扩展方法与多项字符串处理扩展 3. 新增大量业务DTO、服务接口与枚举定义 4. 重构RabbitMQ服务实现,替换旧版消息队列组件 5. 优化签到服务的宠物喂养事务逻辑 6. 移除冗余的项目引用与旧版消息队列代码 7. 新增Excel导出、导入模板相关工具方法
This commit is contained in:
@ -253,79 +253,103 @@ public class PetService(
|
||||
throw new BusinessException("宠物未激活,无法喂养", 400);
|
||||
}
|
||||
|
||||
// 事务保证一致性
|
||||
await UseTranAsync(async () =>
|
||||
{
|
||||
result = await FeedPetInTranAsync(userId, input);
|
||||
});
|
||||
|
||||
logger.LogInformation("喂养宠物成功,PetId: {PetId}, 成长值: {Before} -> {After}, 进化: {HasEvolved}",
|
||||
input.PetId, result.GrowthBefore, result.GrowthAfter, result.HasEvolved);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 喂养宠物(无事务,需在外部事务中调用)
|
||||
/// </summary>
|
||||
public async Task<FeedPetOutput> FeedPetInTranAsync(long userId, FeedPetInput input)
|
||||
{
|
||||
// 查询宠物
|
||||
var pet = await petRepository.GetByIdAsync(input.PetId);
|
||||
if (pet == null || pet.IsDeleted)
|
||||
throw new BusinessException("宠物不存在", 404);
|
||||
|
||||
if (pet.UserId != userId)
|
||||
throw new BusinessException("无权操作该宠物", 403);
|
||||
|
||||
if (pet.Status != (int)UserPetStatusEnum.Active)
|
||||
throw new BusinessException("宠物未激活,无法喂养", 400);
|
||||
|
||||
var growthBefore = pet.GrowthPoints;
|
||||
var growthAfter = growthBefore + input.GrowthPoints;
|
||||
var hasEvolved = false;
|
||||
string? evolvedStageName = null;
|
||||
|
||||
// 事务保证一致性
|
||||
await UseTranAsync(async () =>
|
||||
// 累加成长值和喂养次数
|
||||
var updateResult = await petRepository.Context.Updateable<UserPet>()
|
||||
.SetColumns(p => p.GrowthPoints == growthAfter)
|
||||
.SetColumns(p => p.FeedingCount == p.FeedingCount + 1)
|
||||
.SetColumns(p => p.UpdatedAt == DateTime.Now)
|
||||
.SetColumns(p => p.UpdatedBy == userId.ToString())
|
||||
.Where(p => p.Id == input.PetId)
|
||||
.ExecuteCommandAsync();
|
||||
|
||||
if (updateResult <= 0)
|
||||
{
|
||||
// 累加成长值和喂养次数
|
||||
var updateResult = await petRepository.Context.Updateable<UserPet>()
|
||||
.SetColumns(p => p.GrowthPoints == growthAfter)
|
||||
.SetColumns(p => p.FeedingCount == p.FeedingCount + 1)
|
||||
throw new BusinessException("更新宠物成长值失败", 500);
|
||||
}
|
||||
|
||||
// 进化检查:查找下一阶段进化形态(PreviousEvolutionId 类型为 long?)
|
||||
var nextEvolution = await petEvolutionRepository.Queryable()
|
||||
.Where(e => e.PreviousEvolutionId == pet.CurrentEvolutionId
|
||||
&& e.RequiredGrowth <= growthAfter
|
||||
&& e.Status == (int)DefaultStatusEnum.Active)
|
||||
.OrderBy(e => e.RequiredGrowth, OrderByType.Desc)
|
||||
.FirstAsync();
|
||||
|
||||
if (nextEvolution != null)
|
||||
{
|
||||
// 触发进化
|
||||
var evolveResult = await petRepository.Context.Updateable<UserPet>()
|
||||
.SetColumns(p => p.CurrentEvolutionId == nextEvolution.Id)
|
||||
.SetColumns(p => p.UpdatedAt == DateTime.Now)
|
||||
.SetColumns(p => p.UpdatedBy == userId.ToString())
|
||||
.Where(p => p.Id == input.PetId)
|
||||
.ExecuteCommandAsync();
|
||||
|
||||
if (updateResult <= 0)
|
||||
if (evolveResult > 0)
|
||||
{
|
||||
throw new BusinessException("更新宠物成长值失败", 500);
|
||||
hasEvolved = true;
|
||||
evolvedStageName = nextEvolution.StageName;
|
||||
logger.LogInformation("宠物进化成功,PetId: {PetId}, 新形态: {StageName} (Level {StageLevel})",
|
||||
input.PetId, nextEvolution.StageName, nextEvolution.StageLevel);
|
||||
}
|
||||
}
|
||||
|
||||
// 进化检查:查找下一阶段进化形态(PreviousEvolutionId 类型为 long?)
|
||||
var nextEvolution = await petEvolutionRepository.Queryable()
|
||||
.Where(e => e.PreviousEvolutionId == pet.CurrentEvolutionId
|
||||
&& e.RequiredGrowth <= growthAfter
|
||||
&& e.Status == (int)DefaultStatusEnum.Active)
|
||||
.OrderBy(e => e.RequiredGrowth, OrderByType.Desc)
|
||||
.FirstAsync();
|
||||
// 写入喂养记录
|
||||
var record = new PetFeedingRecord
|
||||
{
|
||||
PetId = input.PetId,
|
||||
UserId = userId,
|
||||
PointsUsed = 0, // 预留:后期可扩展为消耗积分喂养
|
||||
GrowthChange = input.GrowthPoints,
|
||||
GrowthBefore = growthBefore,
|
||||
GrowthAfter = growthAfter,
|
||||
Type = PetFeedingRecordTypeEnum.Normal,
|
||||
Status = (int)PetFeedingRecordStatusEnum.Success,
|
||||
IsDeleted = false,
|
||||
CreatedBy = userId.ToString(),
|
||||
CreatedAt = DateTime.Now,
|
||||
UpdatedBy = userId.ToString(),
|
||||
UpdatedAt = DateTime.Now
|
||||
};
|
||||
|
||||
if (nextEvolution != null)
|
||||
{
|
||||
// 触发进化
|
||||
var evolveResult = await petRepository.Context.Updateable<UserPet>()
|
||||
.SetColumns(p => p.CurrentEvolutionId == nextEvolution.Id)
|
||||
.SetColumns(p => p.UpdatedAt == DateTime.Now)
|
||||
.SetColumns(p => p.UpdatedBy == userId.ToString())
|
||||
.Where(p => p.Id == input.PetId)
|
||||
.ExecuteCommandAsync();
|
||||
|
||||
if (evolveResult > 0)
|
||||
{
|
||||
hasEvolved = true;
|
||||
evolvedStageName = nextEvolution.StageName;
|
||||
logger.LogInformation("宠物进化成功,PetId: {PetId}, 新形态: {StageName} (Level {StageLevel})",
|
||||
input.PetId, nextEvolution.StageName, nextEvolution.StageLevel);
|
||||
}
|
||||
}
|
||||
|
||||
// 写入喂养记录
|
||||
var record = new PetFeedingRecord
|
||||
{
|
||||
PetId = input.PetId,
|
||||
UserId = userId,
|
||||
PointsUsed = 0, // 预留:后期可扩展为消耗积分喂养
|
||||
GrowthChange = input.GrowthPoints,
|
||||
GrowthBefore = growthBefore,
|
||||
GrowthAfter = growthAfter,
|
||||
Type = PetFeedingRecordTypeEnum.Normal,
|
||||
Status = (int)PetFeedingRecordStatusEnum.Success,
|
||||
IsDeleted = false,
|
||||
CreatedBy = userId.ToString(),
|
||||
CreatedAt = DateTime.Now,
|
||||
UpdatedBy = userId.ToString(),
|
||||
UpdatedAt = DateTime.Now
|
||||
};
|
||||
|
||||
var insertResult = await feedingRecordRepository.InsertAsync(record);
|
||||
if (!insertResult)
|
||||
{
|
||||
throw new BusinessException("写入喂养记录失败", 500);
|
||||
}
|
||||
});
|
||||
var insertResult = await feedingRecordRepository.InsertAsync(record);
|
||||
if (!insertResult)
|
||||
{
|
||||
throw new BusinessException("写入喂养记录失败", 500);
|
||||
}
|
||||
|
||||
logger.LogInformation("喂养宠物成功,PetId: {PetId}, 成长值: {Before} -> {After}, 进化: {HasEvolved}",
|
||||
input.PetId, growthBefore, growthAfter, hasEvolved);
|
||||
|
||||
Reference in New Issue
Block a user