//--------------------------------------------------------------------
//     此代码由T4模板自动生成  byzhouhang 20210918
//	   生成时间 2022-01-05 09:17:03 
//     对此文件的更改可能会导致不正确的行为,并且如果重新生成代码,这些更改将会丢失。
//--------------------------------------------------------------------

using IRaCIS.Core.Application.Contracts;
using Microsoft.AspNetCore.Mvc;
using UserRole = IRaCIS.Core.Domain.Models.UserRole;

namespace IRaCIS.Core.Application.Services
{
    /// <summary>
    /// SystemDocumentService
    /// </summary>	
    [ApiExplorerSettings(GroupName = "Trial")]
    public class SystemDocumentService(IRepository<SystemDocument> _systemDocumentRepository,
        IRepository<UserRole> _userRoleRepository,
          IRepository<IdentityUser> _identityUserRepository,
        IRepository<SystemDocConfirmedIdentityUser> _systemDocConfirmedUserRepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, ISystemDocumentService
    {





        /// <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.IsDeleted != null, t => t.IsDeleted == inQuery.IsDeleted)
               .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"]);
                }


                _mapper.Map(addOrEditSystemDocument, document);

                #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();

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



        }

        [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 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()
                        {
                            IsSystemDoc = true,

                            Id = sysDoc.Id,
                            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;



        }

    }
}