//--------------------------------------------------------------------
//     此代码由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
{
    /// <summary>
    /// SystemDocumentService
    /// </summary>
    [ApiExplorerSettings(GroupName = "Trial")]
    public class SystemDocumentService(IRepository<SystemDocument> _systemDocumentRepository,
        IRepository<UserRole> _userRoleRepository,
        IScopedMediator _mediatorScoped,
        IRepository<SystemDocumentAttachment> _systemDocumentAttachmentRepository,
          IRepository<IdentityUser> _identityUserRepository,
          IServiceScopeFactory serviceScopeFactory,
        IRepository<SystemDocConfirmedIdentityUser> _systemDocConfirmedUserRepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, ISystemDocumentService
    {

        /// <summary>
        /// 获取系统文档附件
        /// </summary>
        /// <param name="inQuery"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<SystemDocumentAttachmentView>> 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<SystemDocumentAttachmentView>(_mapper.ConfigurationProvider);

            var pageList = await systemDocumentAttachmentQueryable.ToPagedListAsync(inQuery);

            return pageList;
        }


        /// <summary>
        /// 新增或编辑系统文档附件
        /// </summary>
        /// <param name="addOrEditSystemDocumentAttachment"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddOrUpdateSystemDocumentAttachment(SystemDocumentAttachmentAddOrEdit addOrEditSystemDocumentAttachment)
        {



            var entity = await _systemDocumentAttachmentRepository.InsertOrUpdateAsync(addOrEditSystemDocumentAttachment, true);

            return ResponseOutput.Ok(entity.Id.ToString());

        }

        /// <summary>
        /// 删除系统文档附件
        /// </summary>
        /// <param name="systemDocumentAttachmentId"></param>
        /// <returns></returns>
        [HttpDelete("{systemDocumentAttachmentId:guid}")]
        public async Task<IResponseOutput> DeleteSystemDocumentAttachment(Guid systemDocumentAttachmentId)
        {
            var success = await _systemDocumentAttachmentRepository.DeleteFromQueryAsync(t => t.Id == systemDocumentAttachmentId, true);
            return ResponseOutput.Ok();
        }


        /// <summary>
        /// 管理端列表
        /// </summary>
        /// <param name="inQuery"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<SystemDocumentView>> 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)
               .WhereIf(inQuery.DocLanguageType != null, t => t.DocLanguageType == inQuery.DocLanguageType)
               .ProjectTo<SystemDocumentView>(_mapper.ConfigurationProvider, new { isEn_Us = _userInfo.IsEn_Us, userId = _userInfo.UserRoleId });

            return await systemDocumentQueryable.ToPagedListAsync(inQuery);
        }

        public async Task<IResponseOutput> AddOrUpdateSystemDocumentAsync(AddOrEditSystemDocument addOrEditSystemDocument)
        {
            if (addOrEditSystemDocument.Id == null)
            {
                var entity = _mapper.Map<SystemDocument>(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)
                    {
                        // 发送邮件给新增的角色
                        Log.Logger.Warning("开始 发送系统文档更新邮件给新增角色");
                        Log.Logger.Warning(string.Join(",", newUserTypeIds));

                        // 只发送给新增的角色
                        await _mediatorScoped.Publish(new SystemDocumentPublishEvent
                        {
                            Ids = new List<Guid> { document.Id },
                            NewUserTypeIds = newUserTypeIds
                        });

                    }
                }

                return ResponseOutput.Ok(document.Id.ToString());
            }



        }

        /// <summary>
        /// 发布系统文档
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> PublishSystemDocument(PublishSystemDocumentInDto inDto)
        {
            await _systemDocumentRepository.BatchUpdateNoTrackingAsync(x => inDto.Ids.Contains(x.Id), x => new SystemDocument()
            {
                IsPublish = true,
                PublishDate= DateTime.Now,
                IsDeleted = false,
            });

            Log.Logger.Warning("开始 发布系统文档");

            await _mediatorScoped.Publish(new SystemDocumentPublishEvent { Ids = inDto.Ids });



            return ResponseOutput.Result(true);
        }



        /// <summary>
        /// 下线系统文档
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> 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<IResponseOutput> 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<IResponseOutput> 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);
        }


        /// <summary>
        /// 获取需要签署的系统文档列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<UnionDocumentWithConfirmInfoView>> getWaitSignSysDocList(SystemDocumentQuery inQuery)
        {

            var isInternal = _userInfo.IsZhiZhun;
            var workLanguage = _userInfo.UserWorkLanguage;

            var query = from sysDoc in _systemDocumentRepository.Where(t=>t.IsPublish)
                      .WhereIf(workLanguage == UserWorkLanguage.CN, t => t.DocLanguageType == DocLanguageType.CN || t.DocLanguageType == DocLanguageType.CN_US)
                      .WhereIf(workLanguage == UserWorkLanguage.US, t => t.DocLanguageType == DocLanguageType.US || t.DocLanguageType == DocLanguageType.CN_US)
                     .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,
                            IsPublish=sysDoc.IsPublish,
                            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;



        }

    }
}
