123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Transactions;
- using AutoMapper;
- using GxPress.Common.Tools;
- using GxPress.Entity.Topic;
- using GxPress.Repository.Interface;
- using GxPress.Request.App.Topic;
- using GxPress.Service.Interface.Topic;
- using Datory;
- using GxPress.Common.Exceptions;
- using GxPress.Common.Page;
- using GxPress.Repository.Interface.Topic;
- using GxPress.Result;
- using GxPress.Result.App.Topic;
- using GxPress.Service.Interface.Note;
- using Newtonsoft.Json;
- using GxPress.Repository.Interface.Visit;
- using GxPress.Service.Interface.Visit;
- using GxPress.Service.Interface.Analyze;
- using GxPress.Repository.Interface.DepartmentUser;
- using GxPress.EnumConst;
- using GxPress.Repository.Interface.Note;
- using GxPress.Result.Department;
- using GxPress.Service.Interface.CommonSqlKata;
- using GxPress.Repository.Interface.RecordFolder;
- namespace GxPress.Service.Implement.Topic
- {
- public partial class TopicService : ITopicService
- {
- private readonly ITopicRepository _topicRepository;
- private readonly IUserRepository _userRepository;
- private readonly IDepartmentUserRepository departmentUserRepository;
- private readonly ITopicAddresseeRepository _topicAddresseeRepository;
- private readonly IDepartmentRepository _departmentRepository;
- private readonly ITopicGroupRepository _topicGroupRepository;
- private readonly ITopicGroupUserRepository _topicGroupUserRepository;
- private readonly IAnalyzeService _analyzeService;
- private readonly ICommentRepository _commentRepository;
- private readonly IMapper _mapper;
- private readonly IGroupUserRepository _groupUserRepository;
- private readonly IFolderUserRepository _folderUserRepository;
- private readonly INoteService _noteService;
- private readonly IMiddleRepository _middleRepository;
- private readonly IVisitRepository _visitRepository;
- private readonly IVisitService _visitService;
- private readonly INoteRepository noteRepository;
- private readonly IGroupRepository groupRepository;
- private readonly ICommonSqlKataService commonSqlKataService;
- private readonly IRecordFolderRepository recordFolderRepository;
- public TopicService(ITopicRepository topicRepository, IUserRepository userRepository,
- ITopicAddresseeRepository topicAddresseeRepository,
- ITopicGroupRepository topicGroupRepository,
- ITopicGroupUserRepository topicGroupUserRepository, IAnalyzeService analyzeService,
- ICommentRepository commentRepository, IMapper mapper, IDepartmentRepository departmentRepository,
- IGroupUserRepository groupUserRepository, IFolderUserRepository folderUserRepository,
- INoteService noteService, IMiddleRepository middleRepository,
- IVisitRepository visitRepository, IVisitService visitService,
- IDepartmentUserRepository departmentUserRepository,
- INoteRepository noteRepository,
- IGroupRepository groupRepository,
- ICommonSqlKataService commonSqlKataService,
- IRecordFolderRepository recordFolderRepository)
- {
- _topicRepository = topicRepository;
- _userRepository = userRepository;
- _topicAddresseeRepository = topicAddresseeRepository;
- _topicGroupRepository = topicGroupRepository;
- _topicGroupUserRepository = topicGroupUserRepository;
- _analyzeService = analyzeService;
- _commentRepository = commentRepository;
- _departmentRepository = departmentRepository;
- _mapper = mapper;
- _groupUserRepository = groupUserRepository;
- _folderUserRepository = folderUserRepository;
- _noteService = noteService;
- _middleRepository = middleRepository;
- _visitRepository = visitRepository;
- _visitService = visitService;
- this.departmentUserRepository = departmentUserRepository;
- this.noteRepository = noteRepository;
- this.groupRepository = groupRepository;
- this.commonSqlKataService = commonSqlKataService;
- this.recordFolderRepository=recordFolderRepository;
- }
- /// <summary>
- /// 根据GroupId删除
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- public async Task<bool> DeleteTopicGroupAsync(List<int> ids)
- {
- try
- {
- using (TransactionScope transactionScope = new TransactionScope())
- {
- await _topicGroupRepository.DeleteAsync(ids);
- await _topicGroupUserRepository.DeleteGroupIdAsync(ids);
- transactionScope.Complete();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- throw;
- }
- return false;
- }
- /// <summary>
- ///最新 小组话题分页列表
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PagedList<TopicListPageResult>> GetGroupTopicPageAsync(TopicDetailListRequest request)
- {
- var query = new SqlKata.Query("tede_topic");
- query.Join("tede_group", "tede_group.Id", "tede_topic.GroupId");
- query.Join("tede_user", "tede_user.Id", "tede_topic.UserId");
- query.Where("tede_topic.GroupId", ">", 0);
- query.Where("tede_topic.IsDraft", request.IsDraft);
- if (request.IsDraft)
- query.Where("tede_group.UserId", request.UserId);
- if (request.GroupIds.Count() == 0)
- request.GroupIds.Add(-1);
- if (request.GroupIds.Count > 0)
- {
- var groupIds = new List<int>();
- foreach (var item in request.GroupIds)
- {
- if (item > 0)
- groupIds.Add(item);
- //全网
- if (item == -1)
- groupIds.AddRange(await _topicRepository.GetPublicGroupAsync(request.UserId));
- //我的小组
- if (item == -2)
- groupIds.AddRange(await _topicRepository.GetIntoGroupAsync(request.UserId));
- }
- if (groupIds.Count > 0)
- query.WhereIn("tede_topic.GroupId", groupIds);
- }
- if (!string.IsNullOrEmpty(request.Key))
- query.Where(n => n.OrWhereLike("tede_topic.Title", $"%{request.Key}%").OrWhereLike("tede_topic.TextContent", $"%{request.Key}%").OrWhereLike("tede_user.Name", $"%{request.Key}%"));
- //条数
- var queryCount = query;
- //公共
- commonSqlKataService.GetCommonQueryAsync(AllTypeConst.Topic.GetHashCode(), request.UserId, query, "tede_topic.Id");
- //获取用户所在部门
- var departmentNameQuery = new SqlKata.Query("tede_department");
- var departmentUserQuery = new SqlKata.Query("tede_department_user");
- departmentUserQuery.WhereColumns("tede_department_user.UserId", "=", "tede_topic.UserId");
- departmentUserQuery.Select("tede_department_user.DepartmentId");
- departmentNameQuery.WhereIn("tede_department.Id", departmentUserQuery);
- departmentNameQuery.Limit(1);
- departmentNameQuery.Select("tede_department.Name");
- query.Select(departmentNameQuery, "DepartmentName");
- query.Select(
- "tede_topic.{Id,Title,UserId,ReadCount,CreatedDate,Content,GroupId}",
- "tede_group.{Name as GroupName}",
- "tede_user.{Name as UserName,AvatarUrl}");
- query.OrderByDesc("tede_topic.CreatedDate");
- query.ForPage(request.Page, request.PerPage);
- var result = await _topicRepository.GetGroupTopicPageAsync(query, queryCount, request);
- //判断是否小组成员
- if (request.GroupIds.Count == 1)
- {
- result.IsUser = await _groupUserRepository.ExistsAsync(request.UserId, request.GroupIds[0]);
- //修改用户阅读小组
- await _groupUserRepository.UpdateGroupUserReadDateAsync(request.UserId, request.GroupIds[0]);
- }
- //获取数量
- // result.Total = 10;
- foreach (var item in result.Items)
- {
- if (string.IsNullOrEmpty(item.Title))
- item.Title = string.Empty;
- if (string.IsNullOrWhiteSpace(item.UserName))
- {
- item.Content = "[]";
- item.Data = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
- item.Content = "";
- continue;
- }
- if (string.IsNullOrWhiteSpace(item.Content))
- item.Content = "[]";
- var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
- foreach (var contentJsonDataItem in contentJsonData)
- contentJsonDataItem.File = StringUtils.AddDomain(contentJsonDataItem.File);
- if (contentJsonData.Count == 0)
- item.DataType = 1;
- item.Content = "";
- if (contentJsonData.Count > 0)
- {
- var imgData = new List<ContentJsonData>();
- var FileData = new List<ContentJsonData>();
- var forCount = 1;
- var firstContent = string.Empty;
- foreach (var jsonData in contentJsonData)
- {
- if (jsonData.Type == AllTypeConst.Text.GetHashCode() && forCount == 1)
- //文本
- firstContent = jsonData.Text;
- else if (jsonData.Type == AllTypeConst.Text.GetHashCode() && forCount > 1)
- //文本
- continue;
- //图片
- else if (jsonData.Type == AllTypeConst.Image.GetHashCode() && FileData.Count == 0)
- {
- if (imgData.Count >= 9)
- break;
- imgData.Add(jsonData);
- }
- //附件
- else
- {
- FileData.Add(jsonData);
- break;
- }
- forCount++;
- }
- item.Content = string.IsNullOrWhiteSpace(firstContent) ? "" : firstContent;
- if (imgData.Count > 0) { item.DataType = 2; item.Data = imgData; }
- else if (FileData.Count > 0) { item.DataType = 3; item.Data = FileData; }
- else item.DataType = 1;
- var contType = new List<int> { AllTypeConst.Text.GetHashCode(), AllTypeConst.Image.GetHashCode() };
- item.FileCount = contentJsonData.Count(n => !contType.Contains(n.Type));
- }
- if (item.Data == null || item.Data.Count == 0)
- item.Data = new List<ContentJsonData>();
- if (item.Data != null)
- {
- foreach (var data in item.Data)
- {
- if (string.IsNullOrEmpty(data.Title))
- data.Title = string.Empty;
- if (string.IsNullOrEmpty(data.FileName))
- data.FileName = string.Empty;
- if (data.Type == AllTypeConst.CollectFolder.GetHashCode())
- {
- var userName = await _userRepository.GetNameAsync(data.SourceUserId);
- data.Author = userName;
- }
- }
- }
- }
- return result;
- }
- public async Task<PagedList<TopicListPageResult>> GetTopicByGroupAsync(TopicPageSearchRequest request)
- {
- var result = await _topicRepository.GetTopicByGroupAsync(request);
- //获取数量
- // result.Total = 10;
- foreach (var item in result.Items)
- {
- if (string.IsNullOrWhiteSpace(item.UserName))
- {
- item.Content = "[]";
- item.Data = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
- item.Content = "";
- continue;
- }
- if (string.IsNullOrWhiteSpace(item.Content))
- item.Content = "[]";
- var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
- foreach (var contentJsonDataItem in contentJsonData)
- contentJsonDataItem.File = StringUtils.AddDomain(contentJsonDataItem.File);
- if (contentJsonData.Count == 0)
- item.DataType = 1;
- item.Content = "";
- if (contentJsonData.Count > 0)
- {
- var imgData = new List<ContentJsonData>();
- var FileData = new List<ContentJsonData>();
- var forCount = 1;
- var firstContent = string.Empty;
- foreach (var jsonData in contentJsonData)
- {
- if (jsonData.Type == AllTypeConst.Text.GetHashCode() && forCount == 1)
- //文本
- firstContent = jsonData.Text;
- else if (jsonData.Type == AllTypeConst.Text.GetHashCode() && forCount > 1)
- //文本
- continue;
- //图片
- else if (jsonData.Type == AllTypeConst.Image.GetHashCode() && FileData.Count == 0)
- {
- if (imgData.Count > 9)
- break;
- imgData.Add(jsonData);
- }
- //附件
- else
- {
- FileData.Add(jsonData);
- break;
- }
- forCount++;
- }
- item.Content = string.IsNullOrWhiteSpace(firstContent) ? "" : firstContent;
- if (imgData.Count > 0) { item.DataType = 2; item.Data = imgData; }
- else if (FileData.Count > 0) { item.DataType = 3; item.Data = FileData; }
- else item.DataType = 1;
- var contType = new List<int> { AllTypeConst.Text.GetHashCode(), AllTypeConst.Image.GetHashCode() };
- item.FileCount = contentJsonData.Count(n => !contType.Contains(n.Type));
- }
- if (item.Data == null || item.Data.Count == 0)
- item.Data = new List<ContentJsonData>();
- }
- return result;
- }
- public async Task<string> GetTopicPageSqlAsync(TopicPageSearchRequest request)
- {
- var topicTypeValue = GxPress.EnumConst.AllTypeConst.TopicNote.GetHashCode();
- var sql = $@"SELECT
- a.Id,a.Title,a.UserId,a.Content,a.CreatedDate,a.IsTopic,a.ReadCount,c.ParentId,
- (SELECT
- FolderName
- FROM
- tede_middle
- WHERE
- id = c.ParentId and IsDelete=0) AS FolderName,
- (SELECT
- COUNT(1)
- FROM
- tede_analyze
- WHERE
- TypeValue = {topicTypeValue}
- AND SourceId = a.Id
- AND AnalyzeType = 1) AS PraiseCount,
- (SELECT
- COUNT(1)
- FROM
- tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = {topicTypeValue}
- AND SourceId = a.Id
- AND AnalyzeType = 1
- LIMIT 0 , 1) AS IsPraise,
- (SELECT
- COUNT(1)
- FROM
- tede_comment
- WHERE
- ArticleId = a.Id AND TypeValue = {topicTypeValue}
- AND Pid = 0) AS CommentCount,
- (SELECT
- COUNT(1)
- FROM
- tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = {topicTypeValue}
- AND SourceId = a.Id
- AND AnalyzeType = 4) AS RetransmissionCount,
- (SELECT
- COUNT(1)
- FROM
- tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = {topicTypeValue}
- AND SourceId = a.Id
- AND AnalyzeType = 4
- LIMIT 0 , 1) AS IsRetransmission,
- (SELECT
- COUNT(1)
- FROM
- tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = {topicTypeValue}
- AND SourceId = a.Id
- AND AnalyzeType = 3
- LIMIT 0 , 1) AS IsCollect,
- b.Name as UserName, b.AvatarUrl
- FROM
- tede_note a
- INNER JOIN
- tede_user b ON a.UserId = b.Id
- INNER JOIN
- tede_middle c ON c.MiddleId = a.Id
- WHERE
- c.FolderType = {topicTypeValue} AND a.IsTopic = 1 and c.IsDelete=0 and a.IsDraft=0
- ";
- if (request.UserId <= 0)
- {
- sql += $@" AND c.ParentId IN (SELECT
- id
- FROM
- tede_middle
- WHERE
- FolderType = {topicTypeValue} AND AttributeValue = 2
- AND RoleId = 1)";
- }
- else
- {
- sql += $@" AND (a.userId={request.UserId} or c.ParentId IN (SELECT
- MiddleId
- FROM
- tede_folder_user
- WHERE
- MiddleId = c.ParentId AND UserId = {request.UserId})
- OR a.UserId IN (SELECT
- Id
- FROM
- tede_user
- WHERE
- 1 = (SELECT
- RoleId
- FROM
- tede_middle
- WHERE
- id = c.ParentId) and Id=a.UserId
- ))";
- }
- if (request.TopicGroupIds.Count > 0)
- {
- var topicGroupId = "";
- foreach (var item in request.TopicGroupIds)
- {
- if (item <= 0)
- continue;
- topicGroupId += $"{item},";
- }
- if (!string.IsNullOrEmpty(topicGroupId))
- {
- topicGroupId = topicGroupId.Remove(topicGroupId.Length - 1, 1);
- sql += $@" AND a.UserId IN (SELECT
- UserId
- FROM
- tede_topic_group_user
- WHERE
- TopicGroupId IN ({topicGroupId}))";
- }
- }
- if (request.TopicGroupIds.Count > 0)
- {
- //我的
- if (request.TopicGroupIds.Contains(TopicNotoGropConst.My.GetHashCode()))
- {
- sql += $" and a.UserId={request.UserId} ";
- }
- //同单位
- if (request.TopicGroupIds.Contains(TopicNotoGropConst.Work.GetHashCode()))
- {
- if (request.UserId > 0)
- {
- var departmentIds = await departmentUserRepository.GetDepartmentIdsAsync(request.UserId);
- var departments = new List<DepartmentResult>();
- foreach (var departmentId in departmentIds)
- {
- await _departmentRepository.GetDepartmentById(departmentId, departments);
- }
- if (departments.Count > 0)
- {
- var userIds = await departmentUserRepository.GetUserIdsByDepartmentIdsAsync(departments.Select(n => n.Id));
- if (userIds.Count() > 0)
- {
- var str = "";
- foreach (var item in userIds)
- str += $"{item},";
- str = str.Remove(str.Length - 1, 1);
- sql += $" and a.UserId in({str})";
- }
- }
- }
- }
- }
- if (!string.IsNullOrWhiteSpace(request.Keyword))
- {
- sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
- OR a.Title LIKE '%{request.Keyword}%'
- OR a.HtmlContent LIKE '%{request.Keyword}%')";
- }
- sql += $@" ORDER BY a.CreatedDate DESC
- LIMIT {(request.Page - 1) * request.PerPage} , {request.PerPage}";
- return sql;
- }
- public async Task<string> AssembleSqlCount(TopicPageSearchRequest request)
- {
- var topicTypeValue = GxPress.EnumConst.AllTypeConst.TopicNote.GetHashCode();
- var sql = $@"SELECT count(1) FROM
- tede_note a
- INNER JOIN
- tede_user b ON a.UserId = b.Id
- INNER JOIN
- tede_middle c ON c.MiddleId = a.Id
- WHERE
- c.FolderType = {topicTypeValue} AND a.IsTopic = 1 and c.IsDelete=0 and a.IsDraft=0";
- if (request.UserId <= 0)
- {
- sql += $@" AND c.ParentId IN (SELECT
- id
- FROM
- tede_middle
- WHERE
- FolderType = {topicTypeValue} AND AttributeValue = 2
- AND RoleId = 1)";
- }
- else
- {
- sql += $@" AND (a.userId={request.UserId} or c.ParentId IN (SELECT
- MiddleId
- FROM
- tede_folder_user
- WHERE
- MiddleId = c.ParentId AND UserId = {request.UserId})
- OR a.UserId IN (SELECT
- Id
- FROM
- tede_user
- WHERE
- 1 = (SELECT
- RoleId
- FROM
- tede_middle
- WHERE
- id = c.ParentId) and Id=a.UserId
- ))";
- }
- if (request.TopicGroupIds.Count > 0)
- {
- var topicGroupId = "";
- foreach (var item in request.TopicGroupIds)
- {
- if (item <= 0)
- continue;
- topicGroupId += $"{item},";
- }
- if (!string.IsNullOrEmpty(topicGroupId))
- {
- topicGroupId = topicGroupId.Remove(topicGroupId.Length - 1, 1);
- sql += $@" AND a.UserId IN (SELECT
- UserId
- FROM
- tede_topic_group_user
- WHERE
- TopicGroupId IN ({topicGroupId}))";
- }
- }
- if (request.TopicGroupIds.Count > 0)
- {
- //我的
- if (request.TopicGroupIds.Contains(TopicNotoGropConst.My.GetHashCode()))
- {
- sql += $" and a.UserId={request.UserId} ";
- }
- //同单位
- if (request.TopicGroupIds.Contains(TopicNotoGropConst.Work.GetHashCode()))
- {
- if (request.UserId > 0)
- {
- var departmentIds = await departmentUserRepository.GetDepartmentIdsAsync(request.UserId);
- var departments = new List<DepartmentResult>();
- foreach (var departmentId in departmentIds)
- {
- await _departmentRepository.GetDepartmentById(departmentId, departments);
- }
- if (departments.Count > 0)
- {
- var userIds = await departmentUserRepository.GetUserIdsByDepartmentIdsAsync(departments.Select(n => n.Id));
- if (userIds.Count() > 0)
- {
- var str = "";
- foreach (var item in userIds)
- str += $"{item},";
- str = str.Remove(str.Length - 1, 1);
- sql += $" and a.UserId in({str})";
- }
- }
- }
- }
- }
- if (!string.IsNullOrWhiteSpace(request.Keyword))
- {
- sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
- OR a.Title LIKE '%{request.Keyword}%'
- OR a.HtmlContent LIKE '%{request.Keyword}%')";
- }
- return sql;
- }
- /// <summary>
- /// 新版 获取笔记话题列表
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PagedList<TopicListPageResult>> GetTopicPageAsync(TopicPageSearchRequest request)
- {
- var result = await _topicRepository.GetTopicPage(request, await GetTopicPageSqlAsync(request), await AssembleSqlCount(request));
- if (request.ParentId > 0)
- {
- var middle = await _middleRepository.GetMiddleAsync(request.ParentId);
- result.RoleId = middle != null ? middle.RoleId : 0;
- result.ParentId = middle.ParentId;
- }
- //获取数量
- // result.Total = 10;
- foreach (var item in result.Items)
- {
- // if (string.IsNullOrEmpty(item.FolderName))
- // item.FolderName = "根目录";
- if (string.IsNullOrWhiteSpace(item.UserName))
- {
- item.Content = "[]";
- item.Data = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
- item.Content = "";
- continue;
- }
- if (string.IsNullOrWhiteSpace(item.Content))
- item.Content = "[]";
- var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
- foreach (var contentJsonDataItem in contentJsonData)
- contentJsonDataItem.File = StringUtils.AddDomain(contentJsonDataItem.File);
- if (contentJsonData.Count == 0)
- item.DataType = 1;
- item.Content = "";
- if (contentJsonData.Count > 0)
- {
- var imgData = new List<ContentJsonData>();
- var FileData = new List<ContentJsonData>();
- var forCount = 1;
- var firstContent = string.Empty;
- foreach (var jsonData in contentJsonData)
- {
- if (jsonData.TypeValue == AllTypeConst.Text.GetHashCode() && forCount == 1)
- //文本
- firstContent = jsonData.Text;
- else if (jsonData.TypeValue == AllTypeConst.Text.GetHashCode() && firstContent.Length < 50)
- //文本
- firstContent += jsonData.Text;
- //图片
- else if (jsonData.TypeValue == AllTypeConst.Image.GetHashCode() && FileData.Count == 0)
- {
- if (imgData.Count > 9)
- break;
- imgData.Add(jsonData);
- }
- else if (jsonData.TypeValue == AllTypeConst.Text.GetHashCode())
- continue;
- //附件
- else
- {
- FileData.Add(jsonData);
- break;
- }
- forCount++;
- }
- item.Content = string.IsNullOrWhiteSpace(firstContent) ? "" : firstContent;
- if (imgData.Count > 0) { item.DataType = 2; item.Data = imgData; }
- else if (FileData.Count > 0) { item.DataType = 3; item.Data = FileData; }
- else item.DataType = 1;
- var contType = new List<int> { AllTypeConst.Text.GetHashCode(), AllTypeConst.Image.GetHashCode() };
- item.FileCount = contentJsonData.Count(n => !contType.Contains(n.Type));
- }
- if (item.Data == null || item.Data.Count == 0)
- item.Data = new List<ContentJsonData>();
- if (item.Title == null)
- item.Title = string.Empty;
- if (item.Data != null)
- {
- foreach (var data in item.Data)
- {
- if (string.IsNullOrEmpty(data.Title))
- data.Title = string.Empty;
- if (string.IsNullOrEmpty(data.FileName))
- data.FileName = string.Empty;
- }
- }
- }
- //查询系统默认的公开文件夹是否存在
- var query = Q.NewQuery();
- query.Where(nameof(Entity.Middle.Middle.UserId), request.UserId);
- query.Where(nameof(Entity.Middle.Middle.AttributeValue), 2);
- query.Where(nameof(Entity.Middle.Middle.FolderType), AllTypeConst.TopicNote.GetHashCode());
- query.Where(nameof(Entity.Middle.Middle.IsSystemDefault), true);
- var isExists = await _middleRepository.ExistsAsync(query);
- if (!isExists)
- {
- //生成系统默认的公开文件夹
- await _middleRepository.InsertAsync(new Entity.Middle.Middle
- {
- Id = 0,
- FolderName = "公开",
- AttributeValue = 2,
- UserId = request.UserId,
- FolderType = AllTypeConst.TopicNote.GetHashCode(),
- RoleId = 1,
- IsSystemDefault = true
- });
- }
- result.IsDraft = await noteRepository.IsExistsDraftAsync(request.UserId, true);
- result.DraftId = await noteRepository.GetNoteIdByDraftAsync(request.UserId, true);
- return result;
- }
- /// <summary>
- /// 添加话题分组成员
- /// </summary>
- /// <param name="userIds"></param>
- /// <param name="topicGroupId"></param>
- /// <returns></returns>
- public async Task<bool> InsertTopicGroupUserAsync(List<int> userIds, int topicGroupId)
- {
- try
- {
- using (TransactionScope transactionScope = new TransactionScope())
- {
- //获取话题小组成员
- var topicGroupUsers =
- await _topicGroupUserRepository.GetAllAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
- topicGroupId));
- var groupUsers = topicGroupUsers as TopicGroupUser[] ?? topicGroupUsers.ToArray();
- foreach (var userId in userIds)
- {
- if (groupUsers.Any(n => n.UserId == userId))
- continue;
- var topicGroupUser = new TopicGroupUser { TopicGroupId = topicGroupId, UserId = userId };
- await _topicGroupUserRepository.InsertAsync(topicGroupUser);
- }
- //获取人数
- var includeCount =
- await _topicGroupUserRepository.CountAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
- topicGroupId));
- //修改人数
- await _topicGroupRepository.UpdateAsync(Q.Where(nameof(TopicGroup.Id), topicGroupId)
- .Set(nameof(TopicGroup.IncludeCount), includeCount));
- transactionScope.Complete();
- return true;
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- throw;
- }
- }
- /// <summary>
- /// 根据ID删除换题分组成员
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- public async Task<bool> DeleteTopicGroupUserAsync(List<int> ids)
- {
- if (ids.Count == 0)
- throw new BusinessException("删除的话题分组成员不存在");
- try
- {
- using (TransactionScope transactionScope = new TransactionScope())
- {
- //查询分组
- var topicGroupUser = await _topicGroupUserRepository.GetAsync(ids[0]);
- await _topicGroupUserRepository.DeleteAsync(Q.WhereIn(nameof(TopicGroupUser.Id), ids));
- var count = await _topicGroupUserRepository.CountAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
- topicGroupUser.TopicGroupId));
- //修改数量
- await _topicGroupRepository.UpdateAsync(Q.Set(nameof(TopicGroup.IncludeCount), count)
- .Where(nameof(TopicGroup.Id), topicGroupUser.TopicGroupId));
- transactionScope.Complete();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- throw;
- }
- return true;
- }
- /// <summary>
- /// 话题分组移动成员到
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> MoveToGroupAsync(MoveToGroupRequest request)
- {
- using (TransactionScope transactionScope = new TransactionScope())
- {
- //获取源小组ID
- var topicGroupUser = await _topicGroupUserRepository.GetAsync(request.Ids[0]);
- //
- await _topicGroupUserRepository.UpdateAsync(Q
- .Set(nameof(TopicGroupUser.TopicGroupId), request.TopicGroupId)
- .WhereIn(nameof(TopicGroupUser.Id), request.Ids));
- //原始修改数量
- var originalCount = await _topicGroupUserRepository.CountAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
- topicGroupUser.TopicGroupId));
- await _topicGroupRepository.UpdateAsync(Q.Set(nameof(TopicGroup.IncludeCount), originalCount)
- .Where(nameof(TopicGroup.Id), topicGroupUser.TopicGroupId));
- //修改数量
- var count = await _topicGroupUserRepository.CountAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId),
- request.TopicGroupId));
- await _topicGroupRepository.UpdateAsync(Q.Set(nameof(TopicGroup.IncludeCount), count)
- .Where(nameof(TopicGroup.Id), request.TopicGroupId));
- transactionScope.Complete();
- }
- return true;
- }
- /// <summary>
- /// 根据GroupId获取通知
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PagedList<TopicListPageResult>> FindTopicByGroupIdAsync(TopicDetailListRequest request)
- {
- var query = Q.NewQuery();
- query.Where(nameof(Entity.Topic.Topic.GroupId), request.GroupId);
- query.Where(nameof(Entity.Topic.Topic.IsDraft), false);
- //根据groupID查询话题
- var topics = await _topicRepository.GetAllAsync(query);
- query = Q.NewQuery();
- query.WhereIn(nameof(Entity.Topic.TopicAddressee.TopicId), topics.Select(n => n.Id).ToList());
- query.Where(nameof(Entity.Topic.TopicAddressee.UserId), request.UserId).Set(nameof(Entity.Topic.TopicAddressee.IsRead), true);
- //根据话题ID修改用户的阅读
- await _topicAddresseeRepository.UpdateAsync(query);
- var topicListPageResults = await _topicRepository.FindTopicByGroupIdAsync(request);
- foreach (var item in topicListPageResults.Items)
- {
- if (string.IsNullOrWhiteSpace(item.Content))
- item.Content = "[]";
- var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
- foreach (var contentJsonDataItem in contentJsonData)
- contentJsonDataItem.File = StringUtils.AddDomain(contentJsonDataItem.File);
- if (contentJsonData.Count == 0)
- item.DataType = 1;
- item.Content = "";
- if (contentJsonData.Count > 0)
- {
- var imgData = new List<ContentJsonData>();
- var FileData = new List<ContentJsonData>();
- var forCount = 1;
- var firstContent = string.Empty;
- foreach (var jsonData in contentJsonData)
- {
- if (jsonData.TypeValue == AllTypeConst.Text.GetHashCode() && forCount == 1)
- //文本
- firstContent = jsonData.Text;
- else if (jsonData.TypeValue == AllTypeConst.Text.GetHashCode() && forCount > 1)
- //文本
- continue;
- //图片
- else if (jsonData.TypeValue == AllTypeConst.Image.GetHashCode() && FileData.Count == 0)
- {
- if (imgData.Count > 9)
- break;
- imgData.Add(jsonData);
- }
- //附件
- else
- {
- FileData.Add(jsonData);
- break;
- }
- forCount++;
- }
- item.Content = string.IsNullOrWhiteSpace(firstContent) ? "" : firstContent;
- if (imgData.Count > 0) { item.DataType = 2; item.Data = imgData; }
- else if (FileData.Count > 0) { item.DataType = 3; item.Data = FileData; }
- else item.DataType = 1;
- var contType = new List<int> { AllTypeConst.Text.GetHashCode(), AllTypeConst.Image.GetHashCode() };
- item.FileCount = contentJsonData.Count(n => !contType.Contains(n.Type));
- }
- if (item.Data == null || item.Data.Count == 0)
- item.Data = new List<ContentJsonData>();
- //item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
- }
- return topicListPageResults;
- }
- }
- }
|