mh_jy_safe/APT.MicroApi/APT.SC.WebApi/Controllers/Api/SEController/SETestController.cs

642 lines
27 KiB
C#
Raw Normal View History

2025-08-25 09:56:57 +08:00
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.SE;
using APT.MS.Domain.Enums;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using APT.Infrastructure.Core;
using APT.BaseData.Domain.ApiModel;
using System.Data;
using System.IO;
using APT.BaseData.Domain.Entities.FM;
using Castle.Core.Internal;
using static APT.SC.WebApi.Controllers.Api.BI.BIController;
using Microsoft.AspNetCore.DataProtection;
namespace APT.SE.WebApi.Controllers.Api
{
[Route("api/SE/SETest")]
public partial class SETestController : AuthorizeApiController<T_SE_TEST>
{
/// <summary>
/// 查询
/// </summary>
/// <param name="pageFilter"></param>
/// <returns></returns>
[HttpPost, Route("OrderPaged")]
public PagedActionResult<T_SE_TEST> OrderPaged([FromBody] KeywordPageFilter pageFilter)
{
string TRAINNAME = null;//培训名称
string NAME = null;//题目
SETestTypeEnum? TYPE = null;//题型
string PointNAME = null;//知识点
Guid? POINT_ID = null;//知识点
string POST_NAME = null;
List<int> listUsAge = null;
if (pageFilter.FilterGroup.Rules != null && pageFilter.FilterGroup.Rules.Count() > 0)
{
foreach (var item in pageFilter.FilterGroup.Rules)
{
switch (item.Field)
{
case "TRAINNAME":
TRAINNAME = item.Value.ToString();
break;
case "USAGE"://1,2,4
if (item.Value.ToString() == "1")
{
listUsAge = new List<int>() { 1, 3, 5, 7 };
}
else if (item.Value.ToString() == "2")
{
listUsAge = new List<int>() { 2, 3, 6, 7 };
}
else if (item.Value.ToString() == "4")
{
listUsAge = new List<int>() { 4, 5, 6, 7 };
}
break;
case "NAME":
NAME = item.Value.ToString();
break;
case "TYPE":
TYPE = (SETestTypeEnum)int.Parse(item.Value.ToString());
break;
case "Nav_Points.Nav_Point.NAME":
PointNAME = item.Value.ToString();
break;
case "Nav_Posts.Nav_Post.NAME":
POST_NAME = item.Value.ToString();
break;
default:
break;
}
}
//if (POINT_ID != null)
//{
// pageFilter.FilterGroup.Groups = new List<FilterGroup>();
//}
//pageFilter.FilterGroup.Rules = new List<FilterRule>();
}
if (pageFilter.FilterGroup.Groups != null && pageFilter.FilterGroup.Groups.Any())
{
foreach (var item in pageFilter.FilterGroup.Groups)
{
if (item.Rules.Any())
{
foreach (var itemG in item.Rules)
{
if (itemG.Field == "Nav_Points.POINT_ID")
{
POINT_ID = new Guid(itemG.Value.ToString());
break;
}
}
}
}
}
pageFilter.FilterGroup = new FilterGroup();
//string POST_NAME = "";
//string POINT_NAME = "";
//if (pageFilter.Parameter1 != null)
//{
// POST_NAME = (string)pageFilter.Parameter1;
//}
//if (pageFilter.Parameter2 != null)
//{
// POINT_NAME = (string)pageFilter.Parameter2;
//}
PagedActionResult<T_SE_TEST> ret = WitOrderPaged(t => (POST_NAME == null ||
(t.Nav_Posts.Any(it => it.Nav_Post.NAME.Contains(POST_NAME)))) &&
(PointNAME == null ||
(t.Nav_Points.Any(it => it.Nav_Point.NAME.Contains(PointNAME)))) &&
(POINT_ID == null ||
(t.Nav_Points.Any(it => it.POINT_ID.HasValue && it.POINT_ID.Value == POINT_ID))) && (
TRAINNAME == null || t.TRAINNAME.Contains(TRAINNAME)) && (NAME == null || t.NAME.Contains(NAME)) && (TYPE == null || t.TYPE == TYPE) && (listUsAge == null || listUsAge.Contains(t.USAGE))
, pageFilter);
if (ret == null || ret.Data == null || ret.Data.Count() < 1)
{
ret.Data = new List<T_SE_TEST>();
return ret;
}
var ids = ret.Data.Select(t => t.ID);
BaseFilter filter = new BaseFilter(pageFilter.GetOrgId());
filter.Include = new string[] { "Nav_Point" };
var points = GetEntities<T_SE_TEST_LIST_POINT>(t => ids.Contains(t.TEST_ID), filter);
foreach (var item in ret.Data)
{
var point = points.Where(t => t.TEST_ID == item.ID).Select(t => t.Nav_Point.NAME);
var pointName = string.Join("", point);
item.OPTION_A = pointName;
}
return ret;
}
public class notifyName
{
public string NAME { get; set; }
}
/// <summary>
/// 查询培训名称
/// </summary>
/// <param name="pageFilter"></param>
/// <returns></returns>
[HttpPost, Route("getNotifyNames")]
public JsonActionResult<List<notifyName>> getNotifyNames([FromBody] KeywordPageFilter pageFilter)
{
return SafeExecute<List<notifyName>>(() =>
{
BaseFilter filter = new BaseFilter(pageFilter.GetOrgId());
filter.SelectField = new string[] { "NAME" };
DataOrder order1 = new DataOrder();
order1.Field = "NAME";
order1.Order = DbOrder.ASC;
filter.Orders.Add(order1);
var notifys = GetEntities<T_SE_TRAIN_NOTIFY>(t => t.IS_DELETED == false, filter).DistinctBy(t => t.NAME);
List<notifyName> ret = new List<notifyName>();
foreach (var notify in notifys)
{
notifyName notifyName = new notifyName();
notifyName.NAME = notify.NAME;
ret.Add(notifyName);
}
ret = ret.OrderBy(t => t.NAME).ToList();
return ret;
});
}
/// <summary>
/// 保存
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost, Route("FullUpdate")]
public JsonActionResult<bool> FullUpdate([FromBody] T_SE_TEST entity)
{
return SafeExecute(() =>
{
if (entity.ID == null || entity.ID == Guid.Empty)
{
entity.ID = Guid.NewGuid();
}
if (string.IsNullOrEmpty(entity.NAME))
{
throw new Exception("题目不可为空");
}
if (entity.TYPE is SETestTypeEnum. or SETestTypeEnum.)
{
if ((entity.ANSWER) == 0)
{
throw new Exception("答案不可为空");
}
if (string.IsNullOrEmpty(entity.OPTION_A) ||
string.IsNullOrEmpty(entity.OPTION_B) ||
string.IsNullOrEmpty(entity.OPTION_C) ||
string.IsNullOrEmpty(entity.OPTION_D)
)
{
throw new Exception("选择题选项不可为空");
}
if (entity.TYPE == SETestTypeEnum. &&
entity.ANSWER != 1 && entity.ANSWER != 2 && entity.ANSWER != 4 && entity.ANSWER != 8 && entity.ANSWER != 16)
{
throw new Exception("单选题只能有一个正确选项");
}
if (entity.TYPE == SETestTypeEnum. && (entity.ANSWER < 3 || entity.ANSWER == 4 || entity.ANSWER == 8 || entity.ANSWER == 16))
{
throw new Exception("多选题至少有两个正确选项");
}
}
if (entity.TYPE == SETestTypeEnum.)
{
if (entity.ANSWER != 1 && entity.ANSWER != 2)
{
throw new Exception("是非题答案只能为是或否");
}
}
var points = entity.Nav_Points;
entity.Nav_Points = null;
if (points == null || points.Count == 0)
{
throw new Exception("知识点不可为空");
}
points.ForEach(t =>
{
t.ORG_ID = entity.ORG_ID;
t.TEST_ID = entity.ID;
});
var posts = entity.Nav_Posts;
entity.Nav_Posts = null;
if (posts != null)
{
posts.ForEach(t =>
{
t.TEST_ID = entity.ID;
t.ORG_ID = entity.ORG_ID;
});
}
this.UnifiedCommit(() =>
{
UpdateEntityNoCommit(entity);
if (posts != null)
BantchSaveEntityNoCommit(posts);
if (points != null)
BantchSaveEntityNoCommit(points);
});
return true;
});
}
/// <summary>
/// 获取导入数据
/// 参考 [Route("api/PF/Import")]
/// </summary>
/// <returns></returns>
[HttpPost, Route("GetImportData")]
public JsonActionResult<ImportDataModel> GetImportData()
{
return SafeExecute<ImportDataModel>(() =>
{
var orgId = APT.Infrastructure.Api.AppContext.CurrentSession.OrgId;
var httpRequest = this.HttpContext.Request;
string orgIdStr = httpRequest.Form["OrgId"];// 获取 组织
ImportDataModel result = new ImportDataModel();
var dic = Path.Combine(System.AppContext.BaseDirectory, "tempImportFiles");
if (!Directory.Exists(dic))
Directory.CreateDirectory(dic);
foreach (var key in httpRequest.Form.Files) // 文件键
{
var postedFile = key; // 获取文件键对应的文件对象
string filePath = Path.Combine(dic, DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + "_" + postedFile.FileName);
Byte[] fileData = new Byte[postedFile.Length];
Stream sr = postedFile.OpenReadStream();//创建数据流对象
sr.Read(fileData, 0, (int)postedFile.Length);
using (FileStream fs = new FileStream(filePath, FileMode.CreateNew))
{
fs.Write(fileData, 0, fileData.Length);
fs.Flush();
fs.Close();
}
//获取数据
Dictionary<int, int> startRowIndexs = new Dictionary<int, int>();
startRowIndexs.Add(0, 1);//根据Excel格式数据赋值
var dataTables = FileUtils.ReadExcelByOledb(filePath, startRowIndexs);
string Msg = string.Empty;
bool isOK = InsertModel(dataTables.Tables[0], orgId, ref Msg);
try
{
System.IO.File.Delete(filePath);
}
catch { }
result.Data = Msg;
result.MessageList = new List<string> { Msg };
}
return result;
});
}
/// <summary>
/// 数据插入
/// </summary>
/// <param name="dtSource"></param>
/// <param name="OrgID"></param>
/// <param name="Msg"></param>
/// <param name="rowIndex"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public bool InsertModel(DataTable dtSource, Guid? orgId, ref string Msg, int rowIndex = 1)
{
var userId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
if (dtSource == null || dtSource.Rows.Count < rowIndex)
{
Msg = "未获取到导入数据";
throw new Exception(Msg);
}
List<int> listNotEmpty = new List<int>() { 0, 1, 7, 8, 10 };
Dictionary<int, List<int>> dicIndexEmpty = new Dictionary<int, List<int>>();
int rowAll = dtSource.Rows.Count;
int colAll = dtSource.Columns.Count;
List<T_SE_TEST> mainTables = new List<T_SE_TEST>();
List<T_SE_TEST_LIST_POST> sub1s = new List<T_SE_TEST_LIST_POST>();
List<T_SE_TEST_LIST_POINT> sub2s = new List<T_SE_TEST_LIST_POINT>();
var posts = GetEntities<T_FM_USER_POST>(t => t.IS_DELETED == false, new BaseFilter(orgId));
var points = GetEntities<T_SE_TEST_ENUM_POINT>(t => t.IS_DELETED == false, new BaseFilter(orgId));
T_FM_USER_POST modelPost = null;
List<string> listPostNameEmpty = new List<string>();
2025-10-27 09:31:27 +08:00
List<int> listAnswerErrorIndex = new List<int>();
List<string> listAnswerError = new List<string>();
string strANSWER = string.Empty;
2025-08-25 09:56:57 +08:00
T_SE_TEST_ENUM_POINT modelPoint = null;
List<string> listPointNameEmpty = new List<string>();
DateTime dtNow = DateTime.Now;
for (int i = 0; i < rowAll; i++)
{
#region
List<int> listi = null;
for (int j = 0; j < colAll; j++)
{
if (!listNotEmpty.Contains(j))
continue;
if (string.IsNullOrEmpty(dtSource.Rows[i][j].ToString().Trim()))
{
if (listi == null)
{
listi = new List<int>();
}
listi.Add(j);
}
}
if (listi != null)
dicIndexEmpty.Add(i + rowIndex, listi);//有空的 直接添加
#endregion
if (dicIndexEmpty.Count < 1)
{
T_SE_TEST mainTable = new T_SE_TEST();
mainTable.ID = Guid.NewGuid();
mainTable.ORG_ID = orgId;
var testName = dtSource.Rows[i][0].ToString().Trim();
if (!string.IsNullOrEmpty(testName))
{
mainTable.NAME = testName;
}
else
{
continue;
}
var testType = dtSource.Rows[i][1].ToString().Trim();
if (!string.IsNullOrEmpty(testType))
{
if (dtSource.Rows[i][1].ToString().Trim() == "是非题")
{
mainTable.TYPE = SETestTypeEnum.;
}
else if (dtSource.Rows[i][1].ToString().Trim() == "单选题")
{
mainTable.TYPE = SETestTypeEnum.;
}
else if (dtSource.Rows[i][1].ToString().Trim() == "多选题")
{
mainTable.TYPE = SETestTypeEnum.;
}
else
{
throw new Exception("没有《" + testType + "》类型");
}
}
else
{
throw new Exception("题型不能为空!");
}
if (!string.IsNullOrEmpty(dtSource.Rows[i][2].ToString().Trim()))
{
mainTable.OPTION_A = (dtSource.Rows[i][2].ToString().Trim());
}
if (!string.IsNullOrEmpty(dtSource.Rows[i][3].ToString().Trim()))
{
mainTable.OPTION_B = (dtSource.Rows[i][3].ToString().Trim());
}
if (!string.IsNullOrEmpty(dtSource.Rows[i][4].ToString().Trim()))
{
mainTable.OPTION_C = (dtSource.Rows[i][4].ToString().Trim());
}
if (!string.IsNullOrEmpty(dtSource.Rows[i][5].ToString().Trim()))
{
mainTable.OPTION_D = (dtSource.Rows[i][5].ToString().Trim());
}
if (!string.IsNullOrEmpty(dtSource.Rows[i][6].ToString().Trim()))
{
mainTable.OPTION_E = (dtSource.Rows[i][6].ToString().Trim());
}
2025-10-27 09:31:27 +08:00
strANSWER = dtSource.Rows[i][7].ToString().Trim();
if (!string.IsNullOrEmpty(strANSWER))
2025-08-25 09:56:57 +08:00
{
if (mainTable.TYPE == SETestTypeEnum.)
{
2025-10-27 09:31:27 +08:00
if (strANSWER == "是")
2025-08-25 09:56:57 +08:00
{
mainTable.ANSWER = 1;
}
2025-10-27 09:31:27 +08:00
else if (strANSWER == "否")
2025-08-25 09:56:57 +08:00
{
mainTable.ANSWER = 2;
}
2025-10-27 09:31:27 +08:00
else
{
if (listAnswerError.Contains(strANSWER))
{
listAnswerError.Add(strANSWER);
}
listAnswerErrorIndex.Add(i + rowIndex);
}
2025-08-25 09:56:57 +08:00
}
if (mainTable.TYPE == SETestTypeEnum.)
{
2025-10-27 09:31:27 +08:00
if (strANSWER == "A")
2025-08-25 09:56:57 +08:00
{
mainTable.ANSWER = 1;
}
2025-10-27 09:31:27 +08:00
else if (strANSWER == "B")
2025-08-25 09:56:57 +08:00
{
mainTable.ANSWER = 2;
}
2025-10-27 09:31:27 +08:00
else if (strANSWER == "C")
2025-08-25 09:56:57 +08:00
{
mainTable.ANSWER = 4;
}
2025-10-27 09:31:27 +08:00
else if (strANSWER == "D")
2025-08-25 09:56:57 +08:00
{
mainTable.ANSWER = 8;
}
2025-10-27 09:31:27 +08:00
else
{
if (listAnswerError.Contains(strANSWER))
{
listAnswerError.Add(strANSWER);
}
listAnswerErrorIndex.Add(i + rowIndex);
}
2025-08-25 09:56:57 +08:00
}
if (mainTable.TYPE == SETestTypeEnum.)
{
int answer = 0;
2025-10-27 09:31:27 +08:00
if (strANSWER.Contains("A"))
2025-08-25 09:56:57 +08:00
{
answer = answer + 1;
}
2025-10-27 09:31:27 +08:00
if (strANSWER.Contains("B"))
2025-08-25 09:56:57 +08:00
{
answer = answer + 2;
}
2025-10-27 09:31:27 +08:00
if (strANSWER.Contains("C"))
2025-08-25 09:56:57 +08:00
{
answer = answer + 4;
}
2025-10-27 09:31:27 +08:00
if (strANSWER.Contains("D"))
2025-08-25 09:56:57 +08:00
{
answer = answer + 8;
}
2025-10-27 09:31:27 +08:00
if (strANSWER.Contains("E"))
2025-08-25 09:56:57 +08:00
{
answer = answer + 16;
}
mainTable.ANSWER = answer;
}
}
2025-10-27 09:31:27 +08:00
else
{
if (listAnswerError.Contains(strANSWER))
{
listAnswerError.Add(strANSWER);
}
listAnswerErrorIndex.Add(i + rowIndex);
}
2025-08-25 09:56:57 +08:00
if (!string.IsNullOrEmpty(dtSource.Rows[i][8].ToString().Trim()))
{
int k = 0;
if (dtSource.Rows[i][8].ToString().Contains("安全意识调查"))
{
k = k + 1;
}
if (dtSource.Rows[i][8].ToString().Contains("培训"))
{
k = k + 2;
}
if (dtSource.Rows[i][8].ToString().Contains("事故事件"))
{
k = k + 4;
}
mainTable.USAGE = k;
}
if (!string.IsNullOrEmpty(dtSource.Rows[i][9].ToString().Trim()))
{
var testPosts = dtSource.Rows[i][9].ToString().Trim().Split(';');
foreach (var postName in testPosts)
{
if (!string.IsNullOrEmpty(postName))
{
modelPost = posts.FirstOrDefault(t => t.NAME == postName);
if (modelPost == null)
{
if (!listPostNameEmpty.Contains(postName))
listPostNameEmpty.Add(postName);
}
else
{
T_SE_TEST_LIST_POST sub1 = new T_SE_TEST_LIST_POST
{
ID = Guid.NewGuid(),
ORG_ID = orgId,
TEST_ID = mainTable.ID,
POST_ID = posts.FirstOrDefault(t => t.NAME == postName).ID
};
sub1s.Add(sub1);
}
}
}
}
if (!string.IsNullOrEmpty(dtSource.Rows[i][10].ToString().Trim()))
{
var testPoints = dtSource.Rows[i][10].ToString().Trim().Split(';');
foreach (var pointName in testPoints)
{
if (!string.IsNullOrEmpty(pointName))
{
modelPoint = points.FirstOrDefault(t => t.NAME == pointName);
if (modelPoint != null)
{
T_SE_TEST_LIST_POINT sub2 = new T_SE_TEST_LIST_POINT
{
ID = Guid.NewGuid(),
ORG_ID = orgId,
TEST_ID = mainTable.ID,
POINT_ID = modelPoint.ID,
};
sub2s.Add(sub2);
}
else
{
if (!listPointNameEmpty.Contains(pointName))
listPointNameEmpty.Add(pointName);
}
}
}
}
if (!string.IsNullOrEmpty(dtSource.Rows[i][11].ToString().Trim()))
{
mainTable.TRAINNAME = dtSource.Rows[i][11].ToString().Trim();
}
mainTables.Add(mainTable);
}
}
#region
string strEmptyError = string.Empty;
2025-10-27 09:31:27 +08:00
if (listPostNameEmpty.Count > 0 || dicIndexEmpty.Count > 0 || listPointNameEmpty.Count > 0)
2025-08-25 09:56:57 +08:00
{
if (dicIndexEmpty.Count > 0)
{
int colIndex = 0;
foreach (var item in dicIndexEmpty)
{
strEmptyError += "行:" + item.Key + ",列:";
colIndex = 0;
foreach (var itemVal in item.Value)
{
strEmptyError += (colIndex > 0 ? "、" : "") + itemVal;
colIndex++;
}
strEmptyError += "";
}
strEmptyError = strEmptyError.Substring(0, strEmptyError.Length - 1) + "不能为空!";
}
if (listPostNameEmpty.Count > 0)
{
throw new Exception("岗位未找到:" + string.Join(",", listPostNameEmpty));
}
if (listPointNameEmpty.Count > 0)
{
throw new Exception("知识点未找到:" + string.Join(",", listPointNameEmpty));
}
2025-10-27 09:31:27 +08:00
if (listAnswerErrorIndex.Count > 0)
{
throw new Exception("答案有误,行:【" + string.Join("," + listAnswerErrorIndex) + "】值:" + string.Join(",", listAnswerError));
}
2025-08-25 09:56:57 +08:00
}
if (strEmptyError != string.Empty)
throw new Exception(strEmptyError);
#endregion
UnifiedCommit(() =>
{
if (mainTables != null && mainTables.Any())
BantchAddEntityNoCommit(mainTables);
if (sub1s != null && sub1s.Any())
BantchAddEntityNoCommit(sub1s);
if (sub2s != null && sub2s.Any())
BantchAddEntityNoCommit(sub2s);
});
Msg = "导入成功!";
return true;
}
}
}