操控平台后端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
6.3 KiB

// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
namespace Admin.NET.Core.Service;
/// <summary>
/// 系统通知公告服务
/// </summary>
[ApiDescriptionSettings(Order = 380)]
public class SysNoticeService : IDynamicApiController, ITransient
{
private readonly UserManager _userManager;
private readonly SqlSugarRepository<SysUser> _sysUserRep;
private readonly SqlSugarRepository<SysNotice> _sysNoticeRep;
private readonly SqlSugarRepository<SysNoticeUser> _sysNoticeUserRep;
private readonly SysOnlineUserService _sysOnlineUserService;
public SysNoticeService(
UserManager userManager,
SqlSugarRepository<SysUser> sysUserRep,
SqlSugarRepository<SysNotice> sysNoticeRep,
SqlSugarRepository<SysNoticeUser> sysNoticeUserRep,
SysOnlineUserService sysOnlineUserService)
{
_userManager = userManager;
_sysUserRep = sysUserRep;
_sysNoticeRep = sysNoticeRep;
_sysNoticeUserRep = sysNoticeUserRep;
_sysOnlineUserService = sysOnlineUserService;
}
/// <summary>
/// 获取通知公告分页列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("获取通知公告分页列表")]
public async Task<SqlSugarPagedList<SysNotice>> Page(PageNoticeInput input)
{
return await _sysNoticeRep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.Title), u => u.Title.Contains(input.Title.Trim()))
.WhereIF(input.Type > 0, u => u.Type == input.Type)
.WhereIF(!_userManager.SuperAdmin, u => u.CreateUserId == _userManager.UserId)
.OrderBy(u => u.CreateTime, OrderByType.Desc)
.ToPagedListAsync(input.Page, input.PageSize);
}
/// <summary>
/// 增加通知公告
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "Add"), HttpPost]
[DisplayName("增加通知公告")]
public async Task AddNotice(AddNoticeInput input)
{
var notice = input.Adapt<SysNotice>();
InitNoticeInfo(notice);
await _sysNoticeRep.InsertAsync(notice);
}
/// <summary>
/// 更新通知公告
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[UnitOfWork]
[ApiDescriptionSettings(Name = "Update"), HttpPost]
[DisplayName("更新通知公告")]
public async Task UpdateNotice(UpdateNoticeInput input)
{
var notice = input.Adapt<SysNotice>();
InitNoticeInfo(notice);
await _sysNoticeRep.UpdateAsync(notice);
}
/// <summary>
/// 删除通知公告
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[UnitOfWork]
[ApiDescriptionSettings(Name = "Delete"), HttpPost]
[DisplayName("删除通知公告")]
public async Task DeleteNotice(DeleteNoticeInput input)
{
await _sysNoticeRep.DeleteAsync(u => u.Id == input.Id);
await _sysNoticeUserRep.DeleteAsync(u => u.NoticeId == input.Id);
}
/// <summary>
/// 发布通知公告
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("发布通知公告")]
public async Task Public(NoticeInput input)
{
// 更新发布状态和时间
await _sysNoticeRep.UpdateAsync(u => new SysNotice() { Status = NoticeStatusEnum.PUBLIC, PublicTime = DateTime.Now }, u => u.Id == input.Id);
var notice = await _sysNoticeRep.GetFirstAsync(u => u.Id == input.Id);
// 通知到的人(所有账号)
var userIdList = await _sysUserRep.AsQueryable().Select(u => u.Id).ToListAsync();
await _sysNoticeUserRep.DeleteAsync(u => u.NoticeId == notice.Id);
var noticeUserList = userIdList.Select(u => new SysNoticeUser
{
NoticeId = notice.Id,
UserId = u,
}).ToList();
await _sysNoticeUserRep.InsertRangeAsync(noticeUserList);
// 广播所有在线账号
await _sysOnlineUserService.PublicNotice(notice, userIdList);
}
/// <summary>
/// 设置通知公告已读状态
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("设置通知公告已读状态")]
public async Task SetRead(NoticeInput input)
{
await _sysNoticeUserRep.UpdateAsync(u => new SysNoticeUser
{
ReadStatus = NoticeUserStatusEnum.READ,
ReadTime = DateTime.Now
}, u => u.NoticeId == input.Id && u.UserId == _userManager.UserId);
}
/// <summary>
/// 获取接收的通知公告
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("获取接收的通知公告")]
public async Task<SqlSugarPagedList<SysNoticeUser>> GetPageReceived([FromQuery] PageNoticeInput input)
{
return await _sysNoticeUserRep.AsQueryable().Includes(u => u.SysNotice)
.Where(u => u.UserId == _userManager.UserId)
.WhereIF(!string.IsNullOrWhiteSpace(input.Title), u => u.SysNotice.Title.Contains(input.Title.Trim()))
.WhereIF(input.Type is > 0, u => u.SysNotice.Type == input.Type)
.OrderBy(u => u.SysNotice.CreateTime, OrderByType.Desc)
.ToPagedListAsync(input.Page, input.PageSize);
}
/// <summary>
/// 获取未读的通知公告
/// </summary>
/// <returns></returns>
[DisplayName("获取未读的通知公告")]
public async Task<List<SysNotice>> GetUnReadList()
{
var noticeUserList = await _sysNoticeUserRep.AsQueryable().Includes(u => u.SysNotice)
.Where(u => u.UserId == _userManager.UserId && u.ReadStatus == NoticeUserStatusEnum.UNREAD)
.OrderBy(u => u.SysNotice.CreateTime, OrderByType.Desc).ToListAsync();
return noticeUserList.Select(t => t.SysNotice).ToList();
}
/// <summary>
/// 初始化通知公告信息
/// </summary>
/// <param name="notice"></param>
[NonAction]
private void InitNoticeInfo(SysNotice notice)
{
notice.PublicUserId = _userManager.UserId;
notice.PublicUserName = _userManager.RealName;
notice.PublicOrgId = _userManager.OrgId;
}
}