using Hotline.Application.Tools;
using Hotline.Configurations;
using Hotline.KnowledgeBase;
using Hotline.Pdf;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.Settings.Hotspots;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.Knowledge;
using Hotline.Share.Enums.KnowledgeBase;
using Hotline.Share.Requests;
using Hotline.Share.Tools;
using Mapster;
using MapsterMapper;
using Microsoft.Extensions.Options;
using PanGu;
using SqlSugar;
using XF.Domain.Authentications;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;
using XF.Domain.Repository;

namespace Hotline.Application.Knowledge
{
    public class KnowApplication : IKnowApplication, IScopeDependency
    {
        private readonly IKnowledgeRepository _knowledgeRepository;
        private readonly IRepository<KnowledgePv> _knowledgePvepository;
        private readonly IRepository<Hotspot> _hotspotTypeRepository;
        private readonly IRepository<KnowledgeType> _knowledgeTypeRepository;
        private readonly IRepository<KnowledgeApply> _knowledgeApplyRepository;
        private readonly IRepository<KnowledgeWord> _knowledgeWordRepository;
        private readonly IRepository<KnowledgeHotWord> _knowledgeHotWordRepository;
        private readonly IPdfManager _pdfManager;
        private readonly ISessionContext _sessionContext;
        private readonly IMapper _mapper;
        private readonly IOptionsSnapshot<AppConfiguration> _appOptions;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="knowledgeRepository"></param>
        /// <param name="knowledgeApplyRepository"></param>
        /// <param name="sessionContext"></param>
        /// <param name="knowledgeDomainService"></param>
        /// <param name="mapper"></param>
        public KnowApplication(IKnowledgeRepository knowledgeRepository, IOptionsSnapshot<AppConfiguration> appOptions,
            IRepository<KnowledgeApply> knowledgeApplyRepository, ISessionContext sessionContext, IMapper mapper,
            IRepository<KnowledgeType> knowledgeTypeRepository, IRepository<Hotspot> hotspotTypeRepository,
            IRepository<KnowledgeWord> knowledgeWordRepository, IRepository<KnowledgePv> knowledgePvepository,
            IRepository<KnowledgeHotWord> knowledgeHotWordRepository, IPdfManager pdfManager)
        {
            _knowledgeRepository = knowledgeRepository;
            _knowledgeApplyRepository = knowledgeApplyRepository;
            _sessionContext = sessionContext;
            _mapper = mapper;
            _knowledgeTypeRepository = knowledgeTypeRepository;
            _hotspotTypeRepository = hotspotTypeRepository;
            _knowledgeWordRepository = knowledgeWordRepository;
            _knowledgePvepository = knowledgePvepository;
            _knowledgeHotWordRepository = knowledgeHotWordRepository;
            _pdfManager = pdfManager;
            _appOptions = appOptions;

        }

