//--------------------------------------------------------------------
//     此代码由T4模板自动生成  byzhouhang 20210918
//	   生成时间 2022-01-05 09:17:03
//     对此文件的更改可能会导致不正确的行为,并且如果重新生成代码,这些更改将会丢失。
//--------------------------------------------------------------------
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Domain.Models;
using IRaCIS.Core.Domain;
using Microsoft.AspNetCore.Mvc;
using UserRole = IRaCIS.Core.Domain.Models.UserRole;
using IRaCIS.Core.Application.MassTransit.Consumer;
using MassTransit.Mediator;
using Microsoft.Extensions.DependencyInjection;
namespace IRaCIS.Core.Application.Services
{
    /// 
    /// SystemDocumentService
    /// 
    [ApiExplorerSettings(GroupName = "Trial")]
    public class SystemDocumentService(IRepository _systemDocumentRepository,
        IRepository _userRoleRepository,
        IScopedMediator _mediatorScoped,
        IRepository _systemDocumentAttachmentRepository,
          IRepository _identityUserRepository,
          IServiceScopeFactory serviceScopeFactory,
        IRepository _systemDocConfirmedUserRepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, ISystemDocumentService
    {
        /// 
        /// 获取系统文档附件
        /// 
        /// 
        /// 
        [HttpPost]
        public async Task> GetSystemDocumentAttachmentList(SystemDocumentAttachmentQuery inQuery)
        {
            var systemDocumentAttachmentQueryable = _systemDocumentAttachmentRepository
                        .WhereIf(inQuery.SystemDocumentId != null, t => t.SystemDocumentId == inQuery.SystemDocumentId)
                           .WhereIf(inQuery.OffLine != null, t => t.OffLine == inQuery.OffLine)
                              .WhereIf(inQuery.FileName != null, t => t.FileName == inQuery.FileName)
                                    .WhereIf(inQuery.FileFormat != null, t => t.FileFormat == inQuery.FileFormat)
                                        .ProjectTo(_mapper.ConfigurationProvider);
            var pageList = await systemDocumentAttachmentQueryable.ToPagedListAsync(inQuery);
            return pageList;
        }
        /// 
        /// 新增或编辑系统文档附件
        /// 
        /// 
        /// 
        public async Task AddOrUpdateSystemDocumentAttachment(SystemDocumentAttachmentAddOrEdit addOrEditSystemDocumentAttachment)
        {
            var entity = await _systemDocumentAttachmentRepository.InsertOrUpdateAsync(addOrEditSystemDocumentAttachment, true);
            return ResponseOutput.Ok(entity.Id.ToString());
        }
        /// 
        /// 删除系统文档附件
        /// 
        /// 
        /// 
        [HttpDelete("{systemDocumentAttachmentId:guid}")]
        public async Task DeleteSystemDocumentAttachment(Guid systemDocumentAttachmentId)
        {
            var success = await _systemDocumentAttachmentRepository.DeleteFromQueryAsync(t => t.Id == systemDocumentAttachmentId, true);
            return ResponseOutput.Ok();
        }
        /// 
        /// 管理端列表
        /// 
        /// 
        /// 
        [HttpPost]
        public async Task> GetSystemDocumentListAsync(SystemDocumentQuery inQuery)
        {
            var systemDocumentQueryable = _systemDocumentRepository.AsQueryable(true)
               .WhereIf(!string.IsNullOrEmpty(inQuery.Name), t => t.Name.Contains(inQuery.Name))
               .WhereIf(inQuery.FileTypeId != null, t => t.FileTypeId == inQuery.FileTypeId)
               .WhereIf(inQuery.DocUserSignType != null, t => t.DocUserSignType == inQuery.DocUserSignType)
               .WhereIf(inQuery.BeginCreateTime != null, t => t.CreateTime >= inQuery.BeginCreateTime)
               .WhereIf(inQuery.EndCreateTime != null, t => t.CreateTime <= inQuery.EndCreateTime)
                .WhereIf(inQuery.UserTypeId != null, t => t.NeedConfirmedUserTypeList.Any(t => t.NeedConfirmUserTypeId == inQuery.UserTypeId))
               .WhereIf(inQuery.IsDeleted != null, t => t.IsDeleted == inQuery.IsDeleted)
               .ProjectTo(_mapper.ConfigurationProvider, new { isEn_Us = _userInfo.IsEn_Us, userId = _userInfo.UserRoleId });
            return await systemDocumentQueryable.ToPagedListAsync(inQuery);
        }
        public async Task AddOrUpdateSystemDocumentAsync(AddOrEditSystemDocument addOrEditSystemDocument)
        {
            if (addOrEditSystemDocument.Id == null)
            {
                var entity = _mapper.Map(addOrEditSystemDocument);
                if (await _systemDocumentRepository.AnyAsync(t => t.FileTypeId == addOrEditSystemDocument.FileTypeId && t.Name == addOrEditSystemDocument.Name, true))
                {
                    //---系统中已存在同类型的同名文件。
                    return ResponseOutput.NotOk(_localizer["SystemD_DuplicateFile"]);
                }
                await _systemDocumentRepository.AddAsync(entity, true);
                return ResponseOutput.Ok(entity.Id.ToString());
            }
            else
            {
                var document = (await _systemDocumentRepository.Where(t => t.Id == addOrEditSystemDocument.Id, true, true).Include(t => t.NeedConfirmedUserTypeList).FirstOrDefaultAsync()).IfNullThrowException();
                if (await _systemDocumentRepository.AnyAsync(t => t.FileTypeId == addOrEditSystemDocument.FileTypeId && t.Name == addOrEditSystemDocument.Name && t.Id != addOrEditSystemDocument.Id, true))
                {
                    //---系统中已存在同类型的同名文件。
                    return ResponseOutput.NotOk(_localizer["SystemD_DuplicateFile"]);
                }
                // 保存修改前的状态和需要确认的用户类型列表
                bool beforeIsPublish = document.IsPublish;
                bool beforeIsDeleted = document.IsDeleted;
                var beforeUserTypeIds = document.NeedConfirmedUserTypeList.Select(x => x.NeedConfirmUserTypeId).ToList();
                _mapper.Map(addOrEditSystemDocument, document);
                document.UpdateTime = DateTime.Now;
                #region 之前区分路径文件夹 现在不区分废弃
                //if (document.FileTypeId != addOrEditSystemDocument.FileTypeId)
                //{
                //    var rootPath = Directory.GetParent(_hostEnvironment.ContentRootPath.TrimEnd('\\')).IfNullThrowException().FullName;
                //    var beforeFilePath = Path.Combine(rootPath, document.Path);
                //    document.Path = document.Path.Replace(document.FileTypeId.ToString(), addOrEditSystemDocument.FileTypeId.ToString());
                //    var nowPath = Path.Combine(rootPath, document.Path);
                //    if (File.Exists(beforeFilePath))
                //    {
                //        File.Move(beforeFilePath, nowPath, true);
                //        File.Delete(beforeFilePath);
                //    }
                //}
                #endregion
                var success = await _systemDocumentRepository.SaveChangesAsync();
                // 检查是否需要发送邮件给新增的角色
                if (beforeIsPublish && document.IsPublish && !beforeIsDeleted && !document.IsDeleted)
                {
                    // 找出新增的用户角色ID
                    var newUserTypeIds = addOrEditSystemDocument.NeedConfirmedUserTypeIdList
                        .Where(id => !beforeUserTypeIds.Contains(id))
                        .ToList();
                    if (newUserTypeIds.Any()&& newUserTypeIds.Count()>0)
                    {
                        // 发送邮件给新增的角色
                        Console.WriteLine("开始 发送系统文档更新邮件给新增角色");
                        Console.WriteLine(string.Join(",", newUserTypeIds));
                        Task.Run(async () =>
                        {
                            // 创建独立作用域
                            using (var scope = serviceScopeFactory.CreateScope())
                            {
                                // 从新作用域解析服务
                                var mediator = scope.ServiceProvider.GetRequiredService();
                                // 只发送给新增的角色
                                await mediator.Publish(new SystemDocumentPublishEvent {
                                    Ids = new List { document.Id },
                                    NewUserTypeIds = newUserTypeIds
                                });
                            }
                        });
                    }
                }
                return ResponseOutput.Ok(document.Id.ToString());
            }
        }
        /// 
        /// 发布系统文档
        /// 
        /// 
        [HttpPost]
        public async Task PublishSystemDocument(PublishSystemDocumentInDto inDto)
        {
            await _systemDocumentRepository.BatchUpdateNoTrackingAsync(x => inDto.Ids.Contains(x.Id), x => new SystemDocument()
            {
                IsPublish = true,
                IsDeleted = false,
            });
            Console.WriteLine("开始 发布系统文档");
            Task.Run(async () =>
            {
                // 创建独立作用域
                using (var scope = serviceScopeFactory.CreateScope())
                {
                    // 从新作用域解析服务
                    var mediator = scope.ServiceProvider.GetRequiredService();
                    await mediator.Publish(new SystemDocumentPublishEvent { Ids = inDto.Ids });
                }
            });
            return ResponseOutput.Result(true);
        }
        /// 
        /// 下线系统文档
        /// 
        /// 
        [HttpPost]
        public async Task OutLineSystemDocument(PublishSystemDocumentInDto inDto)
        {
            await _systemDocumentRepository.UpdatePartialFromQueryAsync(x => inDto.Ids.Contains(x.Id), x => new SystemDocument()
            {
                IsDeleted = true,
            });
            var result = await _systemDocumentRepository.SaveChangesAsync();
            return ResponseOutput.Result(result);
        }
        [HttpDelete("{systemDocumentId:guid}")]
        public async Task AbandonSystemDocumentAsync(Guid systemDocumentId)
        {
            await _systemDocumentRepository.UpdatePartialFromQueryAsync(systemDocumentId, u => new SystemDocument() { IsDeleted = true });
            await _systemDocConfirmedUserRepository.UpdatePartialFromQueryAsync(x => x.SystemDocumentId == systemDocumentId, x => new SystemDocConfirmedIdentityUser()
            {
                IsDeleted = true
            });
            await _systemDocConfirmedUserRepository.SaveChangesAsync();
            return ResponseOutput.Result(true);
        }
        [HttpDelete("{systemDocumentId:guid}")]
        public async Task DeleteSystemDocumentAsync(Guid systemDocumentId)
        {
            if (await _systemDocumentRepository.Where(t => t.Id == systemDocumentId).AnyAsync(u => u.SystemDocConfirmedUserList.Any()))
            {
                //---已有用户阅读该文档,并签名,不允许删除。
                return ResponseOutput.NotOk(_localizer["SystemD_CannotDeleteSignedFile"]);
            }
            var success = await _systemDocumentRepository.DeleteFromQueryAsync(t => t.Id == systemDocumentId, true, true);
            return ResponseOutput.Result(true);
        }
        /// 
        /// 获取需要签署的系统文档列表
        /// 
        /// 
        [HttpPost]
        public async Task> getWaitSignSysDocList(SystemDocumentQuery inQuery)
        {
            var isInternal = _userInfo.IsZhiZhun;
            var query = from sysDoc in _systemDocumentRepository.AsQueryable(true)
                        .Where(t => t.NeedConfirmedUserTypeList.Any(t => t.NeedConfirmUserTypeId == _userInfo.UserTypeId))
                    .WhereIf(!string.IsNullOrEmpty(inQuery.Name), t => t.Name.Contains(inQuery.Name))
                    //外部人员 只签署 外部需要签署的
                    .WhereIf(isInternal == false, t => t.DocUserSignType == DocUserSignType.InnerAndOuter)
                    .WhereIf(inQuery.FileTypeId != null, t => t.FileTypeId == inQuery.FileTypeId)
                        join confirm in _systemDocConfirmedUserRepository.Where() on new { ConfirmUserId = _userInfo.IdentityUserId, SystemDocumentId = sysDoc.Id } equals new { confirm.ConfirmUserId, confirm.SystemDocumentId } into cc
                        from confirm in cc.DefaultIfEmpty()
                        join user in _identityUserRepository.Where() on _userInfo.IdentityUserId equals user.Id
                        select new UnionDocumentWithConfirmInfoView()
                        {
                            AttachmentCount=sysDoc.SystemDocumentAttachmentList.Where(z=>!z.OffLine).Count(),
                            IsSystemDoc = true,
                            CurrentStaffTrainDays=sysDoc.CurrentStaffTrainDays,
                            NewStaffTrainDays = sysDoc.NewStaffTrainDays,
                            Id = sysDoc.Id,
                            UserCreateTime= user.CreateTime,
                            CreateTime = sysDoc.CreateTime,
                            IsDeleted = sysDoc.IsDeleted,
                            SignViewMinimumMinutes = sysDoc.SignViewMinimumMinutes,
                            Name = sysDoc.Name,
                            Path = sysDoc.Path,
                            FileType = _userInfo.IsEn_Us ? sysDoc.FileType.Value : sysDoc.FileType.ValueCN,
                            UpdateTime = sysDoc.UpdateTime,
                            FullFilePath = sysDoc.Path,
                            ConfirmUserId = confirm.ConfirmUserId,
                            ConfirmTime = confirm.ConfirmTime,
                            RealName = user.FullName,
                            UserName = user.UserName,
                            DocTypeCode = sysDoc.FileType.Code
                            //UserTypeId = user.UserTypeId,
                            //UserTypeShortName = user.UserTypeRole.UserTypeShortName
                        };
            var list = await query
                              //过滤掉删除的,并且没有签名的
                              .Where(t => !(t.IsDeleted == true && t.ConfirmTime == null))
                              .WhereIf(inQuery.IsSigned == true, t => t.ConfirmTime != null)
                              .WhereIf(inQuery.IsSigned == false, t => t.ConfirmTime == null)
                              .ToPagedListAsync(inQuery, nameof(UnionDocumentWithConfirmInfoView.DocTypeCode));
            return list;
        }
    }
}