using Hotline.Settings.Hotspots;
using Hotline.Share.Dtos.Knowledge;
using Hotline.Share.Dtos.Caselibrary;
using Hotline.Share.Enums.Caselibrary;
using MapsterMapper;
using XF.Domain.Authentications;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;
using XF.Domain.Repository;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.SeedData;
using Hotline.File;
using Hotline.Application.Bulletin;
using Hotline.Share.Tools;
using Hotline.Application.Tools;
using SqlSugar;
using Hotline.CaseLibrary;
using Hotline.Orders;
using Hotline.KnowledgeBase;
using Hotline.Share.Enums.Planlibrary;
using System.Numerics;
using Hotline.Share.Dtos.Planlibrary;
using Hotline.Planlibrary;

namespace Hotline.Application.Caselibrary
{
    /// <summary>
    /// 案例库处理
    /// </summary>
    public class CaseApplication : ICaseApplication, IScopeDependency
    {

        #region 注册

        private readonly IRepository<CaseList> _caseListRepository;                           //案例库列表
        private readonly IRepository<CaseType> _caseTypeRepository;                           //案例库分类管理
        private readonly IRepository<CaseTypeOrg> _caseTypeOrgRepository;                     //案例库分类关联机构
        private readonly IRepository<CaseCollect> _caseCollectRepository;                     //案例库收藏评分
        private readonly IRepository<Order> _orderRepository;                                 //工单
        private readonly IRepository<Hotline.KnowledgeBase.Knowledge> _knowledgeRepository;   //知识库
        private readonly ISessionContext _sessionContext;
        private readonly IMapper _mapper;
        private readonly IRepository<Hotspot> _hotspotTypeRepository;
        private readonly IFileRepository _fileRepository;
        private readonly IBulletinApplication _bulletinApplication;

        public CaseApplication(
            IRepository<CaseList> caseListRepository,
            IRepository<CaseType> caseTypeRepository,
            IRepository<CaseTypeOrg> caseTypeOrgRepository,
            IRepository<CaseCollect> caseCollectRepository,
            IRepository<Order> orderRepository,
            IRepository<Hotline.KnowledgeBase.Knowledge> knowledgeRepository,
            ISessionContext sessionContext,
            IMapper mapper,
            IRepository<Hotspot> hotspotTypeRepository,
            IFileRepository fileRepository,
            IBulletinApplication bulletinApplication)
        {
            _caseListRepository = caseListRepository;
            _caseTypeRepository = caseTypeRepository;
            _caseTypeOrgRepository = caseTypeOrgRepository;
            _caseCollectRepository = caseCollectRepository;
            _orderRepository = orderRepository;
            _knowledgeRepository = knowledgeRepository;
            _sessionContext = sessionContext;
            _mapper = mapper;
            _hotspotTypeRepository = hotspotTypeRepository;
            _fileRepository = fileRepository;
            _bulletinApplication = bulletinApplication;
        }

        #endregion

        #region 案例库类型管理

        #region 案例库类型 - 新增

        /// <summary>
        /// 案例库类型 - 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> AddTypeAsync(AddCaseTypeDto dto, CancellationToken cancellationToken)
        {
            var sandard = await _caseTypeRepository.GetAsync(p => p.ParentId == dto.ParentId && p.Name == dto.Name && p.IsDeleted == false, cancellationToken);
            if (sandard is not null)
                throw UserFriendlyException.SameMessage("当前层级已存在相同名称的分类！");

            var type = _mapper.Map<CaseType>(dto);
            type.InitId();
            type.IsEnable = true;
            //获取分类名称全称
            string FullName = await GetFullName(type.ParentId);
            //处理全称，如果为第一级直接用全称，否则获取全称后拼接名称
            type.SpliceName = string.IsNullOrEmpty(FullName) ? dto.Name : FullName + "-" + dto.Name;
            var id = await _caseTypeRepository.AddAsync(type, cancellationToken);
            if (dto.TypeOrgDtos != null && dto.TypeOrgDtos.Any())
            {
                List<CaseTypeOrg> orgs = _mapper.Map<List<CaseTypeOrg>>(dto.TypeOrgDtos);
                orgs.ForEach(x => x.TypeId = type.Id);
                await _caseTypeOrgRepository.AddRangeAsync(orgs, cancellationToken);
            }
            return id;
        }

