using DocumentFormat.OpenXml.Math;
using DotNetCore.CAP;
using Hotline.Application.Bulletin;
using Hotline.Application.FlowEngine;
using Hotline.Article;
using Hotline.Caching.Interfaces;
using Hotline.File;
using Hotline.Permissions;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.Settings;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.Article;
using Hotline.Share.Dtos.Caselibrary;
using Hotline.Share.Dtos.Users;
using Hotline.Share.Enums.Article;
using Hotline.Users;
using MapsterMapper;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Threading;
using XF.Domain.Authentications;
using XF.Domain.Exceptions;
using XF.Domain.Repository;
using XF.Utility.EnumExtensions;

namespace Hotline.Api.Controllers
{

    public class ArticleController : BaseController
    {
        private readonly IRepository<Bulletin> _bulletinRepository;
        private readonly IMapper _mapper;
        private readonly ISystemOrganizeRepository _organizeRepository;
        private readonly IWorkflowApplication _workflowApplication;
        private readonly IRepository<Circular> _circularRepository;
        private readonly ISessionContext _sessionContext;
        private readonly IRepository<CircularRecord> _circularRecordRepository;
        private readonly IRepository<CircularReadGroup> _circularReadGroupRepository;
        private readonly IRepository<User> _userRepository;
        private readonly ICircularRecordDomainService _circularRecordDomainService;
        private readonly ISystemDicDataCacheManager _systemDicDataCacheManager;
        private readonly IMediator _mediator;
        private readonly ICapPublisher _capPublisher;
        private readonly IBulletinApplication _bulletinApplication;
        private readonly IFileRepository _fileRepository;

        public ArticleController(IRepository<Bulletin> bulletinRepository,
            IMapper mapper,
            ISystemDomainService systemDomainService,
            ISystemOrganizeRepository organizeRepository,
            IWorkflowApplication workflowApplication,
            IRepository<Circular> circularRepository,
            ISessionContext sessionContext,
            IRepository<CircularRecord> circularRecordRepository,
            IRepository<CircularReadGroup> circularReadGroupRepository,
            IRepository<User> userRepository,
            ICircularRecordDomainService circularRecordDomainService,
            ISystemDicDataCacheManager systemDicDataCacheManager,
            IMediator mediator,
            ICapPublisher capPublisher,
            IBulletinApplication bulletinApplication,
            IFileRepository fileRepository
            )
        {
            _bulletinRepository = bulletinRepository;
            _mapper = mapper;
            _organizeRepository = organizeRepository;
            _workflowApplication = workflowApplication;
            _circularRepository = circularRepository;
            _sessionContext = sessionContext;
            _circularRecordRepository = circularRecordRepository;
            _circularReadGroupRepository = circularReadGroupRepository;
            _userRepository = userRepository;
            _circularRecordDomainService = circularRecordDomainService;
            _systemDicDataCacheManager = systemDicDataCacheManager;
            _mediator = mediator;
            _capPublisher = capPublisher;
            _bulletinApplication = bulletinApplication;
            _fileRepository = fileRepository;
        }
        #region 通知

        /// <summary>
        /// 获取所有用户不分页
        /// </summary>
        /// <returns></returns>
        [HttpGet("alluser")]
        public async Task<List<UserDto>> GetAllUser()
        {
            var list = await _userRepository.Queryable(includeDeleted: false)
                .Includes(d => d.Account)
                .Includes(d => d.Organization)
                .OrderByDescending(x => x.CreationTime)
                .ToListAsync();
            return new List<UserDto>(_mapper.Map<IReadOnlyList<UserDto>>(list));
        }

