177 lines
7.6 KiB
C#
177 lines
7.6 KiB
C#
|
|
using Mapster;
|
|||
|
|
using QYZH.InteractiveMagazine.Common.Extensions;
|
|||
|
|
using QYZH.InteractiveMagazine.Infrastructure.OSS;
|
|||
|
|
using QYZH.InteractiveMagazine.IService;
|
|||
|
|
using QYZH.InteractiveMagazine.Models.Common;
|
|||
|
|
using QYZH.InteractiveMagazine.Models.Dto.Journal;
|
|||
|
|
using QYZH.InteractiveMagazine.Models.Entity;
|
|||
|
|
using QYZH.InteractiveMagazine.Models.Enum;
|
|||
|
|
using QYZH.InteractiveMagazine.Repository;
|
|||
|
|
using SqlSugar;
|
|||
|
|
using Yitter.IdGenerator;
|
|||
|
|
|
|||
|
|
|
|||
|
|
namespace QYZH.InteractiveMagazine.Service;
|
|||
|
|
|
|||
|
|
public class JournalPageTaskService(BaseRepository<Journal> journalRepository, OssService ossService) : BaseRepository<JournalPageTask>, IJournalPageTaskService
|
|||
|
|
{
|
|||
|
|
public async Task<long?> InsertAsync(JournalPageTaskAddInput input)
|
|||
|
|
{
|
|||
|
|
var JournalStatus = await journalRepository.Queryable().Where(w => w.Id == input.JournalId).Select(s => s.Status).FirstAsync();
|
|||
|
|
BusinessException.ThrowIf(JournalStatus == (int)JournalStatusEnum.Archive, "书籍已归档不能添加题目");
|
|||
|
|
|
|||
|
|
var map = input.Adapt<JournalPageTask>();
|
|||
|
|
map.Id = YitIdHelper.NextId();
|
|||
|
|
map.GroupId = map.Id;
|
|||
|
|
var no = input.No.Split('-').Select(int.Parse).ToArray();
|
|||
|
|
if (no.Last() >= 2)
|
|||
|
|
{
|
|||
|
|
var startNo = $"{no[0]}-{no[1]}-{no[2]}-1";
|
|||
|
|
var groupTask = await Queryable().Where(w => w.No == startNo && w.JournalId == input.JournalId).FirstAsync();
|
|||
|
|
BusinessException.ThrowIf(groupTask.IsNull(), "未找到关联跨页的第一部分");
|
|||
|
|
map.GroupId = groupTask.GroupId;
|
|||
|
|
map.Score = groupTask.Score;
|
|||
|
|
map.AnswerTime = groupTask.AnswerTime;
|
|||
|
|
map.Type = groupTask.Type;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var data = await base.InsertAsync(map);
|
|||
|
|
return map?.Id;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public async Task<bool> UpdateAsync(JournalPageTaskUpdateInput input)
|
|||
|
|
{
|
|||
|
|
var JournalStatus = await journalRepository.Queryable().Where(w => w.Id == input.JournalId).Select(s => s.Status).FirstAsync();
|
|||
|
|
BusinessException.ThrowIf(JournalStatus == (int)JournalStatusEnum.Archive, "书籍已归档不能修改题目");
|
|||
|
|
|
|||
|
|
var task = await base.Queryable().Where(w => w.Id == input.Id).FirstAsync();
|
|||
|
|
BusinessException.ThrowIf(task.IsNull(), "未找到关联的题号");
|
|||
|
|
|
|||
|
|
var sameTask = await base.Queryable().Where(w => w.No == input.No && w.JournalId == input.JournalId).FirstAsync();
|
|||
|
|
BusinessException.ThrowIf(sameTask.IsNotEmpty(), "已经存在相同题号题目");
|
|||
|
|
|
|||
|
|
|
|||
|
|
var key = $"Journal/{task.JournalId}/{task.JournalPageId}/{task.GroupId}";
|
|||
|
|
//if (task.AnswerUrl != input.AnswerUrl && input.AnswerUrl.NotNull())
|
|||
|
|
//{
|
|||
|
|
// ossService.CopyObject(input.AnswerUrl.RemoveDomain(), $"{key}/answer.{input.AnswerUrl.ToExtension()}");
|
|||
|
|
// input.AnswerUrl = $"{key}/answer.{input.AnswerUrl.ToExtension()}";
|
|||
|
|
//}
|
|||
|
|
//if (task.AnalysisUrl != input.AnalysisUrl && input.AnalysisUrl.NotNull())
|
|||
|
|
//{
|
|||
|
|
// ossService.CopyObject(input.AnalysisUrl.RemoveDomain(), $"{key}/analysis.{input.AnalysisUrl.ToExtension()}");
|
|||
|
|
// input.AnalysisUrl = $"{key}/analysis.{input.AnalysisUrl.ToExtension()}";
|
|||
|
|
//}
|
|||
|
|
//if (task.VideoUrl != input.VideoUrl && input.VideoUrl.NotNull())
|
|||
|
|
//{
|
|||
|
|
// ossService.CopyObject(input.VideoUrl.RemoveDomain(), $"{key}/video.{input.VideoUrl.ToExtension()}");
|
|||
|
|
// input.VideoUrl = $"{key}/video.{input.VideoUrl.ToExtension()}";
|
|||
|
|
//}
|
|||
|
|
|
|||
|
|
input.Task = Deal(task.Task, input.Task, key, "task");
|
|||
|
|
input.Answer = Deal(task.Answer, input.Answer, key, "answer");
|
|||
|
|
input.Analysis = Deal(task.Analysis, input.Analysis, key, "analysis");
|
|||
|
|
|
|||
|
|
task.Type = input.Type;
|
|||
|
|
task.Task = input.Task;
|
|||
|
|
task.Options = input.Options;
|
|||
|
|
task.Answer = input.Answer;
|
|||
|
|
//task.AnswerUrl = input.AnswerUrl;
|
|||
|
|
task.Analysis = input.Analysis;
|
|||
|
|
//task.AnalysisUrl = input.AnalysisUrl;
|
|||
|
|
task.Score = input.Score;
|
|||
|
|
task.AnswerTime = input.AnswerTime;
|
|||
|
|
// task.VideoUrl = input.VideoUrl;
|
|||
|
|
task.No = input.No;
|
|||
|
|
var no = input.No.Split('-').Select(int.Parse).ToArray();
|
|||
|
|
if (no.Last() >= 2)
|
|||
|
|
{
|
|||
|
|
var startNo = $"{no[0]}-{no[1]}-{no[2]}-1";
|
|||
|
|
var groupTask = await Queryable().Where(w => w.No == startNo && w.JournalId == input.JournalId).FirstAsync();
|
|||
|
|
BusinessException.ThrowIf(groupTask.IsNull(), "未找到关联跨页的第一部分");
|
|||
|
|
task.GroupId = groupTask.GroupId;
|
|||
|
|
task.Score = groupTask.Score;
|
|||
|
|
task.AnswerTime = groupTask.AnswerTime;
|
|||
|
|
task.Type = groupTask.Type;
|
|||
|
|
}
|
|||
|
|
//task.JournalId = input.JournalId;
|
|||
|
|
//task.JournalPageId = input.JournalPageId;
|
|||
|
|
// 这里不能直接用Set,因为jsonmap无法赋值 不知道为啥
|
|||
|
|
return await base.Updateable(task).ExecuteCommandAsync() > 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<bool> KeywordAnalysisAsync(JournalPageTaskKeywordAnalysis input)
|
|||
|
|
{
|
|||
|
|
return await base.Updateable().SetColumns(s => s.KeywordAnalysis, input.KeywordAnalysis).Where(w => w.GroupId == input.Id).ExecuteCommandAsync() > 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<JournalPageTaskOutput> DetailAsync(long id)
|
|||
|
|
{
|
|||
|
|
var data = await base.Queryable()
|
|||
|
|
.Where(w => w.Id == id)
|
|||
|
|
.Select(w => new JournalPageTaskOutput()
|
|||
|
|
, true).FirstAsync();
|
|||
|
|
return data;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<bool> DeleteAsync(long id)
|
|||
|
|
{
|
|||
|
|
var task = await base.Queryable().Where(w => w.Id == id).FirstAsync();
|
|||
|
|
BusinessException.ThrowIf(task.IsNull(), "问题不存在");
|
|||
|
|
|
|||
|
|
var key = $"Journal/{task.JournalId}/{task.JournalPageId}/{id}";
|
|||
|
|
|
|||
|
|
ossService.DeleteObject(key);
|
|||
|
|
|
|||
|
|
return await base.DeleteAsync(d => d.Id == id);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<bool> ComplementAsync(JournalPageTaskComplementInput input)
|
|||
|
|
{
|
|||
|
|
var task = await base.Queryable().Where(w => w.Id == input.Id).FirstAsync();
|
|||
|
|
BusinessException.ThrowIf(task.IsNull(), "问题不存在");
|
|||
|
|
|
|||
|
|
if (input.AudioUrl.NotNull())
|
|||
|
|
{
|
|||
|
|
var key = $"Journal/{task.JournalId}/{task.JournalPageId}/{task.Id}/audio.{input.AudioUrl.ToExtension()}";
|
|||
|
|
ossService.CopyObject(input.AudioUrl, key);
|
|||
|
|
input.AudioUrl = key;
|
|||
|
|
}
|
|||
|
|
if (input.PointsUrl.NotNull())
|
|||
|
|
{
|
|||
|
|
var key = $"Journal/{task.JournalId}/{task.JournalPageId}/{task.Id}/points.json";
|
|||
|
|
ossService.CopyObject(input.PointsUrl, key);
|
|||
|
|
input.PointsUrl = key;
|
|||
|
|
}
|
|||
|
|
return await Updateable(input.Adapt<JournalPageTask>()).Where(w => w.Id == input.Id).ExecuteCommandAsync() > 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private string Deal(string oldUrls, string newUrls, string key, string type)
|
|||
|
|
{
|
|||
|
|
// 如果 newUrls 为空,则直接返回
|
|||
|
|
if (newUrls.IsNull()) return null;
|
|||
|
|
|
|||
|
|
// 处理 oldUrls 为 null 的情况,将其视为空字符串
|
|||
|
|
var oldTaskUrls = oldUrls.AllUrl().Distinct();
|
|||
|
|
var taskUrls = newUrls.AllUrl().Distinct().Select(s => s.RemoveDomain());
|
|||
|
|
|
|||
|
|
// 删除不存在的(存在于旧集合但不在新集合中)
|
|||
|
|
var removedUrls = oldTaskUrls.Except(taskUrls).ToList();
|
|||
|
|
ossService.DeleteObjects(removedUrls);
|
|||
|
|
|
|||
|
|
// 添加新增的(存在于新集合但不在旧集合中)
|
|||
|
|
var addedUrls = taskUrls.Except(oldTaskUrls).ToList();
|
|||
|
|
foreach (var item in addedUrls)
|
|||
|
|
{
|
|||
|
|
var value = $"{key}/{type}/{YitIdHelper.NextId()}.{item.ToExtension()}";
|
|||
|
|
ossService.CopyObject(item, value);
|
|||
|
|
|
|||
|
|
newUrls = newUrls.Replace(item, value);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
return newUrls;
|
|||
|
|
}
|
|||
|
|
}
|