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 /// /// 获取专项教学列表 /// public async Task> GetSpecialList(Ai_SpecialRequest paramDto) { var res = new PageDataDto(); var query = from s in _specialRepository.DbContext.Set() 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; } /// /// 获取专项水平列表 /// public async Task> GetSpecialLevelList([FromQuery] Ai_SpecialLevelRequest paramDto) { var res = new PageDataDto(); if (paramDto.SpecialId <= 0) throw new ArgumentNullException("请求参数错误"); var query = from s in _specialRepository.DbContext.Set() 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; } /// /// 新增专项动作 /// public async Task AddSpecialAction(SpecialActionParam paramDto) { if (string.IsNullOrWhiteSpace(paramDto.ActionName)) throw new ArgumentNullException("请填写动作名称"); var specialLevel = await _specialRepository.FindAsyncFirst(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(specialActionEntity); await _specialRepository.SaveChangesAsync(); } /// /// 更新专项动作 /// public async Task ModifySpecialAction(SpecialActionParam paramDto) { var currentUserId = UserContext.Current.UserId; var currentDateTime = DateTime.Now; var specialActionEntity = await _specialRepository.FindAsyncFirst(c => c.Id == paramDto.Id); if (specialActionEntity == null) throw new ArgumentNullException("未找到专项动作"); specialActionEntity.ActionName = paramDto.ActionName; specialActionEntity.Modifier = currentUserId; specialActionEntity.ModifyDate = currentDateTime; _specialRepository.Update(specialActionEntity); await _specialRepository.SaveChangesAsync(); } /// /// 更新专项动作视频 /// public async Task ModifySpecialActionVideo(SpecialActionParam paramDto) { var currentUserId = UserContext.Current.UserId; var currentDateTime = DateTime.Now; var specialActionEntity = await _specialRepository.FindAsyncFirst(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(specialActionEntity); await _specialRepository.SaveChangesAsync(); } /// /// 删除专项动作 /// public async Task DeleteSpecialAction(int id) { var model = await _specialRepository.FindAsyncFirst(x => x.Id == id); if (model == null) throw new ArgumentNullException($"未找到删除的数据"); _specialRepository.DbContext.Remove(model); await _specialRepository.SaveChangesAsync(); } /// /// 专项上传视频 /// public async Task SpecialUploadVideo(IFormFile file, int levelId, string videoName, int actionId) { var levelEntity = await _specialRepository.FindAsyncFirst(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; } /// /// 上传封面图像 /// /// /// public string UploadImage(IFormFile file) { var url = ALiYunOss.Upload(file, $"Upload/Special/", DateTime.Now.ToUnixTimeStamp()); return url; } } }