CompetitionAPI_dotnet/CompetitionAPI/Controllers/back/AddExamController.cs

629 lines
37 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Competition.Common.Util;
using CompetitionAPI.api.back;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using CompetitionAPI.Util;
namespace CompetitionAPI.Controllers.back
{
[Route("api/[controller]")]
[ApiController]
public class AddExamController : Controller
{
private readonly IWebHostEnvironment _webHostEnvironment;
Competition.Mysql.BLL.pow_exam exam_bll = new Competition.Mysql.BLL.pow_exam();
Competition.Mysql.BLL.pow_operation_ticket ticket_bll = new Competition.Mysql.BLL.pow_operation_ticket();
Competition.Mysql.BLL.pow_fault fault_bll = new Competition.Mysql.BLL.pow_fault();
Competition.Mysql.BLL.pow_tool tool_bll = new Competition.Mysql.BLL.pow_tool();
private static Object Lockobj = new object();
public AddExamController(IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment = webHostEnvironment;
}
/// <summary>
/// 新增考试接口
/// </summary>
/// <param name="req">请求参数</param>
/// <returns></returns>
[Authorize]
[HttpPost]
[APIFilter]
public JsonResult Index([FromBody] AddExamRequest req)
{
try
{
lock (Lockobj)
{
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.Type))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "类型不能为空"));
}
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, "实训封面不能为空"));
}
//获取当前web目录
var webRootPath = _webHostEnvironment.WebRootPath;
if (!System.IO.File.Exists(webRootPath + "/" + req.CoverPath))
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "封面不存在,请先上传封面"));
}
}
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.IsNullOrEmpty(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<string>();
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<string>();
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<string, string> delay_protection = new Dictionary<string, string>();
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<string, string> delay_protection = new Dictionary<string, string>();
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<string, string> delay_protection = new Dictionary<string, string>();
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<string, string> delay_protection = new Dictionary<string, string>();
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}' ", req.ExamName, req.Type)) > 0)
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "考试名称禁止重复"));
}
var model = new Competition.Mysql.Model.pow_exam();
model.ExamId = Guid.NewGuid().ToString("N");
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.Status = "未发布";
model.Synopsis = req.Synopsis;
model.CoverName = req.CoverName;
model.CoverPath = req.CoverPath;
model.CreateTime = DateTime.Now;
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.Type = req.Type;
model.Weather = req.Weather;
var ticket_list = ticket_bll.GetModelList("");
foreach (var item in ticket_list)
{
item.Content = item.Content.Replace("xxxxx", req.PlatformArea);
}
var tool_list = tool_bll.GetModelList(" Purpose='巡线' or ToolName='绝缘手套' or ToolName='熔断器操作手柄' ");
var fault_list = fault_bll.GetModelList("");
var new_fault_list = fault_list.Where(a => a.Type == "断路器" || a.FaultDesc == "熔芯不匹配").ToList();
foreach (var item in new_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;
}
}
var cable_fault_list = fault_list.Where(a => a.FaultDesc == "出线电缆编号牌型号编写错误").ToList();
if (cable_fault_list.Count > 0)
{
foreach (var item in cable_fault_list)
{
var name1 = "1#配变0.4kV411东线出线电缆";
var start1 = "1#配变低压综合配电箱411熔断器";
var end1 = "1#配变0.4kV411东线001#杆";
var name2 = "1#配变0.4kV412南线出线电缆";
var start2 = "1#配变低压综合配电箱412熔断器";
var end2 = "1#配变0.4kV412南线001#杆";
//411
if (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c")
{
item.CableName = req.PlatformArea + name1;
item.CableStart = req.PlatformArea + start1;
item.CableEnd = req.PlatformArea + end1;
item.ModelLength = req.IncomingLineModelLength;
}
//412
if (item.DeviceId == "a238da230c0f11ed844e7cd30a92bb1c")
{
item.CableName = req.PlatformArea + name2;
item.CableStart = req.PlatformArea + start2;
item.CableEnd = req.PlatformArea + end2;
item.ModelLength = req.OutgoingLineModelLength;
}
var success = new List<string>();
var dic_name = "/Upload/Cable/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
var dir_path = _webHostEnvironment.WebRootPath + dic_name;
if (!Directory.Exists(dir_path))
Directory.CreateDirectory(dir_path);
var path1 = dic_name + Guid.NewGuid().ToString("N") + ".jpg";
var result1 = Tool.GeneratePictures("台区一" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? name1 : name2), "台区一" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? start1 : start2), "台区一" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? end1 : end2), (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? req.IncomingLineModelLength : req.OutgoingLineModelLength), path1, _webHostEnvironment.WebRootPath);
if (result1)
{
item.SensitiveKnowledge = path1;
success.Add(path1);
}
var path2 = dic_name + Guid.NewGuid().ToString("N") + ".jpg";
var result2 = Tool.GeneratePictures("台区二" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? name1 : name2), "台区二" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? start1 : start2), "台区二" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? end1 : end2), (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? req.IncomingLineModelLength : req.OutgoingLineModelLength), path2, _webHostEnvironment.WebRootPath);
if (result2)
{
item.Minhui = path2;
success.Add(path2);
}
var path3 = dic_name + Guid.NewGuid().ToString("N") + ".jpg";
var result3 = Tool.GeneratePictures("台区三" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? name1 : name2), "台区三" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? start1 : start2), "台区三" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? end1 : end2), (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? req.IncomingLineModelLength : req.OutgoingLineModelLength), path3, _webHostEnvironment.WebRootPath);
if (result3)
{
item.SensitiveLine = path3;
success.Add(path3);
}
var path4 = dic_name + Guid.NewGuid().ToString("N") + ".jpg";
var result4 = Tool.GeneratePictures("台区四" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? name1 : name2), "台区四" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? start1 : start2), "台区四" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? end1 : end2), (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? req.IncomingLineModelLength : req.OutgoingLineModelLength), path4, _webHostEnvironment.WebRootPath);
if (result4)
{
item.Allergy = path4;
success.Add(path4);
}
var path5 = dic_name + Guid.NewGuid().ToString("N") + ".jpg";
var result5 = Tool.GeneratePictures("台区五" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? name1 : name2), "台区五" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? start1 : start2), "台区五" + (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? end1 : end2), (item.DeviceId == "9d8da9360c0f11ed844e7cd30a92bb1c" ? req.IncomingLineModelLength : req.OutgoingLineModelLength), path5, _webHostEnvironment.WebRootPath);
if (result5)
{
item.GracefulGraceful = path5;
success.Add(path5);
}
if (success.Count != 5)
{
foreach (var pic in success)
{
string path = _webHostEnvironment.WebRootPath + pic;
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
}
return Json(Tool.GetJsonWithCode(APICode.Fail, "添加失败"));
}
}
}
if (exam_bll.OperationAddData(model, ticket_list, fault_list, tool_list) > 0)
{
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, "发生错误,请联系管理员。"));
}
}
}
}