using Competition.Common.Util; using CompetitionAPI.api.unity; using CompetitionAPI.Util; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Text; namespace CompetitionAPI.Controllers.unity.newinterface { [Route("unity/[controller]")] [ApiController] public class GetExamListController : Controller { Competition.Mysql.BLL.pow_exam exam_bll = new Competition.Mysql.BLL.pow_exam(); public GetExamListController() { } /// /// 获取我的考试列表接口 /// /// 请求参数 /// [Authorize] [HttpPost] [APIFilter] public JsonResult Index([FromForm] GetExamListRequest req) { try { var now = DateTime.Now; var query = new StringBuilder(string.Format(" UserId='{0}' and (Status='已发布' or Status='已结束') and Type='考试' ", req.UserId)); var total_query = new StringBuilder(string.Format(" UserId='{0}' and (Status='已发布' or Status='已结束') and Type='考试' ", req.UserId)); if (!string.IsNullOrEmpty(req.ExamName)) { query.AppendFormat(" and ExamName like '%{0}%' ", req.ExamName); total_query.AppendFormat(" and ExamName like '%{0}%' ", req.ExamName); } if (!string.IsNullOrEmpty(req.IsExam)) { if (req.IsExam == "2") { query.AppendFormat(" and (EndExamTime<='{0}' or Status='已结束') ", now.ToString("yyyy-MM-dd HH:mm:ss")); total_query.AppendFormat(" and (EndExamTime<='{0}' or Status='已结束') ", now.ToString("yyyy-MM-dd HH:mm:ss")); } else if (req.IsExam == "3") { query.AppendFormat(" and StartExamTime>'{0}' ", now.ToString("yyyy-MM-dd HH:mm:ss")); total_query.AppendFormat(" and StartExamTime>'{0}' ", now.ToString("yyyy-MM-dd HH:mm:ss")); } else { query.AppendFormat(" and IsExam='{0}' ", req.IsExam); total_query.AppendFormat(" and IsExam='{0}' ", req.IsExam); } } var offset = (req.PageIndex - 1) * req.PageSize; query.AppendFormat(" order by CreateTime desc LIMIT {0} OFFSET {1} ", req.PageSize, offset); var total = exam_bll.GetUserRecordCount(total_query.ToString()); var list = exam_bll.GetUserList(query.ToString()); foreach (var item in list) { if (item.EndExamTime <= now) { item.Status = "已结束"; } if (item.StartExamTime > now) { item.Status = "未开始"; } } return Json(Tool.GetJsonWithCode(APICode.Success, new { total = total, list = list })); } catch (Exception ex) { LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。")); } } } }