refactor: 批量新增枚举类型并完成实体、DTO、服务层枚举替换
- 新增30+业务枚举类型覆盖用户、宠物、商城、社区、积分等模块 - 完成实体类、DTO、服务层的字符串枚举替换为强类型枚举 - 修复用户状态枚举名称变更,将Frozen改为Disabled - 新增批量发布社区消息接口与控制器实现 - 新增操作日志、积分管理、补偿任务相关服务与DTO
This commit is contained in:
@ -4,6 +4,7 @@ using QYZH.InteractiveMagazine.Models.Common;
|
||||
using QYZH.InteractiveMagazine.Models.Dto;
|
||||
using QYZH.InteractiveMagazine.Models.Dto.Pet;
|
||||
using QYZH.InteractiveMagazine.Models.Entity;
|
||||
using QYZH.InteractiveMagazine.Models.Enum;
|
||||
using QYZH.InteractiveMagazine.Repository;
|
||||
using SqlSugar;
|
||||
|
||||
@ -84,8 +85,8 @@ public class PetService(
|
||||
CurrentSkinId = pet.CurrentSkinId,
|
||||
CurrentSkinName = skinName,
|
||||
CurrentImages = images,
|
||||
Type = pet.Type,
|
||||
Status = pet.Status,
|
||||
Type = pet.Type.ToString(),
|
||||
Status = pet.Status.ToString(),
|
||||
CreatedAt = pet.CreatedAt
|
||||
};
|
||||
}
|
||||
@ -108,7 +109,7 @@ public class PetService(
|
||||
|
||||
// 查询默认宠物模板(取排序权重最低且状态为Active的模板)
|
||||
var defaultTemplate = await petTemplateRepository.Queryable()
|
||||
.Where(t => t.Status == "Active" && !t.IsDeleted)
|
||||
.Where(t => t.Status == PetTemplateStatusEnum.Active && !t.IsDeleted)
|
||||
.OrderBy(t => t.SortOrder)
|
||||
.FirstAsync();
|
||||
|
||||
@ -122,7 +123,7 @@ public class PetService(
|
||||
var initialEvolution = await petEvolutionRepository.Queryable()
|
||||
.Where(e => e.TemplateId == defaultTemplate.Id
|
||||
&& e.PreviousEvolutionId == null
|
||||
&& e.Status == "Active")
|
||||
&& e.Status == PetEvolutionStatusEnum.Active)
|
||||
.OrderBy(e => e.StageLevel)
|
||||
.FirstAsync();
|
||||
|
||||
@ -135,8 +136,8 @@ public class PetService(
|
||||
GrowthPoints = 0,
|
||||
FeedingCount = 0,
|
||||
CurrentSkinId = 0,
|
||||
Type = "Normal",
|
||||
Status = "Inactive",
|
||||
Type = UserPetTypeEnum.Normal,
|
||||
Status = UserPetStatusEnum.Inactive,
|
||||
IsDeleted = false,
|
||||
CreatedBy = userId.ToString(),
|
||||
CreatedAt = DateTime.Now,
|
||||
@ -172,14 +173,14 @@ public class PetService(
|
||||
return;
|
||||
}
|
||||
|
||||
if (pet.Status != "Inactive")
|
||||
if (pet.Status != UserPetStatusEnum.Inactive)
|
||||
{
|
||||
logger.LogInformation("用户宠物已非未激活状态,跳过激活,UserId: {UserId}, Status: {Status}", userId, pet.Status);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = await petRepository.UpdateAsync(
|
||||
p => new UserPet { Status = "Active" },
|
||||
p => new UserPet { Status = UserPetStatusEnum.Active },
|
||||
p => p.UserId == userId);
|
||||
|
||||
if (!result)
|
||||
@ -225,7 +226,7 @@ public class PetService(
|
||||
}
|
||||
|
||||
// 校验宠物状态
|
||||
if (pet.Status != "Active")
|
||||
if (pet.Status != UserPetStatusEnum.Active)
|
||||
{
|
||||
logger.LogWarning("喂养失败,宠物未激活,PetId: {PetId}, Status: {Status}", input.PetId, pet.Status);
|
||||
throw new BusinessException("宠物未激活,无法喂养", 400);
|
||||
@ -257,7 +258,7 @@ public class PetService(
|
||||
var nextEvolution = await petEvolutionRepository.Queryable()
|
||||
.Where(e => e.PreviousEvolutionId == pet.CurrentEvolutionId
|
||||
&& e.RequiredGrowth <= growthAfter
|
||||
&& e.Status == "Active")
|
||||
&& e.Status == PetEvolutionStatusEnum.Active)
|
||||
.OrderBy(e => e.RequiredGrowth, OrderByType.Desc)
|
||||
.FirstAsync();
|
||||
|
||||
@ -289,8 +290,8 @@ public class PetService(
|
||||
GrowthChange = input.GrowthPoints,
|
||||
GrowthBefore = growthBefore,
|
||||
GrowthAfter = growthAfter,
|
||||
Type = "Normal",
|
||||
Status = "Success",
|
||||
Type = PetFeedingRecordTypeEnum.Normal,
|
||||
Status = PetFeedingRecordStatusEnum.Success,
|
||||
IsDeleted = false,
|
||||
CreatedBy = userId.ToString(),
|
||||
CreatedAt = DateTime.Now,
|
||||
@ -339,12 +340,663 @@ public class PetService(
|
||||
GrowthChange = r.GrowthChange,
|
||||
GrowthBefore = r.GrowthBefore,
|
||||
GrowthAfter = r.GrowthAfter,
|
||||
Type = r.Type,
|
||||
Status = r.Status,
|
||||
Type = r.Type.ToString(),
|
||||
Status = r.Status.ToString(),
|
||||
CreatedAt = r.CreatedAt
|
||||
}, true)
|
||||
.ToPageListAsync(pageQuery.PageIndex, pageQuery.PageSize, totalNumber);
|
||||
|
||||
return new PageListModel<FeedingRecordOutput>(records, pageQuery.PageIndex, pageQuery.PageSize, totalNumber);
|
||||
}
|
||||
|
||||
// ==================== 后台管理:宠物模板 ====================
|
||||
|
||||
/// <summary>
|
||||
/// 创建宠物模板
|
||||
/// </summary>
|
||||
public async Task<PetTemplateOutput> CreateTemplateAsync(PetTemplateInput input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input.Name))
|
||||
throw new BusinessException("模板名称不能为空", 400);
|
||||
|
||||
var template = new PetTemplate
|
||||
{
|
||||
Name = input.Name.Trim(),
|
||||
Description = input.Description,
|
||||
DefaultEvolutionId = input.DefaultEvolutionId,
|
||||
IconUrl = input.IconUrl,
|
||||
SortOrder = input.SortOrder,
|
||||
Type = Enum.Parse<PetTemplateTypeEnum>(input.Type, true),
|
||||
Status = PetTemplateStatusEnum.Active,
|
||||
CreatedBy = "System",
|
||||
CreatedAt = DateTime.Now,
|
||||
UpdatedBy = "System",
|
||||
UpdatedAt = DateTime.Now,
|
||||
IsDeleted = false
|
||||
};
|
||||
|
||||
var result = await petTemplateRepository.InsertAsync(template);
|
||||
if (!result)
|
||||
throw new BusinessException("创建宠物模板失败", 500);
|
||||
|
||||
logger.LogInformation("创建宠物模板成功,Id: {Id}, Name: {Name}", template.Id, template.Name);
|
||||
return BuildTemplateOutput(template);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新宠物模板
|
||||
/// </summary>
|
||||
public async Task<PetTemplateOutput> UpdateTemplateAsync(long id, PetTemplateInput input)
|
||||
{
|
||||
var template = await petTemplateRepository.GetByIdAsync(id);
|
||||
if (template == null || template.IsDeleted)
|
||||
throw new BusinessException("宠物模板不存在", 404);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.Name))
|
||||
throw new BusinessException("模板名称不能为空", 400);
|
||||
|
||||
template.Name = input.Name.Trim();
|
||||
template.Description = input.Description;
|
||||
template.DefaultEvolutionId = input.DefaultEvolutionId;
|
||||
template.IconUrl = input.IconUrl;
|
||||
template.SortOrder = input.SortOrder;
|
||||
template.Type = Enum.Parse<PetTemplateTypeEnum>(input.Type, true);
|
||||
template.UpdatedBy = "System";
|
||||
template.UpdatedAt = DateTime.Now;
|
||||
|
||||
var result = await petTemplateRepository.UpdateAsync(template);
|
||||
if (!result)
|
||||
throw new BusinessException("更新宠物模板失败", 500);
|
||||
|
||||
logger.LogInformation("更新宠物模板成功,Id: {Id}", id);
|
||||
return BuildTemplateOutput(template);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除宠物模板(软删除,校验是否有用户宠物关联)
|
||||
/// </summary>
|
||||
public async Task DeleteTemplateAsync(long id)
|
||||
{
|
||||
var template = await petTemplateRepository.GetByIdAsync(id);
|
||||
if (template == null || template.IsDeleted)
|
||||
throw new BusinessException("宠物模板不存在", 404);
|
||||
|
||||
// 校验是否有用户宠物实例关联
|
||||
var hasUserPet = petRepository.Context.Queryable<UserPet>()
|
||||
.Any(p => p.TemplateId == id && !p.IsDeleted);
|
||||
if (hasUserPet)
|
||||
throw new BusinessException("该模板下存在用户宠物实例,无法删除", 400);
|
||||
|
||||
template.IsDeleted = true;
|
||||
template.UpdatedBy = "System";
|
||||
template.UpdatedAt = DateTime.Now;
|
||||
await petTemplateRepository.UpdateAsync(template);
|
||||
|
||||
logger.LogInformation("删除宠物模板成功,Id: {Id}", id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取单个宠物模板
|
||||
/// </summary>
|
||||
public async Task<PetTemplateOutput> GetTemplateByIdAsync(long id)
|
||||
{
|
||||
var template = await petTemplateRepository.GetByIdAsync(id);
|
||||
if (template == null || template.IsDeleted)
|
||||
throw new BusinessException("宠物模板不存在", 404);
|
||||
|
||||
return BuildTemplateOutput(template);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询宠物模板列表
|
||||
/// </summary>
|
||||
public async Task<PageListModel<PetTemplateOutput>> GetTemplatesAsync(PetTemplateQueryInput input)
|
||||
{
|
||||
RefAsync<int> totalNumber = 0;
|
||||
var query = petTemplateRepository.Queryable()
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), t => t.Name.Contains(input.Name))
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Type), t => t.Type.ToString() == input.Type)
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Status), t => t.Status.ToString() == input.Status)
|
||||
.OrderBy(t => t.SortOrder)
|
||||
.OrderByDescending(t => t.CreatedAt);
|
||||
|
||||
var list = await query
|
||||
.Select(t => new PetTemplateOutput
|
||||
{
|
||||
Id = t.Id,
|
||||
Name = t.Name,
|
||||
Description = t.Description,
|
||||
DefaultEvolutionId = t.DefaultEvolutionId,
|
||||
IconUrl = t.IconUrl,
|
||||
SortOrder = t.SortOrder,
|
||||
Type = t.Type.ToString(),
|
||||
Status = t.Status.ToString(),
|
||||
CreatedBy = t.CreatedBy,
|
||||
CreatedAt = t.CreatedAt,
|
||||
UpdatedBy = t.UpdatedBy,
|
||||
UpdatedAt = t.UpdatedAt
|
||||
})
|
||||
.ToPageListAsync(input.PageIndex, input.PageSize, totalNumber);
|
||||
|
||||
return new PageListModel<PetTemplateOutput>(list, input.PageIndex, input.PageSize, totalNumber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新模板状态(启用/禁用)
|
||||
/// </summary>
|
||||
public async Task UpdateTemplateStatusAsync(long id, string status)
|
||||
{
|
||||
var template = await petTemplateRepository.GetByIdAsync(id);
|
||||
if (template == null || template.IsDeleted)
|
||||
throw new BusinessException("宠物模板不存在", 404);
|
||||
|
||||
template.Status = Enum.Parse<PetTemplateStatusEnum>(status, true);
|
||||
template.UpdatedBy = "System";
|
||||
template.UpdatedAt = DateTime.Now;
|
||||
await petTemplateRepository.UpdateAsync(template);
|
||||
|
||||
logger.LogInformation("更新模板状态成功,Id: {Id}, Status: {Status}", id, status);
|
||||
}
|
||||
|
||||
private static PetTemplateOutput BuildTemplateOutput(PetTemplate t)
|
||||
{
|
||||
return new PetTemplateOutput
|
||||
{
|
||||
Id = t.Id,
|
||||
Name = t.Name,
|
||||
Description = t.Description,
|
||||
DefaultEvolutionId = t.DefaultEvolutionId,
|
||||
IconUrl = t.IconUrl,
|
||||
SortOrder = t.SortOrder,
|
||||
Type = t.Type.ToString(),
|
||||
Status = t.Status.ToString(),
|
||||
CreatedBy = t.CreatedBy,
|
||||
CreatedAt = t.CreatedAt,
|
||||
UpdatedBy = t.UpdatedBy,
|
||||
UpdatedAt = t.UpdatedAt
|
||||
};
|
||||
}
|
||||
|
||||
// ==================== 后台管理:进化链 ====================
|
||||
|
||||
/// <summary>
|
||||
/// 创建进化阶段
|
||||
/// </summary>
|
||||
public async Task<PetEvolutionOutput> CreateEvolutionAsync(PetEvolutionInput input)
|
||||
{
|
||||
if (input.TemplateId <= 0)
|
||||
throw new BusinessException("模板Id不能为空", 400);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.StageName))
|
||||
throw new BusinessException("阶段名称不能为空", 400);
|
||||
|
||||
// 校验模板是否存在
|
||||
var templateExists = petTemplateRepository.Context.Queryable<PetTemplate>()
|
||||
.Any(t => t.Id == input.TemplateId && !t.IsDeleted);
|
||||
if (!templateExists)
|
||||
throw new BusinessException("宠物模板不存在", 404);
|
||||
|
||||
var evolution = new PetEvolution
|
||||
{
|
||||
TemplateId = input.TemplateId,
|
||||
StageName = input.StageName.Trim(),
|
||||
StageLevel = input.StageLevel,
|
||||
RequiredGrowth = input.RequiredGrowth,
|
||||
PreviousEvolutionId = input.PreviousEvolutionId,
|
||||
BaseStrength = input.BaseStrength,
|
||||
BaseAgility = input.BaseAgility,
|
||||
BaseIntelligence = input.BaseIntelligence,
|
||||
BaseCharm = input.BaseCharm,
|
||||
Type = Enum.Parse<PetEvolutionTypeEnum>(input.Type, true),
|
||||
Status = PetEvolutionStatusEnum.Active,
|
||||
CreatedBy = "System",
|
||||
CreatedAt = DateTime.Now,
|
||||
UpdatedBy = "System",
|
||||
UpdatedAt = DateTime.Now,
|
||||
IsDeleted = false
|
||||
};
|
||||
|
||||
var result = await petEvolutionRepository.InsertAsync(evolution);
|
||||
if (!result)
|
||||
throw new BusinessException("创建进化阶段失败", 500);
|
||||
|
||||
logger.LogInformation("创建进化阶段成功,Id: {Id}, StageName: {StageName}", evolution.Id, evolution.StageName);
|
||||
return BuildEvolutionOutput(evolution);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新进化阶段
|
||||
/// </summary>
|
||||
public async Task<PetEvolutionOutput> UpdateEvolutionAsync(long id, PetEvolutionInput input)
|
||||
{
|
||||
var evolution = await petEvolutionRepository.GetByIdAsync(id);
|
||||
if (evolution == null || evolution.IsDeleted)
|
||||
throw new BusinessException("进化阶段不存在", 404);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.StageName))
|
||||
throw new BusinessException("阶段名称不能为空", 400);
|
||||
|
||||
evolution.TemplateId = input.TemplateId;
|
||||
evolution.StageName = input.StageName.Trim();
|
||||
evolution.StageLevel = input.StageLevel;
|
||||
evolution.RequiredGrowth = input.RequiredGrowth;
|
||||
evolution.PreviousEvolutionId = input.PreviousEvolutionId;
|
||||
evolution.BaseStrength = input.BaseStrength;
|
||||
evolution.BaseAgility = input.BaseAgility;
|
||||
evolution.BaseIntelligence = input.BaseIntelligence;
|
||||
evolution.BaseCharm = input.BaseCharm;
|
||||
evolution.Type = Enum.Parse<PetEvolutionTypeEnum>(input.Type, true);
|
||||
evolution.UpdatedBy = "System";
|
||||
evolution.UpdatedAt = DateTime.Now;
|
||||
|
||||
var result = await petEvolutionRepository.UpdateAsync(evolution);
|
||||
if (!result)
|
||||
throw new BusinessException("更新进化阶段失败", 500);
|
||||
|
||||
logger.LogInformation("更新进化阶段成功,Id: {Id}", id);
|
||||
return BuildEvolutionOutput(evolution);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除进化阶段(软删除,校验是否有用户宠物处于该形态)
|
||||
/// </summary>
|
||||
public async Task DeleteEvolutionAsync(long id)
|
||||
{
|
||||
var evolution = await petEvolutionRepository.GetByIdAsync(id);
|
||||
if (evolution == null || evolution.IsDeleted)
|
||||
throw new BusinessException("进化阶段不存在", 404);
|
||||
|
||||
// 校验是否有用户宠物处于该形态
|
||||
var hasUserPet = petRepository.Context.Queryable<UserPet>()
|
||||
.Any(p => p.CurrentEvolutionId == id && !p.IsDeleted);
|
||||
if (hasUserPet)
|
||||
throw new BusinessException("有用户宠物正处于该形态,无法删除", 400);
|
||||
|
||||
evolution.IsDeleted = true;
|
||||
evolution.UpdatedBy = "System";
|
||||
evolution.UpdatedAt = DateTime.Now;
|
||||
await petEvolutionRepository.UpdateAsync(evolution);
|
||||
|
||||
logger.LogInformation("删除进化阶段成功,Id: {Id}", id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取单个进化阶段
|
||||
/// </summary>
|
||||
public async Task<PetEvolutionOutput> GetEvolutionByIdAsync(long id)
|
||||
{
|
||||
var evolution = await petEvolutionRepository.GetByIdAsync(id);
|
||||
if (evolution == null || evolution.IsDeleted)
|
||||
throw new BusinessException("进化阶段不存在", 404);
|
||||
|
||||
return BuildEvolutionOutput(evolution);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询进化阶段列表
|
||||
/// </summary>
|
||||
public async Task<PageListModel<PetEvolutionOutput>> GetEvolutionsAsync(PetEvolutionQueryInput input)
|
||||
{
|
||||
RefAsync<int> totalNumber = 0;
|
||||
var query = petEvolutionRepository.Queryable()
|
||||
.Where(e => e.TemplateId == input.TemplateId)
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.StageName), e => e.StageName.Contains(input.StageName))
|
||||
.OrderBy(e => e.StageLevel);
|
||||
|
||||
var list = await query
|
||||
.Select(e => new PetEvolutionOutput
|
||||
{
|
||||
Id = e.Id,
|
||||
TemplateId = e.TemplateId,
|
||||
StageName = e.StageName,
|
||||
StageLevel = e.StageLevel,
|
||||
RequiredGrowth = e.RequiredGrowth,
|
||||
PreviousEvolutionId = e.PreviousEvolutionId,
|
||||
BaseStrength = e.BaseStrength,
|
||||
BaseAgility = e.BaseAgility,
|
||||
BaseIntelligence = e.BaseIntelligence,
|
||||
BaseCharm = e.BaseCharm,
|
||||
Type = e.Type.ToString(),
|
||||
Status = e.Status.ToString(),
|
||||
CreatedBy = e.CreatedBy,
|
||||
CreatedAt = e.CreatedAt,
|
||||
UpdatedBy = e.UpdatedBy,
|
||||
UpdatedAt = e.UpdatedAt
|
||||
})
|
||||
.ToPageListAsync(input.PageIndex, input.PageSize, totalNumber);
|
||||
|
||||
return new PageListModel<PetEvolutionOutput>(list, input.PageIndex, input.PageSize, totalNumber);
|
||||
}
|
||||
|
||||
private static PetEvolutionOutput BuildEvolutionOutput(PetEvolution e)
|
||||
{
|
||||
return new PetEvolutionOutput
|
||||
{
|
||||
Id = e.Id,
|
||||
TemplateId = e.TemplateId,
|
||||
StageName = e.StageName,
|
||||
StageLevel = e.StageLevel,
|
||||
RequiredGrowth = e.RequiredGrowth,
|
||||
PreviousEvolutionId = e.PreviousEvolutionId,
|
||||
BaseStrength = e.BaseStrength,
|
||||
BaseAgility = e.BaseAgility,
|
||||
BaseIntelligence = e.BaseIntelligence,
|
||||
BaseCharm = e.BaseCharm,
|
||||
Type = e.Type.ToString(),
|
||||
Status = e.Status.ToString(),
|
||||
CreatedBy = e.CreatedBy,
|
||||
CreatedAt = e.CreatedAt,
|
||||
UpdatedBy = e.UpdatedBy,
|
||||
UpdatedAt = e.UpdatedAt
|
||||
};
|
||||
}
|
||||
|
||||
// ==================== 后台管理:皮肤 ====================
|
||||
|
||||
/// <summary>
|
||||
/// 创建皮肤
|
||||
/// </summary>
|
||||
public async Task<PetSkinOutput> CreateSkinAsync(PetSkinInput input)
|
||||
{
|
||||
if (input.TemplateId <= 0)
|
||||
throw new BusinessException("模板Id不能为空", 400);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.Name))
|
||||
throw new BusinessException("皮肤名称不能为空", 400);
|
||||
|
||||
// 校验模板是否存在
|
||||
var templateExists = petTemplateRepository.Context.Queryable<PetTemplate>()
|
||||
.Any(t => t.Id == input.TemplateId && !t.IsDeleted);
|
||||
if (!templateExists)
|
||||
throw new BusinessException("宠物模板不存在", 404);
|
||||
|
||||
var skin = new PetSkin
|
||||
{
|
||||
TemplateId = input.TemplateId,
|
||||
Name = input.Name.Trim(),
|
||||
Description = input.Description,
|
||||
Rarity = input.Rarity,
|
||||
SortOrder = input.SortOrder,
|
||||
Type = Enum.Parse<PetSkinTypeEnum>(input.Type, true),
|
||||
CreatedBy = "System",
|
||||
CreatedAt = DateTime.Now,
|
||||
UpdatedBy = "System",
|
||||
UpdatedAt = DateTime.Now,
|
||||
IsDeleted = false
|
||||
};
|
||||
|
||||
var result = await petSkinRepository.InsertAsync(skin);
|
||||
if (!result)
|
||||
throw new BusinessException("创建皮肤失败", 500);
|
||||
|
||||
logger.LogInformation("创建皮肤成功,Id: {Id}, Name: {Name}", skin.Id, skin.Name);
|
||||
return BuildSkinOutput(skin, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新皮肤
|
||||
/// </summary>
|
||||
public async Task<PetSkinOutput> UpdateSkinAsync(long id, PetSkinInput input)
|
||||
{
|
||||
var skin = await petSkinRepository.GetByIdAsync(id);
|
||||
if (skin == null || skin.IsDeleted)
|
||||
throw new BusinessException("皮肤不存在", 404);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.Name))
|
||||
throw new BusinessException("皮肤名称不能为空", 400);
|
||||
|
||||
skin.TemplateId = input.TemplateId;
|
||||
skin.Name = input.Name.Trim();
|
||||
skin.Description = input.Description;
|
||||
skin.Rarity = input.Rarity;
|
||||
skin.SortOrder = input.SortOrder;
|
||||
skin.Type = Enum.Parse<PetSkinTypeEnum>(input.Type, true);
|
||||
skin.UpdatedBy = "System";
|
||||
skin.UpdatedAt = DateTime.Now;
|
||||
|
||||
var result = await petSkinRepository.UpdateAsync(skin);
|
||||
if (!result)
|
||||
throw new BusinessException("更新皮肤失败", 500);
|
||||
|
||||
logger.LogInformation("更新皮肤成功,Id: {Id}", id);
|
||||
|
||||
// 查询关联图片
|
||||
var images = await petSkinImageRepository.Queryable()
|
||||
.Where(i => i.SkinId == id && !i.IsDeleted)
|
||||
.OrderBy(i => i.SortOrder)
|
||||
.ToListAsync();
|
||||
|
||||
return BuildSkinOutput(skin, images);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除皮肤(软删除,校验是否有用户宠物装备中)
|
||||
/// </summary>
|
||||
public async Task DeleteSkinAsync(long id)
|
||||
{
|
||||
var skin = await petSkinRepository.GetByIdAsync(id);
|
||||
if (skin == null || skin.IsDeleted)
|
||||
throw new BusinessException("皮肤不存在", 404);
|
||||
|
||||
// 校验是否有用户宠物正在使用该皮肤
|
||||
var inUse = petRepository.Context.Queryable<UserPet>()
|
||||
.Any(p => p.CurrentSkinId == id && !p.IsDeleted);
|
||||
if (inUse)
|
||||
throw new BusinessException("有用户宠物正在使用该皮肤,无法删除", 400);
|
||||
|
||||
skin.IsDeleted = true;
|
||||
skin.UpdatedBy = "System";
|
||||
skin.UpdatedAt = DateTime.Now;
|
||||
await petSkinRepository.UpdateAsync(skin);
|
||||
|
||||
logger.LogInformation("删除皮肤成功,Id: {Id}", id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取单个皮肤(含图片列表)
|
||||
/// </summary>
|
||||
public async Task<PetSkinOutput> GetSkinByIdAsync(long id)
|
||||
{
|
||||
var skin = await petSkinRepository.GetByIdAsync(id);
|
||||
if (skin == null || skin.IsDeleted)
|
||||
throw new BusinessException("皮肤不存在", 404);
|
||||
|
||||
var images = await petSkinImageRepository.Queryable()
|
||||
.Where(i => i.SkinId == id && !i.IsDeleted)
|
||||
.OrderBy(i => i.SortOrder)
|
||||
.ToListAsync();
|
||||
|
||||
return BuildSkinOutput(skin, images);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询皮肤列表
|
||||
/// </summary>
|
||||
public async Task<PageListModel<PetSkinOutput>> GetSkinsAsync(PetSkinQueryInput input)
|
||||
{
|
||||
RefAsync<int> totalNumber = 0;
|
||||
var query = petSkinRepository.Queryable()
|
||||
.Where(s => s.TemplateId == input.TemplateId)
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), s => s.Name.Contains(input.Name))
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(input.Rarity), s => s.Rarity == input.Rarity)
|
||||
.OrderBy(s => s.SortOrder)
|
||||
.OrderByDescending(s => s.CreatedAt);
|
||||
|
||||
var list = await query
|
||||
.Select(s => new PetSkinOutput
|
||||
{
|
||||
Id = s.Id,
|
||||
TemplateId = s.TemplateId,
|
||||
Name = s.Name,
|
||||
Description = s.Description,
|
||||
Rarity = s.Rarity,
|
||||
SortOrder = s.SortOrder,
|
||||
Type = s.Type.ToString(),
|
||||
CreatedBy = s.CreatedBy,
|
||||
CreatedAt = s.CreatedAt,
|
||||
UpdatedBy = s.UpdatedBy,
|
||||
UpdatedAt = s.UpdatedAt
|
||||
})
|
||||
.ToPageListAsync(input.PageIndex, input.PageSize, totalNumber);
|
||||
|
||||
// 批量查询预览图片
|
||||
var skinIds = list.Select(s => s.Id).ToList();
|
||||
if (skinIds.Count > 0)
|
||||
{
|
||||
var allImages = await petSkinImageRepository.Queryable()
|
||||
.Where(i => skinIds.Contains(i.SkinId) && !i.IsDeleted)
|
||||
.OrderBy(i => i.SortOrder)
|
||||
.ToListAsync();
|
||||
|
||||
var imageMap = allImages
|
||||
.GroupBy(i => i.SkinId)
|
||||
.ToDictionary(g => g.Key, g => g.Select(i => BuildSkinImageOutput(i)).ToList());
|
||||
|
||||
foreach (var skin in list)
|
||||
{
|
||||
if (imageMap.ContainsKey(skin.Id))
|
||||
skin.Images = imageMap[skin.Id];
|
||||
}
|
||||
}
|
||||
|
||||
return new PageListModel<PetSkinOutput>(list, input.PageIndex, input.PageSize, totalNumber);
|
||||
}
|
||||
|
||||
private static PetSkinOutput BuildSkinOutput(PetSkin s, List<PetSkinImage>? images)
|
||||
{
|
||||
return new PetSkinOutput
|
||||
{
|
||||
Id = s.Id,
|
||||
TemplateId = s.TemplateId,
|
||||
Name = s.Name,
|
||||
Description = s.Description,
|
||||
Rarity = s.Rarity,
|
||||
SortOrder = s.SortOrder,
|
||||
Type = s.Type.ToString(),
|
||||
CreatedBy = s.CreatedBy,
|
||||
CreatedAt = s.CreatedAt,
|
||||
UpdatedBy = s.UpdatedBy,
|
||||
UpdatedAt = s.UpdatedAt,
|
||||
Images = images?.Select(BuildSkinImageOutput).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
// ==================== 后台管理:皮肤图片 ====================
|
||||
|
||||
/// <summary>
|
||||
/// 创建皮肤图片
|
||||
/// </summary>
|
||||
public async Task<PetSkinImageOutput> CreateSkinImageAsync(PetSkinImageInput input)
|
||||
{
|
||||
if (input.SkinId <= 0)
|
||||
throw new BusinessException("皮肤Id不能为空", 400);
|
||||
|
||||
if (input.EvolutionStageId <= 0)
|
||||
throw new BusinessException("进化阶段Id不能为空", 400);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.ImageUrl))
|
||||
throw new BusinessException("图片地址不能为空", 400);
|
||||
|
||||
// 校验皮肤是否存在
|
||||
var skinExists = petSkinRepository.Context.Queryable<PetSkin>()
|
||||
.Any(s => s.Id == input.SkinId && !s.IsDeleted);
|
||||
if (!skinExists)
|
||||
throw new BusinessException("皮肤不存在", 404);
|
||||
|
||||
var image = new PetSkinImage
|
||||
{
|
||||
SkinId = input.SkinId,
|
||||
EvolutionStageId = input.EvolutionStageId,
|
||||
ImageUrl = input.ImageUrl.Trim(),
|
||||
SortOrder = input.SortOrder,
|
||||
Type = Enum.Parse<PetSkinImageTypeEnum>(input.Type, true),
|
||||
CreatedBy = "System",
|
||||
CreatedAt = DateTime.Now,
|
||||
UpdatedBy = "System",
|
||||
UpdatedAt = DateTime.Now,
|
||||
IsDeleted = false
|
||||
};
|
||||
|
||||
var result = await petSkinImageRepository.InsertAsync(image);
|
||||
if (!result)
|
||||
throw new BusinessException("创建皮肤图片失败", 500);
|
||||
|
||||
logger.LogInformation("创建皮肤图片成功,Id: {Id}, SkinId: {SkinId}", image.Id, image.SkinId);
|
||||
return BuildSkinImageOutput(image);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新皮肤图片
|
||||
/// </summary>
|
||||
public async Task<PetSkinImageOutput> UpdateSkinImageAsync(long id, PetSkinImageInput input)
|
||||
{
|
||||
var image = await petSkinImageRepository.GetByIdAsync(id);
|
||||
if (image == null || image.IsDeleted)
|
||||
throw new BusinessException("皮肤图片不存在", 404);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.ImageUrl))
|
||||
throw new BusinessException("图片地址不能为空", 400);
|
||||
|
||||
image.SkinId = input.SkinId;
|
||||
image.EvolutionStageId = input.EvolutionStageId;
|
||||
image.ImageUrl = input.ImageUrl.Trim();
|
||||
image.SortOrder = input.SortOrder;
|
||||
image.Type = Enum.Parse<PetSkinImageTypeEnum>(input.Type, true);
|
||||
image.UpdatedBy = "System";
|
||||
image.UpdatedAt = DateTime.Now;
|
||||
|
||||
var result = await petSkinImageRepository.UpdateAsync(image);
|
||||
if (!result)
|
||||
throw new BusinessException("更新皮肤图片失败", 500);
|
||||
|
||||
logger.LogInformation("更新皮肤图片成功,Id: {Id}", id);
|
||||
return BuildSkinImageOutput(image);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除皮肤图片(软删除)
|
||||
/// </summary>
|
||||
public async Task DeleteSkinImageAsync(long id)
|
||||
{
|
||||
var image = await petSkinImageRepository.GetByIdAsync(id);
|
||||
if (image == null || image.IsDeleted)
|
||||
throw new BusinessException("皮肤图片不存在", 404);
|
||||
|
||||
image.IsDeleted = true;
|
||||
image.UpdatedBy = "System";
|
||||
image.UpdatedAt = DateTime.Now;
|
||||
await petSkinImageRepository.UpdateAsync(image);
|
||||
|
||||
logger.LogInformation("删除皮肤图片成功,Id: {Id}", id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定皮肤的所有图片
|
||||
/// </summary>
|
||||
public async Task<List<PetSkinImageOutput>> GetSkinImagesBySkinIdAsync(long skinId)
|
||||
{
|
||||
var images = await petSkinImageRepository.Queryable()
|
||||
.Where(i => i.SkinId == skinId && !i.IsDeleted)
|
||||
.OrderBy(i => i.SortOrder)
|
||||
.ToListAsync();
|
||||
|
||||
return images.Select(BuildSkinImageOutput).ToList();
|
||||
}
|
||||
|
||||
private static PetSkinImageOutput BuildSkinImageOutput(PetSkinImage i)
|
||||
{
|
||||
return new PetSkinImageOutput
|
||||
{
|
||||
Id = i.Id,
|
||||
SkinId = i.SkinId,
|
||||
EvolutionStageId = i.EvolutionStageId,
|
||||
ImageUrl = i.ImageUrl,
|
||||
SortOrder = i.SortOrder,
|
||||
Type = i.Type.ToString(),
|
||||
CreatedBy = i.CreatedBy,
|
||||
CreatedAt = i.CreatedAt,
|
||||
UpdatedBy = i.UpdatedBy,
|
||||
UpdatedAt = i.UpdatedAt
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user