Files
Yi.Admin/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/Forum/DiscussService.cs

344 lines
15 KiB
C#
Raw Normal View History

2023-12-13 16:29:58 +08:00
using System.Linq;
2025-01-19 15:17:48 +08:00
using Mapster;
2023-12-13 16:33:42 +08:00
using Microsoft.AspNetCore.Authorization;
2023-12-11 09:55:12 +08:00
using Microsoft.AspNetCore.Mvc;
2023-04-15 22:44:33 +08:00
using SqlSugar;
2024-01-29 11:20:40 +08:00
using TencentCloud.Pds.V20210701.Models;
2023-12-11 09:55:12 +08:00
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.EventBus.Local;
2023-12-13 16:29:58 +08:00
using Volo.Abp.Users;
using Yi.Framework.Bbs.Application.Contracts.Dtos.BbsUser;
2023-12-11 09:55:12 +08:00
using Yi.Framework.Bbs.Application.Contracts.Dtos.Discuss;
2025-01-19 15:17:48 +08:00
using Yi.Framework.Bbs.Application.Contracts.Dtos.DiscussLable;
2023-12-11 09:55:12 +08:00
using Yi.Framework.Bbs.Application.Contracts.IServices;
using Yi.Framework.Bbs.Domain.Entities;
2024-01-11 18:51:53 +08:00
using Yi.Framework.Bbs.Domain.Entities.Forum;
2023-12-11 09:55:12 +08:00
using Yi.Framework.Bbs.Domain.Managers;
2025-01-19 15:17:48 +08:00
using Yi.Framework.Bbs.Domain.Repositories;
2023-12-11 09:55:12 +08:00
using Yi.Framework.Bbs.Domain.Shared.Consts;
using Yi.Framework.Bbs.Domain.Shared.Enums;
2023-12-14 14:38:46 +08:00
using Yi.Framework.Bbs.Domain.Shared.Etos;
2023-12-11 09:55:12 +08:00
using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.User;
2023-12-24 11:45:43 +08:00
using Yi.Framework.Rbac.Domain.Authorization;
2023-12-11 09:55:12 +08:00
using Yi.Framework.Rbac.Domain.Entities;
2024-01-18 16:28:09 +08:00
using Yi.Framework.Rbac.Domain.Extensions;
2023-12-19 13:00:14 +08:00
using Yi.Framework.Rbac.Domain.Shared.Consts;
2023-12-11 09:55:12 +08:00
using Yi.Framework.SqlSugarCore.Abstractions;
2024-01-11 18:51:53 +08:00
namespace Yi.Framework.Bbs.Application.Services.Forum
2023-04-15 22:44:33 +08:00
{
/// <summary>
2024-03-07 11:32:49 +08:00
/// Discuss应用服务实现,用于参数校验、领域服务业务组合、日志记录、事务处理、账户信息
2023-04-15 22:44:33 +08:00
/// </summary>
2024-10-04 00:00:44 +08:00
public class DiscussService : YiCrudAppService<DiscussAggregateRoot, DiscussGetOutputDto, DiscussGetListOutputDto,
2025-04-12 23:18:06 +08:00
Guid, DiscussGetListInputVo, DiscussCreateInput, DiscussUpdateInput>,
2024-10-04 00:00:44 +08:00
IDiscussService
2023-04-15 22:44:33 +08:00
{
2024-10-04 00:00:44 +08:00
private ISqlSugarRepository<DiscussTopEntity> _discussTopRepository;
private ISqlSugarRepository<AgreeEntity> _agreeRepository;
2024-01-29 11:20:40 +08:00
private BbsUserManager _bbsUserManager;
2025-01-19 15:17:48 +08:00
private IDiscussLableRepository _discussLableRepository;
2025-06-02 02:12:38 +08:00
2024-10-04 00:00:44 +08:00
public DiscussService(BbsUserManager bbsUserManager, ForumManager forumManager,
ISqlSugarRepository<DiscussTopEntity> discussTopRepository,
ISqlSugarRepository<PlateAggregateRoot> plateEntityRepository, ILocalEventBus localEventBus,
2025-06-02 02:12:38 +08:00
ISqlSugarRepository<AgreeEntity> agreeRepository, IDiscussLableRepository discussLableRepository) : base(
forumManager._discussRepository)
2023-04-15 22:44:33 +08:00
{
_forumManager = forumManager;
2023-12-11 09:55:12 +08:00
_plateEntityRepository = plateEntityRepository;
_localEventBus = localEventBus;
2024-10-04 00:00:44 +08:00
_agreeRepository = agreeRepository;
2025-01-19 15:17:48 +08:00
_discussLableRepository = discussLableRepository;
2024-10-04 00:00:44 +08:00
_discussTopRepository = discussTopRepository;
_bbsUserManager = bbsUserManager;
2023-04-15 22:44:33 +08:00
}
2024-10-04 00:00:44 +08:00
2023-12-11 09:55:12 +08:00
private readonly ILocalEventBus _localEventBus;
2023-04-15 22:44:33 +08:00
private ForumManager _forumManager { get; set; }
2024-05-22 14:35:08 +08:00
private ISqlSugarRepository<PlateAggregateRoot> _plateEntityRepository { get; set; }
2025-06-02 02:12:38 +08:00
2023-04-15 22:44:33 +08:00
/// <summary>
/// 单查
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
2025-06-02 02:12:38 +08:00
public override async Task<DiscussGetOutputDto> GetAsync(Guid id)
2023-04-15 22:44:33 +08:00
{
//查询主题发布 浏览主题 事件,浏览数+1
2025-01-19 15:17:48 +08:00
var output = await _forumManager._discussRepository._DbQueryable
2024-10-04 00:00:44 +08:00
.LeftJoin<UserAggregateRoot>((discuss, user) => discuss.CreatorId == user.Id)
.LeftJoin<BbsUserExtraInfoEntity>((discuss, user, info) => user.Id == info.UserId)
2024-05-22 14:35:08 +08:00
.LeftJoin<PlateAggregateRoot>((discuss, user, info, plate) => plate.Id == discuss.PlateId)
2024-10-04 00:00:44 +08:00
.Select((discuss, user, info, plate) => new DiscussGetOutputDto
{
Id = discuss.Id,
2025-01-18 01:07:38 +08:00
IsAgree = false,
2024-10-04 00:00:44 +08:00
User = new BbsUserGetListOutputDto()
{
UserName = user.UserName,
Nick = user.Nick,
Icon = user.Icon,
Id = user.Id,
Level = info.Level,
UserLimit = info.UserLimit,
Money = info.Money,
Experience = info.Experience
},
Plate = new Contracts.Dtos.Plate.PlateGetOutputDto()
{
Name = plate.Name,
Id = plate.Id,
Code = plate.Code,
Introduction = plate.Introduction,
Logo = plate.Logo
}
}, true)
2025-01-18 01:07:38 +08:00
.FirstAsync(discuss => discuss.Id == id);
2023-04-15 22:44:33 +08:00
2025-06-02 02:12:38 +08:00
if (output is null)
2023-04-15 22:44:33 +08:00
{
2025-01-18 01:07:38 +08:00
throw new UserFriendlyException("该主题不存在", "404");
2023-04-15 22:44:33 +08:00
}
2025-04-12 23:18:06 +08:00
switch (output.DiscussType)
{
case DiscussTypeEnum.Article: break;
//查询的是悬赏主题
2025-06-02 02:12:38 +08:00
case DiscussTypeEnum.Reward:
var reward = await _forumManager._discussRewardRepository.GetAsync(x => x.DiscussId == output.Id);
2025-04-12 23:18:06 +08:00
output.RewardData = reward.Adapt<DiscussRewardGetOutputDto>();
break;
}
2025-06-02 02:12:38 +08:00
2025-01-18 01:07:38 +08:00
//组装点赞
var agreeCreatorList =
2025-06-02 02:12:38 +08:00
(await _agreeRepository._DbQueryable.Where(x => x.DiscussId == output.Id).Select(x => x.CreatorId)
.ToListAsync());
2025-01-18 01:07:38 +08:00
//已登录
if (CurrentUser.Id is not null)
{
2025-01-19 15:17:48 +08:00
output.IsAgree = agreeCreatorList.Contains(CurrentUser.Id);
2025-01-18 01:07:38 +08:00
}
2025-06-02 02:12:38 +08:00
2025-01-19 15:17:48 +08:00
//组装标签
2025-06-02 02:12:38 +08:00
var lableDic = await _discussLableRepository.GetDiscussLableCacheMapAsync();
2025-01-19 18:40:42 +08:00
foreach (var lableId in output.DiscussLableIds)
2025-01-19 15:17:48 +08:00
{
2025-06-02 02:12:38 +08:00
if (lableDic.TryGetValue(lableId, out var item))
2025-01-19 15:17:48 +08:00
{
output.Lables.Add(item.Adapt<DiscussLableGetOutputDto>());
}
}
2025-01-19 18:40:42 +08:00
//如果没有权限
2025-06-02 02:12:38 +08:00
if (!await _forumManager.VerifyDiscussPermissionAsync(output.Id, CurrentUser.Id, CurrentUser.Roles))
2025-01-19 18:40:42 +08:00
{
output.SetNoPermission();
}
else
{
output.SetPassPermission();
}
2025-06-02 02:12:38 +08:00
2025-01-18 01:07:38 +08:00
await _localEventBus.PublishAsync(new SeeDiscussEventArgs
2025-01-19 15:17:48 +08:00
{ DiscussId = output.Id, OldSeeNum = output.SeeNum });
return output;
2023-04-15 22:44:33 +08:00
}
2025-06-02 02:12:38 +08:00
2023-04-15 22:44:33 +08:00
/// <summary>
/// 查询
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
2024-10-04 00:00:44 +08:00
public override async Task<PagedResultDto<DiscussGetListOutputDto>> GetListAsync(
[FromQuery] DiscussGetListInputVo input)
2023-04-15 22:44:33 +08:00
{
//需要关联创建者用户
RefAsync<int> total = 0;
2023-12-11 09:55:12 +08:00
var items = await _forumManager._discussRepository._DbQueryable
2024-10-04 00:00:44 +08:00
.WhereIF(!string.IsNullOrEmpty(input.Title), x => x.Title.Contains(input.Title))
.WhereIF(input.PlateId is not null, x => x.PlateId == input.PlateId)
.WhereIF(input.IsTop is not null, x => x.IsTop == input.IsTop)
.WhereIF(input.UserId is not null, x => x.CreatorId == input.UserId)
.LeftJoin<UserAggregateRoot>((discuss, user) => discuss.CreatorId == user.Id)
.WhereIF(input.UserName is not null, (discuss, user) => user.UserName == input.UserName!)
.LeftJoin<BbsUserExtraInfoEntity>((discuss, user, info) => user.Id == info.UserId)
.OrderByDescending(discuss => discuss.OrderNum)
2025-02-05 11:36:20 +08:00
//已提示杰哥新增表达式
// .OrderByIF(input.Type == QueryDiscussTypeEnum.New,
2025-02-05 11:36:20 +08:00
// @"COALESCE(discuss.LastModificationTime, discuss.CreationTime) DESC")
//采用上方写法
2025-06-02 02:12:38 +08:00
.OrderByIF(input.Type == QueryDiscussTypeEnum.New, discuss => discuss.CreationTime, OrderByType.Desc)
// .OrderByIF(input.Type == QueryDiscussTypeEnum.New,discuss=>SqlFunc.Coalesce(discuss.LastModificationTime,discuss.CreationTime),OrderByType.Desc)
2024-10-04 00:00:44 +08:00
.OrderByIF(input.Type == QueryDiscussTypeEnum.Host, discuss => discuss.SeeNum, OrderByType.Desc)
.OrderByIF(input.Type == QueryDiscussTypeEnum.Suggest, discuss => discuss.AgreeNum, OrderByType.Desc)
.Select((discuss, user, info) => new DiscussGetListOutputDto
{
Id = discuss.Id,
// 优化查询,不使用子查询
// IsAgree = SqlFunc.Subqueryable<AgreeEntity>().WhereIF(CurrentUser.Id != null, x => x.CreatorId == CurrentUser.Id && x.DiscussId == discuss.Id).Any(),
User = new BbsUserGetListOutputDto()
{
Id = user.Id,
UserName = user.UserName,
Nick = user.Nick,
Icon = user.Icon,
Level = info.Level,
UserLimit = info.UserLimit,
Money = info.Money,
Experience = info.Experience
}
}, true)
2023-12-11 09:55:12 +08:00
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
2024-10-04 00:00:44 +08:00
var discussId = items.Select(x => x.Id);
//点赞字典key为主题idy为用户ids
var agreeDic =
(await _agreeRepository._DbQueryable.Where(x => discussId.Contains(x.DiscussId)).ToListAsync())
.GroupBy(x => x.DiscussId)
.ToDictionary(x => x.Key, y => y.Select(y => y.CreatorId).ToList());
2025-06-02 02:12:38 +08:00
var levelCacheDic = await _bbsUserManager.GetLevelCacheMapAsync();
var lableDic = await _discussLableRepository.GetDiscussLableCacheMapAsync();
2025-01-19 15:17:48 +08:00
//组装等级、是否点赞赋值、标签
2024-10-04 00:00:44 +08:00
items?.ForEach(x =>
{
2024-12-08 03:43:04 +08:00
x.User.LevelName = levelCacheDic[x.User.Level].Name;
2024-10-04 00:00:44 +08:00
if (CurrentUser.Id is not null)
{
//默认fasle
2025-06-02 02:12:38 +08:00
if (agreeDic.TryGetValue(x.Id, out var userIds))
2024-10-04 00:00:44 +08:00
{
x.IsAgree = userIds.Contains(CurrentUser.Id);
}
}
2025-06-02 02:12:38 +08:00
2025-01-19 18:40:42 +08:00
foreach (var lableId in x.DiscussLableIds)
2025-01-19 15:17:48 +08:00
{
2025-06-02 02:12:38 +08:00
if (lableDic.TryGetValue(lableId, out var item))
2025-01-19 15:17:48 +08:00
{
x.Lables.Add(item.Adapt<DiscussLableGetOutputDto>());
}
}
2024-10-04 00:00:44 +08:00
});
2023-04-15 22:44:33 +08:00
return new PagedResultDto<DiscussGetListOutputDto>(total, items);
}
2023-12-18 23:15:13 +08:00
/// <summary>
/// 获取首页的置顶主题
/// </summary>
/// <returns></returns>
public async Task<List<DiscussGetListOutputDto>> GetListTopAsync()
{
2024-10-04 00:00:44 +08:00
var output = await _discussTopRepository._DbQueryable
.LeftJoin<DiscussAggregateRoot>((top, discuss) => top.DiscussId == discuss.Id)
2024-05-22 14:35:08 +08:00
.LeftJoin<UserAggregateRoot>((top, discuss, user) => discuss.CreatorId == user.Id)
2023-12-22 23:32:21 +08:00
.LeftJoin<BbsUserExtraInfoEntity>((top, discuss, user, info) => user.Id == info.UserId)
.OrderByDescending(top => top.OrderNum)
.Select((top, discuss, user, info) => new DiscussGetListOutputDto
{
Id = discuss.Id,
2024-10-04 00:00:44 +08:00
IsAgree = SqlFunc.Subqueryable<AgreeEntity>().WhereIF(CurrentUser.Id != null,
x => x.CreatorId == CurrentUser.Id && x.DiscussId == discuss.Id).Any(),
2023-12-22 23:32:21 +08:00
User = new BbsUserGetListOutputDto
{
Id = user.Id,
2024-01-11 18:51:53 +08:00
Name = user.Name,
2023-12-22 23:32:21 +08:00
Sex = user.Sex,
State = user.State,
Address = user.Address,
Age = user.Age,
CreationTime = user.CreationTime,
2024-01-11 18:51:53 +08:00
Level = info.Level,
Introduction = user.Introduction,
Icon = user.Icon,
Nick = user.Nick,
UserName = user.UserName,
Remark = user.Remark,
2024-01-11 22:11:38 +08:00
UserLimit = info.UserLimit,
Money = info.Money,
2024-01-29 11:20:40 +08:00
Experience = info.Experience,
2023-12-22 23:32:21 +08:00
}
}, true)
.ToListAsync();
2025-06-02 02:12:38 +08:00
var levelCacheDic = await _bbsUserManager.GetLevelCacheMapAsync();
var lableDic = await _discussLableRepository.GetDiscussLableCacheMapAsync();
2025-01-19 15:17:48 +08:00
output?.ForEach(x =>
{
2025-06-02 02:12:38 +08:00
x.User.LevelName = levelCacheDic[x.User.Level].Name;
2025-01-19 18:40:42 +08:00
foreach (var lableId in x.DiscussLableIds)
2025-01-19 15:17:48 +08:00
{
2025-06-02 02:12:38 +08:00
if (lableDic.TryGetValue(lableId, out var item))
2025-01-19 15:17:48 +08:00
{
x.Lables.Add(item.Adapt<DiscussLableGetOutputDto>());
}
}
});
2023-12-18 23:15:13 +08:00
return output;
}
2023-04-15 22:44:33 +08:00
/// <summary>
/// 创建主题
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
2023-12-24 11:45:43 +08:00
[Permission("bbs:discuss:add")]
[Authorize]
2025-04-12 23:18:06 +08:00
public override async Task<DiscussGetOutputDto> CreateAsync(DiscussCreateInput input)
2023-04-15 22:44:33 +08:00
{
2023-12-19 13:00:14 +08:00
var plate = await _plateEntityRepository.FindAsync(x => x.Id == input.PlateId);
if (plate is null)
2023-04-15 22:44:33 +08:00
{
2023-10-04 23:16:07 +08:00
throw new UserFriendlyException(PlateConst.No_Exist);
2023-04-15 22:44:33 +08:00
}
2023-12-19 13:00:14 +08:00
2024-10-04 00:00:44 +08:00
if (await _forumManager._discussRepository.IsAnyAsync(x => x.Title == input.Title))
{
throw new UserFriendlyException(DiscussConst.Repeat);
}
2023-12-19 13:00:14 +08:00
//如果开启了禁用创建主题
if (plate.IsDisableCreateDiscuss == true)
{
2023-12-24 11:45:43 +08:00
//只有超级管理员权限才能进行发布
if (!CurrentUser.GetPermissions().Contains(UserConst.AdminPermissionCode))
2023-12-19 13:00:14 +08:00
{
throw new UserFriendlyException("该板块已禁止创建主题,请在其他板块中发布");
}
}
2025-06-02 02:12:38 +08:00
await _bbsUserManager.VerifyUserLimitAsync(CurrentUser.GetId());
var entity = await _forumManager.CreateDiscussAsync(await MapToEntityAsync(input),
input.RewardData.Adapt<DiscussRewardAggregateRoot>());
2023-04-15 22:44:33 +08:00
return await MapToGetOutputDtoAsync(entity);
}
2025-01-19 15:17:48 +08:00
2025-04-12 23:18:06 +08:00
/// <summary>
/// 设置悬赏主题已解决
/// </summary>
/// <param name="discussId"></param>
/// <exception cref="UserFriendlyException"></exception>
[HttpPut("discuss/reward/resolve/{discussId}")]
[Authorize]
2025-06-02 02:12:38 +08:00
public async Task SetRewardResolvedAsync([FromRoute] Guid discussId)
2025-04-12 23:18:06 +08:00
{
2025-06-02 02:12:38 +08:00
var reward = await _forumManager._discussRewardRepository.GetFirstAsync(x => x.DiscussId == discussId);
if (reward is null)
{
throw new UserFriendlyException("未找到该悬赏主题", "404");
}
2025-01-19 15:17:48 +08:00
2025-06-02 02:12:38 +08:00
//设置已解决
reward.SetResolved();
await _forumManager._discussRewardRepository.UpdateAsync(reward);
2025-01-19 15:17:48 +08:00
}
2023-04-15 22:44:33 +08:00
}
2024-10-04 00:00:44 +08:00
}