2025-06-06 16:55:14 +08:00

260 lines
9.9 KiB
C#

using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Quartz.Util;
using VOL.Business.IRepositories;
using VOL.Business.IServices;
using VOL.Business.IServices.School;
using VOL.Business.IServices.Training;
using VOL.Business.Repositories;
using VOL.Core.CacheManager;
using VOL.Core.Configuration;
using VOL.Core.Extensions;
using VOL.Core.Extensions.AutofacManager;
using VOL.Core.ManageUser;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels;
using VOL.Entity.Enum;
using VOL.Model;
using VOL.Model.Ai;
using VOL.Model.Ai.Request;
using VOL.Model.IOT.Response;
using VOL.Model.Norm.Response;
using VOL.Model.School.Response;
using VOL.System.IRepositories;
using VOL.System.Repositories;
namespace VOL.Business.Services
{
public class Ai_SpecialService : IAi_SpecialService, IDependency
{
#region
private readonly IMapper _mapper;
private readonly ICacheService _cacheService;
private readonly IAi_SpecialRepository _specialRepository;
[ActivatorUtilitiesConstructor]
public Ai_SpecialService(IMapper mapper,
ICacheService cacheService,
IAi_SpecialRepository specialRepository)
{
_mapper = mapper;
_cacheService = cacheService;
_specialRepository = specialRepository;
}
#endregion
/// <summary>
/// 获取专项教学列表
/// </summary>
public async Task<PageDataDto<Ai_SpecialDto>> GetSpecialList(Ai_SpecialRequest paramDto)
{
var res = new PageDataDto<Ai_SpecialDto>();
var query = from s in _specialRepository.DbContext.Set<Ai_Special>()
select new Ai_SpecialDto()
{
Id = s.Id,
SpecialName = s.SpecialName,
ImageUrl = s.ImageUrl
};
if (!string.IsNullOrWhiteSpace(paramDto.SpecialName))
{
query = query.Where(x => x.SpecialName.Contains(paramDto.SpecialName));
}
res.Total = await query.CountAsync();
var list = await query
.Skip((paramDto.PageIndex - 1) * paramDto.PageSize)
.Take(paramDto.PageSize)
.OrderBy(x => x.Id)
.ToListAsync();
res.Datas = list;
return res;
}
/// <summary>
/// 获取专项水平列表
/// </summary>
public async Task<PageDataDto<Ai_SpecialLevelDto>> GetSpecialLevelList([FromQuery] Ai_SpecialLevelRequest paramDto)
{
var res = new PageDataDto<Ai_SpecialLevelDto>();
if (paramDto.SpecialId <= 0)
throw new ArgumentNullException("请求参数错误");
var query = from s in _specialRepository.DbContext.Set<Ai_SpecialLevel>()
where s.SpecialId == paramDto.SpecialId
select new Ai_SpecialLevelDto()
{
Id = s.Id,
LevelName = s.LevelName,
EndGrade = s.EndGrade,
StartGrade = s.StartGrade,
BasicSkillList = s.SpecialActionList
.Where(a => a.ActionType == SpecialActionType.BasicSkill)
.Select(a => new Ai_SpecialActionDto
{
Id = a.Id,
ActionName = a.ActionName,
ActionType = a.ActionType,
ActionVideoPath = a.ActionVideoPath,
ExplainVideoPath = a.ExplainVideoPath,
ExerciseVideoPath = a.ExerciseVideoPath,
StandardVideoPath = a.StandardVideoPath
}).ToList(),
SpecialStaminaList = s.SpecialActionList
.Where(a => a.ActionType == SpecialActionType.SpecialStamina)
.Select(a => new Ai_SpecialActionDto
{
Id = a.Id,
ActionName = a.ActionName,
ActionType = a.ActionType,
ActionVideoPath = a.ActionVideoPath,
ExplainVideoPath = a.ExplainVideoPath,
ExerciseVideoPath = a.ExerciseVideoPath,
StandardVideoPath = a.StandardVideoPath
}).ToList()
};
res.Total = await query.CountAsync();
var list = await query
.Skip((paramDto.PageIndex - 1) * paramDto.PageSize)
.Take(paramDto.PageSize)
.OrderBy(x => x.Id)
.ToListAsync();
res.Datas = list;
return res;
}
/// <summary>
/// 新增专项动作
/// </summary>
public async Task AddSpecialAction(SpecialActionParam paramDto)
{
if (string.IsNullOrWhiteSpace(paramDto.ActionName))
throw new ArgumentNullException("请填写动作名称");
var specialLevel = await _specialRepository.FindAsyncFirst<Ai_SpecialLevel>(c => c.Id == paramDto.LevelId);
if (specialLevel == null)
throw new ArgumentNullException("未找到专项水平");
var currentUserId = UserContext.Current.UserId;
var currentDateTime = DateTime.Now;
var specialActionEntity = new Ai_SpecialAction()
{
SpecialLevelId = paramDto.LevelId,
ActionName = paramDto.ActionName,
ActionType = paramDto.ActionType,
Creator = currentUserId,
CreateDate = currentDateTime
};
_specialRepository.Add<Ai_SpecialAction>(specialActionEntity);
await _specialRepository.SaveChangesAsync();
}
/// <summary>
/// 更新专项动作
/// </summary>
public async Task ModifySpecialAction(SpecialActionParam paramDto)
{
var currentUserId = UserContext.Current.UserId;
var currentDateTime = DateTime.Now;
var specialActionEntity = await _specialRepository.FindAsyncFirst<Ai_SpecialAction>(c => c.Id == paramDto.Id);
if (specialActionEntity == null)
throw new ArgumentNullException("未找到专项动作");
specialActionEntity.ActionName = paramDto.ActionName;
specialActionEntity.Modifier = currentUserId;
specialActionEntity.ModifyDate = currentDateTime;
_specialRepository.Update<Ai_SpecialAction>(specialActionEntity);
await _specialRepository.SaveChangesAsync();
}
/// <summary>
/// 更新专项动作视频
/// </summary>
public async Task ModifySpecialActionVideo(SpecialActionParam paramDto)
{
var currentUserId = UserContext.Current.UserId;
var currentDateTime = DateTime.Now;
var specialActionEntity = await _specialRepository.FindAsyncFirst<Ai_SpecialAction>(c => c.Id == paramDto.Id);
if (specialActionEntity == null)
throw new ArgumentNullException("未找到专项动作");
specialActionEntity.ActionVideoPath = paramDto.ActionVideoPath;
specialActionEntity.ExplainVideoPath = paramDto.ExplainVideoPath;
specialActionEntity.ExerciseVideoPath = paramDto.ExerciseVideoPath;
specialActionEntity.StandardVideoPath = paramDto.StandardVideoPath;
specialActionEntity.Modifier = currentUserId;
specialActionEntity.ModifyDate = currentDateTime;
_specialRepository.Update<Ai_SpecialAction>(specialActionEntity);
await _specialRepository.SaveChangesAsync();
}
/// <summary>
/// 删除专项动作
/// </summary>
public async Task DeleteSpecialAction(int id)
{
var model = await _specialRepository.FindAsyncFirst<Ai_SpecialAction>(x => x.Id == id);
if (model == null)
throw new ArgumentNullException($"未找到删除的数据");
_specialRepository.DbContext.Remove<Ai_SpecialAction>(model);
await _specialRepository.SaveChangesAsync();
}
/// <summary>
/// 专项上传视频
/// </summary>
public async Task<string> SpecialUploadVideo(IFormFile file, int levelId, string videoName, int actionId)
{
var levelEntity = await _specialRepository.FindAsyncFirst<Ai_SpecialLevel>(c => c.Id == levelId);
if (levelEntity == null)
throw new ArgumentNullException("专项水平不存在");
var timeStampString = DateTime.Now.ToUnixTimeStamp();
var prefix = $"Upload/Special-{levelEntity.SpecialId}/Leave-{levelEntity.Id}/Action-{actionId}/{videoName}/";
// 删除文件
ALiYunOss.DeleteFilesByPrefix(prefix);
// 上传文件
var url = ALiYunOss.Upload(file, prefix, timeStampString);
return url;
}
/// <summary>
/// 上传封面图像
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public string UploadImage(IFormFile file)
{
var url = ALiYunOss.Upload(file, $"Upload/Special/", DateTime.Now.ToUnixTimeStamp());
return url;
}
}
}