        #endregion

        #region 案例库类型 - 编辑

        /// <summary>
        /// 案例库类型 - 编辑
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task UpdateTypeAsync(UpdateCaseTypeDto dto, CancellationToken cancellationToken)
        {
            //查询原有数据
            var type = await _caseTypeRepository.GetAsync(dto.Id, cancellationToken);
            if (type is null)
                throw UserFriendlyException.SameMessage("编辑失败！");
            bool result = type.Name != dto.Name || type.ParentId != dto.ParentId;
            //是否更改分类名称或者层级

            //转换
            _mapper.Map(dto, type);
            //如果更改了名称或者修改了层级，则修改全称，未更改不修改
            if (result)
            {
                string FullName = await GetFullName(type.ParentId);//获取分类名称全称
                type.SpliceName = string.IsNullOrEmpty(FullName) ? dto.Name : FullName + "-" + dto.Name;//处理全称，如果为第一级直接用全称，否则获取全称后拼接名称
            }

            //修改数据
            await _caseTypeRepository.UpdateAsync(type, cancellationToken);

            //如果修改了名称，对应修改子分类全称
            if (result)
                await UpdateChildNode(type.Id);

            // 修改关联机构
            await _caseTypeOrgRepository.RemoveAsync(x => x.TypeId == type.Id, false, cancellationToken);
            if (dto.TypeOrgDtos != null && dto.TypeOrgDtos.Any())
            {
                List<CaseTypeOrg> orgs = _mapper.Map<List<CaseTypeOrg>>(dto.TypeOrgDtos);
                orgs.ForEach(x => x.TypeId = type.Id);
                await _caseTypeOrgRepository.AddRangeAsync(orgs, cancellationToken);
            }
        }

        #endregion

        #region 案例库类型 - 删除

        /// <summary>
        /// 案例库类型 - 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task RemoveTypeAsync(string Id, CancellationToken cancellationToken)
        {
            //查询数据是否存在
            var sandard = await _caseTypeRepository.GetAsync(p => p.Id == Id && p.IsDeleted == false, cancellationToken);
            if (sandard is null)
                throw UserFriendlyException.SameMessage("分类不存在！");

            //查询是否有子级分类
            var checkChild = await _caseTypeRepository.CountAsync(p => p.ParentId == Id && p.IsDeleted == false, cancellationToken);
            if (checkChild > 0)
                throw UserFriendlyException.SameMessage("存在子级分类！");

            //查询是否有案例分类
            var checkKnowledge = await _caseListRepository.CountAsync(p => p.CaseTypes.Any(t => t.Id == Id && t.IsDeleted == false), cancellationToken);
            if (checkKnowledge > 0)
                throw UserFriendlyException.SameMessage("分类存在案例！");

            //删除操作
            await _caseTypeRepository.RemoveAsync(sandard, true, cancellationToken);
        }

        #endregion

        #endregion

        #region 案例库管理

        #region 案例库 - 列表

