using Competition.Common.Util; using CompetitionAPI.Util; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Org.BouncyCastle.Ocsp; namespace CompetitionAPI.Controllers.back.study { [Route("api/[controller]")] [ApiController] public class DeleteStudyController : 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(); public DeleteStudyController(IWebHostEnvironment webHostEnvironment) { _webHostEnvironment = webHostEnvironment; } /// /// 删除学习接口 /// /// 学习id /// [Authorize] [HttpGet] [APIFilter] public JsonResult Index(string Id) { try { //获取当前web目录 var webRootPath = _webHostEnvironment.WebRootPath; if (string.IsNullOrEmpty(Id)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "学习id不能为空")); } var old_file_list = study_file_bll.GetModelList(" StudyId='" + Id + "' "); var ret = study_bll.OperationDeleteAllData(Id); if (ret > 0) { foreach (var item in old_file_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, "发生错误,请联系管理员。")); } } } }