        /// <summary>
        /// 我的通知列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet("circular/muquery")]
        public async Task<PagedDto<CircularDto>> GetMyCircularList([FromQuery] QueryCircularListRequestDto dto)
        {
            var (total, items) = await _circularRepository.Queryable()
                .Includes(x => x.CircularReadGroups)
                .Includes(x => x.ExaminMan)
                .WhereIF(!string.IsNullOrEmpty(dto.CircularTypeId), d => d.CircularTypeId == dto.CircularTypeId)
                .WhereIF(!string.IsNullOrEmpty(dto.Title), d => d.Title.Contains(dto.Title))
                .WhereIF(dto.CircularTimeStart.HasValue, d => d.CircularTime >= dto.CircularTimeStart)
                .WhereIF(dto.CircularTimeEnd.HasValue, d => d.CircularTime <= dto.CircularTimeEnd)
                .WhereIF(dto.CircularState != null, d => d.CircularState == dto.CircularState)
                //.WhereIF(dto.IsRead!=null,d=>d.CircularReadGroups.Any(x=>x.IsRead == dto.IsRead) && (d.CircularReadGroups.Any(x => x.UserId == _sessionContext.RequiredUserId) || d.CircularReadGroups.Any(x => x.OrgId == _sessionContext.RequiredOrgId)))
                .WhereIF(dto.IsRead != null, d => (d.CircularReadGroups.Any(x => x.IsRead == dto.IsRead && x.UserId == _sessionContext.UserId)) ||
                (d.CircularReadGroups.Any(x => x.IsRead == dto.IsRead && x.OrgId == _sessionContext.OrgId)))
                //.Where(d=>d.CircularReadGroups.Any(x=> x.UserId == _sessionContext.RequiredUserId) || d.CircularReadGroups.Any(x=>x.OrgId == _sessionContext.RequiredOrgId))
                .OrderByDescending(d => d.CreationTime)
                .ToPagedListAsync(dto.PageIndex, dto.PageSize, HttpContext.RequestAborted);
            return new PagedDto<CircularDto>(total, _mapper.Map<IReadOnlyList<CircularDto>>(items));
        }


        /// <summary>
        /// 查询通知列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet("circular/query")]
        public async Task<PagedDto<CircularDto>> QueryCircularList([FromQuery] QueryCircularListRequestDto dto)
        {
            var (total, items) = await _circularRepository.Queryable()
                .Includes(x => x.CircularReadGroups)
                .Includes(x => x.ExaminMan)
                .WhereIF(!string.IsNullOrEmpty(dto.CircularTypeId), d => d.CircularTypeId == dto.CircularTypeId)
                .WhereIF(!string.IsNullOrEmpty(dto.Title), d => d.Title.Contains(dto.Title))
                .WhereIF(dto.CircularTimeStart.HasValue, d => d.CircularTime >= dto.CircularTimeStart)
                .WhereIF(dto.CircularTimeEnd.HasValue, d => d.CircularTime <= dto.CircularTimeEnd)
                .WhereIF(dto.CircularState != null, d => d.CircularState == dto.CircularState)
                .OrderByDescending(d => d.CreationTime)
                .ToPagedListAsync(dto.PageIndex, dto.PageSize, HttpContext.RequestAborted);
            return new PagedDto<CircularDto>(total, _mapper.Map<IReadOnlyList<CircularDto>>(items));
        }

        /// <summary>
        /// 通知详情（内部）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("circular/entity/{id}")]
        public async Task<CircularDto> CircularEntity(string id)
        {
            var model = await _circularRepository.Queryable()
                .Includes(x => x.CircularReadGroups)
                .Includes(x => x.ExaminMan)
                .FirstAsync(x => x.Id == id, HttpContext.RequestAborted);

            var CircularDto = _mapper.Map<CircularDto>(model);

            // 详情附件
            if (CircularDto.FileJson != null && CircularDto.FileJson.Any())
            {
                var ids = CircularDto.FileJson.Select(x => x.Id).ToList();
                CircularDto.Files = await _fileRepository.GetFilesAsync(ids, HttpContext.RequestAborted);
            }
            return _mapper.Map<CircularDto>(CircularDto);
        }

