251 lines
12 KiB
C#
251 lines
12 KiB
C#
using Competition.Common.Util;
|
||
using CompetitionAPI.api.unity;
|
||
using CompetitionAPI.Util;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Hosting;
|
||
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Newtonsoft.Json;
|
||
using System.Collections.Generic;
|
||
|
||
namespace CompetitionAPI.Controllers.unity
|
||
{
|
||
[Route("unity/[controller]")]
|
||
[ApiController]
|
||
public class WriteUserOperationTicketController : Controller
|
||
{
|
||
Competition.Mysql.BLL.pow_user_exam user_exam_bll = new Competition.Mysql.BLL.pow_user_exam();
|
||
|
||
Competition.Mysql.BLL.pow_exam exam_bll = new Competition.Mysql.BLL.pow_exam();
|
||
|
||
Competition.Mysql.BLL.pow_user_operation_ticket bll_pow_user_operation_ticket = new Competition.Mysql.BLL.pow_user_operation_ticket();
|
||
|
||
Competition.Mysql.BLL.pow_exam_operation_ticket bll_pow_exam_operation_ticket = new Competition.Mysql.BLL.pow_exam_operation_ticket();
|
||
|
||
Competition.Mysql.BLL.pow_achievement bll_pow_achievement = new Competition.Mysql.BLL.pow_achievement();
|
||
|
||
Competition.Mysql.BLL.admin_user bll_admin_User = new Competition.Mysql.BLL.admin_user();
|
||
|
||
public WriteUserOperationTicketController()
|
||
{
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 操作票写入接口
|
||
/// </summary>
|
||
/// <param name="req"></param>
|
||
/// <returns></returns>
|
||
[Authorize]
|
||
[HttpPost]
|
||
[APIFilter]
|
||
public JsonResult Index([FromForm] WriteUserOperationTicketRequest req)
|
||
{
|
||
try
|
||
{
|
||
if (string.IsNullOrEmpty(req.data))
|
||
{
|
||
return Json(Tool.GetJsonWithCode(APICode.Fail, "data参数不能为空"));
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(req.ExamId))
|
||
{
|
||
return Json(Tool.GetJsonWithCode(APICode.Fail, "ExamId参数不能为空"));
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(req.Type))
|
||
{
|
||
return Json(Tool.GetJsonWithCode(APICode.Fail, "Type参数不能为空"));
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(req.UserId))
|
||
{
|
||
return Json(Tool.GetJsonWithCode(APICode.Fail, "UserId参数不能为空"));
|
||
}
|
||
|
||
var user_exam_model = user_exam_bll.GetModelList(" ExamId='" + req.ExamId + "' and UserId='" + req.UserId + "' ").FirstOrDefault();
|
||
if (user_exam_model != null)
|
||
{
|
||
//if (user_exam_model.Status == "已结束")
|
||
//{
|
||
// return Json(Tool.GetJsonWithCode(APICode.Fail, "考生考试已结束"));
|
||
//}
|
||
}
|
||
else
|
||
{
|
||
return Json(Tool.GetJsonWithCode(APICode.Fail, "考生考试信息不存在"));
|
||
}
|
||
|
||
var achievement = bll_pow_achievement.GetModelList(" ExamId='" + req.ExamId + "' and UserId='" + req.UserId + "' ").FirstOrDefault();
|
||
if (null == achievement)
|
||
{
|
||
return Json(Tool.GetJsonWithCode(APICode.Fail, "考生成绩数据不存在"));
|
||
}
|
||
|
||
//用户操作ticket
|
||
var list_user_operation_ticket = JsonConvert.DeserializeObject<List<OperationTicketData>>(req.data);
|
||
var qry = string.Format(" ExamId='{0}' and Type='{1}' and LENGTH(Content)>0 ", req.ExamId, req.Type);
|
||
var list_exam_operation_ticket = bll_pow_exam_operation_ticket.GetModelList(qry).OrderBy(s => s.SerialNumber).ToList();
|
||
int total_score = 5;
|
||
var list_TicketId_HasScore = new List<string>();
|
||
var list = new List<OperationTicketData>();
|
||
var score = Compute(ref total_score, out list, list_TicketId_HasScore, list_user_operation_ticket, list_exam_operation_ticket);
|
||
list_user_operation_ticket = list_user_operation_ticket.OrderBy(a => a.SerialNumber).ToList();
|
||
for (int i = 0; i < list.Count; i++)
|
||
{
|
||
var item = list[i];
|
||
var model = new Competition.Mysql.Model.pow_user_operation_ticket();
|
||
model.UserOperationTicketId = Guid.NewGuid().ToString("N");
|
||
model.UserId = req.UserId;
|
||
model.ExamId = req.ExamId;
|
||
model.OperationTicketId = item.OperationTicketId;
|
||
model.Type = req.Type;
|
||
model.SerialNumber = i + 1; // int.Parse(item.SerialNumber);
|
||
model.Content = item.Content;
|
||
if (item.ExamOperationTicketId == "2")
|
||
{
|
||
model.IsSelect = "0"; //是否选择 0:未选择,1:已选择
|
||
}
|
||
else
|
||
{
|
||
model.IsSelect = "1"; //是否选择 0:未选择,1:已选择
|
||
}
|
||
model.IsScore = list_TicketId_HasScore.Contains(item.Content) ? "0" : "1"; //是否扣分 0:否,1:是
|
||
model.CreateTime = DateTime.Now;
|
||
bll_pow_user_operation_ticket.Add(model);
|
||
}
|
||
|
||
var details_model = new Competition.Mysql.Model.pow_achievement_details();
|
||
details_model.DetailsId = Guid.NewGuid().ToString("N");
|
||
details_model.AchievementId = achievement.AchievementId;
|
||
details_model.ItemName = req.Type + "操作票选择";
|
||
details_model.ItemizedScore = total_score;
|
||
details_model.Type = req.Type + "操作票";
|
||
details_model.CreateTime = DateTime.Now;
|
||
details_model.TotalScore = 5;
|
||
bll_pow_achievement.OperationAddUpdateData(new List<Competition.Mysql.Model.pow_achievement_details>() { details_model }, achievement.AchievementId, total_score);
|
||
return Json(Tool.GetJsonWithCode(APICode.Success, "操作票总分:" + total_score));
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace);
|
||
return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。"));
|
||
}
|
||
}
|
||
|
||
private int Compute(ref int total, out List<OperationTicketData> list, List<string> list_TicketId_HasScore, List<OperationTicketData> list_user_operation_ticket, List<Competition.Mysql.Model.pow_exam_operation_ticket> list_exam_operation_ticket)
|
||
{
|
||
list = new List<OperationTicketData>();
|
||
var distance = 0;
|
||
for (int i = 0; i < list_exam_operation_ticket.Count; i++)
|
||
{
|
||
var item_exam = list_exam_operation_ticket[i];
|
||
var item_user = list_user_operation_ticket.FirstOrDefault(s => s.Content == item_exam.Content);
|
||
if (item_user != null)
|
||
{
|
||
var user_Index = list_user_operation_ticket.IndexOf(item_user);
|
||
if (i != user_Index)
|
||
{
|
||
distance = distance + 1;
|
||
}
|
||
else
|
||
{
|
||
list_TicketId_HasScore.Add(item_exam.Content);
|
||
}
|
||
|
||
list.Add(new OperationTicketData() { ExamOperationTicketId = "1", OperationTicketId = item_exam.OperationTicketId, Type = item_exam.Type, Content = item_exam.Content });
|
||
}
|
||
}
|
||
|
||
//多选 用户操作票里有,答案操作票里没有
|
||
var CaseNumberList = list_exam_operation_ticket.Select(s => s.Content).ToArray();
|
||
var DifferentData = list_user_operation_ticket.Where(s => !CaseNumberList.Contains(s.Content)).ToList();
|
||
if (DifferentData.Count() > 0)
|
||
{
|
||
distance = distance + DifferentData.Count();
|
||
list.AddRange(DifferentData);
|
||
}
|
||
//少选 答案操作票里有,用户操作票里没有
|
||
var localCaseNumberList = list_user_operation_ticket.Select(s => s.Content).ToArray();
|
||
var FieldCaseData = list_exam_operation_ticket.Where(s => !localCaseNumberList.Contains(s.Content)).ToList();
|
||
if (FieldCaseData.Count() > 0)
|
||
{
|
||
distance = distance + FieldCaseData.Count();
|
||
var new_list = (from f in FieldCaseData
|
||
select new OperationTicketData() { ExamOperationTicketId = "2", OperationTicketId = f.OperationTicketId, Type = f.Type, Content = f.Content }).ToList();
|
||
list.AddRange(new_list);
|
||
}
|
||
total = total - distance;
|
||
if (total <= 0)
|
||
{
|
||
total = 0;
|
||
}
|
||
return total;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 计算操作票得分
|
||
/// </summary>
|
||
/// <param name="total">总分</param>
|
||
/// <param name="list_TicketId_HasScore">保存未扣分项</param>
|
||
/// <param name="list_user_operation_ticket">用户操作票</param>
|
||
/// <param name="list_exam_operation_ticket">答案操作票</param>
|
||
/// <returns></returns>
|
||
private int ComputeTicketScore(ref int total, List<string> list_TicketId_HasScore, List<OperationTicketData> list_user_operation_ticket, List<Competition.Mysql.Model.pow_exam_operation_ticket> list_exam_operation_ticket)
|
||
{
|
||
for (int i = 0; i < list_exam_operation_ticket.Count; i++)
|
||
{
|
||
var item_exam = list_exam_operation_ticket[i];
|
||
var item_user = list_user_operation_ticket.FirstOrDefault(s => s.OperationTicketId == item_exam.OperationTicketId);
|
||
//没找到答案 扣1分
|
||
if (null == item_user)
|
||
{
|
||
total--;
|
||
if (total <= 0)
|
||
{
|
||
return 0;
|
||
}
|
||
continue;
|
||
}
|
||
//找到答案 扣除距离分
|
||
var exam_index = i;
|
||
var user_Index = list_user_operation_ticket.IndexOf(item_user);
|
||
var distance = user_Index - exam_index;
|
||
//不扣分项
|
||
if (distance == 0)
|
||
{
|
||
list_TicketId_HasScore.Add(item_user.OperationTicketId);
|
||
}
|
||
total = total - distance;
|
||
if (total <= 0)
|
||
{
|
||
return 0;
|
||
}
|
||
//剩余用户答案
|
||
var remain_user_operation_ticket = new List<OperationTicketData>();
|
||
for (var jindex = user_Index + 1; jindex < list_user_operation_ticket.Count; jindex++)
|
||
{
|
||
remain_user_operation_ticket.Add(list_user_operation_ticket[jindex]);
|
||
}
|
||
//剩余标准答案
|
||
var remain_exam_operation_ticket = new List<Competition.Mysql.Model.pow_exam_operation_ticket>();
|
||
for (var e_index = exam_index + 1; e_index < list_exam_operation_ticket.Count; e_index++)
|
||
{
|
||
remain_exam_operation_ticket.Add(list_exam_operation_ticket[e_index]);
|
||
}
|
||
//多余的用户操作票扣分
|
||
if (remain_exam_operation_ticket.Count <= 0)
|
||
{
|
||
total = total - remain_user_operation_ticket.Count;
|
||
if (total <= 0)
|
||
{
|
||
return 0;
|
||
}
|
||
}
|
||
return ComputeTicketScore(ref total, list_TicketId_HasScore, remain_user_operation_ticket, remain_exam_operation_ticket);
|
||
}
|
||
return total;
|
||
}
|
||
}
|
||
}
|