using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Datory;
using GxPress.Common.Exceptions;
using GxPress.Common.Page;
using GxPress.Common.Tools;
using GxPress.Entity.Middle;
using GxPress.Entity.WorkMeeting;
using GxPress.EnumConst;
using GxPress.Repository.Interface;
using GxPress.Repository.Interface.Collection;
using GxPress.Repository.Interface.Missive;
using GxPress.Repository.Interface.Note;
using GxPress.Repository.Interface.WorkMeeting;
using GxPress.Request.App.Analyze;
using GxPress.Request.App.Collection;
using GxPress.Request.App.Middle;
using GxPress.Result;
using GxPress.Result.App.Collection;
using GxPress.Service.Interface.Analyze;
using GxPress.Service.Interface.Article;
using GxPress.Service.Interface.Collection;
using GxPress.Service.Interface.Middle;
using GxPress.Service.Interface.UserMiddle;
using Newtonsoft.Json;
namespace GxPress.Service.Implement.Collection
{
public class CollectionService : ICollectionService
{
private readonly ICollectionRepository _collectionRepository;
private readonly IMiddleService _middleService;
private readonly IArticleRepository _articleRepository;
private readonly ITopicRepository _topicRepository;
private readonly ITopicAnalyzeRepository _topicAnalyzeRepository;
private readonly IMiddleRepository _middleRepository;
private readonly INoteRepository _noteRepository;
private readonly IUserRepository _userRepository;
private readonly IArticleService _articleService;
private readonly INoticeRepository _noticeRepository;
private readonly IMissiveRepository _missiveRepository;
private readonly IMissiveAnalyzeRepository _missiveAnalyzeRepository;
private readonly IGroupRepository _groupRepository;
private readonly IMeetingSummaryRepository _meetingSummaryRepository;
private readonly IMapper _mapper;
private readonly IFolderUserRepository _folderUserRepository;
private readonly IAnalyzeService _analyzeService;
private readonly IFileLibraryRepository fileLibraryRepository;
private readonly IUserMiddleService _userMiddleService;
public CollectionService(ICollectionRepository collectionRepository, IMiddleService middleService,
IArticleRepository articleRepository, ITopicRepository topicRepository, IMiddleRepository middleRepository,
INoteRepository noteRepository, IUserRepository userRepository, IArticleService articleService,
ITopicAnalyzeRepository topicAnalyzeRepository, INoticeRepository noticeRepository,
IMissiveRepository missiveRepository, IMissiveAnalyzeRepository missiveAnalyzeRepository, IMapper mapper,
IGroupRepository groupRepository, IMeetingSummaryRepository meetingSummaryRepository, IFolderUserRepository folderUserRepository, IAnalyzeService analyzeService, IFileLibraryRepository fileLibraryRepository, IUserMiddleService userMiddleService)
{
_collectionRepository = collectionRepository;
_middleService = middleService;
_articleRepository = articleRepository;
_topicRepository = topicRepository;
_middleRepository = middleRepository;
_noteRepository = noteRepository;
_userRepository = userRepository;
_articleService = articleService;
_topicAnalyzeRepository = topicAnalyzeRepository;
_noticeRepository = noticeRepository;
_missiveAnalyzeRepository = missiveAnalyzeRepository;
_missiveRepository = missiveRepository;
_mapper = mapper;
_groupRepository = groupRepository;
_meetingSummaryRepository = meetingSummaryRepository;
_folderUserRepository = folderUserRepository;
_analyzeService = analyzeService;
this.fileLibraryRepository = fileLibraryRepository;
_userMiddleService = userMiddleService;
}
///
/// 插入
///
///
///
public async Task Insert(CollectionInRequest request)
{
var collection = new Entity.Collection
{
CollectionDataId = request.CollectionDataId,
CollectionType = request.CollectionType,
UserId = request.UserId,
SourceUserId = request.SourceUserId,
EndCollectionDataId = request.CollectionDataId,
EndCollectionType = request.CollectionType
};
var query = new SqlKata.Query();
query.Where(nameof(Entity.Collection.UserId), request.UserId)
.Where(nameof(Entity.Collection.CollectionType), request.CollectionType)
.Where(nameof(Entity.Collection.CollectionDataId), request.CollectionDataId);
if (request.SourceUserId > 0)
query.Where(nameof(Entity.Collection.SourceUserId), request.SourceUserId);
//查询是否存在
var findCollection = await _collectionRepository.GetAsync(query);
var analyzeRequest = new AnalyzeRequest
{
UserId = request.UserId,
AnalyzeType = 3,
SourceId = request.CollectionDataId,
TypeValue = request.CollectionType,// 10 会议纪要 0 文章 1 话题 2 通知 3 笔记 4站内信 12 收藏文件夹 13 笔记文件夹
SourceUserId = request.SourceUserId
};
if (request.CollectionType == 1)
analyzeRequest.TypeValue = 0;
if (request.CollectionType == 2)
analyzeRequest.TypeValue = 1;
if (request.CollectionType == 4)
analyzeRequest.TypeValue = 3;
if (request.CollectionType == 5)
analyzeRequest.TypeValue = 2;
if (findCollection != null)
{
await _collectionRepository.DeleteAsync(findCollection.Id);
await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.FolderType), 3).Where(nameof(Entity.Middle.Middle.MiddleId), findCollection.Id).Where(nameof(Entity.Middle.Middle.UserId), request.UserId));
var analyze = await _analyzeService.GetAnalyzeAsync(analyzeRequest);
if (analyze != null)
await _analyzeService.DeleteAsync(analyze.Id);
return true;
}
//收藏文章
if (request.CollectionType == 1)
{
var article = await _articleRepository.GetArticleAsync(request.CollectionDataId);
collection.Title = article.Title;
analyzeRequest.TypeValue = 0;
await _analyzeService.SetAnalyzeAsync(analyzeRequest);
}
//话题
else if (request.CollectionType == 2)
{
var topic = await _topicRepository.GetAsync(request.CollectionDataId);
collection.Title = topic.Title;
//收藏
analyzeRequest.TypeValue = 1;
await _analyzeService.SetAnalyzeAsync(analyzeRequest);
}
//收藏收藏
else if (request.CollectionType == 3)
{
//获取
var collectionEntity = await _collectionRepository.GetAsync(request.CollectionDataId);
collection.EndCollectionDataId = collectionEntity.EndCollectionDataId;
collection.EndCollectionType = collectionEntity.EndCollectionType;
}
//笔记
else if (request.CollectionType == 4 || request.CollectionType == 21)
{
//收藏
analyzeRequest.TypeValue = 3;
await _analyzeService.SetAnalyzeAsync(analyzeRequest);
var note = await _noteRepository.GetAsync(request.CollectionDataId);
collection.Title = note.Title;
}
//通知
else if (request.CollectionType == 5)
{
//通知
analyzeRequest.TypeValue = 2;
await _analyzeService.SetAnalyzeAsync(analyzeRequest);
var notice = await _noticeRepository.GetAsync(request.CollectionDataId);
collection.Title = notice.Title;
}
//站内信
else if (request.CollectionType == 6)
{
var missive = await _missiveRepository.GetAsync(request.CollectionDataId);
collection.Title = missive.Title;
//站内信
analyzeRequest.TypeValue = 4;
await _analyzeService.SetAnalyzeAsync(analyzeRequest);
}
//收藏会议
if (request.CollectionType == 8)
{
var meetingSummary = await _meetingSummaryRepository.GetAsync(Q.Where(nameof(MeetingSummary.MeetingId), request.CollectionDataId));
if (meetingSummary == null)
throw new BusinessException("会议无效");
collection.Title = meetingSummary.Title;
//收藏会议
analyzeRequest.TypeValue = 10;
await _analyzeService.SetAnalyzeAsync(analyzeRequest);
}
//文件
if (request.CollectionType == 300)
{
//获取文件信息
var fileLibrary = await fileLibraryRepository.GetFileLibraryByIdAsync(collection.CollectionDataId);
if (fileLibrary == null)
throw new BusinessException("文件不存在");
collection.Title = fileLibrary.FileName;
}
//会议纪要
if (request.CollectionType == 400)
{
var meetingSummary = await _meetingSummaryRepository.GetAsync(Q.Where(nameof(MeetingSummary.Id), request.CollectionDataId));
if (meetingSummary == null)
throw new BusinessException("会议无效");
collection.Title = meetingSummary.Title;
//会议纪要
analyzeRequest.TypeValue = 10;
await _analyzeService.SetAnalyzeAsync(analyzeRequest);
}
//收藏、笔记文件
if (request.CollectionType == 12 || request.CollectionType == 13)
{
if (request.CollectionDataId > 0)
{
var middle = await _middleRepository.GetMiddleAsync(request.CollectionDataId);
if (middle == null)
throw new BusinessException("文件夹无效");
collection.Title = middle.FolderName;
}
else
{
var user = await _userRepository.GetAsync(request.SourceUserId);
if (user == null)
throw new BusinessException("用户不存在!");
collection.Title = $"{user.Name}的";
collection.Title += request.CollectionType == 12 ? "收藏" : "笔记";
}
//笔记文件
analyzeRequest.TypeValue = request.CollectionType;
await _analyzeService.SetAnalyzeAsync(analyzeRequest);
}
var collectionId = await _collectionRepository.InsertAsync(collection);
//添加收藏信息
var middleInsertTypeRequest = new MiddleInsertTypeRequest
{
AttributeValue = 1,
FolderId = request.FolderId,
FolderType = 3,
MiddleId = collectionId,
MiddleSonId = 0,
UserId = request.UserId
};
return await _middleService.AddOrUpdateAsync(middleInsertTypeRequest);
}
///
/// 查询结果
///
///
///
public async Task> PageListAsync(CollectionPageSearchRequest request)
{
var result = await _collectionRepository.PageListAsync(request);
if (request.FolderId > 0)
{
var middle = await _middleRepository.GetMiddleAsync(request.FolderId);
result.RoleId = middle != null ? middle.RoleId : 0;
}
//是否收藏和转发数量
if (request.VisitUserId > 0)
{
var analyzeRequest = new AnalyzeRequest();
analyzeRequest.SourceUserId = request.VisitUserId;
analyzeRequest.AnalyzeType = 3;
analyzeRequest.CommentId = 0;
analyzeRequest.SourceId = request.FolderId;
analyzeRequest.UserId = request.UserId;
analyzeRequest.TypeValue = 12;
result.IsCollect = await _analyzeService.ExistsSourceUserAsync(analyzeRequest);
analyzeRequest.AnalyzeType = 4;
result.RetransmissionCount = await _analyzeService.RetransmissionCountAsync(analyzeRequest);
}
foreach (var item in result.Items)
{
item.Data = await StructCollectionData(item.CollectionDataId, item.CollectionType, item.SourceUserId);
if (string.IsNullOrWhiteSpace(item.Title))
{
item.Title = "";
if (item.Data.Type == 1)
item.Title = item.Data.Title;
else if (item.Data.Type == 400)
item.Title = item.Data.Title;
else
{
if (item.Data.Data != null)
{
if (item.Data.Type == 1)
item.Title = item.Data.Text;
else if (item.Data.Data.Type == 1)
item.Title = item.Data.Data.Text;
else if (item.Data.Data.Type == 2)
item.Title = "[图片]";
else
item.Title = "[附件]";
}
}
}
// //获取文件夹数量
// if (item.AttributeValue == 2)
// {
// var missiveCount =
// await _middleRepository.CountAsync(
// Q.Where(nameof(Entity.Middle.Middle.ParentId), item.Id)
// .Where(nameof(Entity.Middle.Middle.IsDelete), false));
// // //获取文件夹数量
// // var missiveFolderCount =
// // await _middleRepository.CountAsync(
// // Q.Where(nameof(Entity.Middle.Middle.ParentId), item.Id)
// // .Where(nameof(Entity.Middle.Middle.FolderType), 3)
// // .Where(nameof(Entity.Middle.Middle.IsDelete), false).Where(nameof(Entity.Middle.Middle.AttributeValue),2));
// //文件夹里面数量
// item.FileCount = missiveCount;
// }
//获取共享人员
// if (item.RoleId == 3)
// {
// var sourceId = item.Id;
// if (item.ParentId > 0)
// sourceId = item.MiddleSonId;
// var userMiddles = await _userMiddleService.FindUserMiddlesAsync(item.FolderType, sourceId);
// item.EnjoyUser = StringUtils.ObjectCollectionToString(userMiddles.Select(n => n.SourceName), "、");
// // var folderUsers = await _folderUserRepository.GetAllAsync(Q.Where(nameof(FolderUser.MiddleId), item.Id).OrderByDesc(nameof(FolderUser.CreatedDate)).Take(10));
// // item.EnjoyUser = StringUtils.ObjectCollectionToString(folderUsers.Select(n => n.UserName), "、");
// }
}
return result;
}
///
/// 递归获取不是收藏的数据
///
///
///
public async Task GetCollectionRecursion(int middleId)
{
//var middle = await _middleRepository.GetMiddleAsync(middleId);
var collection = await _collectionRepository.GetAsync(middleId);
if (collection.CollectionType == 3)
await GetCollectionRecursion(collection.CollectionType);
return collection;
}
///
/// 笔记解析
///
///
///
public async Task AnalyzeAsync(List data)
{
var result = new CollectionContentJsonData();
var fileCount = data.Count(n => n.Type == 3);
//提取图片
if (data.Any(n => n.Type == 2))
{
var contentJsonData = new CollectionContentJsonData { FileCount = fileCount, Type = 2 };
//获取图片
var imgList = data.Where(n => n.Type == 2);
contentJsonData.ArticleImgUrl = imgList.Select(n => StringUtils.AddDomain(n.File)).ToList();
result = (contentJsonData);
return result;
}
//提取文字
if (data.Any(n => n.Type == 1))
{
var contentJsonData = new CollectionContentJsonData { FileCount = fileCount, Type = 1 };
//获取图片
var textList = data.Where(n => n.Type == 1);
contentJsonData.Text = textList.FirstOrDefault().Text;
result = (contentJsonData);
return result;
}
//获取
foreach (var item in data.Where(n => n.Type != 1 && n.Type != 3))
{
if (item.Type == 4)
//4 Article:文章 5 notice通知 6 topic话题 7 note笔记 8 collection收藏 9站内信 10小组 11会议
item.Type = 1;
else if (item.Type == 6)
item.Type = 2;
else if (item.Type == 7)
item.Type = 4;
else if (item.Type == 8)
item.Type = 3;
else if (item.Type == 9)
item.Type = 6;
else if (item.Type == 10)
item.Type = 7;
else if (item.Type == 11)
item.Type = 8;
return await StructCollectionData(item.Id, item.Type, 0);
}
return result;
}
///
/// 构造收藏Data 收藏类型 1文章 2话题 3 收藏 4笔记 5通知 6站内信 7小组 8会议
///
///
///
///
///
public async Task StructCollectionData(int collectionDataId, int collectionType, int sourceUserId, bool isReturn = false)
{
var list = new CollectionContentJsonData();
//1文章 2话题 3 收藏 4笔记 5通知 6站内信 7小组 8会议 14工作流审核 300 文件 400会议纪要
//文章类型
if (collectionType == 1)
{
var articleResult = await _articleService.GetAsync(collectionDataId, 0);
if (articleResult == null)
return new CollectionContentJsonData();
var articleImgUrl = new List();
if (articleResult.ImageUrls != null)
foreach (var block in articleResult.ImageUrls)
articleImgUrl.Add(StringUtils.AddDomain(block));
list = new CollectionContentJsonData
{
Author = articleResult.Source,
Title = articleResult.Title,
CreatedData = articleResult.AddDate,
Type = 4,
Id = articleResult.Id,
ArticleImgUrl = articleImgUrl,
ReadCount = articleResult.ReadCount
};
}
//话题类型
if (collectionType == AllTypeConst.Topic.GetHashCode())
{
var topic = await _topicRepository.GetAsync(collectionDataId);
if (topic == null)
return new CollectionContentJsonData();
//获取是否存在小组
var group = new Entity.Group();
if (topic.GroupId > 0)
{
group = await _groupRepository.GetAsync(topic.GroupId);
if (group == null)
return new CollectionContentJsonData();
}
//查询ID
var user = await _userRepository.GetAsync(topic.UserId);
if (user == null)
return new CollectionContentJsonData();
list = new CollectionContentJsonData
{
Author = user.Name,
Title = topic.Title,
CreatedData = topic.CreatedDate,
Type = 6,
Id = topic.Id,
File = StringUtils.AddDomain(user.AvatarUrl),
ReadCount = topic.ReadCount,
Name = user.Name,
AvatarUrl = StringUtils.AddDomain(user.AvatarUrl),
Data = await GetCollectionContentJsonData(topic.Content),
NickName = user.Nick,
IsGroup = group.Id > 0,
GroupId = group.Id > 0 ? group.Id : 0,
GroupName = group.Id > 0 ? group.Name : ""
};
}
//收藏
if (collectionType == AllTypeConst.Collect.GetHashCode())
{
var collection = await GetCollectionRecursion(collectionDataId);
list = await StructCollectionData(collection.EndCollectionDataId, collection.EndCollectionType, collection.SourceUserId);
//list.Type = 8;
}
//笔记
if (collectionType == AllTypeConst.Note.GetHashCode() || collectionType == 21)
{
var note = await _noteRepository.GetAsync(collectionDataId);
if (note != null)
{
var user = await _userRepository.GetAsync(note.UserId);
if (user == null)
return new CollectionContentJsonData();
list = (new CollectionContentJsonData
{
Author = user.Name,
NickName = user.Nick,
Name = user.Name,
Title = note.Title,
CreatedData = note.CreatedDate,
Type = collectionType == 4 ? 7 : 2,
Id = note.Id,
File = "",
AvatarUrl = StringUtils.AddDomain(user.AvatarUrl),
Data = await GetCollectionContentJsonData(note.Content)
});
}
}
//通知
if (collectionType == 5)
{
var notice = await _noticeRepository.GetAsync(collectionDataId);
if (notice == null)
return new CollectionContentJsonData();
var user = await _userRepository.GetAsync(notice.UserId);
if (user == null)
return new CollectionContentJsonData();
list = (new CollectionContentJsonData
{
Author = user.Name,
Title = notice.Title,
CreatedData = notice.CreatedDate,
Type = 5,
Id = notice.Id,
File = "",
AvatarUrl = StringUtils.AddDomain(user.AvatarUrl),
Name = user.Name,
Data = await GetCollectionContentJsonData(notice.Content),
NickName = user.Nick
});
}
//站内信
if (collectionType == 6)
{
var missive = await _missiveRepository.GetAsync(collectionDataId);
if (missive == null)
return new CollectionContentJsonData();
var user = await _userRepository.GetAsync(missive.UserId);
if (user == null)
return new CollectionContentJsonData();
list = new CollectionContentJsonData
{
Author = user.Name,
Title = missive.Title,
CreatedData = missive.CreatedDate,
Type = 9,
Id = missive.Id,
File = "",
AvatarUrl = StringUtils.AddDomain(user.AvatarUrl),
Name = user.Name,
Data = await GetCollectionContentJsonData(missive.Content),
NickName = user.Nick,
MissiveType = missive.MissiveType
};
}
//小组
if (collectionType == 7)
{
var group = await _groupRepository.GetAsync(collectionDataId);
var user = await _userRepository.GetAsync(group.UserId);
if (user == null)
return new CollectionContentJsonData();
list = new CollectionContentJsonData
{
Author = user.Name,
Title = group.Name,
CreatedData = group.CreatedDate,
Type = 10,
Id = group.Id,
File = StringUtils.AddDomain(group.AvatarUrl),
Name = user.Name,
NickName = user.Nick
};
}
//会议
if (collectionType == 8)
{
var meetingSummary = await _meetingSummaryRepository.GetAsync(Q.Where(nameof(MeetingSummary.MeetingId), collectionDataId));
if (meetingSummary == null)
return new CollectionContentJsonData();
var user = await _userRepository.GetAsync(meetingSummary.UserId);
if (user == null)
return new CollectionContentJsonData();
list = new CollectionContentJsonData
{
Author = "",
Title = meetingSummary.Title,
CreatedData = meetingSummary.CreatedDate,
Type = 11,
Id = meetingSummary.Id,
File = "",
Name = user.Name,
NickName = user.Nick
};
}
//工作流
//300 文件
if (collectionType == 300)
{
var fileLibrary = await fileLibraryRepository.GetFileLibraryByIdAsync(collectionDataId);
if (fileLibrary == null)
return new CollectionContentJsonData();
// var user = await _userRepository.GetAsync(fileLibrary.UserId);
list = new CollectionContentJsonData
{
Author = "",
Title = fileLibrary.FileName,
CreatedData = fileLibrary.CreatedData,
Type = 3,
Id = fileLibrary.Id,
File = StringUtils.AddDomain(fileLibrary.FileUrl),
MinFile = fileLibrary.FileType == "image" ? StringUtils.AddDomainMin(fileLibrary.FileUrl) : "",
AvatarUrl = "",
FileName = fileLibrary.FileName,
Name = "",
Data = new CollectionContentJsonData(),
NickName = "",
FileSize = fileLibrary.Size
};
}
//400 会议纪要
if (collectionType == 400)
{
var meetingSummary = await _meetingSummaryRepository.GetAsync(collectionDataId);
if (meetingSummary == null)
return new CollectionContentJsonData();
var user = await _userRepository.GetAsync(meetingSummary.UserId);
if (user == null)
return new CollectionContentJsonData();
list = new CollectionContentJsonData
{
Author = user.Name,
Title = meetingSummary.Title,
CreatedData = meetingSummary.CreatedDate,
Type = 400,
Id = meetingSummary.Id,
File = "",
MinFile = "",
AvatarUrl = "",
FileName = "",
Name = "",
Data = new CollectionContentJsonData(),
NickName = user.Nick,
FileSize = 0
};
}
//收藏笔记文件夹
if (collectionType == 12 || collectionType == 13)
{
if (sourceUserId == 0)
return new CollectionContentJsonData();
var user = await _userRepository.GetAsync(sourceUserId);
if (user == null)
return new CollectionContentJsonData();
list = new CollectionContentJsonData
{
Author = user.Name,
Title = "",
Type = collectionType,
Id = 0,
File = "",
MinFile = "",
AvatarUrl = "",
FileName = "",
Name = "",
Data = new CollectionContentJsonData(),
NickName = user.Nick,
FileSize = 0
};
}
return list;
}
///
/// 获取收藏详情
///
///
///
public async Task GetCollectionDetailAsync(int id)
{
var collection = await _collectionRepository.GetAsync(id);
if (collection == null)
throw new BusinessException("收藏不存在");
var result = _mapper.Map(collection);
result.Data = await StructCollectionData(result.CollectionDataId, result.CollectionType, 0);
return result;
}
public async Task GetCollectionContentJsonData(string json)
{
var data = JsonConvert.DeserializeObject>(json);
return await AnalyzeAsync(data);
}
}
}