        /// <summary>
        /// 通知审核
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("circular/examine")]
        public async Task ExamineCircular([FromBody] ExamineCircularDto dto)
        {
            var circular = await _circularRepository.GetAsync(dto.Id, HttpContext.RequestAborted);
            if (circular == null)
                throw UserFriendlyException.SameMessage("无效数据");

            if (circular.CircularState != ECircularState.InReview)
                throw UserFriendlyException.SameMessage("当前状态不能审核");

            if (dto.IsPass)
            {
                //更新审核状态
                circular.CircularState = Share.Enums.Article.ECircularState.ReviewPass;
                circular.CircularTime = DateTime.Now;
                circular.ExaminOpinion = dto.Reason;
                circular.ExaminTime = DateTime.Now;
                circular.ExaminManId = _sessionContext.RequiredUserId;
                await _circularRepository.UpdateAsync(circular, HttpContext.RequestAborted);
                //更新阅读数量
                if (circular.CircularType == Share.Enums.Article.ECircularType.Person)
                {
                    //个人
                    var userlist = await _circularReadGroupRepository.Queryable()
                        .Where(x => x.CircularId == circular.Id)
                    .Select(x => x.UserId)
                        .ToListAsync(HttpContext.RequestAborted);
                    if (userlist != null && userlist.Count > 0)
                    {
                        await _circularRecordDomainService.RecordUserHandle(userlist, true, circular.Title, circular.Content);
                    }
                }
                else
                {
                    //部门
                    var orglist = await _circularReadGroupRepository.Queryable()
                        .Where(x => x.CircularId == circular.Id)
                    .Select(x => x.OrgId)
                        .ToListAsync(HttpContext.RequestAborted);
                    if (orglist != null && orglist.Count > 0)
                    {
                        await _circularRecordDomainService.RecordOrgHandle(orglist, true, circular.Title, circular.Content);
                    }
                }
            }
            else
            {
                //驳回
                circular.CircularState = Share.Enums.Article.ECircularState.ReviewNoPass;
                circular.ExaminOpinion = dto.Reason;
                circular.ExaminTime = DateTime.Now;
                circular.ExaminManId = _sessionContext.RequiredUserId;
                await _circularRepository.UpdateAsync(circular, HttpContext.RequestAborted);
            }
        }

