配合前端对接题库导入excel接口

master
xiaochanghai 1 year ago
parent 459a9f72fa
commit 44c1e4ac1f
  1. 4
      Tiobon.Core.Api/Controllers/Base/BaseController.cs
  2. BIN
      Tiobon.Core.Api/wwwroot/files/ExcelTemplate/Ghre_Question.xlsx
  3. 2
      Tiobon.Core.Common/Helper/NPOIHelper.cs
  4. 4
      Tiobon.Core.Services/CommonServices.cs
  5. 95
      Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs

@ -223,9 +223,9 @@ public class BaseController<IServiceBase, TEntity, TEntityDto, TInsertDto, TEdit
#region Excel导入 #region Excel导入
[HttpPost("ImportExcel")] [HttpPost("ImportExcel")]
public async Task<ServiceResult<string>> ImportExcel(IFormFile file) public async Task<ServiceResult<ExcelData>> ImportExcel(IFormFile file)
{ {
var data = (await InvokeServiceAsync("ImportExcel", [file])) as ServiceResult<string>; var data = (await InvokeServiceAsync("ImportExcel", [file])) as ServiceResult<ExcelData>;
return data; return data;
} }
#endregion #endregion

@ -285,7 +285,7 @@ public class NPOIHelper
for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
{ {
IRow row = sheet.GetRow(i); IRow row = sheet.GetRow(i);
if (row.GetCell(row.FirstCellNum) != null && row.GetCell(row.FirstCellNum).ToString().Length > 0) //if (row.GetCell(row.FirstCellNum) != null && row.GetCell(row.FirstCellNum).ToString().Length > 0)
//if (row.GetCell(row.FirstCellNum) != null) //if (row.GetCell(row.FirstCellNum) != null)
{ {
DataRow dataRow = dt.NewRow(); DataRow dataRow = dt.NewRow();

@ -851,8 +851,8 @@ public partial class CommonServices : BaseServices<RootEntityTkey<int>>, ICommon
result.DT_Procedure.IUDProcedure = dt.Rows[0]["IUDProcedure"].ToString(); result.DT_Procedure.IUDProcedure = dt.Rows[0]["IUDProcedure"].ToString();
if (!string.IsNullOrWhiteSpace(result.DT_Procedure.EditProcedure)) if (!string.IsNullOrWhiteSpace(result.DT_Procedure.EditProcedure))
{ {
result.DT_Procedure.ImportExcel = result.DT_Procedure.EditProcedure.Replace("QueryForm", "ImportExcel"); result.DT_Procedure.ImportExcel = "/api" + result.DT_Procedure.EditProcedure.Replace("QueryForm", "ImportExcel");
result.DT_Procedure.DownloadExcel = result.DT_Procedure.EditProcedure.Replace("QueryForm", "DownloadExcel"); result.DT_Procedure.DownloadExcel = "/api" + result.DT_Procedure.EditProcedure.Replace("QueryForm", "DownloadExcel");
} }
} }

@ -804,8 +804,8 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
"单选题", "单选题",
"多选题", "多选题",
"判断题", "判断题",
//"填空题", "填空题",
//"简答题", "简答题",
}; };
var answers = new List<string> var answers = new List<string>
{ {
@ -830,10 +830,11 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
// NPOIHelper.ExportExcel(dt, null, x, path + id1 + extension); // NPOIHelper.ExportExcel(dt, null, x, path + id1 + extension);
//}); //});
for (int jj = 0; jj < types.Count; jj++)
types.ForEach(async x =>
{ {
var x = types[jj];
string questionType = ConvertQuestionType1(x); string questionType = ConvertQuestionType1(x);
DataTable dt = NPOIHelper.ImportExcel(filepath, x); DataTable dt = NPOIHelper.ImportExcel(filepath, x);
if (dt.Columns["Comments"] == null) if (dt.Columns["Comments"] == null)
@ -844,7 +845,10 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
var comments = new List<string>(); var comments = new List<string>();
var courseName = dt.Rows[i]["课程(必填)"].ToString(); var courseName = dt.Rows[i]["课程(必填)"].ToString();
var questionContent = dt.Rows[i]["题目内容(必填)"].ToString(); var questionContent = dt.Rows[i]["题目内容(必填)"].ToString();
var correctAnswer = dt.Rows[i]["正确答案(必填)"].ToString(); var correctAnswer = string.Empty;
if (questionType != "Completion" && questionType != "ShortAnswer")
correctAnswer = dt.Rows[i]["正确答案(必填)"].ToString();
var questionAnalysis = dt.Rows[i]["解析"].ToString(); var questionAnalysis = dt.Rows[i]["解析"].ToString();
var difficultyLevel = dt.Rows[i]["难度"].ToString() ?? "普通"; var difficultyLevel = dt.Rows[i]["难度"].ToString() ?? "普通";
if (string.IsNullOrEmpty(questionContent)) if (string.IsNullOrEmpty(questionContent))
@ -856,20 +860,24 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
if (string.IsNullOrEmpty(courseName)) if (string.IsNullOrEmpty(courseName))
comments.Add("课程为必填项!"); comments.Add("课程为必填项!");
if (string.IsNullOrWhiteSpace(correctAnswer)) if (string.IsNullOrWhiteSpace(correctAnswer) && questionType != "Completion" && questionType != "ShortAnswer")
comments.Add("正确答案未标记!"); comments.Add("正确答案未标记!");
if (string.IsNullOrWhiteSpace(difficultyLevel)) if (string.IsNullOrWhiteSpace(difficultyLevel))
comments.Add("难易程度必填!"); comments.Add("难易程度必填!");
if (string.IsNullOrWhiteSpace(questionAnalysis)) if (string.IsNullOrWhiteSpace(questionAnalysis))
comments.Add("题目解析必填!"); comments.Add("题目解析必填!");
var course = await _ghre_CourseServices.QuerySingleDto(x => x.CourseName == courseName); Ghre_CourseDto course = null;
if (course is null) if (!string.IsNullOrEmpty(courseName))
comments.Add("课程不存在,请修正!"); {
course = await _ghre_CourseServices.QuerySingleDto(x => x.CourseName == courseName);
if (course is null)
comments.Add("课程不存在,请修正!");
}
var insert = new InsertGhre_QuestionInput() var insert = new InsertGhre_QuestionInput()
{ {
CourseId = course.Id, CourseId = course?.Id,
QuestionNo = await GenerateContinuousSequence(questionType.Substring(0, 1)), QuestionNo = await GenerateContinuousSequence(questionType.Substring(0, 1)),
DifficultyLevel = ConvertDifficultyLevel1(difficultyLevel), DifficultyLevel = ConvertDifficultyLevel1(difficultyLevel),
QuestionType = questionType, QuestionType = questionType,
@ -940,6 +948,63 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
}); });
#endregion #endregion
} }
else if (questionType == "Completion")
{
questionContent = questionContent.Replace("()", "()");
var completionCount = 0;
for (int ii = 1; ii < 11; ii++)
{
var answer = dt.Rows[i]["答案" + ii].ToString();
if (!string.IsNullOrWhiteSpace(answer))
{
completionCount++;
int index = questionContent.IndexOf("()");
if (index > -1)
{
if (questionContent.Length > index + 2)
{
if (questionContent.Substring(index + 2, 2) == "()")
questionContent = questionContent.Substring(0, index) + "____、" + questionContent.Substring(index + 2);
}
else
questionContent = questionContent.Substring(0, index) + "____" + questionContent.Substring(index + 2);
insertAnswers.Add(new InsertGhre_QuestionAnswerInput()
{
TaxisNo = ii * 100,
QuestionNo = answer,
//QuestionId = id,
AnswerContent = answer,
IsCorrect = true
});
}
}
}
if (completionCount != insertAnswers.Count)
comments.Add("题目内容填空个数与答案个数不匹配!");
else
insert.QuestionContent = questionContent;
}
else if (questionType == "ShortAnswer")
{
for (int ii = 1; ii < 11; ii++)
{
var answer = dt.Rows[i]["关键词" + ii].ToString();
if (!string.IsNullOrWhiteSpace(answer))
{
insertAnswers.Add(new InsertGhre_QuestionAnswerInput()
{
TaxisNo = ii * 100,
QuestionNo = answer,
//QuestionId = id,
AnswerContent = answer,
IsCorrect = true
});
}
}
}
if (comments.Any()) if (comments.Any())
{ {
data.ErrorCount++; data.ErrorCount++;
@ -950,7 +1015,7 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
else else
{ {
data.SuccessCount++; data.SuccessCount++;
var id = await base.Add(insert); id = await base.Add(insert);
insertAnswers.ForEach(x => x.QuestionId = id); insertAnswers.ForEach(x => x.QuestionId = id);
await _ghre_QuestionAnswerServices.Add(insertAnswers); await _ghre_QuestionAnswerServices.Add(insertAnswers);
} }
@ -958,6 +1023,7 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
else else
{ {
dt.Rows[i]["Comments"] = "试题在系统中已存在!"; dt.Rows[i]["Comments"] = "试题在系统中已存在!";
data.ErrorCount++;
isExistError = true; isExistError = true;
continue; continue;
} }
@ -970,12 +1036,9 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
NPOIHelper.ExportExcel(dt, null, x, physicsPath + errorFileName); NPOIHelper.ExportExcel(dt, null, x, physicsPath + errorFileName);
data.filePath = errorFileName; data.filePath = errorFileName;
} }
}); }
if (isExistError) return ServiceResult<ExcelData>.OprateSuccess("导入成功!", data);
return ServiceResult<ExcelData>.OprateFailed("导入文件存在错误,请下载后查看!", data);
else
return ServiceResult<ExcelData>.OprateSuccess("导入成功!");
} }
#endregion #endregion
} }
Loading…
Cancel
Save