123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- using DocumentFormat.OpenXml.Office2010.ExcelAc;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Mvc;
- using Newtonsoft.Json.Linq;
- using OpenXmlPowerTools;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TEAMModelOS.Helper.Common.FileHelper;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
- using TEAMModelOS.SDK.Helper.Common.DateTimeHelper;
- using TEAMModelOS.SDK.Helper.Common.JsonHelper;
- using TEAMModelOS.SDK.Helper.Common.JsonHelper.JsonPath;
- using TEAMModelOS.SDK.Helper.Security.ShaHash;
- using TEAMModelOS.SDK.Module.AzureBlob.Container;
- using TEAMModelOS.Service.Models;
- namespace TEAMModelOS.Controllers.Analysis
- {
- [Route("api/[controller]")]
- [ApiController]
- public class ChangeController : Controller
- {
- private readonly SnowflakeId _snowflakeId;
- private readonly IWebHostEnvironment _hostingEnvironment;
- public readonly AzureCosmosFactory _azureCosmos;
- public readonly AzureStorageFactory _azureStorage;
- private const string CacheCosmosPrefix = "Analysis:";
- public ChangeController(IWebHostEnvironment hostingEnvironment, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, SnowflakeId snowflakeId)
- {
- _snowflakeId= snowflakeId;
- _hostingEnvironment = hostingEnvironment;
- _azureCosmos = azureCosmos;
- _azureStorage = azureStorage;
- }
- [HttpGet("SaveObjectAdas")]
- public async Task<BaseResponse> SaveObjectAdasAsync(string identity)
- {
- // 获取系统路径
- string contentRootPath = _hostingEnvironment.ContentRootPath;
- identity = "IES/20200716";
- //string query = "$.info";
- //int countClasses = 0;
- //string queryClass = "$member.1.item_answer[*]";
- string query = "$.exercises[*]";
- string queryInfo = "$.info";
- string queryPoint = "$.testpaper.items[*]";
- string querySchoolInfo = "$.exercises[0].info";
- string data = FileTool.getJson(contentRootPath, identity);
- data = FileTool.UnicodeToString(data);
- JObject parsedJson = JObject.Parse(data);
- JsonPathContext context = new JsonPathContext
- { ValueSystem = new JsonApiValueSystem() };
- //解析试卷作答详情
- List<dynamic> papers = context.SelectNodes(parsedJson,
- query).Select(node => node.Value).ToList();
- //解析试卷基本信息
- List<dynamic> info = context.SelectNodes(parsedJson,
- queryInfo).Select(node => node.Value).ToList();
- //解析题库信息
- List<dynamic> Itempoint = context.SelectNodes(parsedJson,
- queryPoint).Select(node => node.Value).ToList();
- //解析学校信息
- List<dynamic> SchoolInfo = context.SelectNodes(parsedJson,
- querySchoolInfo).Select(node => node.Value).ToList();
- int m = 0;
- //人数总和
- int Count = 0;
- //起始坐标
- int Strat = 0;
- // 初始化学生人数
- int Stus = 0;
- ExamResult simple = new ExamResult();
- ExamAnswer examAnswer = new ExamAnswer();
- List<Student> students = new List<Student>();
- List<Classroom> classrooms = new List<Classroom>();
- //List<ExamAnswer> examAnswers = new List<ExamAnswer>();
- List<List<List<int>>> ClassPoint = new List<List<List<int>>>();
- List<List<double>> StuPoint = new List<List<double>>();
- List<string> StuIds = new List<string>();
- List<List<string>> stuAnswers = new List<List<string>>();
- List<Dictionary<string, int[]>> Classes = new List<Dictionary<string, int[]>>();
- Dictionary<object, object> ListInfo = new Dictionary<object, object>();
- List<double> points = new List<double>();
- //配分数组
- Paper paper = new Paper();
- List<ItemInfo> items = new List<ItemInfo>();
- int k = 0;
- Itempoint.ForEach(p =>
- {
- string pt = p.point;
- string OpenTracing = p + "";
- JObject jArray = JObject.Parse(OpenTracing);
- string queryOptions = "$.options[*]";
- List<dynamic> itemOption = context.SelectNodes(jArray,
- queryOptions).Select(node => node.Value).ToList();
- List<CodeValue> codeValues = new List<CodeValue>();
- itemOption.ToList<dynamic>().ForEach(i =>
- {
- CodeValue code = new CodeValue
- {
- code = i.data,
- value = i.type
- };
- codeValues.Add(code);
- });
- //题序
- string order = p.itemindex;
- string ans = p.answer;
- string knowlege = p.idx_desc;
- ItemInfo item = new ItemInfo
- {
- question = p.content,
- score = double.Parse(pt),
- answer = new List<string> {
- ans
- },
- type = p.type,
- order = int.Parse(order),
- //level = p.edu_goal,
- option = codeValues,
- points = new List<string> {
- knowlege
- }
- };
- items.Add(item);
- k++;
- points.Add(double.Parse(pt.Trim()));
- });
- //处理试卷信息
- paper.item = items;
- paper.subjectCode = "Subject_Math";
- //基本信息
- info.ForEach(e =>
- {
- simple.name = e.test_name;
- simple.time = e.test_date;
- paper.name = e.test_name;
- });
- School sc = null;
- ExamInfo examInfo = null;
- SchoolInfo.ForEach(s =>
- {
- string count = s.stucount;
- string st = s.ex_time;
- string ed = s.ex_endtime;
- DateTime.TryParse(st, out DateTime tt);
- DateTime.TryParse(ed, out DateTime etime);
- long time = DateTimeHelper.ToUnixTimestamp(tt);
- long edtime = DateTimeHelper.ToUnixTimestamp(etime);
- sc = new School
- {
- schoolCode = "88924E4C-F00D-4A1D-B2C3-234F930F29D4",
- schoolName = s.school_name,
- period = new List<Period> { new Period {periodName = "小学" , periodCode = "c3e1e95a-561e-fabf-cce3-3a564782e443",grades= new List<Grade> {
- new Grade { gradeName = "四年级" ,gradeCode = "4"} } } }
- };
- //考试基本信息
- examInfo = new ExamInfo
- {
- id = _snowflakeId.NextId()+"",
- code = "88924E4C-F00D-4A1D-B2C3-234F930F29D4",
- name = s.ex_name,
- type = s.ex_type,
- examCode = "QCSCS",
- startTime = time,
- endTime = edtime,
- stuCount = int.Parse(count),
- conditions = new Condition
- {
- period = "c3e1e95a-561e-fabf-cce3-3a564782e443",
- grade = s.grade_name,
- semester = s.semester,
- subject = new List<string> { "Subject_Math" }
- }
- };
- });
- papers.ForEach(x =>
- {
- //var c = papers[i].member;
- string stu = x.info.stucount;
- int k = int.Parse(stu);
- string key = x.info.class_name;
- string schoolName = x.info.school_name;
- Classroom classInfo = new Classroom
- {
- id = _snowflakeId.NextId() + "",
- classroomName = key,
- //classroomType = schoolName,
- studentCount = k,
- classroomCode = "qcs"
- };
- classrooms.Add(classInfo);
- Classroom classInfoOfStudent = new Classroom
- {
- classroomName = key,
- //schoolCode = schoolName,
- //studentCount = k,
- classroomCode = "qcs"
- };
- Dictionary<string, int[]> map = new Dictionary<string, int[]>();
- int[] Range = new int[2];
- //处理学生作答数据
- for (int j = 0; j < 22; j++)
- {
- Student stus = new Student
- {
- type = 2,
- //schoolCode = schoolName
- };
- string men = x.members[j] + "";
- JObject jArray = JObject.Parse(men);
- string queryClass = "$.item_answers[*]";
- string queryStu = "$.info";
- List<string> ans = new List<string>();
- List<dynamic> answers = context.SelectNodes(jArray,
- queryClass).Select(node => node.Value).ToList();
- //学生ID集合
- List<dynamic> studentIds = context.SelectNodes(jArray,
- queryStu).Select(node => node.Value).ToList();
- List<double> point = new List<double>();
- if (answers.Count == 0)
- {
- StuIds.Add("0");
- k++;
- }
- else
- {
- //处理学生信息
- studentIds.ToList<dynamic>().ForEach(s =>
- {
- string id = s.student_id;
- //id = System.Guid.NewGuid().ToString();
- stus.id = id.Trim();
- stus.name = s.name;
- stus.studentId = id.Trim();
- string sno = s.seatno;
- if (sno.Equals(""))
- {
- stus.seatNo = 0;
- }
- else
- {
- stus.seatNo = int.Parse(sno);
- }
- //stus.classroom = classInfoOfStudent;
- StuIds.Add(id.Trim());
- });
- students.Add(stus);
- }
- answers.ToList<dynamic>().ForEach(p =>
- {
- string pt = p.real_point;
- string ansContent = p.ans_content;
- if (ans.Equals(""))
- {
- ansContent = "无";
- }
- ans.Add(ansContent);
- point.Add(double.Parse(pt.Trim()));
- });
- stuAnswers.Add(ans);
- StuPoint.Add(point);
- Stus++;
- }
- //每次人数累加得到实际总人数
- Count += Stus;
- if (m == 0)
- {
- Strat = Stus;
- Range[0] = 0;
- Range[1] = Stus - 1;
- }
- else
- {
- Range[0] = Count - Stus;
- Range[1] = Count - 1;
- }
- map.Add(key, Range);
- Classes.Add(map);
- m++;
- //初始化每个班级人数
- Stus = 0;
- //ClassPoint.Add(StuPoint);
- });
- examAnswer.answer = stuAnswers;
- examAnswer.code = examInfo.examCode;
- paper.code = examInfo.examCode;
- simple.code = examInfo.examCode;
- simple.classes = Classes;
- simple.ids = StuIds;
- simple.point = points;
- simple.result = StuPoint;
- simple.subjectCode = paper.subjectCode;
- ResponseBuilder builder = ResponseBuilder.custom();
- Dictionary<string, object> keyValues = new Dictionary<string, object> {
- { "schoolInfo",sc},
- { "classInfo",classrooms},
- { "examInfo",examInfo},
- { "students",students},
- { "paperInfo",paper},
- { "examResult",simple},
- { "examAnswer",examAnswer}
- };
- AzureBlobModel modle = await _azureStorage.UploadFileByContainer("hbcn", keyValues.ToJsonString(), "exam", sc.schoolCode + examInfo.examCode + paper.subjectCode + ".json");
- //examInfo.blobUrl = modle.BlobUrl;
- //判定是否存储考试基本信息
- Dictionary<string, object> sub = new Dictionary<string, object>
- {
- { "examCode", examInfo.examCode}
- };
- List<ExamInfo> eInfo = await _azureCosmos.FindByDict<ExamInfo>(sub);
- if (eInfo.Count > 0)
- {
- eInfo.ForEach(e =>
- {
- if (e.conditions.subject.Contains(paper.subjectCode))
- {
- builder.Data("").build();
- }
- else
- {
- e.conditions.subject = new List<string>{ paper.subjectCode };
- e.blobUrl = new List<string> { modle.BlobUrl };
- }
- });
- }
- else {
- examInfo.blobUrl = new List<string> { modle.BlobUrl };
- await _azureCosmos.Save(examInfo);
- }
-
- //Redis处理考试结果数据集合
- if (RedisHelper.Instance != null)
- {
- await RedisHelper.HSetAsync(CacheCosmosPrefix + "examResult" + simple.subjectCode, ShaHashHelper.GetSHA1(examInfo.examCode), simple);
- await RedisHelper.HSetAsync(CacheCosmosPrefix + "students", ShaHashHelper.GetSHA1(sc.schoolCode), students);
- await RedisHelper.HSetAsync(CacheCosmosPrefix + "paperInfo" + simple.subjectCode, ShaHashHelper.GetSHA1(examInfo.examCode), paper);
- await RedisHelper.HSetAsync(CacheCosmosPrefix + "examAnswer" + simple.subjectCode, ShaHashHelper.GetSHA1(examInfo.examCode), examAnswer);
- await RedisHelper.HSetAsync(CacheCosmosPrefix + "classInfo", ShaHashHelper.GetSHA1(sc.schoolCode), classrooms);
- await RedisHelper.HSetAsync(CacheCosmosPrefix + "schoolInfo", ShaHashHelper.GetSHA1(sc.schoolCode), sc);
- }
-
- //_examInfoService.SaveToCosmos(simple);
- //_cosmosrepository.Save(sc);
- /*_cosmosrepository.Save(simple);
- _cosmosrepository.Save(examAnswer);*/
- //_cosmosrepository.SaveAll(classrooms);
- //_cosmosrepository.SaveOrUpdateAll(students);
- //_examInfoService.SaveListToCosmos(students);
- return builder.Data(sc).build();
- //return builder.build();
- }
- [HttpGet("SaveExamToBlob")]
- public async Task<BaseResponse> SendExam(JosnRequest<Dictionary<string, object>> request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- Student student = null;
- List<Student> AnalysisStudents = new List<Student>();
- ExamInfo examInfo = null;
- ExamResult result = null;
- if (request.@params.TryGetValue("id", out object id) && request.@params.TryGetValue("pk", out object pk))
- {
- examInfo = await _azureCosmos.FindByIdPk<ExamInfo>(id.ToString(), pk.ToString());
- //获取发布对象
- if (request.@params.TryGetValue("schoolCode", out object schoolCode))
- {
- List<string> targetCode = examInfo.target;
- List<ClassStudent> classroomStudents = await _azureCosmos.FindByDict<ClassStudent>(new Dictionary<string, object> { { "id", targetCode } });
- List<Student> students = await _azureCosmos.FindByDict<Student>(
- new Dictionary<string, object>() { { "classroomCode", targetCode }, { "code", schoolCode } },
- new List<string> { "id", "name", "code", "seatNo", "studentId", "classroomCode" });
- List<dynamic> stus = new List<dynamic>();
- List<ClassStudent> newClassStudents = new List<ClassStudent>();
- ///新增的学生
- if (students.IsNotEmpty())
- {
- students.ForEach(x =>
- {
- if (!classroomStudents.Select(m => m.code).Contains(x.studentId))
- {
- newClassStudents.Add(new ClassStudent() { id = x.classroomCode, code = x.studentId });
- }
- var stu = new { x.id, x.name, x.code, x.seatNo, x.studentId, x.classroomCode };
- stus.Add(stu);
- });
- }
- ///反射已存在的id信息
- if (classroomStudents.IsNotEmpty())
- {
- ///处理冗余的id查询
- List<string> ids = new List<string>();
- classroomStudents.ForEach(x =>
- {
- if (!students.Select(m => m.studentId).Contains(x.code))
- {
- ids.Add(x.code);
- }
- });
- var sts = await _azureCosmos.FindByDict<Student>(new Dictionary<string, object>() { { "studentId", ids.ToArray() } });
- if (sts.IsNotEmpty())
- {
- sts.ForEach(x =>
- {
- var stu = new { x.id, x.name, x.code, x.seatNo, x.studentId, x.classroomCode };
- stus.Add(stu);
- });
- }
- }
- //保存新增学生
- if (newClassStudents.IsNotEmpty())
- {
- classroomStudents.AddRange(await _azureCosmos.SaveOrUpdateAll(newClassStudents));
- }
-
- //处理学生信息以便学情使用
- int n = 1;
- stus.ForEach(s => {
- student = new Student
- {
- id = s[0],
- name = s[1],
- studentId = s[4],
- seatNo = n,
- classroomCode = s[5]
- };
- n++;
- AnalysisStudents.Add(student);
- });
- //依次处理班级信息
- targetCode.ForEach(c =>
- {
- int count = 0;
- AnalysisStudents.ForEach(s => {
- if (c.Equals(s.classroomCode))
- {
- count++;
- }
- //处理学生作答以及学生得分记录
- });
- });
- result = new ExamResult
- {
- code = examInfo.id,
- name = examInfo.name,
-
- };
-
- //return builder.Data(classroomStudents).Extend(new Dictionary<string, object> { { "students", stus }, { "count", stus.Count } }).build();
- }
- else {
- return builder.build();
- }
- }
- else
- {
- return builder.build();
- }
- return builder.Data("").build();
- }
- }
- }
|