        /// <summary>
        /// 提交通知
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("circular/commit/{id}")]
        public async Task CommitCircular(string id)
        {
            var circular = await _circularRepository.GetAsync(id, HttpContext.RequestAborted);
            if (circular == null)
                throw UserFriendlyException.SameMessage("无效数据");

            if (circular.CircularState != ECircularState.Draft && circular.CircularState != ECircularState.ReviewNoPass)
                throw UserFriendlyException.SameMessage("当前状态不能提交");

            circular.CircularState = ECircularState.InReview;
            circular.CommitTime = DateTime.Now;
            await _circularRepository.UpdateAsync(circular, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 修改通知
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("circular/update")]
        public async Task UpdateCircular([FromBody] UpdateCircularDto dto)
        {
            var circular = await _circularRepository.GetAsync(dto.Id, HttpContext.RequestAborted);
            if (circular == null)
                throw UserFriendlyException.SameMessage("无效数据");

            if (circular.CircularState != ECircularState.Draft && circular.CircularState != ECircularState.ReviewNoPass)
                throw UserFriendlyException.SameMessage("当前状态不能修改");

            //处理主表
            circular.Title = dto.Title;
            circular.Content = dto.Content;
            circular.CircularTypeId = dto.CircularTypeId;
            circular.CircularTypeName = dto.CircularTypeName;
            circular.CircularType = dto.CircularType;
            circular.IsMustRead = dto.IsMustRead;
            circular.SourceOrgId = dto.SourceOrgId;
            circular.SourceOrgName = dto.SourceOrgName;
            circular.NeedReadNum = dto.CircularReadGroups.Count;
            circular.LostEfficacyTime = DateTime.Now.AddYears(50);
            if (dto.Files != null && dto.Files.Count > 0)
                circular.FileJson = await _fileRepository.AddFileAsync(dto.Files, circular.Id, "", HttpContext.RequestAborted);
            else
                circular.FileJson = new List<Share.Dtos.File.FileJson>();
            await _circularRepository.UpdateAsync(circular, HttpContext.RequestAborted);
            //移除子表重新添加
            await _circularReadGroupRepository.RemoveAsync(x => x.CircularId == dto.Id, false, HttpContext.RequestAborted);

            var list = dto.CircularReadGroups;
            var readGroupList = new List<CircularReadGroup>();
            foreach (var item in list)
            {
                var readGroup = _mapper.Map<CircularReadGroup>(item);
                readGroup.CircularId = dto.Id;
                readGroup.IsRead = false;
                readGroupList.Add(readGroup);
            }
            await _circularReadGroupRepository.AddRangeAsync(readGroupList);
        }

        /// <summary>
        /// 删除通知
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("circular/del/{id}")]
        public async Task DelCircular(string id)
        {
            var circular = await _circularRepository.GetAsync(id, HttpContext.RequestAborted);
            if (circular == null)
                throw UserFriendlyException.SameMessage("无效数据");

            if (circular.CircularState != ECircularState.Draft)
                throw UserFriendlyException.SameMessage("当前状态不能删除");

            await _circularReadGroupRepository.RemoveAsync(x => x.CircularId == id, false, HttpContext.RequestAborted);
            await _circularRepository.RemoveAsync(x => x.Id == id, false, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 新增通知
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("circular/add")]
        public async Task AddCircular([FromBody] AddCircularDto dto)
        {
            var model = _mapper.Map<Circular>(dto);
            model.CircularState = ECircularState.Draft;
            model.ReadedNum = 0;
            model.NeedReadNum = model.CircularReadGroups.Count;
            model.LostEfficacyTime = DateTime.Now.AddYears(50);
            model.InitId();
            if (dto.Files != null && dto.Files.Count > 0)
                model.FileJson = await _fileRepository.AddFileAsync(dto.Files, model.Id, "", HttpContext.RequestAborted);
            var id = await _circularRepository.AddAsync(model, HttpContext.RequestAborted);
            //子表
            var list = dto.CircularReadGroups;
            var readGroupList = new List<CircularReadGroup>();
            foreach (var item in list)
            {
                var readGroup = _mapper.Map<CircularReadGroup>(item);
                readGroup.CircularId = id;
                readGroup.IsRead = false;
                readGroupList.Add(readGroup);
            }
            await _circularReadGroupRepository.AddRangeAsync(readGroupList);
        }

        /// <summary>
        /// 列表页面基础信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("circular/listbasedata")]
        public async Task<object> CircularBaseData()
        {
            var rsp = new
            {
                CircularType = _systemDicDataCacheManager.GetSysDicDataCache(SysDicTypeConsts.CircularType)
            };
            return rsp;
        }

        /// <summary>
        /// 新增页面基础数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("circular/addbasedata")]
        public async Task<object> CircularAddBaseData()
        {
            var rsp = new
            {
                CircularType = _systemDicDataCacheManager.GetSysDicDataCache(SysDicTypeConsts.CircularType),
                OrgsOptions = await _organizeRepository.GetOrgJson(),
                CircularTypeEnum = EnumExts.GetDescriptions<ECircularType>()
            };
            return rsp;
        }

        /// <summary>
        /// 通知阅读（内部）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("circular/read/{id}")]
        public async Task<CircularDto> CircularRead(string id)
        {
            var model = await _circularRepository.Queryable()
                .Includes(x => x.CircularReadGroups)
                .FirstAsync(x => x.Id == id, HttpContext.RequestAborted);

            //更新阅读信息
            if (model.CircularType == ECircularType.Person)
            {
                //个人阅读
                var readGroup = model.CircularReadGroups.FirstOrDefault(x => x.UserId == _sessionContext.RequiredUserId && !x.IsRead);
                if (readGroup != null)
                {
                    readGroup.IsRead = true;
                    readGroup.ReadTime = DateTime.Now;
                    if (DateTime.Now > model.LostEfficacyTime)
                        readGroup.IsTimeOut = true;
                    else
                        readGroup.IsTimeOut = false;
                    await _circularReadGroupRepository.UpdateAsync(readGroup, HttpContext.RequestAborted);
                    model.ReadedNum++;
                    await _circularRepository.UpdateAsync(model, HttpContext.RequestAborted);
                    //处理计数
                    await _circularRecordDomainService.RecordUserHandle(new List<string> { _sessionContext.RequiredUserId }, false, "", "", cancellationToken: HttpContext.RequestAborted);
                }
            }
            else
            {
                //部门阅读
                var readGroup = model.CircularReadGroups.FirstOrDefault(x => x.OrgId == _sessionContext.RequiredOrgId && !x.IsRead);
                if (readGroup != null)
                {
                    readGroup.IsRead = true;
                    readGroup.ReadTime = DateTime.Now;
                    if (DateTime.Now > model.LostEfficacyTime)
                        readGroup.IsTimeOut = true;
                    else
                        readGroup.IsTimeOut = false;
                    await _circularReadGroupRepository.UpdateAsync(readGroup, HttpContext.RequestAborted);
                    model.ReadedNum++;
                    await _circularRepository.UpdateAsync(model, HttpContext.RequestAborted);
                    //处理部门计数
                    await _circularRecordDomainService.RecordOrgHandle(new List<string> { _sessionContext.RequiredOrgId }, false, "", "", cancellationToken: HttpContext.RequestAborted);
                }
            }
            var res = _mapper.Map<CircularDto>(model);
			if (res.FileJson != null && res.FileJson.Any())
			{
				var ids = res.FileJson.Select(x => x.Id).ToList();
				res.Files = await _fileRepository.GetFilesAsync(ids, HttpContext.RequestAborted);
			}
			return res;
        }

        /// <summary>
        /// 获取小红点计数
        /// </summary>
        /// <returns></returns>
        [HttpGet("megcount")]
        public async Task<CircularRecordDto> QueryMegCount()
        {
            var list = await _circularRecordRepository.Queryable().Where(x => x.UserId == _sessionContext.RequiredUserId || x.OrgId == _sessionContext.RequiredOrgId).ToListAsync();
            if (list != null && list.Count > 0)
            {
                var rsp = new CircularRecordDto();
                rsp.OrgCount = list.FirstOrDefault(x => x.CircularType == ECircularType.Org)?.RecordCount ?? 0;
                rsp.PersonCount = list.FirstOrDefault(x => x.CircularType == ECircularType.Person)?.RecordCount ?? 0;
                return rsp;
            }
            return new CircularRecordDto() { OrgCount = 0, PersonCount = 0 };
        }

        #endregion


        #region 公告

        /// <summary>
        /// 查询公告列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet("bulletin/query")]
        public async Task<PagedDto<BulletinDto>> QueryBulletinList([FromQuery] QueryBulletinListRequestDto dto)
        {
            var (total, items) = await _bulletinRepository.Queryable()
                .Includes(x => x.ExaminMan)
                .WhereIF(!string.IsNullOrEmpty(dto.BulletinTypeId), d => d.BulletinTypeId == dto.BulletinTypeId)
                .WhereIF(!string.IsNullOrEmpty(dto.Title), d => d.Title.Contains(dto.Title))
                .WhereIF(dto.BulletinTimeStart.HasValue, d => d.BulletinTime >= dto.BulletinTimeStart)
                .WhereIF(dto.BulletinTimeEnd.HasValue, d => d.BulletinTime <= dto.BulletinTimeEnd)
                .WhereIF(dto.BulletinState != null, d => d.BulletinState == dto.BulletinState)
                .WhereIF(dto.IsLoseEfficacy != null && dto.IsLoseEfficacy == false, d => d.LoseEfficacyTime > DateTime.Now)
                .OrderByDescending(d => d.CreationTime)
                .ToPagedListAsync(dto.PageIndex, dto.PageSize, HttpContext.RequestAborted);

            return new PagedDto<BulletinDto>(total, _mapper.Map<IReadOnlyList<BulletinDto>>(items));
        }

        /// <summary>
        /// 公告详情（内部）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("bulletin/entity/{id}")]
        public async Task<BulletinDto> BulletinEntity(string id)
        {
            var model = await _bulletinRepository.Queryable()
                .Includes(x => x.ExaminMan)
                .FirstAsync(x => x.Id == id, HttpContext.RequestAborted);

            if (model != null && !string.IsNullOrEmpty(model.Content))
                model.Content = _bulletinApplication.GetSiteUrls(model.Content);

            var BulletinDto = _mapper.Map<BulletinDto>(model);

            // 详情附件
            if (BulletinDto.FileJson != null && BulletinDto.FileJson.Any())
            {
                var ids = BulletinDto.FileJson.Select(x => x.Id).ToList();
                BulletinDto.Files = await _fileRepository.GetFilesAsync(ids, HttpContext.RequestAborted);
            }

            return BulletinDto;
        }

        /// <summary>
        /// 公告审核
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("bulletin/examine")]
        public async Task ExamineBulletin([FromBody] ExamineBulletinDto dto)
        {
            var bulletin = await _bulletinRepository.GetAsync(dto.Id, HttpContext.RequestAborted);
            if (bulletin == null)
                throw UserFriendlyException.SameMessage("无效数据");

            if (bulletin.BulletinState != EBulletinState.InReview)
                throw UserFriendlyException.SameMessage("当前状态不能审核");

            if (dto.IsPass)
            {
                bulletin.BulletinState = Share.Enums.Article.EBulletinState.ReviewPass;
                bulletin.BulletinTime = DateTime.Now;
                bulletin.ExaminOpinion = dto.Reason;
                bulletin.ExaminTime = DateTime.Now;
                bulletin.ExaminManId = _sessionContext.RequiredUserId;
                await _bulletinRepository.UpdateAsync(bulletin, HttpContext.RequestAborted);
                var publishBulletin = _mapper.Map<PublishBulletinDto>(bulletin);

                await _capPublisher.PublishAsync(Hotline.Share.Mq.EventNames.HotlinePushBulletin, publishBulletin, cancellationToken: HttpContext.RequestAborted);

                //todo await _bulletinService.PushBulletin(publishBulletin, HttpContext.RequestAborted);
            }
            else
            {
                bulletin.ExaminOpinion = dto.Reason;
                bulletin.ExaminTime = DateTime.Now;
                bulletin.ExaminManId = _sessionContext.RequiredUserId;
                bulletin.BulletinState = Share.Enums.Article.EBulletinState.ReviewNoPass;
                await _bulletinRepository.UpdateAsync(bulletin, HttpContext.RequestAborted);
            }
        }

        /// <summary>
        /// 提交公告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("bulletin/commit")]
        public async Task CommitBulletin(string id)
        {
            var bulletin = await _bulletinRepository.GetAsync(id, HttpContext.RequestAborted);
            if (bulletin == null)
                throw UserFriendlyException.SameMessage("无效数据");

            if (bulletin.BulletinState != EBulletinState.Draft && bulletin.BulletinState != EBulletinState.ReviewNoPass)
                throw UserFriendlyException.SameMessage("当前状态不能提交");

            bulletin.BulletinState = EBulletinState.InReview;
            bulletin.CommitTime = DateTime.Now;
            await _bulletinRepository.UpdateAsync(bulletin, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 修改公告
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("bulletin/update")]
        public async Task UpdateBulletin([FromBody] UpdateBulletinDto dto)
        {
            var bulletin = await _bulletinRepository.GetAsync(dto.Id, HttpContext.RequestAborted);
            if (bulletin == null)
                throw UserFriendlyException.SameMessage("无效数据");

            if (bulletin.BulletinState != EBulletinState.Draft && bulletin.BulletinState != EBulletinState.ReviewNoPass)
                throw UserFriendlyException.SameMessage("当前状态不能修改");

            bulletin.Title = dto.Title;
            bulletin.Content = dto.Content;
            bulletin.BulletinTypeId = dto.BulletinTypeId;
            bulletin.BulletinTypeName = dto.BulletinTypeName;
            bulletin.LoseEfficacyTime = DateTime.Now.AddYears(50);
            bulletin.PushRanges = dto.PushRanges;
            bulletin.DisplayLocation = dto.DisplayLocation;
            bulletin.SourceOrgId = dto.SourceOrgId;
            bulletin.SourceOrgName = dto.SourceOrgName;
            if (dto.Files != null && dto.Files.Count > 0)
                bulletin.FileJson = await _fileRepository.AddFileAsync(dto.Files, bulletin.Id, "", HttpContext.RequestAborted);
            else
                bulletin.FileJson = new List<Share.Dtos.File.FileJson>();
            await _bulletinRepository.UpdateAsync(bulletin, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 删除公告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("bulletin/del/{id}")]
        public async Task DelBulletin(string id)
        {
            var bulletin = await _bulletinRepository.GetAsync(id, HttpContext.RequestAborted);
            if (bulletin == null)
                throw UserFriendlyException.SameMessage("无效数据");

            if (bulletin.BulletinState != EBulletinState.Draft)
                throw UserFriendlyException.SameMessage("当前状态不能删除");

            await _bulletinRepository.RemoveAsync(x => x.Id == id, false, HttpContext.RequestAborted);
        }


        /// <summary>
        /// 新增公告
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("bulletin/add")]
        public async Task AddBulletin([FromBody] AddBulletinDto dto)
        {
            var model = _mapper.Map<Bulletin>(dto);
            model.BulletinState = Share.Enums.Article.EBulletinState.Draft;
            model.ReadedNum = 0;
            model.IsArrive = false;
            model.LoseEfficacyTime = DateTime.Now.AddYears(50);
            model.InitId();
            if (dto.Files != null && dto.Files.Count > 0)
                model.FileJson = await _fileRepository.AddFileAsync(dto.Files, model.Id, "", HttpContext.RequestAborted);
            await _bulletinRepository.AddAsync(model, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 公告上架或者下架
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("bulletin-arrive")]
        public async Task BulletinArrive([FromBody] BulletinArriveDto dto)
        {
            var bulletin = await _bulletinRepository.GetAsync(dto.Id, HttpContext.RequestAborted);
            if (bulletin == null)
                throw UserFriendlyException.SameMessage("无效数据");

            if (bulletin.BulletinState != EBulletinState.ReviewPass)
                throw UserFriendlyException.SameMessage("当前状态不能操作上架或下架");

            bulletin.IsArrive = dto.IsArrive;
            if (bulletin.IsArrive == false)
            {
                bulletin.ExaminTime = null;
                bulletin.ExaminManId = null;
                bulletin.ExaminOpinion = null;
                bulletin.CommitTime = null;
                bulletin.BulletinState = EBulletinState.Draft;
            }
            await _bulletinRepository.UpdateAsync(bulletin, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 列表页面基础数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("bulletin/listbasedata")]
        public async Task<object> BulletinListBaseData()
        {
            var rsp = new
            {
                BulletinType = _systemDicDataCacheManager.GetSysDicDataCache(SysDicTypeConsts.BulletinType)
            };
            return rsp;
        }


        /// <summary>
        /// 新增页面基础数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("bulletin/addbasedata")]
        public async Task<object> BulletinAddBaseData()
        {
            var rsp = new
            {
                BulletinType = _systemDicDataCacheManager.GetSysDicDataCache(SysDicTypeConsts.BulletinType),
                BulletinDisplayLocation = _systemDicDataCacheManager.GetSysDicDataCache(SysDicTypeConsts.BulletinDisplayLocation),
                PushRanges = _systemDicDataCacheManager.GetSysDicDataCache(SysDicTypeConsts.BulletinPushRange),
                OrgsOptions = await _organizeRepository.GetOrgJson(),
            };
            return rsp;
        }


        #endregion



    }
}