        /// <summary>
        /// 案例库 - 列表
        /// </summary>
        /// <param name="pagedDto"></param>
        /// <returns></returns>
        public async Task<(int, IList<CaseDataDto>)> QueryAllCaseListAsync(CaseListDto pagedDto, CancellationToken cancellationToken)
        {
            var typeSpliceName = string.Empty;

            if (!string.IsNullOrEmpty(pagedDto.CaseTypeID))
            {
                var type = await _caseTypeRepository.GetAsync(x => x.Id == pagedDto.CaseTypeID);
                typeSpliceName = type?.SpliceName;
            }

            //单表分页
            var (total, temp) = await _caseListRepository.Queryable()
                .Includes(x => x.CaseTypes)
                .Includes(x => x.ExaminMan)
                .Includes(x => x.Order)
                .Includes(x => x.Knowledge)
                .Where(x => x.IsDeleted == false)

                .WhereIF(pagedDto.IsPopular == true, x => x.IsPopular == true)

                .WhereIF(OrgSeedData.CenterId != pagedDto.CreateOrgId && !string.IsNullOrEmpty(pagedDto.CreateOrgId), x => x.CreatorOrgId != null && x.CreatorOrgId.StartsWith(pagedDto.CreateOrgId!))

                .WhereIF(!string.IsNullOrEmpty(pagedDto.Title) &&
                          string.IsNullOrEmpty(pagedDto.Keywords) &&
                          string.IsNullOrEmpty(pagedDto.Abstract), x => x.Title.Contains(pagedDto.Title!))

                .WhereIF(string.IsNullOrEmpty(pagedDto.Title) &&
                        !string.IsNullOrEmpty(pagedDto.Keywords) &&
                         string.IsNullOrEmpty(pagedDto.Abstract), x => x.Keywords.Contains(pagedDto.Keywords!))

                .WhereIF(string.IsNullOrEmpty(pagedDto.Title) &&
                         string.IsNullOrEmpty(pagedDto.Keywords) &&
                        !string.IsNullOrEmpty(pagedDto.Abstract), x => x.Abstract.Contains(pagedDto.Abstract!))

                .WhereIF(!string.IsNullOrEmpty(pagedDto.Title) &&
                         !string.IsNullOrEmpty(pagedDto.Keywords) &&
                         !string.IsNullOrEmpty(pagedDto.Abstract)
                                                                , x => x.Title.Contains(pagedDto.Title!) ||
                                                                  x.Keywords!.Contains(pagedDto.Keywords!) ||
                                                                  x.Abstract!.Contains(pagedDto.Abstract!))

                .WhereIF(pagedDto.Status.HasValue && pagedDto.Status == ECaseStatus.OnShelf, x => x.Status == ECaseStatus.OnShelf)
                .WhereIF(pagedDto.Status.HasValue && pagedDto.Status == ECaseStatus.OffShelf, x => x.Status == ECaseStatus.OffShelf)
                .WhereIF(pagedDto.Status.HasValue && pagedDto.Status == ECaseStatus.Auditing, x => x.Status == ECaseStatus.Auditing)
                .WhereIF(pagedDto.Status.HasValue && pagedDto.Status == ECaseStatus.NewDrafts, x =>
                                                                                               (x.Status == ECaseStatus.Drafts && x.CreatorId == _sessionContext.UserId) ||
                                                                                               (x.Status == ECaseStatus.Revert && x.CreatorId == _sessionContext.UserId) ||
                                                                                               (x.Status == ECaseStatus.NewDrafts && x.CreatorId == _sessionContext.UserId))
                .WhereIF(pagedDto.Status.HasValue && pagedDto.Status == ECaseStatus.All, x => (x.Status == ECaseStatus.OnShelf ||
                                                                                               x.Status == ECaseStatus.OffShelf ||
                                                                                               x.Status == ECaseStatus.Auditing) ||
                                                                                              (x.Status == ECaseStatus.Drafts && x.CreatorId == _sessionContext.UserId) ||
                                                                                              (x.Status == ECaseStatus.Revert && x.CreatorId == _sessionContext.UserId) ||
                                                                                              (x.Status == ECaseStatus.NewDrafts && x.CreatorId == _sessionContext.UserId))

                .WhereIF(!string.IsNullOrEmpty(typeSpliceName), x => x.CaseTypes.Any(t => t.SpliceName.StartsWith(typeSpliceName)))

                .WhereIF(pagedDto.CreationTimeStart.HasValue, x => x.CreationTime >= pagedDto.CreationTimeStart)
                .WhereIF(pagedDto.CreationTimeEnd.HasValue, x => x.CreationTime <= pagedDto.CreationTimeEnd)

                .WhereIF(pagedDto.OnShelfTimeStart.HasValue, x => x.OnShelfTime >= pagedDto.OnShelfTimeStart)
                .WhereIF(pagedDto.OnShelfTimeEnd.HasValue, x => x.OnShelfTime <= pagedDto.OnShelfTimeEnd)

                .WhereIF(pagedDto.OffShelfTimeStart.HasValue, x => x.OffShelfTime >= pagedDto.OffShelfTimeStart)
                .WhereIF(pagedDto.OffShelfTimeEnd.HasValue, x => x.OffShelfTime <= pagedDto.OffShelfTimeEnd)

                .WhereIF(pagedDto.UpdateTimeStart.HasValue, x => x.UpdateTime >= pagedDto.UpdateTimeStart)
                .WhereIF(pagedDto.UpdateTimeEnd.HasValue, x => x.UpdateTime <= pagedDto.UpdateTimeEnd)

                .WhereIF(pagedDto.ExaminTimeStart.HasValue, x => x.ExaminTime >= pagedDto.ExaminTimeStart)
                .WhereIF(pagedDto.ExaminTimeEnd.HasValue, x => x.ExaminTime <= pagedDto.ExaminTimeEnd)

                .OrderByIF(string.IsNullOrEmpty(pagedDto.SortField), x => x.CreationTime, OrderByType.Desc)
                .OrderByIF(pagedDto is { SortField: "pageView" }, x => x.PageView, OrderByType.Desc)         //阅读量
                .OrderByIF(pagedDto is { SortField: "score" }, x => x.Score, OrderByType.Desc)               //评分
                .OrderByIF(pagedDto is { SortField: "creationTime" }, x => x.CreationTime, OrderByType.Desc) //创建时间
                                                                                                             //.Select(x => new CaseDataDto
                                                                                                             //{
                                                                                                             //    Id = x.Id.SelectAll(),
                                                                                                             //    OrderTitle = x.Order.Title,
                                                                                                             //    KnowledgeTitle = x.Knowledge.Title
                                                                                                             //})
                .ToPagedListAsync(pagedDto.PageIndex, pagedDto.PageSize, cancellationToken);

            return (total, _mapper.Map<IList<CaseDataDto>>(temp));
            //return (total, temp);
        }

