using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Transactions;
using GxPress.Common.Exceptions;
using GxPress.Common.Tools;
using GxPress.EnumConst;
using GxPress.Request.App.Topic;
using GxPress.Result;
using Newtonsoft.Json;
namespace GxPress.Service.Implement.Topic
{
public partial class TopicService
{
///
/// 创建话题
///
///
///
public async Task> InsertTopicAsync(TopicInRequest request)
{
var result = new AppResultJson();
if (string.IsNullOrWhiteSpace(request.Title) && string.IsNullOrWhiteSpace(request.HtmlContent))
throw new BusinessException("标题和内容必填一项!");
try
{
using (TransactionScope transactionScope = new TransactionScope())
{
//创建话题
var topic = new Entity.Topic.Topic
{
UserId = request.UserId,
Content = request.Content,
HtmlContent = request.HtmlContent,
Title = request.Title,
GroupId = request.GroupId,
FolderId = request.FolderId,
IsDraft = request.IsDraft
};
if (!string.IsNullOrEmpty(request.Content))
{
var contentJsonData = JsonConvert.DeserializeObject>(request.Content);
foreach (var item in contentJsonData)
{
item.File = StringUtils.RemoveDomain(item.File);
if (item.Type == AllTypeConst.Text.GetHashCode())
topic.TextContent += item.Text;
}
topic.Content = JsonConvert.SerializeObject(contentJsonData);
}
else
{
topic.TextContent = "[]";
topic.Content = "[]";
}
var topicId = await _topicRepository.InsertAsync(topic);
//修改小组
if (request.GroupId > 0)
{
var group = await groupRepository.GetAsync(request.GroupId);
group.LastModifiedDate = DateTime.Now;
await groupRepository.UpdateAsync(group);
}
// //添加默认文件夹
// await recordFolderRepository.AddAsync(AllTypeConst.TopicNote.GetHashCode(), request.UserId, request.FolderId);
//修改话题访问量
await _visitService.AddVisit(request.UserId, GxPress.EnumConst.AllTypeConst.Topic.GetHashCode(), topicId);
result = new AppResultJson
{
Code = StatusCodeConst.SucceedCode.GetHashCode(),
Msg = "",
Success = false,
Data = await _topicRepository.GetAsync(topicId)
};
transactionScope.Complete();
}
}
catch
{
return result = new AppResultJson
{
Code = StatusCodeConst.ErrorCode.GetHashCode(),
Msg = StatusCodeConst.ErrorMsg.GetDescriptionOriginal(),
Success = false,
Data = new Entity.Topic.Topic()
};
}
return result;
}
}
}