|
@@ -62,10 +62,10 @@ namespace TEAMModelOS.Controllers
|
|
|
// [AuthToken(Roles = "teacher,admin")]
|
|
|
[HttpPost("read-excel")]
|
|
|
//[Authorize(Roles = "IES")]
|
|
|
- //[AuthToken(Roles = "teacher,admin,business")]
|
|
|
+ [AuthToken(Roles = "teacher,admin,business")]
|
|
|
[RequestSizeLimit(102_400_000_00)] //最大10000m左右
|
|
|
- public async Task<IActionResult> ReadExcel([FromForm] IFormFile file) {
|
|
|
- // var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
|
|
|
+ public async Task<IActionResult> ReadExcel([FromForm] IFormFile file,[FromForm] string periodId) {
|
|
|
+ var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
|
|
|
ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
|
|
|
using (ExcelPackage package = new ExcelPackage(file.OpenReadStream()))
|
|
|
{
|
|
@@ -74,9 +74,11 @@ namespace TEAMModelOS.Controllers
|
|
|
List<string> baseData = new List<string>();
|
|
|
List<string> itemTitle = new List<string>();
|
|
|
List<List<string>> itemDatas = new List<List<string>>();
|
|
|
- Dictionary<string, string> baseInfo = new Dictionary<string, string>();
|
|
|
- List<Dictionary<string, string>> itemInfo = new List<Dictionary<string, string>>();
|
|
|
+ Dictionary<string, object> baseInfo = new Dictionary<string, object>();
|
|
|
+ List<Dictionary<string, object>> itemInfo = new List<Dictionary<string, object>>();
|
|
|
HashSet<string> subjects = new HashSet<string>();
|
|
|
+ //科目标题的栏位序列
|
|
|
+ int subjectTitelIndex = -1;
|
|
|
//读取Exam_
|
|
|
var exam_sheet = sheet.Where(z => z.Name.StartsWith("Exam_")).FirstOrDefault();
|
|
|
if (exam_sheet!=null )
|
|
@@ -117,6 +119,9 @@ namespace TEAMModelOS.Controllers
|
|
|
{
|
|
|
if (!string.IsNullOrWhiteSpace($"{value}"))
|
|
|
{
|
|
|
+ if ($"{value}".Equals("subject")) {
|
|
|
+ subjectTitelIndex= c;
|
|
|
+ }
|
|
|
itemTitle.Add($"{value}");
|
|
|
}
|
|
|
else
|
|
@@ -148,93 +153,224 @@ namespace TEAMModelOS.Controllers
|
|
|
}
|
|
|
for (int i = 0; i < itemDatas.Count; i++)
|
|
|
{
|
|
|
- Dictionary<string, string> item = new Dictionary<string, string>();
|
|
|
+ Dictionary<string, object> item = new Dictionary<string, object>();
|
|
|
for (int j = 0; j < itemTitle.Count; j++)
|
|
|
{
|
|
|
- item.Add(itemTitle[j], itemDatas[i][j]);
|
|
|
+ if (itemTitle[j].Equals("index")&& int.TryParse(itemDatas[i][j],out int index))
|
|
|
+ {
|
|
|
+ item.Add(itemTitle[j], index);
|
|
|
+ }
|
|
|
+ else if (itemTitle[j].Equals("score") && double.TryParse(itemDatas[i][j], out double score))
|
|
|
+ {
|
|
|
+ item.Add(itemTitle[j], score);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ item.Add(itemTitle[j], itemDatas[i][j]);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
itemInfo.Add(item);
|
|
|
- subjects.Add(itemDatas[i][0]);
|
|
|
+ subjects.Add(itemDatas[i][subjectTitelIndex-1]);
|
|
|
}
|
|
|
}
|
|
|
- List<Dictionary<string, object>> students = new List<Dictionary<string, object>>();
|
|
|
+ List<Dictionary<string, object>> subjectDatas = new List<Dictionary<string, object>>();
|
|
|
if (subjects.Count > 0) {
|
|
|
foreach (var subject in subjects) {
|
|
|
- List<string> titles = new List<string>();
|
|
|
- List<List<string>> datas = new List<List<string>>();
|
|
|
+
|
|
|
var subject_sheet = sheet.Where(z => z.Name.StartsWith($"Sub_{subject}")).FirstOrDefault();
|
|
|
- if (subject_sheet != null)
|
|
|
+ var students = GetSubSheetData(subject_sheet);
|
|
|
+ var items = itemInfo.FindAll(x => x["subject"].Equals(subject));
|
|
|
+ Dictionary<string, object> subjectData = new Dictionary<string, object> { { "subject", subject } ,{ "items",items },{ "students", students } };
|
|
|
+ subjectDatas.Add(subjectData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //德育数据导入
|
|
|
+ {
|
|
|
+ var subject_sheet = sheet.Where(z => z.Name.StartsWith($"Sub_德育")).FirstOrDefault();
|
|
|
+ var students = GetSubSheetData(subject_sheet);
|
|
|
+ Dictionary<string, object> subjectData = new Dictionary<string, object> { { "subject", "德育" }, { "students", students } };
|
|
|
+ subjectDatas.Add(subjectData);
|
|
|
+ }
|
|
|
+ baseInfo.Add("periodId", periodId);
|
|
|
+ baseInfo.Add("school", school);
|
|
|
+ baseInfo.Add("subjects", subjectDatas);
|
|
|
+ var importExam =baseInfo.ToJsonString().ToObject<ImportExam>();
|
|
|
+ var validData = importExam.Valid();
|
|
|
+ if (!validData.isVaild)
|
|
|
+ {
|
|
|
+ return Ok(new { code = 400, error = validData.errors.SelectMany(z => z.Value).ToList() });
|
|
|
+ }
|
|
|
+ List<string> error = new List<string>();
|
|
|
+ List<string> warn = new List<string>();
|
|
|
+ string sql = $"select value c from c where c.name ='{importExam.name}'";
|
|
|
+ var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<ExamImport>(sql, $"ExamImport-{importExam.school}", pageSize: 1);
|
|
|
+ ExamImport examImport = null;
|
|
|
+ if (string.IsNullOrEmpty(result.continuationToken))
|
|
|
+ {
|
|
|
+ warn.Add("name_duplicate");//评测名称重复
|
|
|
+ }
|
|
|
+ if (result.list.IsNotEmpty())
|
|
|
+ {
|
|
|
+ examImport = result.list[0];
|
|
|
+ warn.Add("name_update");//同名评测数据更新
|
|
|
+ }
|
|
|
+ if (examImport == null)
|
|
|
+ {
|
|
|
+ examImport = new ExamImport { name = importExam.name, type = importExam.type, school = importExam.school };
|
|
|
+ if (DateTimeOffset.TryParse(importExam.time, out DateTimeOffset dateTime))
|
|
|
+ {
|
|
|
+ examImport.time = dateTime.ToUnixTimeMilliseconds();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ error.Add("time_format");// 时间格式错误
|
|
|
+ return Ok(new { code = 400, error });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ School schoolBase = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(importExam.school, new Azure.Cosmos.PartitionKey("Base"));
|
|
|
+ Period period = schoolBase.period.Find(x => x.id.Equals(importExam.periodId));
|
|
|
+ if (period != null)
|
|
|
+ {
|
|
|
+ foreach (var item in importExam.subjects)
|
|
|
+ {
|
|
|
+ var subject = period.subjects.Find(z => z.name.Equals(item.subject));
|
|
|
+ if (subject != null)
|
|
|
{
|
|
|
-
|
|
|
- var rows = subject_sheet.Dimension.Rows;
|
|
|
- var columns = subject_sheet.Dimension.Columns;
|
|
|
- for (int r = 1; r <=rows; r++)
|
|
|
+ // 学号确定的学生
|
|
|
+ var ids = item.students.Where(z => !string.IsNullOrWhiteSpace(z.id)).Select(x => x.id);
|
|
|
+ //没有学号 ,且姓名和班级信息不全的
|
|
|
+ var studentInvalid = item.students.Where(z => string.IsNullOrWhiteSpace(z.id) && (string.IsNullOrWhiteSpace(z.name) || string.IsNullOrWhiteSpace(z.classId)));
|
|
|
+ if (studentInvalid.Any())
|
|
|
{
|
|
|
- List<string> data = new List<string>();
|
|
|
- for (int c = 1; c <= columns; c++)
|
|
|
+ error.Add("student_invalid ");// 学生信息不全
|
|
|
+ return Ok(new { code = 400, error, studentInvalid });
|
|
|
+ }
|
|
|
+ var classesStu = item.students.Where(z => !string.IsNullOrWhiteSpace(z.classId) && string.IsNullOrWhiteSpace(z.id)).Select(x => x.id);
|
|
|
+ List<string> classNames = new List<string>();
|
|
|
+ foreach (var clazz in classesStu)
|
|
|
+ {
|
|
|
+ var cls = clazz.Split("-");
|
|
|
+ if (cls.Length == 2 && int.TryParse(cls[0], out int year) && year > 2000 && int.TryParse(cls[1], out int no) && no > 0)
|
|
|
{
|
|
|
- var value = subject_sheet.GetValue(r, c);
|
|
|
- if (r == 1)
|
|
|
- {
|
|
|
- if (!string.IsNullOrWhiteSpace($"{value}"))
|
|
|
- {
|
|
|
- titles.Add($"{value}");
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
- if (c > titles.Count)
|
|
|
- {
|
|
|
- break;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- data.Add($"{value}");
|
|
|
- }
|
|
|
- }
|
|
|
+ string sqlClazz = $"select value c from c where c.year={year} and c.no ='{cls[1]}' and ";
|
|
|
}
|
|
|
- if (data.Any()) {
|
|
|
- datas.Add(data);
|
|
|
+ else
|
|
|
+ {
|
|
|
+ classNames.Add(clazz);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
- for (int i = 0; i < datas.Count; i++)
|
|
|
+ else
|
|
|
+ {
|
|
|
+ error.Add("subject_invalid");// 科目不存在
|
|
|
+ return Ok(new { code = 400, error });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Ok(new {baseInfo });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Dictionary<string, object>> GetSubSheetData(ExcelWorksheet subject_sheet) {
|
|
|
+ List<Dictionary<string, object>> students = new List<Dictionary<string, object>>();
|
|
|
+ List<string> titles = new List<string>();
|
|
|
+ List<List<string>> datas = new List<List<string>>();
|
|
|
+ if (subject_sheet != null)
|
|
|
+ {
|
|
|
+
|
|
|
+ var rows = subject_sheet.Dimension.Rows;
|
|
|
+ var columns = subject_sheet.Dimension.Columns;
|
|
|
+ for (int r = 1; r <= rows; r++)
|
|
|
+ {
|
|
|
+ List<string> data = new List<string>();
|
|
|
+ for (int c = 1; c <= columns; c++)
|
|
|
+ {
|
|
|
+ var value = subject_sheet.GetValue(r, c);
|
|
|
+ if (r == 1)
|
|
|
+ {
|
|
|
+ if (!string.IsNullOrWhiteSpace($"{value}"))
|
|
|
+ {
|
|
|
+ titles.Add($"{value}");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
{
|
|
|
- Dictionary<string, object> item = new Dictionary<string, object>();
|
|
|
- List<KeyValuePair<int, int>> _index_order = new List<KeyValuePair<int, int>>();
|
|
|
- for (int j = 0; j < titles.Count; j++)
|
|
|
+ if (c > titles.Count)
|
|
|
{
|
|
|
- if (int.TryParse(titles[j], out int index))
|
|
|
- {
|
|
|
- _index_order.Add(new KeyValuePair<int, int>(index , j));
|
|
|
- }
|
|
|
- else {
|
|
|
- item.Add(titles[j], datas[i][j]);
|
|
|
- }
|
|
|
-
|
|
|
+ break;
|
|
|
}
|
|
|
- var orders = _index_order.OrderBy(x => x.Key).ToList();
|
|
|
- List<double> scores = new List<double>();
|
|
|
- for (int j = 0; j < orders.Count; j++)
|
|
|
+ else
|
|
|
{
|
|
|
- double score = 0;
|
|
|
- double.TryParse($"{datas[i][orders[j].Value]}", out score);
|
|
|
- scores.Add(score);
|
|
|
+ data.Add($"{value}");
|
|
|
}
|
|
|
- item.Add("scores", scores);
|
|
|
- students.Add(item);
|
|
|
}
|
|
|
}
|
|
|
+ if (data.Any())
|
|
|
+ {
|
|
|
+ datas.Add(data);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
- return Ok(new { baseTitle, baseData, itemTitle, itemDatas, subjects, baseInfo, itemInfo, students });
|
|
|
- }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < datas.Count; i++)
|
|
|
+ {
|
|
|
+ Dictionary<string, object> item = new Dictionary<string, object>();
|
|
|
+ List<KeyValuePair<int, int>> _index_order = new List<KeyValuePair<int, int>>();
|
|
|
+ List<ItemVlaue> items = new List<ItemVlaue>();
|
|
|
+ for (int j = 0; j < titles.Count; j++)
|
|
|
+ {
|
|
|
+ if (int.TryParse(titles[j], out int index))
|
|
|
+ {
|
|
|
+ _index_order.Add(new KeyValuePair<int, int>(index, j));
|
|
|
+ }
|
|
|
+ else if (titles[j].StartsWith("item_"))
|
|
|
+ {
|
|
|
+ double score = 0;
|
|
|
+ double.TryParse($"{datas[i][j]}", out score);
|
|
|
+ items.Add(new ItemVlaue() {code= titles[j].Replace("item_",""), value= score });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (titles[j].Equals("score") && double.TryParse(datas[i][j], out double score))
|
|
|
+ {
|
|
|
+ item.Add(titles[j], score);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ item.Add(titles[j], datas[i][j]);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var orders = _index_order.OrderBy(x => x.Key).ToList();
|
|
|
+ List<double> scores = new List<double>();
|
|
|
+ for (int j = 0; j < orders.Count; j++)
|
|
|
+ {
|
|
|
+ double score = 0;
|
|
|
+ double.TryParse($"{datas[i][orders[j].Value]}", out score);
|
|
|
+ scores.Add(score);
|
|
|
+ }
|
|
|
+ if (scores.IsNotEmpty())
|
|
|
+ {
|
|
|
+ item.Add("scores", scores);
|
|
|
+ }
|
|
|
+ if (items.IsNotEmpty())
|
|
|
+ {
|
|
|
+ item.Add("items", items);
|
|
|
+ }
|
|
|
+ students.Add(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ return students;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
[ProducesDefaultResponseType]
|
|
|
// [AuthToken(Roles = "teacher,admin")]
|
|
|
[HttpPost("import-check")]
|