using Competition.Common.Util; using CompetitionAPI.api.back; using CompetitionAPI.Util; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; namespace CompetitionAPI.Controllers.back.study { [Route("api/[controller]")] [ApiController] public class EditStudyController : Controller { private readonly IWebHostEnvironment _webHostEnvironment; Competition.Mysql.BLL.pow_study study_bll = new Competition.Mysql.BLL.pow_study(); Competition.Mysql.BLL.pow_study_file study_file_bll = new Competition.Mysql.BLL.pow_study_file(); private static Object Lockobj = new object(); public EditStudyController(IWebHostEnvironment webHostEnvironment) { _webHostEnvironment = webHostEnvironment; } /// /// 编辑学习接口 /// /// 请求参数 /// [Authorize] [HttpPost] [APIFilter] public JsonResult Index([FromBody] EditStudyRequest req) { try { lock (Lockobj) { if (string.IsNullOrWhiteSpace(req.StudyId)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "学习id不能为空")); } if (string.IsNullOrWhiteSpace(req.StudyName)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "学习名称不能为空")); } if (string.IsNullOrWhiteSpace(req.StudyClassId)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "学习分类id不能为空")); } if (string.IsNullOrWhiteSpace(req.StudyClass)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "学习分类不能为空")); } if (req.VideoData.Count() == 0) { return Json(Tool.GetJsonWithCode(APICode.Fail, "视频路径不能为空")); } //获取当前web目录 var webRootPath = _webHostEnvironment.WebRootPath; foreach (var item in req.VideoData) { if (!System.IO.File.Exists(webRootPath + "/" + item.FilePath)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "视频集合里有视频不存在,请先上传视频")); } } if (req.FileData.Count() > 0) { foreach (var item in req.FileData) { if (!System.IO.File.Exists(webRootPath + "/" + item.FilePath)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "文档集合里有文档不存在,请先上传文档")); } } } if (study_bll.GetRecordCount(string.Format(" StudyName = '{0}' and StudyId != '{1}'", req.StudyName, req.StudyId)) > 0) { return Json(Tool.GetJsonWithCode(APICode.Fail, "学习名称禁止重复")); } var model = study_bll.GetModel(req.StudyId); model.StudyName = req.StudyName; model.StudyClassId = req.StudyClassId; model.StudyClass = req.StudyClass; model.StudySynopsis = req.StudySynopsis; var file_list = new List(); foreach (var item in req.VideoData) { var file_model = new Competition.Mysql.Model.pow_study_file(); file_model.FileId = Guid.NewGuid().ToString("N"); file_model.StudyId = model.StudyId; file_model.FileName = item.FileName; file_model.FilePath = item.FilePath; file_model.FileType = "视频"; file_model.FileSubType = Path.GetExtension(item.FilePath).Replace(".", ""); file_model.CreateTime = DateTime.Now; file_list.Add(file_model); } foreach (var item in req.FileData) { var file_model = new Competition.Mysql.Model.pow_study_file(); file_model.FileId = Guid.NewGuid().ToString("N"); file_model.StudyId = model.StudyId; file_model.FileName = item.FileName; file_model.FilePath = item.FilePath; file_model.FileType = "文档"; file_model.FileSubType = Path.GetExtension(item.FilePath).Replace(".", ""); file_model.CreateTime = DateTime.Now; file_list.Add(file_model); } var old_file_list = study_file_bll.GetModelList(" StudyId='" + req.StudyId + "' "); var local_list = file_list.Select(s => s.FilePath).ToArray(); var delete_list = old_file_list.Where(s => !local_list.Contains(s.FilePath)).ToList(); if (study_bll.OperationEditAllData(model, file_list) > 0) { foreach (var item in delete_list) { if (System.IO.File.Exists(webRootPath + "/" + item.FilePath)) { var array = item.FilePath.Split('/'); if (array.Length > 0) { Array.Resize(ref array, array.Length - 1); } var new_path = string.Join("/", array); Tool.DeleteDir(webRootPath + new_path); //System.IO.File.Delete(webRootPath + "/" + item.FilePath); } } return Json(Tool.GetJsonWithCode(APICode.Success, "编辑成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "编辑失败")); } } } catch (Exception ex) { LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。")); } } } }