        #endregion

        #region 案例库 - 新增

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> AddCaseAsync(AddCaseListDto dto, CancellationToken cancellationToken)
        {
            var cList = _mapper.Map<CaseList>(dto);

            var any = await _caseListRepository.Queryable().Where(x => x.Status == ECaseStatus.OnShelf && x.Title == dto.Title).AnyAsync();
            if (any)
                throw UserFriendlyException.SameMessage("当前案例标题存在重复标题！");

            cList.InitId();

            if (dto.Files != null && dto.Files.Count > 0)
                cList.FileJson = await _fileRepository.AddFileAsync(dto.Files, cList.Id, "", cancellationToken);

            if (dto.CaseTypes.Any())
            {
                cList.CaseTypes = dto.CaseTypes.Select(d => new CaseType
                {
                    Id = d.Id,
                    Name = d.Name,
                    SpliceName = d.SpliceName
                }).ToList();
            }

            await _caseListRepository.AddNav(cList)
                                     .Include(d => d.CaseTypes)
                                     .ExecuteCommandAsync();

            return cList.Id;
        }

        #endregion

        #region 案例库 - 修改

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task UpdateCaseAsync(UpdateCaseListDto dto, CancellationToken cancellationToken)
        {
            var Case = await _caseListRepository.GetAsync(dto.Id);

            if (Case == null)
                throw UserFriendlyException.SameMessage("案例库查询失败");

            if ((Case.Status == ECaseStatus.OnShelf || Case.Status == ECaseStatus.Auditing) && (Case.ExpiredTime.HasValue && Case.ExpiredTime.Value > DateTime.Now))
                throw UserFriendlyException.SameMessage("案例库数据不可修改");

            var any = await _caseListRepository.Queryable().Where(x => x.Status == ECaseStatus.OnShelf && x.Title == dto.Title && x.Id != dto.Id).AnyAsync();
            if (any)
                throw UserFriendlyException.SameMessage("当前案例标题存在重复标题！");

            if (dto.ApplyStatus == ECaseApplyStatus.Delete)
            {
                Case.Status = (ECaseStatus)dto.Status;
                Case.ApplyStatus = (ECaseApplyStatus)dto.ApplyStatus;
                Case.Id = dto.Id;
                Case.ApplyReason = dto.ApplyReason;
            }
            else
            {
                _mapper.Map(dto, Case);

                if (dto.Files != null && dto.Files.Count > 0)
                    Case.FileJson = await _fileRepository.AddFileAsync(dto.Files, Case.Id, "", cancellationToken);
                else
                    Case.FileJson = new List<Share.Dtos.File.FileJson>();
            }


            if (dto.ApplyStatus != ECaseApplyStatus.Delete)
            {
                if (dto.CaseTypes.Any())
                {
                    Case.CaseTypes = dto.CaseTypes.Select(d => new CaseType
                    {
                        Id = d.Id
                    }).ToList();

                    await _caseListRepository.UpdateNav(Case)
                        .Include(d => d.CaseTypes, new UpdateNavOptions
                        {
                            ManyToManyIsUpdateA = true
                        })
                        .ExecuteCommandAsync();
                }
                else
                {
                    await _caseListRepository.UpdateAsync(Case, cancellationToken);
                }
            }
            else
            {
                await _caseListRepository.UpdateAsync(Case, cancellationToken);
            }
        }

