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.

190 lines
6.9 KiB

6 months ago
using System.ComponentModel;
using Common.Enum;
using Entity.Base;
using Entity.DbModel.System.SysBaseObject;
using Entity.Dto.Req;
using HybirdFrameworkCore.Autofac.Attribute;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Repository.System;
using Service.Mgr;
using SqlSugar;
namespace Service.System
{
[Scope("SingleInstance")]
public class SysNoticeService : BaseServices<SysNotice>
{
private readonly SysUserRepository _sysUserRep;
private readonly SysNoticeRepository _sysNoticeRep;
private readonly SysNoticeUserRepository _sysNoticeUserRep;
//private readonly SysOnlineUserService _sysOnlineUserService;
public SysNoticeService(
SysUserRepository sysUserRep,
SysNoticeRepository sysNoticeRep,
SysNoticeUserRepository sysNoticeUserRep
// SysOnlineUserService sysOnlineUserService
)
{
_sysUserRep = sysUserRep;
_sysNoticeRep = sysNoticeRep;
_sysNoticeUserRep = sysNoticeUserRep;
//_sysOnlineUserService = sysOnlineUserService;
}
/// <summary>
/// 获取通知公告分页列表 📢
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("获取通知公告分页列表")]
public async Task<SqlSugarPagedList<SysNotice>> Page(PageNoticeReq input)
{
RefAsync<int> total = 0;
var items = await _sysNoticeRep.QueryPageAsync(
entity => true,
6 months ago
!string.IsNullOrWhiteSpace(input.Title), u => u.Title.Contains(input.Title.Trim()),
input.Type > 0, u => u.Type == input.Type,
!UserManager.SuperAdmin, u => u.CreateUserId == UserManager.UserId,
u => u.CreateTime, input.Page, input.PageSize, total);
return SqlSugarPagedExtensions.CreateSqlSugarPagedList(items, total, input.Page, input.PageSize);
}
/// <summary>
/// 增加通知公告 📢
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "Add"), HttpPost]
[DisplayName("增加通知公告")]
public async Task AddNotice(AddNoticeReq input)
{
var notice = input.Adapt<SysNotice>();
InitNoticeInfo(notice);
await _sysNoticeRep.InsertAsync(notice);
}
/// <summary>
/// 更新通知公告 📢
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "Update"), HttpPost]
[DisplayName("更新通知公告")]
public async Task UpdateNotice(UpdateNoticeReq input)
{
var notice = input.Adapt<SysNotice>();
InitNoticeInfo(notice);
await _sysNoticeRep.UpdateAsync(notice);
}
/// <summary>
/// 删除通知公告 📢
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "Delete"), HttpPost]
[DisplayName("删除通知公告")]
public async Task DeleteNotice(DeleteNoticeReq 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(NoticeReq input)
{
// 更新发布状态和时间
await _sysNoticeRep.UpdateAsync(u => new SysNotice() { Status = NoticeStatusEnum.PUBLIC, PublicTime = DateTime.Now }, u => u.Id == input.Id);
var notice = await _sysNoticeRep.QueryByClauseAsync(u => u.Id == input.Id);
// 通知到的人(所有账号)
var userIdList = await _sysUserRep.QueryListBySelectClauseAsync(u => u.Id);
await _sysNoticeUserRep.DeleteAsync(u => u.NoticeId == notice.Id);
var noticeUserList = userIdList.Select(u => new SysNoticeUser
{
NoticeId = notice.Id,
UserId = u,
}).ToList();
await _sysNoticeUserRep.InsertAsync(noticeUserList);
// 广播所有在线账号
//TODO::
//await _sysOnlineUserService.PublicNotice(notice, userIdList);
}
/// <summary>
/// 设置通知公告已读状态 📢
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("设置通知公告已读状态")]
public async Task SetRead(NoticeReq 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] PageNoticeReq input)
//{
// RefAsync<int> total = 0;
// var items = await _sysNoticeUserRep.QueryPageByIncludeAsync(
// u => u.SysNotice,
// u => u.UserId == UserManager.UserId,
// !string.IsNullOrWhiteSpace(input.Title), u => u.SysNotice.Title.Contains(input.Title.Trim()),
// input.Type is > 0, u => u.SysNotice.Type == input.Type,
// u => u.SysNotice.CreateTime,
// input.Page, input.PageSize, total);
// return SqlSugarPagedExtensions.CreateSqlSugarPagedList(items, total, input.Page, input.PageSize);
//}
6 months ago
/// <summary>
/// 获取未读的通知公告 📢
/// </summary>
/// <returns></returns>
[DisplayName("获取未读的通知公告")]
public async Task<List<SysNotice>> GetUnReadList()
{
var noticeUserList = await _sysNoticeUserRep.QueryListByInludeClauseAsync(u => u.SysNotice,
u => u.UserId == UserManager.UserId && u.ReadStatus == NoticeUserStatusEnum.UNREAD,
u => u.SysNotice.CreateTime);
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;
}
}
}