using Competition.Common.Util; using CompetitionAPI.api.back; using CompetitionAPI.Util; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace CompetitionAPI.Controllers.back { [Route("api/[controller]")] [ApiController] public class FinishedExamController : Controller { Competition.Mysql.BLL.pow_exam exam_bll = new Competition.Mysql.BLL.pow_exam(); Competition.Mysql.BLL.pow_user_exam user_exam_bll = new Competition.Mysql.BLL.pow_user_exam(); Competition.Mysql.BLL.pow_achievement achievement_bll = new Competition.Mysql.BLL.pow_achievement(); Competition.Mysql.BLL.pow_config config_bll = new Competition.Mysql.BLL.pow_config(); public FinishedExamController() { } /// /// 考试结束接口 /// /// 考试id /// [Authorize] [HttpGet] [APIFilter] public JsonResult Index(string ExamId) { try { var config_model = config_bll.GetModelList("").FirstOrDefault(); if (config_model == null) { return Json(Tool.GetJsonWithCode(APICode.Fail, "配置信息不存在")); } var exam_model = exam_bll.GetModel(ExamId); if (exam_model != null) { if (exam_model.Type == "考试") { var user_exam_list = user_exam_bll.GetModelList(" ExamId='" + ExamId + "' and Status='已结束' "); var time_count = int.Parse(config_model.TimeCount); //业务逻辑 :按照分数排序,再按照考试时长,最后计算时间分 //查询本次所有考试结束的考生 var user_id_list_all = string.Join("','", user_exam_list.Select(a => a.UserId).ToArray()); //从成绩表把20个考试最高分考生查询出来 var achievement_list_20 = achievement_bll.GetModelList(" ExamId='" + ExamId + "' and UserId in ('" + user_id_list_all + "') and ExamEndTime is not NULL ").OrderByDescending(s => s.TotalScore).Take(time_count).ToList(); //查询这20个考生的考试记录 var user_id_list_20 = string.Join("','", achievement_list_20.Select(a => a.UserId).ToArray()); var user_exam_list_20 = user_exam_bll.GetModelList(" ExamId='" + ExamId + "' and UserId in ('" + user_id_list_20 + "') "); List list_userInfo = new List(); foreach (var item in achievement_list_20) { UserExamInfo info = new UserExamInfo(); info.UserId = item.UserId; info.TotalScore = item.TotalScore.HasValue ? item.TotalScore.Value : 0; var user_exam = user_exam_list_20.FirstOrDefault(s => s.UserId == item.UserId); if (null != user_exam) { info.ExaminationDuration = user_exam.ExaminationDuration; } list_userInfo.Add(info); } var front_user_exam_list = list_userInfo.OrderByDescending(s => s.TotalScore).ThenByDescending(a => a.ExaminationDuration.Split(':')[0]).ThenByDescending(a => a.ExaminationDuration.Split(':')[1]).ToList(); var achievement_list = achievement_list_20; var time_score = decimal.Parse(config_model.TimeScore); var total_score = time_score; var time_decreasing_score = decimal.Parse(config_model.TimeDecreasingScore); var update_achievement_list = new List(); var add_achievement_details_list = new List(); var index = 0; foreach (var item in front_user_exam_list) { var model = achievement_list.Where(a => a.UserId == item.UserId).FirstOrDefault(); if (model != null) { if (index != 0) { time_score = time_score - time_decreasing_score; } model.TotalScore += time_score; update_achievement_list.Add(model); //故障算分bug临时处理 if (model.TotalScore > 100) { model.TotalScore = 100; } var details_model = new Competition.Mysql.Model.pow_achievement_details(); details_model.DetailsId = Guid.NewGuid().ToString("N"); details_model.AchievementId = model.AchievementId; details_model.ItemName = "时间分"; details_model.ItemizedScore = time_score; details_model.Type = "时间分"; details_model.CreateTime = DateTime.Now; details_model.TotalScore = total_score; add_achievement_details_list.Add(details_model); } index++; } if (exam_bll.OperationUpdateExamData(ExamId, "已结束", update_achievement_list, add_achievement_details_list) > 0) { return Json(Tool.GetJsonWithCode(APICode.Success, "结束考试成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "结束考试失败")); } } else { if (exam_bll.UpdateEnd(ExamId, "已结束")) { return Json(Tool.GetJsonWithCode(APICode.Success, "结束考试成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "结束考试失败")); } } } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "未找到考试信息")); } } catch (Exception ex) { LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。")); } } } }