using Competition.Common.Util; using CompetitionAPI.api.back; using CompetitionAPI.Util; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Org.BouncyCastle.Ocsp; using static NPOI.HSSF.Util.HSSFColor; namespace CompetitionAPI.Controllers.back { [Route("api/[controller]")] [ApiController] public class DeleteExamController : Controller { private readonly IWebHostEnvironment _webHostEnvironment; Competition.Mysql.BLL.pow_achievement achievement_bll = new Competition.Mysql.BLL.pow_achievement(); Competition.Mysql.BLL.pow_exam bll = new Competition.Mysql.BLL.pow_exam(); Competition.Mysql.BLL.pow_exam_fault exam_fault_bll = new Competition.Mysql.BLL.pow_exam_fault(); Competition.Mysql.BLL.pow_video video_bll = new Competition.Mysql.BLL.pow_video(); Competition.Mysql.BLL.pow_user_fault user_fault_bll = new Competition.Mysql.BLL.pow_user_fault(); public DeleteExamController(IWebHostEnvironment webHostEnvironment) { _webHostEnvironment = webHostEnvironment; } /// /// 删除考试接口 /// /// 考试id /// [Authorize] [HttpGet] [APIFilter] public JsonResult Index(string ExamId) { try { var count = achievement_bll.GetRecordCount(" ExamId='" + ExamId + "' "); var exam_model = bll.GetModel(ExamId); if (exam_model != null) { if (exam_model.Type == "考试") { if (count > 0) { var exam_count = bll.GetRecordCount(" ExamId='" + ExamId + "' and Status='已结束' "); if (exam_count == 0) { return Json(Tool.GetJsonWithCode(APICode.Fail, "已有考生使用该考试不能删除,需结束本次考试后才能删除")); } } } } var delete_list = new List(); var user_fault_list = user_fault_bll.GetModelList(string.Format(" ExamId='{0}' and FaultDesc='出线电缆编号牌型号编写错误' ", ExamId)); if (user_fault_list.Count > 0) { delete_list.AddRange(user_fault_list); } var video_list = new List(); var achievement_list = achievement_bll.GetModelList(" ExamId='" + ExamId + "' ").Select(a => a.AchievementId).ToArray(); var achievement_id_list = ""; if (achievement_list.Length > 0) { achievement_id_list = string.Join("','", achievement_list); video_list = video_bll.GetModelList(" AchievementId in ('" + string.Join("','", achievement_list) + "') "); } var fault_list = exam_fault_bll.GetModelList(" ExamId='" + ExamId + "' and FaultDesc='出线电缆编号牌型号编写错误' "); if (bll.OperationDelData(ExamId, achievement_id_list) > 0) { if (exam_model != null) { if (exam_model.Type == "实训") { string path1 = _webHostEnvironment.WebRootPath + exam_model.CoverPath; if (System.IO.File.Exists(path1)) { var array = exam_model.CoverPath.Split('/'); if (array.Length > 0) { Array.Resize(ref array, array.Length - 1); } var new_path = string.Join("/", array); Tool.DeleteDir(_webHostEnvironment.WebRootPath + new_path); //System.IO.File.Delete(path1); } } } foreach (var item in video_list) { if (!string.IsNullOrEmpty(item.VideoPath)) { string path = _webHostEnvironment.WebRootPath + item.VideoPath; if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } } foreach (var item in delete_list) { string path1 = _webHostEnvironment.WebRootPath + item.SensitiveKnowledge; if (System.IO.File.Exists(path1)) { System.IO.File.Delete(path1); } string path2 = _webHostEnvironment.WebRootPath + item.Minhui; if (System.IO.File.Exists(path2)) { System.IO.File.Delete(path2); } string path3 = _webHostEnvironment.WebRootPath + item.SensitiveLine; if (System.IO.File.Exists(path3)) { System.IO.File.Delete(path3); } string path4 = _webHostEnvironment.WebRootPath + item.Allergy; if (System.IO.File.Exists(path4)) { System.IO.File.Delete(path4); } string path5 = _webHostEnvironment.WebRootPath + item.GracefulGraceful; if (System.IO.File.Exists(path5)) { System.IO.File.Delete(path5); } } foreach (var item in fault_list) { string path1 = _webHostEnvironment.WebRootPath + item.SensitiveKnowledge; if (System.IO.File.Exists(path1)) { System.IO.File.Delete(path1); } string path2 = _webHostEnvironment.WebRootPath + item.Minhui; if (System.IO.File.Exists(path2)) { System.IO.File.Delete(path2); } string path3 = _webHostEnvironment.WebRootPath + item.SensitiveLine; if (System.IO.File.Exists(path3)) { System.IO.File.Delete(path3); } string path4 = _webHostEnvironment.WebRootPath + item.Allergy; if (System.IO.File.Exists(path4)) { System.IO.File.Delete(path4); } string path5 = _webHostEnvironment.WebRootPath + item.GracefulGraceful; if (System.IO.File.Exists(path5)) { System.IO.File.Delete(path5); } } 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, "发生错误,请联系管理员。")); } } } }