using Competition.Common.Util; using CompetitionAPI.api.back; using CompetitionAPI.Util; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace CompetitionAPI.Controllers.back.study { [Route("api/[controller]")] [ApiController] public class AddStudyController : Controller { private readonly IWebHostEnvironment _webHostEnvironment; Competition.Mysql.BLL.pow_study study_bll = new Competition.Mysql.BLL.pow_study(); private static Object Lockobj = new object(); public AddStudyController(IWebHostEnvironment webHostEnvironment) { _webHostEnvironment = webHostEnvironment; } /// /// 新增学习接口 /// /// 请求参数 /// [Authorize] [HttpPost] [APIFilter] public JsonResult Index([FromBody] AddStudyRequest req) { try { lock (Lockobj) { 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}' ", req.StudyName)) > 0) { return Json(Tool.GetJsonWithCode(APICode.Fail, "学习名称禁止重复")); } var model = new Competition.Mysql.Model.pow_study(); model.StudyId = Guid.NewGuid().ToString("N"); model.StudyName = req.StudyName; model.StudyClassId = req.StudyClassId; model.StudyClass = req.StudyClass; model.StudySynopsis = req.StudySynopsis; model.CreateTime = DateTime.Now; model.Status = "未发布"; 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); } if (study_bll.OperationAllData(model, file_list) > 0) { 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, "发生错误,请联系管理员。")); } } } }