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 EditExamController : Controller
{
private readonly IWebHostEnvironment _webHostEnvironment;
Competition.Mysql.BLL.pow_exam exam_bll = new Competition.Mysql.BLL.pow_exam();
Competition.Mysql.BLL.pow_exam_fault exam_fault_bll = new Competition.Mysql.BLL.pow_exam_fault();
private static Object Lockobj = new object();
public EditExamController(IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment = webHostEnvironment;
}
///
/// 编辑考试接口
///
/// 请求参数
///
[Authorize]
[HttpPost]
[APIFilter]
public JsonResult Index([FromBody] EditExamRequest req)
{
try
{
lock (Lockobj)
{
//获取当前web目录
var webRootPath = _webHostEnvironment.WebRootPath;
if (string.IsNullOrWhiteSpace(req.ExamId))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "考试id不能为空"));
}
if (req.Type == "考试")
{
if (string.IsNullOrWhiteSpace(req.StartExamTime))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "开始考试时间不能为空"));
}
if (string.IsNullOrWhiteSpace(req.EndExamTime))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "结束考试时间不能为空"));
}
}
else
{
if (string.IsNullOrWhiteSpace(req.CoverName))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "实训封面不能为空"));
}
if (string.IsNullOrWhiteSpace(req.CoverPath))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "实训封面不能为空"));
}
if (!System.IO.File.Exists(webRootPath + "/" + req.CoverPath))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "封面不存在,请先上传封面"));
}
}
if (string.IsNullOrWhiteSpace(req.ExamName))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "考试名称不能为空"));
}
if (req.ExamName.Length > 20)
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "考试名称长度不能超过20个字符"));
}
if (string.IsNullOrWhiteSpace(req.LinePatrolDate))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "巡线日期不能为空"));
}
if (string.IsNullOrWhiteSpace(req.HandleDate))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "处理日期不能为空"));
}
if (string.IsNullOrEmpty(req.SceneId))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "场景不能为空"));
}
if (string.IsNullOrEmpty(req.PlatformAreaId))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "台区不能为空"));
}
if (string.IsNullOrEmpty(req.LineId))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "线路不能为空"));
}
if (req.Type == "考试")
{
if (string.IsNullOrWhiteSpace(req.ExaminationDuration))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "考试时长不能为空"));
}
var start_time = DateTime.Parse(req.StartExamTime);
var end_time = DateTime.Parse(req.EndExamTime);
if (start_time >= end_time)
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "开始考试时间必须要小于结束考试时间"));
}
var duration = (end_time - start_time).TotalMinutes;
var exam_duration = double.Parse(req.ExaminationDuration);
if (exam_duration > duration)
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "考试时长不能超过开始考试时间和结束考试时间的区间分钟数"));
}
}
if (string.IsNullOrWhiteSpace(req.IncomingLineModelLength))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "进线电缆型号及长度不能为空"));
}
if (string.IsNullOrWhiteSpace(req.OutgoingLineModelLength))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "出线电缆型号及长度不能为空"));
}
if (string.IsNullOrWhiteSpace(req.WorkOrderContent))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "工单内容不能为空"));
}
if (!req.WorkOrderContent.Contains(req.PlatformArea))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "工单内容里不包含选择的台区!"));
}
if (string.IsNullOrWhiteSpace(req.BackgroundInformation))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "背景资料不能为空"));
}
if (string.IsNullOrEmpty(req.Weather))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "天气不能为空"));
}
var protection_list = new List();
protection_list.Add("411开关(保护器)额定剩余电流保护:");
protection_list.Add("411开关(保护器)过载保护:");
protection_list.Add("411开关(保护器)短路延时保护:");
protection_list.Add("411开关(保护器)短路瞬时保护:");
protection_list.Add("411开关(保护器)欠压保护:");
protection_list.Add("411开关(保护器)过压保护:");
protection_list.Add("411开关(保护器)缺相保护:");
protection_list.Add("411熔芯熔断电流:");
var leakage_protection = "";
var overload_protection = "";
var short_circuit_delay_protection = "";
var short_circuit_protection = "";
var undervoltage_protection = "";
var overvoltage_protection = "";
var phase_loss_protection = "";
var fusible_core_current = "";
foreach (var item in protection_list)
{
if (!req.BackgroundInformation.Contains(item))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "背景资料格式不正确,未包含" + item));
}
var content_list = req.BackgroundInformation.Split(new char[2] { '\r', '\n' }).ToList();
var leakage_protection_value = content_list.Where(a => a.Contains(item)).FirstOrDefault();
if (!string.IsNullOrEmpty(leakage_protection_value))
{
var value = leakage_protection_value.Split(':');
if (value.Length != 2)
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "背景资料的" + item + "格式不正确"));
}
else
{
if (item != "411开关(保护器)缺相保护:" && item != "411熔芯熔断电流:")
{
if (!value[1].Contains("(") || !value[1].Contains(")"))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "背景资料的" + item + "格式不正确,值未包含()格式"));
}
var value_list = new List();
var pro = value[1].Split('(');
if (item == "411开关(保护器)额定剩余电流保护:")
{
if (pro[0] == "50mA" || pro[0] == "100mA" || pro[0] == "200mA" || pro[0] == "300mA" || pro[0] == "400mA" || pro[0] == "500mA" || pro[0] == "600mA" || pro[0] == "800mA")
{
if (pro[0] == "50mA")
{
value_list.Add("050mA");
}
else
{
value_list.Add(pro[0]);
}
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "背景资料的" + item + "格式不正确,档位值不在指定范围内(50mA、100mA、200mA、300mA、400mA、500mA、600mA、800mA)"));
}
}
if (item == "411开关(保护器)过载保护:")
{
if (pro[0] == "160A" || pro[0] == "200A" || pro[0] == "240A" || pro[0] == "280A" || pro[0] == "320A" || pro[0] == "360A" || pro[0] == "400A")
{
if (pro[0] == "160A")
{
value_list.Add("0.4ln");
}
else if (pro[0] == "200A")
{
value_list.Add("0.5ln");
}
else if (pro[0] == "240A")
{
value_list.Add("0.6ln");
}
else if (pro[0] == "280A")
{
value_list.Add("0.7ln");
}
else if (pro[0] == "320A")
{
value_list.Add("0.8ln");
}
else if (pro[0] == "360A")
{
value_list.Add("0.9ln");
}
else if (pro[0] == "400A")
{
value_list.Add("1.0ln");
}
else
{
value_list.Add(pro[0]);
}
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "背景资料的" + item + "格式不正确,档位值不在指定范围内(160A、200A、240A、280A、320A、360A、400A)"));
}
}
if (item == "411开关(保护器)短路延时保护:")
{
Dictionary delay_protection = new Dictionary();
delay_protection.Add("800A", "2ln");
delay_protection.Add("1000A", "2.5ln");
delay_protection.Add("1200A", "3ln");
delay_protection.Add("1600A", "4ln");
delay_protection.Add("2000A", "5ln");
delay_protection.Add("2400A", "6ln");
delay_protection.Add("2800A", "7ln");
delay_protection.Add("3400A", "8ln");
delay_protection.Add("4000A", "10ln");
if (delay_protection.ContainsKey(pro[0]))
{
value_list.Add(delay_protection[pro[0]]);
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "背景资料的" + item + "格式不正确,档位值不在指定范围内(" + string.Join("、", delay_protection.Keys.ToArray()) + ")"));
}
}
if (item == "411开关(保护器)短路瞬时保护:")
{
Dictionary delay_protection = new Dictionary();
delay_protection.Add("2400A", "6ln");
delay_protection.Add("2800A", "7ln");
delay_protection.Add("3200A", "8ln");
delay_protection.Add("4000A", "10ln");
delay_protection.Add("4400A", "11ln");
delay_protection.Add("4800A", "12ln");
delay_protection.Add("5200A", "13ln");
delay_protection.Add("5600A", "14ln");
if (delay_protection.ContainsKey(pro[0]))
{
value_list.Add(delay_protection[pro[0]]);
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "背景资料的" + item + "格式不正确,档位值不在指定范围内(" + string.Join("、", delay_protection.Keys.ToArray()) + ")"));
}
}
if (item == "411开关(保护器)欠压保护:")
{
Dictionary delay_protection = new Dictionary();
delay_protection.Add("170V", "170");
delay_protection.Add("175V", "175");
delay_protection.Add("180V", "180");
delay_protection.Add("185V", "185");
delay_protection.Add("190V", "190");
delay_protection.Add("195V", "195");
delay_protection.Add("200V", "200");
if (delay_protection.ContainsKey(pro[0]))
{
value_list.Add(delay_protection[pro[0]]);
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "背景资料的" + item + "格式不正确,档位值不在指定范围内(" + string.Join("、", delay_protection.Keys.ToArray()) + ")"));
}
}
if (item == "411开关(保护器)过压保护:")
{
Dictionary delay_protection = new Dictionary();
delay_protection.Add("255V", "255");
delay_protection.Add("260V", "260");
delay_protection.Add("265V", "265");
delay_protection.Add("270V", "270");
delay_protection.Add("275V", "275");
delay_protection.Add("280V", "280");
delay_protection.Add("285V", "285");
delay_protection.Add("290V", "290");
if (delay_protection.ContainsKey(pro[0]))
{
value_list.Add(delay_protection[pro[0]]);
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "背景资料的" + item + "格式不正确,档位值不在指定范围内(" + string.Join("、", delay_protection.Keys.ToArray()) + ")"));
}
}
pro[1] = pro[1].Replace(")", "");
if (pro[1] == "开" || pro[1] == "关")
{
value_list.Add(pro[1]);
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "背景资料的" + item + "格式不正确,()里的值不是开或者关"));
}
if (item == "411开关(保护器)额定剩余电流保护:")
{
leakage_protection = string.Join("|", value_list.ToArray());
}
if (item == "411开关(保护器)过载保护:")
{
overload_protection = string.Join("|", value_list.ToArray());
}
if (item == "411开关(保护器)短路延时保护:")
{
short_circuit_delay_protection = string.Join("|", value_list.ToArray());
}
if (item == "411开关(保护器)短路瞬时保护:")
{
short_circuit_protection = string.Join("|", value_list.ToArray());
}
if (item == "411开关(保护器)欠压保护:")
{
undervoltage_protection = string.Join("|", value_list.ToArray());
}
if (item == "411开关(保护器)过压保护:")
{
overvoltage_protection = string.Join("|", value_list.ToArray());
}
}
else
{
if (item == "411开关(保护器)缺相保护:")
{
if (value[1] == "开" || value[1] == "关")
{
phase_loss_protection = value[1];
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "背景资料的" + item + "格式不正确,开关值不是开或者关"));
}
}
else
{
var pro = value[1];
if (value[1] == "100A" || value[1] == "200A" || value[1] == "400A" || value[1] == "800A")
{
fusible_core_current = value[1];
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "背景资料的" + item + "格式不正确,电流值不在指定范围内:100A、200A、400A、800A"));
}
}
}
}
}
}
if (exam_bll.GetRecordCount(string.Format(" ExamName = '{0}' and Type='{1}' and ExamId != '{2}' ", req.ExamName, req.Type, req.ExamId)) > 0)
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "考试名称禁止重复!"));
}
var model = exam_bll.GetModel(req.ExamId);
model.ExamName = req.ExamName;
if (!string.IsNullOrEmpty(req.StartExamTime))
{
model.StartExamTime = DateTime.Parse(req.StartExamTime);
}
if (!string.IsNullOrEmpty(req.EndExamTime))
{
model.EndExamTime = DateTime.Parse(req.EndExamTime);
}
model.Synopsis = req.Synopsis;
var old_cover_path = model.CoverPath;
model.CoverName = req.CoverName;
model.CoverPath = req.CoverPath;
model.WorkOrderContent = req.WorkOrderContent;
model.BackgroundInformation = req.BackgroundInformation;
model.SceneId = req.SceneId;
model.PlatformAreaId = req.PlatformAreaId;
model.LineId = req.LineId;
model.LinePatrolDate = DateTime.Parse(req.LinePatrolDate);
model.HandleDate = DateTime.Parse(req.HandleDate);
model.IncomingLineModelLength = req.IncomingLineModelLength;
model.OutgoingLineModelLength = req.OutgoingLineModelLength;
model.ExaminationDuration = req.ExaminationDuration;
model.LeakageProtection = leakage_protection;
model.OverloadProtection = overload_protection;
model.ShortCircuitDelayProtection = short_circuit_delay_protection;
model.ShortCircuitProtection = short_circuit_protection;
model.UndervoltageProtection = undervoltage_protection;
model.OvervoltageProtection = overvoltage_protection;
model.PhaseLossProtection = phase_loss_protection;
model.FusibleCoreCurrent = fusible_core_current;
model.Weather = req.Weather;
var fault_list = exam_fault_bll.GetModelList(" ChoiceQuestion='0' and Type='断路器' or FaultDesc='熔芯不匹配' and ExamId='" + req.ExamId + "' ");
foreach (var item in fault_list)
{
if (item.FaultDesc == "漏电保护设置不合理")
{
item.Value = leakage_protection;
}
else if (item.FaultDesc == "过载保护设置错误")
{
var old_value = item.Value.Split('|');
var new_value = overload_protection.Split('|');
old_value[0] = new_value[0];
old_value[1] = new_value[1];
item.Value = String.Join("|", old_value);
}
else if (item.FaultDesc == "短路延时保护设置错误")
{
var old_value = item.Value.Split('|');
var new_value = short_circuit_delay_protection.Split('|');
old_value[0] = new_value[0];
old_value[1] = new_value[1];
item.Value = String.Join("|", old_value);
}
else if (item.FaultDesc == "短路瞬时保护设置错误")
{
item.Value = short_circuit_protection;
}
else if (item.FaultDesc == "欠压保护设置错误")
{
item.Value = undervoltage_protection;
}
else if (item.FaultDesc == "过压保护设置错误")
{
item.Value = overvoltage_protection;
}
else if (item.FaultDesc == "缺相保护设置错误")
{
item.Value = phase_loss_protection;
}
else if (item.FaultDesc == "熔芯不匹配")
{
if (fusible_core_current == item.FusibleCoreFault)
{
if (fusible_core_current == "800A")
{
item.FusibleCoreFault = "100A";
}
if (fusible_core_current == "100A")
{
item.FusibleCoreFault = "200A";
}
if (fusible_core_current == "200A")
{
item.FusibleCoreFault = "400A";
}
if (fusible_core_current == "400A")
{
item.FusibleCoreFault = "800A";
}
}
item.FusibleCoreNormal = fusible_core_current;
}
}
if (exam_bll.OperationUpdateExamData(model, fault_list) > 0)
{
if (!string.IsNullOrEmpty(old_cover_path))
{
if (req.CoverPath != old_cover_path)
{
if (System.IO.File.Exists(webRootPath + "/" + old_cover_path))
{
var array = old_cover_path.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 + "/" + old_cover_path);
}
}
}
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, "发生错误,请联系管理员。"));
}
}
}
}