123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- using Hotline.Planlibrary;
- using Hotline.Settings.Hotspots;
- using Hotline.Share.Dtos.Knowledge;
- using Hotline.Share.Dtos.Planlibrary;
- using Hotline.Share.Enums.Planlibrary;
- 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 Hotline.KnowledgeBase;
- using NPOI.SS.Formula.Functions;
- using SqlSugar;
- using Senparc.CO2NET.Extensions;
- namespace Hotline.Application.Planlibrary
- {
- /// <summary>
- /// 预案库处理
- /// </summary>
- public class PlanApplication : IPlanApplication, IScopeDependency
- {
- #region 注册
- private readonly IRepository<PlanList> _planListRepository; //预案库列表
- private readonly IRepository<PlanRelationType> _planRelationTypeRepository; //预案库关联类型
- private readonly IRepository<PlanType> _planTypeRepository; //预案库分类管理
- private readonly IRepository<PlanTypeOrg> _planTypeOrgRepository; //预案库分类关联机构
- private readonly ISessionContext _sessionContext;
- private readonly IMapper _mapper;
- private readonly IRepository<Hotspot> _hotspotTypeRepository;
- private readonly IFileRepository _fileRepository;
- private readonly IBulletinApplication _bulletinApplication;
- public PlanApplication(
- IRepository<PlanList> planListRepository,
- IRepository<PlanRelationType> planRelationTypeRepository,
- IRepository<PlanType> planTypeRepository,
- IRepository<PlanTypeOrg> planTypeOrgRepository,
- ISessionContext sessionContext,
- IMapper mapper,
- IRepository<Hotspot> hotspotTypeRepository,
- IFileRepository fileRepository,
- IBulletinApplication bulletinApplication)
- {
- _planListRepository = planListRepository;
- _planRelationTypeRepository = planRelationTypeRepository;
- _planTypeRepository = planTypeRepository;
- _planTypeOrgRepository = planTypeOrgRepository;
- _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(AddPlanTypeDto dto, CancellationToken cancellationToken)
- {
- var sandard = await _planTypeRepository.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<PlanType>(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 _planTypeRepository.AddAsync(type, cancellationToken);
- return id;
- }
- #endregion
- #region 预案库类型 - 编辑
- /// <summary>
- /// 预案库类型 - 编辑
- /// </summary>
- /// <param name="dto"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task UpdateTypeAsync(UpdatePlanTypeDto dto, CancellationToken cancellationToken)
- {
- //查询原有数据
- var type = await _planTypeRepository.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 _planTypeRepository.UpdateAsync(type, cancellationToken);
- //如果修改了名称,对应修改子分类全称
- if (result)
- await UpdateChildNode(type.Id);
- // 修改关联机构
- await _planTypeOrgRepository.RemoveAsync(x => x.TypeId == type.Id, false, cancellationToken);
- if (dto.TypeOrgDtos != null && dto.TypeOrgDtos.Any())
- {
- List<PlanTypeOrg> orgs = _mapper.Map<List<PlanTypeOrg>>(dto.TypeOrgDtos);
- orgs.ForEach(x => x.TypeId = type.Id);
- await _planTypeOrgRepository.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 _planTypeRepository.GetAsync(p => p.Id == Id && p.IsDeleted == false, cancellationToken);
- if (sandard is null)
- throw UserFriendlyException.SameMessage("分类不存在!");
- //查询是否有子级分类
- var checkChild = await _planTypeRepository.CountAsync(p => p.ParentId == Id && p.IsDeleted == false, cancellationToken);
- if (checkChild > 0)
- throw UserFriendlyException.SameMessage("存在子级分类!");
- //查询是否有知识分类
- var checkKnowledge = await _planListRepository.CountAsync(p => p.PlanTypes.Any(t => t.PlanId == Id), cancellationToken);
- if (checkKnowledge > 0)
- throw UserFriendlyException.SameMessage("分类存在预案!");
- //删除操作
- await _planTypeRepository.RemoveAsync(sandard, true, cancellationToken);
- }
- #endregion
- #endregion
- #region 预案库管理
- #region 预案库 - 列表
- /// <summary>
- /// 预案库 - 列表
- /// </summary>
- /// <param name="pagedDto"></param>
- /// <returns></returns>
- public async Task<(int, IList<PlanDataDto>)> QueryAllPlanListAsync(PlanListDto pagedDto, CancellationToken cancellationToken)
- {
- //if (!_sessionContext.OrgIsCenter)
- //{// 部门只能查询【部门预案库】
- // pagedDto.Attribution = "部门预案库";
- //}
- var typeSpliceName = string.Empty;
- var hotspotHotSpotFullName = string.Empty;
- if (!string.IsNullOrEmpty(pagedDto.PlanTypeID))
- {
- var type = await _planTypeRepository.GetAsync(x => x.Id == pagedDto.PlanTypeID);
- typeSpliceName = type?.SpliceName;
- }
- if (!string.IsNullOrEmpty(pagedDto.HotspotId))
- {
- var hotspot = await _hotspotTypeRepository.GetAsync(x => x.Id == pagedDto.HotspotId);
- hotspotHotSpotFullName = hotspot?.HotSpotFullName;
- }
- //单表分页
- var (total, temp) = await _planListRepository.Queryable()
- .Includes(x => x.PlanTypes)
- .Includes(x => x.HotspotType)
- .Where(x => x.IsDeleted == false)
- .Where(x => (x.Status == EPlanStatus.Drafts && x.CreatorId == _sessionContext.UserId) || (x.Status != EPlanStatus.Drafts))
- .WhereIF(OrgSeedData.CenterId != pagedDto.CreateOrgId && !string.IsNullOrEmpty(pagedDto.CreateOrgId), x => x.CreatorOrgId != null && x.CreatorOrgId.StartsWith(pagedDto.CreateOrgId!))
- .WhereIF(!string.IsNullOrEmpty(pagedDto.Attribution), x => x.Attribution == pagedDto.Attribution)
- .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!))
- .WhereIF(pagedDto.Status.HasValue && pagedDto.Status != EPlanStatus.OffShelf &&
- pagedDto.Status != EPlanStatus.NewDrafts &&
- pagedDto.Status != EPlanStatus.All,
- x => x.Status == pagedDto.Status && ((x.ExpiredTime != null && x.ExpiredTime > DateTime.Now) || x.ExpiredTime == null))
- .WhereIF(pagedDto.Status.HasValue && pagedDto.Status == EPlanStatus.OffShelf, x => x.Status == pagedDto.Status || (x.ExpiredTime != null && x.ExpiredTime < DateTime.Now && x.Status != EPlanStatus.Drafts))
- .WhereIF(pagedDto.Status.HasValue && pagedDto.Status == EPlanStatus.NewDrafts, x => x.Status == EPlanStatus.Drafts || x.Status == EPlanStatus.Revert)
- .WhereIF(pagedDto.IsPublic.HasValue, x => x.IsPublic == pagedDto.IsPublic)
- .WhereIF(!string.IsNullOrEmpty(typeSpliceName), x => x.PlanTypes.Any(t => t.PlanTypeSpliceName.StartsWith(typeSpliceName)))
- .WhereIF(!string.IsNullOrEmpty(hotspotHotSpotFullName), x => x.HotspotType.HotSpotFullName.EndsWith(hotspotHotSpotFullName!))
- .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), d => d.CreationTime, OrderByType.Desc)
- .OrderByIF(pagedDto is { SortField: "PageView" }, d => d.PageView, OrderByType.Desc) //阅读量
- .OrderByIF(pagedDto is { SortField: "Score" }, d => d.Score, OrderByType.Desc) //评分
- .OrderByIF(pagedDto is { SortField: "CreationTime" }, d => d.CreationTime, OrderByType.Desc) //创建时间
- .ToPagedListAsync(pagedDto.PageIndex, pagedDto.PageSize, cancellationToken);
- return (total, _mapper.Map<IList<PlanDataDto>>(temp));
- }
- #endregion
- #region 预案库 - 新增
- /// <summary>
- /// 新增
- /// </summary>
- /// <param name="dto"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task<string> AddPlanAsync(AddPlanListDto dto, CancellationToken cancellationToken)
- {
- var pList = _mapper.Map<PlanList>(dto);
- var any = await _planListRepository.Queryable().Where(x => x.Status == EPlanStatus.OnShelf && x.Title == dto.Title).AnyAsync();
- if (any)
- throw UserFriendlyException.SameMessage("当前知识标题存在重复标题!");
- if (dto.Files != null && dto.Files.Count > 0)
- pList.FileJson = await _fileRepository.AddFileAsync(dto.Files, pList.Id, "", cancellationToken);
- await _planListRepository.AddAsync(pList, cancellationToken);
- if (dto.PlanType.Any())
- {
- List<PlanRelationType> types = _mapper.Map<List<PlanRelationType>>(dto.PlanType);
- types.ForEach(x => x.PlanId = pList.Id);
- await _planRelationTypeRepository.AddRangeAsync(types, cancellationToken);
- }
- return pList.Id;
- }
- #endregion
- #region 预案库 - 修改
- /// <summary>
- /// 修改
- /// </summary>
- /// <param name="dto"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task UpdatePlanAsync(UpdatePlanListDto dto, CancellationToken cancellationToken)
- {
- var plan = await _planListRepository.GetAsync(dto.Id);
- if (plan == null)
- throw UserFriendlyException.SameMessage("预案库查询失败");
- if ((plan.Status == EPlanStatus.OnShelf || plan.Status == EPlanStatus.Auditing) && (plan.ExpiredTime.HasValue && plan.ExpiredTime.Value > DateTime.Now))
- throw UserFriendlyException.SameMessage("预案库数据不可修改");
- var any = await _planListRepository.Queryable().Where(x => x.Status == EPlanStatus.OnShelf && x.Title == dto.Title && x.Id != dto.Id).AnyAsync();
- if (any)
- throw UserFriendlyException.SameMessage("当前知识标题存在重复标题!");
- _mapper.Map(dto, plan);
- plan.HotspotId = dto.HotspotId;
- if (dto.Files != null && dto.Files.Count > 0)
- plan.FileJson = await _fileRepository.AddFileAsync(dto.Files, plan.Id, "", cancellationToken);
- else
- plan.FileJson = new List<Share.Dtos.File.FileJson>();
- await _planListRepository.UpdateNullAsync(plan, cancellationToken);
- if (dto.PlanType.Any())
- {
- var anyRelationTypes = await _planRelationTypeRepository.Queryable().Where(x => x.PlanId == plan.Id).ToListAsync();
- if (anyRelationTypes.Any())
- await _planRelationTypeRepository.RemoveRangeAsync(anyRelationTypes);
- List<PlanRelationType> types = _mapper.Map<List<PlanRelationType>>(dto.PlanType);
- types.ForEach(x => x.PlanId = dto.Id);
- await _planRelationTypeRepository.AddRangeAsync(types, cancellationToken);
- }
- }
- #endregion
- #region 预案库 - 下架审核
- /// <summary>
- /// 下架审核
- /// </summary>
- /// <param name="dto"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task AuditPlanAsync(UpdatePlanListDto dto, CancellationToken cancellationToken)
- {
- var plan = await _planListRepository.GetAsync(dto.Id);
- if (plan == null)
- throw UserFriendlyException.SameMessage("预案库查询失败");
- plan.Status = (EPlanStatus)dto.Status!;
- plan.ApplyStatus = (EPlanApplyStatus)dto.ApplyStatus!;
- plan.ExaminTime = dto.ExaminTime;
- plan.ExaminManId = dto.ExaminManId;
- plan.ExaminOrganizeId = dto.ExaminOrganizeId;
- plan.UpdateTime = dto.UpdateTime;
- await _planListRepository.UpdateNullAsync(plan, cancellationToken);
- }
- #endregion
- #region 预案库 - 详情
- /// <summary>
- /// 详情
- /// </summary>
- /// <param name="Id"></param>
- /// <param name="IsAddPv">默认不增加,false不增加,true增加浏览量</param>
- /// <returns></returns>
- public async Task<PlanInfoDto> GetPlanAsync(string Id, bool? IsAddPv, CancellationToken cancellationToken)
- {
- var plan = await _planListRepository.GetAsync(Id);
- if (plan == null)
- throw UserFriendlyException.SameMessage("预案库查询失败");
- ;
- //转化
- var planInfoDto = _mapper.Map<PlanInfoDto>(plan);
- if (plan != null && !string.IsNullOrEmpty(plan.Content))
- planInfoDto.Content = _bulletinApplication.GetSiteUrls(plan.Content);
- // 热点
- //var hot = await _hotspotTypeRepository.GetAsync(plan.HotspotId, cancellationToken);
- //if (hot != null)
- // planDto.HotspotId = hot.HotSpotFullName;
- if (planInfoDto.FileJson != null && planInfoDto.FileJson.Any())
- {
- var ids = planInfoDto.FileJson.Select(x => x.Id).ToList();
- planInfoDto.Files = await _fileRepository.GetFilesAsync(ids, cancellationToken);
- }
- // 更新浏览量
- if (IsAddPv == true)
- {
- //修改浏览量
- plan.PageView++;
- //修改点击量
- await _planListRepository.UpdateAsync(plan, cancellationToken);
- }
- return planInfoDto;
- }
- #endregion
- #region 预案库 - 批量导出
- /// <summary>
- /// 预案库 - 批量导出
- /// </summary>
- /// <param name="dto"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task<Dictionary<string, Stream>> PlanInfoListExportAsync(PlanInfoExportDto dto, CancellationToken cancellationToken)
- {
- var streamList = new Dictionary<string, Stream>();
- var knowList = await _planListRepository.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), 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<PlanType> 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 _planTypeRepository.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 _planTypeRepository.UpdateAsync(item);
- }
- }
- }
- /// <summary>
- /// 查询子级节点数据
- /// </summary>
- /// <param name="Id"></param>
- /// <returns></returns>
- private async Task<List<PlanType>> GetChildNode(string Id)
- {
- List<PlanType> list = new();
- //查询数据
- var typelist = await _planTypeRepository.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
- }
- }
|