        #endregion

        #region 案例库 - 删除

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task RemoveCaseAsync(UpdateCaseListDto dto, CancellationToken cancellationToken)
        {
            var Case = await _caseListRepository.GetAsync(dto.Id);

            if (Case == null)
                throw UserFriendlyException.SameMessage("案例库查询失败");

            _mapper.Map(dto, Case);

            Case.IsDeleted = true;

            await _caseListRepository.UpdateNullAsync(Case, cancellationToken);
        }

        #endregion

        #region 案例库 - 审核

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task AuditCaseAsync(UpdateCaseListDto dto, CancellationToken cancellationToken)
        {
            var Case = await _caseListRepository.GetAsync(dto.Id);

            if (Case == null)
                throw UserFriendlyException.SameMessage("案例库查询失败");

            Case.Status = (ECaseStatus)dto.Status!;
            Case.ApplyStatus = (ECaseApplyStatus)dto.ApplyStatus!;
            Case.ExaminTime = dto.ExaminTime;
            Case.ExaminManId = dto.ExaminManId;
            Case.ExaminOrganizeId = dto.ExaminOrganizeId;
            Case.ExaminOpinion = dto.ExaminOpinion;
            Case.UpdateTime = dto.UpdateTime;
            Case.OnShelfTime = dto.OnShelfTime;
            Case.OffShelfTime = dto.OffShelfTime;

            if (Case.ApplyStatus == ECaseApplyStatus.Delete)
            {
                Case.IsDeleted = true;
            }

            await _caseListRepository.UpdateNullAsync(Case, cancellationToken);
        }

        #endregion

        #region 案例库 - 详情

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="IsAddPv">默认不增加，false不增加，true增加浏览量</param>
        /// <returns></returns>
        public async Task<CaseInfoDto> GetCaseAsync(string Id, bool? IsAddPv, CancellationToken cancellationToken)
        {
            var Case = await _caseListRepository.Queryable()
                .Includes(x => x.CaseTypes)
                .Includes(x => x.Order)
                .Includes(x => x.Knowledge)
                .Where(x => x.Id == Id).FirstAsync();
            if (Case == null)
                throw UserFriendlyException.SameMessage("案例库查询失败");
            ;
            //转化
            var caseInfoDto = _mapper.Map<CaseInfoDto>(Case);

            if (Case != null)
            {
                if (!string.IsNullOrEmpty(Case.Abstract))
                    caseInfoDto.Abstract = _bulletinApplication.GetSiteUrls(Case.Abstract);
                if (!string.IsNullOrEmpty(Case.Describe))
                    caseInfoDto.Describe = _bulletinApplication.GetSiteUrls(Case.Describe);
                if (!string.IsNullOrEmpty(Case.Result))
                    caseInfoDto.Result = _bulletinApplication.GetSiteUrls(Case.Result);
                if (!string.IsNullOrEmpty(Case.Reason))
                    caseInfoDto.Reason = _bulletinApplication.GetSiteUrls(Case.Reason);
            }

            // 分类
            //var relationType = await _caseRelationTypeRepository.QueryAsync(x => x.CaseId == Id && x.CreatorId == _sessionContext.UserId);
            //if (relationType != null)
            //{
            //    caseInfoDto.CaseTypes = _mapper.Map<List<CaseRelationTypeDto>>(relationType);
            //}

            // 收藏
            var collect = await _caseCollectRepository.GetAsync(x => x.CaseId == Id && x.CreatorId == _sessionContext.UserId);
            if (collect != null)
                caseInfoDto.Collect = _mapper.Map<CaseCollectDto>(collect);

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

            // 更新浏览量
            if (IsAddPv == true)
            {
                //修改浏览量
                Case.PageView++;
                //修改点击量
                await _caseListRepository.UpdateAsync(Case, cancellationToken);
            }
            return caseInfoDto;
        }