        /// <summary>
        /// 知识库查询
        /// </summary>
        /// <param name="pagedDto"></param>
        /// <returns></returns>
        public async Task<(int, IList<KnowledgeDataDto>)> GetKnowList(KnowPagedListDto pagedDto, CancellationToken cancellationToken)
        {
            if (!_sessionContext.OrgIsCenter)
            {
                pagedDto.Attribution = "部门知识库";
            }

            if (_appOptions.Value.IsZiGong || _appOptions.Value.IsLuZhou)
            {
                pagedDto.Attribution = string.Empty;
            }

            var typeSpliceName = string.Empty;
            var hotspotHotSpotFullName = string.Empty;
            if (!string.IsNullOrEmpty(pagedDto.KnowledgeTypeId))
            {
                var type = await _knowledgeTypeRepository.GetAsync(x => x.Id == pagedDto.KnowledgeTypeId);
                typeSpliceName = type?.SpliceName;
            }
            if (!string.IsNullOrEmpty(pagedDto.HotspotId))
            {
                var hotspot = await _hotspotTypeRepository.GetAsync(x => x.Id == pagedDto.HotspotId);
                hotspotHotSpotFullName = hotspot?.HotSpotFullName;
            }

            var query = _knowledgeRepository.Queryable();

            switch (pagedDto.Status)
            {
                case EKnowledgeStatusRequest.OnShelf:
                    query.Where(d => d.Status == EKnowledgeStatus.OnShelf
                    && (d.ExpiredTime == null || (d.ExpiredTime != null && d.ExpiredTime > DateTime.Now)));
                    break;
                case EKnowledgeStatusRequest.OffShelf:
                    query.Where(d => d.Status == EKnowledgeStatus.OffShelf
                    || (d.ExpiredTime != null && d.ExpiredTime <= DateTime.Now && d.Status != EKnowledgeStatus.Drafts));
                    break;
                case EKnowledgeStatusRequest.Auditing:
                    query.Where(d => d.Status == EKnowledgeStatus.Auditing);
                    switch (pagedDto.ModuleCode)
                    {
                        case "add":
                            query.Where(d => d.KnowledgeApproves.Any(x => x.KnowledgeApproveType == EKnowledgeApproveType.Add
                                                                          && x.KnowledgeApproveStatus == EKnowledgeApproveStatus.Unhandle));
                            break;
                        case "update":
                            query.Where(d => d.KnowledgeApproves.Any(x => x.KnowledgeApproveType == EKnowledgeApproveType.Update
                                                                          && x.KnowledgeApproveStatus == EKnowledgeApproveStatus.Unhandle));
                            break;
                        case "delete":
                            query.Where(d => d.KnowledgeApproves.Any(x => x.KnowledgeApproveType == EKnowledgeApproveType.Delete
                                                                          && x.KnowledgeApproveStatus == EKnowledgeApproveStatus.Unhandle));
                            break;
                        case "offshelf":
                            query.Where(d => d.KnowledgeApproves.Any(x => x.KnowledgeApproveType == EKnowledgeApproveType.OffShelf
                                                                          && x.KnowledgeApproveStatus == EKnowledgeApproveStatus.Unhandle));
                            break;
                    }
                    break;
                case EKnowledgeStatusRequest.Drafts:
                    switch (pagedDto.NewDraftsStatus)
                    {
                        case EKnowledgeDraftTypeRequest.All:
                            query.Where(d => (d.Status == EKnowledgeStatus.Drafts && d.CreatorId == _sessionContext.UserId)
                                             || d.Status == EKnowledgeStatus.Revert);
                            break;
                        case EKnowledgeDraftTypeRequest.UnApproval:
                            query.Where(d => (d.Status == EKnowledgeStatus.Drafts && d.CreatorId == _sessionContext.UserId));
                            break;
                        case EKnowledgeDraftTypeRequest.Failed:
                            query.Where(d => d.Status == EKnowledgeStatus.Revert);
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                    }
                    break;
                case EKnowledgeStatusRequest.All:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            if (!_sessionContext.OrgIsCenter)
                query.Where(x => x.KnowledgeTypes.Any(t => t.Orgs.Any(to => to.Id == _sessionContext.RequiredOrgId)));


            var (total, temp) = await query
                //.Includes(x => x.User)
                .Includes(x => x.SystemOrganize)
                .Includes(x => x.SourceOrganize)
                .Includes(x => x.KnowledgeTypes)
                .Includes(x => x.HotspotType)
                //.Includes(x => x.Workflow)
                //.Includes(x => x.KnowledgeType)
                //.Where(x => x.IsDeleted == false)
                //.Where(x => x.KnowledgeType.Any(t => t.KnowledgeType.KnowledgeTypeOrgs.Any(to => to.OrgId == _sessionContext.RequiredOrgId)
                //                                     || t.KnowledgeType.KnowledgeTypeOrgs.Any() == false))
                //.Where(x => x.KnowledgeType.Any(t => t.KnowledgeType.Orgs.Any(to => to.Id == _sessionContext.RequiredOrgId)
                //                                     || t.KnowledgeType.Orgs.Any() == false))
                //.Where(x => x.KnowledgeTypes.Any(t => t.KnowledgeTypeOrgs.Any(to => to.OrgId == _sessionContext.RequiredOrgId)
                //                                      || t.KnowledgeTypeOrgs.Any() == false))
                //.Where(x => x.KnowledgeTypes.Any(t => t.Orgs.Any(to => to.Id == _sessionContext.RequiredOrgId)
                //                                      /*|| t.Orgs.Any() == false*/))
                //.Where(x => (x.Status == EKnowledgeStatus.Drafts && x.CreatorId == _sessionContext.UserId) || (x.Status != EKnowledgeStatus.Drafts))
                .WhereIF(!string.IsNullOrEmpty(pagedDto.Title), x => x.Title.Contains(pagedDto.Title!))
                .WhereIF(!string.IsNullOrEmpty(pagedDto.Keyword), x => x.Title.Contains(pagedDto.Keyword!) || x.CreatorName.Contains(pagedDto.Keyword!) || x.CreatorOrgName.Contains(pagedDto.Keyword!) || x.SourceOrganize.Name.Contains(pagedDto.Keyword!))
                //.WhereIF(pagedDto.Status.HasValue && pagedDto.Status != EKnowledgeStatus.OffShelf && pagedDto.Status != EKnowledgeStatus.NewDrafts && pagedDto.Status != EKnowledgeStatus.All,
                //    x => x.Status == pagedDto.Status && ((x.ExpiredTime != null && x.ExpiredTime > DateTime.Now) || x.ExpiredTime == null))
                //.WhereIF(pagedDto.Status.HasValue && pagedDto.Status == EKnowledgeStatus.OffShelf,
                //    x => x.Status == pagedDto.Status || (x.ExpiredTime != null && x.ExpiredTime < DateTime.Now && x.Status != EKnowledgeStatus.Drafts))
                .WhereIF(pagedDto.IsPublic.HasValue, x => x.IsPublic == pagedDto.IsPublic)
                .WhereIF(!string.IsNullOrEmpty(pagedDto.Summary), x => x.Summary != null && x.Summary.Contains(pagedDto.Summary!))
                //.WhereIF(!string.IsNullOrEmpty(typeSpliceName), x => x.KnowledgeType.Any(t => t.KnowledgeTypeSpliceName.StartsWith(typeSpliceName)))
                .WhereIF(!string.IsNullOrEmpty(typeSpliceName), x => x.KnowledgeTypes.Any(t => t.SpliceName.StartsWith(typeSpliceName)))
                .WhereIF(!string.IsNullOrEmpty(hotspotHotSpotFullName), x => x.HotspotType.HotSpotFullName.EndsWith(hotspotHotSpotFullName!))
                .WhereIF(!string.IsNullOrEmpty(pagedDto.CreateOrgId), x => x.CreatorOrgId != null && x.CreatorOrgId.StartsWith(pagedDto.CreateOrgId!))
                //.WhereIF(!string.IsNullOrEmpty(pagedDto.ModuleCode), x => x.Workflow.ModuleCode == pagedDto.ModuleCode)
                //.WhereIF(pagedDto.Status == EKnowledgeStatus.NewDrafts, x => x.Status == EKnowledgeStatus.Drafts || x.Status == EKnowledgeStatus.Revert)
                //.WhereIF(pagedDto.NewDraftsStatus is EKnowledgeStatus.Drafts, x => x.Status == EKnowledgeStatus.Drafts)
                //.WhereIF(pagedDto.NewDraftsStatus is EKnowledgeStatus.Revert, x => x.Status == EKnowledgeStatus.Revert)

                .WhereIF(pagedDto.CreationStartTime.HasValue, x => x.CreationTime >= pagedDto.CreationStartTime)
                .WhereIF(pagedDto.CreationEndTime.HasValue, x => x.CreationTime <= pagedDto.CreationEndTime)

                  .WhereIF(pagedDto.StartOnShelfTime.HasValue, x => x.OnShelfTime >= pagedDto.StartOnShelfTime)
                  .WhereIF(pagedDto.EndOnShelfTime.HasValue, x => x.OnShelfTime <= pagedDto.EndOnShelfTime)

                   .WhereIF(pagedDto.StartOffShelfTime.HasValue, x => x.OffShelfTime >= pagedDto.StartOffShelfTime)
                  .WhereIF(pagedDto.EndOffShelfTime.HasValue, x => x.OffShelfTime <= pagedDto.EndOffShelfTime)

                  .WhereIF(pagedDto.StartUpdateTime.HasValue, x => x.LastModificationTime >= pagedDto.StartUpdateTime)
                  .WhereIF(pagedDto.EndUpdateTime.HasValue, x => x.LastModificationTime <= pagedDto.EndUpdateTime)
                .WhereIF(!string.IsNullOrEmpty(pagedDto.Attribution), x => x.Attribution == pagedDto.Attribution)
                .WhereIF(!string.IsNullOrEmpty(pagedDto.CreatorName), x => x.CreatorName.Contains(pagedDto.CreatorName))
                .OrderByDescending(d => d.CreationTime)
                .ToPagedListAsync(pagedDto, cancellationToken);
            //返回数据
            return (total, _mapper.Map<IList<KnowledgeDataDto>>(temp));
        }

        /// <summary>
        /// 知识申请查询
        /// </summary>
        /// <param name="pagedDto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<PagedDto<KnowledgeApplyHandlePageDto>> GetApplyList(KnowledgeApplyPagedDto pagedDto, CancellationToken cancellationToken)
        {
            var (total, items) = await _knowledgeApplyRepository
               .Queryable()
               .Includes(it => it.User)
               .Includes(it => it.SystemOrganize)
               .Where(d => d.CreatorId == _sessionContext.RequiredUserId)
               .WhereIF(pagedDto.Status.HasValue, d => d.Status == pagedDto.Status)
               .WhereIF(pagedDto.ApplyType.HasValue, d => d.KnowledgeApplyType == pagedDto.ApplyType)
               .WhereIF(!string.IsNullOrEmpty(pagedDto.IsOvertime) && pagedDto.IsOvertime == "0", d => d.IsOvertime == true)
               .WhereIF(!string.IsNullOrEmpty(pagedDto.IsOvertime) && pagedDto.IsOvertime == "1", d => d.IsOvertime == false)
               .WhereIF(pagedDto.StartTime.HasValue, d => d.CreationTime >= pagedDto.StartTime)
               .WhereIF(pagedDto.EndTime.HasValue, d => d.CreationTime <= pagedDto.EndTime)
               .OrderByDescending(p => p.CreationTime)
               .ToPagedListAsync(pagedDto.PageIndex, pagedDto.PageSize, cancellationToken);

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

        /// <summary>
        /// 申请处理查询
        /// </summary>
        /// <param name="pagedDto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<PagedDto<KnowledgeApplyHandlePageDto>> GetApplyHandleList(KnowledgeHandlePagedDto pagedDto, CancellationToken cancellationToken)
        {
            var (total, items) = await _knowledgeApplyRepository
                 .Queryable()
                 .Includes(it => it.User)
                 .Includes(it => it.SystemOrganize)
                 .Where(p => p.DepartmentId == _sessionContext.RequiredOrgId)
                 .WhereIF(pagedDto.Status.HasValue, d => d.Status == pagedDto.Status)
                 .WhereIF(!string.IsNullOrEmpty(pagedDto.Keyword), d => d.User.Name.Contains(pagedDto.Keyword!) || d.SystemOrganize.Name.Contains(pagedDto.Keyword!))
                 .WhereIF(pagedDto.ApplyType.HasValue, d => d.KnowledgeApplyType == pagedDto.ApplyType)
                 .WhereIF(!string.IsNullOrEmpty(pagedDto.IsOvertime) && pagedDto.IsOvertime == "0", d => d.IsOvertime == true)
                 .WhereIF(!string.IsNullOrEmpty(pagedDto.IsOvertime) && pagedDto.IsOvertime == "1", d => d.IsOvertime == false)

                 .WhereIF(pagedDto.StartTime.HasValue && pagedDto.Status != EKnowledgeApplyStatus.Failed && pagedDto.Status != EKnowledgeApplyStatus.Succeed, d => d.CreationTime >= pagedDto.StartTime)
                 .WhereIF(pagedDto.EndTime.HasValue && pagedDto.Status != EKnowledgeApplyStatus.Failed && pagedDto.Status != EKnowledgeApplyStatus.Succeed, d => d.CreationTime <= pagedDto.EndTime)
                 .WhereIF(pagedDto.StartTime.HasValue && pagedDto.Status == EKnowledgeApplyStatus.Failed, d => d.ReturnTime >= pagedDto.StartTime)
                 .WhereIF(pagedDto.EndTime.HasValue && pagedDto.Status == EKnowledgeApplyStatus.Failed, d => d.ReturnTime <= pagedDto.EndTime)
                  .WhereIF(pagedDto.StartTime.HasValue && pagedDto.Status == EKnowledgeApplyStatus.Succeed, d => d.HandleTime >= pagedDto.StartTime)
                 .WhereIF(pagedDto.EndTime.HasValue && pagedDto.Status == EKnowledgeApplyStatus.Succeed, d => d.HandleTime <= pagedDto.EndTime)

                 .OrderByDescending(p => p.CreationTime)
                 .ToPagedListAsync(pagedDto.PageIndex, pagedDto.PageSize, cancellationToken);

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

        //public async Task<Dictionary<string, Stream>> KnowledgeInfoListExportAsync(KnowledgeInfoExportInDto dto, CancellationToken cancellationToken)
        //{
        //    var streamList = new Dictionary<string, Stream>();
        //    var knowList = await _knowledgeRepository.Queryable()
        //        .Where(m => dto.Ids.Contains(m.Id))
        //        .Select(m => new { m.Title, m.Content })
        //        .ToListAsync(cancellationToken);
        //    foreach (var item in knowList)
        //    {
        //        Stream stream = item.Content.HtmlToStream(dto.FileType);
        //        streamList.Add(item.Title + dto.FileType.GetFileExtension(), stream);
        //    }
        //    return streamList;
        //}

        public async Task<Dictionary<string, Stream>> KnowledgeInfoListExportAsync(KnowledgeInfoExportInDto dto, CancellationToken cancellationToken)
        {
            var streamList = new Dictionary<string, Stream>();
            var knowList = await _knowledgeRepository.Queryable()
                .Where(m => dto.Ids.Contains(m.Id))
                .Select(m => new { m.Title, m.Content })
                .ToListAsync(cancellationToken);

            var tasks = knowList.Select(async item =>
            {
                var stream = await Task.Run(() => item.Content.HtmlToStream(dto.FileType, item.Title, _pdfManager), cancellationToken);
                return new KeyValuePair<string, Stream>(
                    item.Title + dto.FileType.GetFileExtension(),
                    stream
                );
            });

            var results = await Task.WhenAll(tasks);

            foreach (var kvp in results)
            {
                if (!streamList.ContainsKey(kvp.Key))
                {
                    streamList.Add(kvp.Key, kvp.Value);
                }
            }

            return streamList;
        }

        public async Task<IList<KnowledgeWordOutDto>> TitleParticiple(string title)
        {
            var keywords = new List<KnowledgeWordOutDto>();
            var splitWords = new Segment().DoSegment(title);
            for (int i = 0; i < splitWords.Count; i++)
            {
                var word = splitWords.ElementAt(i);
                if (word is not { WordType: WordType.SimplifiedChinese, Word.Length: > 1 }) continue;

                var tag = splitWords.ElementAt(i).Word;
                var entity = await _knowledgeWordRepository.Queryable().Where(m => m.Tag == tag).FirstAsync();
                if (entity is not null)
                {
                    keywords.Add(new KnowledgeWordOutDto(entity.Id, entity.Tag));
                    continue;
                }

                var wordEntity = new KnowledgeWord
                {
                    Tag = tag,
                    Classify = "普通标签",
                    Remark = "系统自动从知识标题分词",
                    Synonym = "",
                    IsEnable = 0
                };
                var keyId = await _knowledgeWordRepository.AddAsync(wordEntity);
                keywords.Add(new KnowledgeWordOutDto(keyId, tag));
            }
            return keywords;
        }

        public async Task<(int, IList<PageViewOutDto>)> GetPageViewListAsync(PageViewInDto dto, CancellationToken requestAborted = default)
        {
            var typeSpliceName = string.Empty;
            if (!string.IsNullOrEmpty(dto.KnowledgeTypeId))
            {
                var type = await _knowledgeTypeRepository.GetAsync(x => x.Id == dto.KnowledgeTypeId);
                typeSpliceName = type?.SpliceName;
            }

            var query = _knowledgePvepository.Queryable(includeDeleted: true)
                .LeftJoin<KnowledgeBase.Knowledge>((p, k) => p.KnowledgeCode == k.Code)
                .LeftJoin<KnowledgeRelationType>((p, k, r) => r.KnowledgeId == k.Id)
                .WhereIF(dto.Title.NotNullOrEmpty(), (p, k, r) => k.Title.Contains(dto.Title))
                .WhereIF(dto.CreatorName.NotNullOrEmpty(), (p, k, r) => p.CreatorName.Contains(dto.CreatorName))
                .WhereIF(dto.KnowledgeTypeId.NotNullOrEmpty(), (p, k, r) => r.KnowledgeTypeSpliceName.StartsWith(typeSpliceName))
                .WhereIF(dto.StartTime.HasValue && dto.EndTime.HasValue, (p, k, r) => p.CreationTime >= dto.StartTime && p.CreationTime <= dto.EndTime)
                .OrderByDescending((p, k, r) => p.CreationTime);

            if (_sessionContext.OrgIsCenter == false)
                query = query.Where((p, k, r) => p.CreatorOrgId.StartsWith(_sessionContext.OrgId));

            return await query.Select<PageViewOutDto>().ToPagedListAsync(dto.PageIndex, dto.PageSize, requestAborted);
        }

        public async Task<(int total, IList<KnowledgeHotWordOutDto> items)> GetKnowledgeHotWordListAsync(KnowledgeHotWordInDto dto, CancellationToken requestAborted)
        {
            var query = _knowledgeHotWordRepository.Queryable()
                .WhereIF(dto.IsEnable != null, m => m.IsEnable == dto.IsEnable)
                .WhereIF(dto.KeyWord.NotNullOrEmpty(), m => m.KeyWord.Contains(dto.KeyWord))
                .WhereIF(dto.Type != null, m => m.Type == dto.Type)
                // .WhereIF(dto.StartTime.HasValue && dto.EndTime.HasValue, m => m.CreationTime >= dto.StartTime && m.CreationTime <= dto.EndTime)
                ;

            if (dto.SortField.NotNullOrEmpty())
                query = query.OrderByPropertyName(dto.SortField, (OrderByType)dto.OrderByType);
            else
            {
                query = query.OrderByDescending(m => m.Sort);
                query = query.OrderByDescending(m => m.CreationTime);
            }

            return await query.Select<KnowledgeHotWordOutDto>().ToPagedListAsync(dto.PageIndex, dto.PageSize, requestAborted);
        }

        public async Task UpdateKnowledgeHotWordAsync(UpdateKnowledgeHotWordInDto dto, CancellationToken requestAborted)
        {
            var entity = await _knowledgeHotWordRepository.GetAsync(dto.Id)
                ?? throw UserFriendlyException.SameMessage("数据不存在");
            dto.Adapt(entity);
            await _knowledgeHotWordRepository.UpdateAsync(entity);
        }

        public async Task AddKnowledgeHotWordAsync(AddKnowledgeHotWordInDto dto, CancellationToken requestAborted)
        {
            var isExist = await _knowledgeHotWordRepository.Queryable()
                .Where(m => m.KeyWord == dto.KeyWord).AnyAsync();
            if (isExist) throw UserFriendlyException.SameMessage("热词已存在");
            var entity = dto.Adapt<KnowledgeHotWord>();
            await _knowledgeHotWordRepository.AddAsync(entity, requestAborted);
        }

        public async Task<(int, List<KnowledgeRetrievalDataDto>)> KnowRetrievalAsync(KnowledgeRetrievalPagedListDto dto)
        {
            var typeSpliceName = string.Empty;
            var hotspotHotSpotFullName = string.Empty;
            if (!string.IsNullOrEmpty(dto.KnowledgeTypeId))
            {
                var type = await _knowledgeTypeRepository.GetAsync(x => x.Id == dto.KnowledgeTypeId);
                typeSpliceName = type?.SpliceName;
            }
            if (!string.IsNullOrEmpty(dto.HotspotId))
            {
                var hotspot = await _hotspotTypeRepository.GetAsync(x => x.Id == dto.HotspotId);
                hotspotHotSpotFullName = hotspot?.HotSpotFullName;
            }

            if (!_sessionContext.OrgIsCenter)
            {
                dto.Attribution = "部门知识库";
            }

            var query = _knowledgeRepository.Queryable(false, false, false);

            if (!_sessionContext.OrgIsCenter)
                query.Where(x => x.KnowledgeTypes.Any(t => t.Orgs.Any(to => to.Id == _sessionContext.RequiredOrgId)));

            var sugar = query
                .Includes(x => x.User)
                .Includes(x => x.SystemOrganize)
                .Includes(x => x.HotspotType)
                .Includes(x => x.KnowledgeType)
                .Where(x => x.IsDeleted == false)
                .Where(x => x.Status == EKnowledgeStatus.OnShelf)
                .Where(x => SqlFunc.HasValue(x.ExpiredTime) == false || x.ExpiredTime > DateTime.Now)
                //.Where(x => x.KnowledgeType.Any(t => t.KnowledgeType.KnowledgeTypeOrgs.Any(to => to.OrgId == _sessionContext.RequiredOrgId)
                //                                     || t.KnowledgeType.KnowledgeTypeOrgs.Any() == false))
                //.WhereIF(!string.IsNullOrEmpty(typeSpliceName), x => x.KnowledgeType.Any(t => t.KnowledgeTypeSpliceName.StartsWith(typeSpliceName)))
                .WhereIF(!string.IsNullOrEmpty(typeSpliceName), x => x.KnowledgeTypes.Any(t => t.SpliceName.StartsWith(typeSpliceName)))
                .WhereIF(!string.IsNullOrEmpty(hotspotHotSpotFullName), x => x.HotspotType.HotSpotFullName.EndsWith(hotspotHotSpotFullName!))
                .WhereIF(!string.IsNullOrEmpty(dto.HotspotName), x => x.HotspotType.HotSpotFullName.EndsWith(dto.HotspotName!))
                .WhereIF(!string.IsNullOrEmpty(dto.CreateOrgId), x => x.CreatorOrgId != null && x.CreatorOrgId.EndsWith(dto.CreateOrgId!))
                .WhereIF(!string.IsNullOrEmpty(dto.Attribution), x => x.Attribution == dto.Attribution!);
            if (dto.Keyword.NotNullOrEmpty())
            {
                var keywords = dto.Keyword!.SplitKeywords();
                var exp = Expressionable.Create<KnowledgeBase.Knowledge>();
                foreach (var keyword in keywords)
                {
                    if (_appOptions.Value.IsZiGong)
                    {
                        //自贡任务 359 泸州自贡通用-调整知识检索时，多关键词组合查询的查询方式
                        if (dto.RetrievalType == EKnowledgeRetrievalType.All)
                            exp.And(x => x.Title.Contains(keyword) || x.Content.Contains(keyword));
                        if (dto.RetrievalType == EKnowledgeRetrievalType.Title)
                            exp.And(x => x.Title.Contains(keyword));
                        if (dto.RetrievalType == EKnowledgeRetrievalType.Content)
                            exp.And(x => x.Content.Contains(keyword));
                        if (dto.RetrievalType == EKnowledgeRetrievalType.Summary)
                            exp.And(x => x.Summary != null && x.Summary.Contains(keyword));
                        if (dto.RetrievalType == EKnowledgeRetrievalType.KeyWord)
                        {
                            var keywordEntity = await _knowledgeWordRepository.GetAsync(m => m.Tag == keyword && m.IsEnable == 1);
                            if (keywordEntity is null) continue;
                            exp.And(x => SqlFunc.JsonArrayAny(x.Keywords, keywordEntity.Id));
                        }
                    }
                    else
                    {
                        if (dto.RetrievalType == EKnowledgeRetrievalType.All)
                            exp.Or(x => x.Title.Contains(keyword) || x.Content.Contains(keyword));
                        if (dto.RetrievalType == EKnowledgeRetrievalType.Title)
                            exp.Or(x => x.Title.Contains(keyword));
                        if (dto.RetrievalType == EKnowledgeRetrievalType.Content)
                            exp.Or(x => x.Content.Contains(keyword));
                        if (dto.RetrievalType == EKnowledgeRetrievalType.Summary)
                            exp.Or(x => x.Summary != null && x.Summary.Contains(keyword));
                        if (dto.RetrievalType == EKnowledgeRetrievalType.KeyWord)
                        {
                            var keywordEntity = await _knowledgeWordRepository.GetAsync(m => m.Tag == keyword && m.IsEnable == 1);
                            if (keywordEntity is null) continue;
                            exp.Or(x => SqlFunc.JsonArrayAny(x.Keywords, keywordEntity.Id));
                        }
                    }
                }
                sugar = sugar.Where(exp.ToExpression());
            }
            if (dto.Content.NotNullOrEmpty())
            {
                var keywords = dto.Content!.GetSegment();
                var exp = Expressionable.Create<KnowledgeBase.Knowledge>();
                _knowledgeWordRepository.Queryable()
                   .Where(m => keywords.Contains(m.Tag) && m.IsEnable == 1)
                   .Select(m => m.Id)
                   .ToList()
                   .ForEach(m =>
                       exp.Or(x => SqlFunc.JsonArrayAny(x.Keywords, m))
                   );
                sugar = sugar.Where(exp.ToExpression());
            }

            switch (dto.Sort)
            {
                case "2":
                    sugar = sugar.OrderByDescending(p => p.CollectCount);
                    break;
                case "3":
                    sugar = sugar.OrderByDescending(p => p.CreationTime);
                    break;
                default:
                    sugar = sugar.OrderByDescending(p => p.PageView);
                    break;
            }
            return await sugar.Select<KnowledgeRetrievalDataDto>().ToPagedListAsync(dto.PageIndex, dto.PageSize);
        }

        public ISugarQueryable<KnowedgeStatisticsDto> GetKnowedgeStatistics(PagedKeywordRequest dto)
        {
            return _knowledgeRepository.Queryable()
                    .Includes(p => p.SourceOrganize)
                    .WhereIF(dto.StartTime.HasValue, p => p.LastModificationTime >= dto.StartTime)
                    .WhereIF(dto.EndTime.HasValue, p => p.LastModificationTime <= dto.EndTime)
                    .WhereIF(!string.IsNullOrEmpty(dto.Keyword), p => p.SourceOrganize.Name.Contains(dto.Keyword))
                  //  .GroupBy(p => new { p.SourceOrganize.Name, p.LastModificationTime })
                    .Select(p => new KnowedgeStatisticsDto
                    {
                       // Index = SqlFunc.RowNumber($"{p.LastModificationTime} desc "),\
                       Index= SqlFunc.RowNumber($"{p.LastModificationTime} desc ", $"{p.SourceOrganize.Name}"),
                        LastModificationTime = p.LastModificationTime,
                        OrgName = p.SourceOrganize.Name
                    })
                    .MergeTable()
                    .Where(p => p.Index == 1)
                    .OrderByDescending(p => p.LastModificationTime);
        }
    }
}