        #endregion

        #region 案例库 - 批量导出

        /// <summary>
        /// 案例库 - 批量导出
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Dictionary<string, Stream>> CaseInfoListExportAsync(CaseInfoExportDto dto, CancellationToken cancellationToken)
        {
            var streamList = new Dictionary<string, Stream>();
            var knowList = await _caseListRepository.Queryable()
                .Where(m => dto.Ids.Contains(m.Id))
                .Select(m => new { m.Title, m.Result })
                .ToListAsync(cancellationToken);

            var tasks = knowList.Select(async item =>
            {
                var stream = await Task.Run(() => item.Result.HtmlToStream(dto.FileType), 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;
        }

        #endregion

        #endregion

        #region 私有方法

        #region 查询所有子级

        /// <summary>
        /// 查询所有子级
        /// </summary>
        /// <param name="treeDatas">分类数据</param>
        /// <param name="ID">需要查询哪级下面的分类</param>
        /// <param name="checkId">选中的数据ID</param>
        /// <returns></returns>
        private List<TreeListDto> GetChildren(List<CaseType> treeDatas, string ID, string? checkId)
        {
            List<TreeListDto> nodeList = new();
            //根据ID查询子级
            var children = treeDatas.Where(q => q.ParentId == ID);
            foreach (var dr in children)
            {
                //组装数据
                TreeListDto node = new()
                {
                    name = dr.Name,
                    ParentID = dr.ParentId,
                    value = dr.Id,
                    IsEnable = dr.IsEnable
                };
                //是否选中
                if (!string.IsNullOrEmpty(checkId) && checkId != Guid.Empty.ToString() && checkId == dr.Id)
                    node.selected = true;
                //子级数据赋值
                node.children = GetChildren(treeDatas, node.value, checkId);
                //添加数据
                nodeList.Add(node);
            }
            return nodeList;
        }

        #endregion

        #region  获取全称

        /// <summary>
        /// 获取全称
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        private async Task<string> GetFullName(string? Id)
        {
            //获取全部父级名称
            var list = await GetParentNode(Id);
            //倒叙
            list.Reverse();
            //拆分
            return string.Join("-", list.ToArray());
        }

        /// <summary>
        /// 查询父级名称
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        private async Task<List<string>> GetParentNode(string? Id)
        {
            List<string> list = new();
            //查询父级数据
            var type = await _caseTypeRepository.GetAsync(p => p.Id == Id);
            if (type != null)
            {
                //添加名称
                list.Add(type.Name);
                list.AddRange(await GetParentNode(type.ParentId));
            }
            return list;
        }

        #endregion

        #region 修改子级分类全称

        /// <summary>
        /// 修改子级分类全称
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        private async Task UpdateChildNode(string Id)
        {
            //查询子分类
            var list = await GetChildNode(Id);
            if (list is not null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    //获取全称
                    string FullName = await GetFullName(item.ParentId);
                    item.SpliceName = string.IsNullOrEmpty(FullName) ? item.Name : FullName + "-" + item.Name;
                    //修改全称
                    await _caseTypeRepository.UpdateAsync(item);
                }
            }
        }

        /// <summary>
        /// 查询子级节点数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        private async Task<List<CaseType>> GetChildNode(string Id)
        {
            List<CaseType> list = new();
            //查询数据
            var typelist = await _caseTypeRepository.QueryAsync(p => p.ParentId == Id);
            if (typelist != null)
            {
                //处理数据
                foreach (var item in typelist)
                {
                    list.Add(item);
                    list.AddRange(await GetChildNode(item.Id));
                }
            }
            return list;
        }

        #endregion

        #endregion

    }
}
