using Learn.Archives.API.Controllers.Dto; using Learn.Archives.API.Expand; using Learn.Archives.Core.Common; using Learn.Archives.Core.Model; using Learn.Archives.Core.Model.Dto; using Learn.Archives.Core.Model.Enum; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using MiniExcelLibs; using SqlSugar; using Swashbuckle.AspNetCore.SwaggerGen; using System.Diagnostics; using System.Security.Claims; using System.Text.RegularExpressions; using UserCenter.Model; using UserCenter.Model.Common; using UserCenter.Model.Enum; namespace Learn.Archives.API.Controllers { public class SchoolBusinessController : BackController { readonly Repository baseService; readonly Repository schoolService; readonly Repository adminService; readonly IHttpContextAccessor accessor; public SchoolBusinessController(Repository baseService, Repository schoolService, IHttpContextAccessor accessor, Repository adminService) : base(baseService) { this.baseService = baseService; this.schoolService = schoolService; this.accessor = accessor; this.adminService = adminService; } public class QueryPageDto { /// /// 学校id /// public long SchoolId { get; set; } /// /// 年级 /// public string? Grade { get; set; } /// /// 赴校人员ID /// public long? UserId { get; set; } /// /// 是否查询完结 /// public bool? SolutionEnd { get; set; } public DateTime? StartTime { get; set; } public DateTime? EndTime { get; set; } /// /// 页面索引 /// public int PageIndex { get; set; } = 0; /// /// 页面数量 /// public int PageSize { get; set; } = 20; } public override Task Edit([FromBody] SchoolBusiness model) { if (model.SolutionRecord != null && model.SolutionRecord.SolutionEnd == true) model.SolutionEnd = true; if (!string.IsNullOrEmpty(model._grade)) { var g = GradeHelper.GetStudentGradeBaseByGrade(model._grade); model.GradeLevel = g.GradeLevel; model.GradeYear = g.GradeYear; } return base.Edit(model); } /// /// 查询 赴校列表 /// /// [HttpPost] public async Task> QueryPageList(QueryPageDto dto) { var uid = dto.UserId.ToString(); var gInfo = GradeHelper.GetStudentGradeBaseByGrade(dto.Grade); RefAsync total = 0; var resData = await baseService.AsQueryable() .WhereIF(dto.SchoolId != 0, s => s.SchoolId == dto.SchoolId) .WhereIF(gInfo != null, s => s.GradeLevel == gInfo.GradeLevel && s.GradeYear == gInfo.GradeYear) .WhereIF(dto.SolutionEnd != null, s => s.SolutionEnd == dto.SolutionEnd) .WhereIF(dto.StartTime != null, s => s.StartTime >= dto.StartTime) .WhereIF(dto.EndTime != null, s => s.StartTime <= dto.EndTime) .WhereIF(uid != null, s => s.SchoolBusinessUser != null && SqlFunc.JsonLike(s.SchoolBusinessUser, uid)) .OrderByDescending(s => s.Id) .ToPageListAsync(dto.PageIndex, dto.PageSize, total); return new PageResult() { Data = resData, Total = total }; } /// /// 导入考试信息 /// /// /// [HttpPost, ResultIgnore] [HttpLogEnable] public async Task Import(IFormFile? file) { var fl = file != null ? file : accessor.HttpContext?.Request.Form.Files[0]; if (fl == null) Oh.ModelError("传入无效的数据"); if (!Path.GetExtension(fl.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase)) Oh.ModelError("请选择导入文件为.xlsx的后缀名!"); //分析excel IEnumerable dataList; using var stream = new MemoryStream(); { await fl.CopyToAsync(stream); dataList = stream.Query() .Where(s => !string.IsNullOrEmpty(s.School)); } if (dataList == null || dataList.Count() == 0) Oh.ModelError("导入失败:无有效数据"); //todo //处理数据 var errorExcelInfo = new List(); var insertInfo = new List(); var db = schoolService.Context; foreach (var imp in dataList) { var school = await schoolService.GetFirstAsync(s => s.Name == imp.School); if (school == null) { imp.Error = $"导入失败:学校 [{imp.School}] 未找到!"; errorExcelInfo.Add(imp); continue; } var gradeInfo = GradeHelper.GetStudentGradeBaseByGrade(imp.Grade); if (gradeInfo == null) { imp.Error = $"导入失败:学校 [{imp.School}] 年级[{imp.Grade}]不符合规范!"; errorExcelInfo.Add(imp); continue; } var adminNameArr = imp.Users.Split(","); if (adminNameArr == null) { imp.Error = $"赴校人员未能识别"; errorExcelInfo.Add(imp); continue; } var adminArr = await adminService.AsQueryable() .Where(s => adminNameArr.Contains(s.Name)) .Select(s => s.Id).ToArrayAsync(); var qStr = new Dictionary() { {FeedbackQuestionTypeEnum.学校领导班子,(imp.Q学校领导班子,imp.P学校领导班子) }, {FeedbackQuestionTypeEnum.双师课堂,(imp.Q双师课堂,imp.P双师课堂) }, {FeedbackQuestionTypeEnum.设备,(imp.Q设备,imp.P设备) }, {FeedbackQuestionTypeEnum.学生,(imp.Q学生,imp.P学生) }, {FeedbackQuestionTypeEnum.其他,(imp.Q其他,imp.P其他) } }; var resultQuestion = new List(); var regex = new Regex(@"(问题\d+):(.*?)(?=;|$)", RegexOptions.Singleline); foreach (var kvp in qStr) { var questionType = kvp.Key; var questions = kvp.Value.Questions; var solutions = kvp.Value.Solutions; // 解析问题 var questionMatches = regex.Matches(questions); var solutionMatches = regex.Matches(solutions); // 创建字典以便快速查找解决方案 var solutionDict = new Dictionary(); foreach (Match match in solutionMatches) if (match.Groups.Count == 3) solutionDict[match.Groups[1].Value] = match.Groups[2].Value; // 创建DTO对象 foreach (Match match in questionMatches) { if (match.Groups.Count == 3) { var sort = match.Groups[1].Value; var question = match.Groups[2].Value; solutionDict.TryGetValue(sort, out var solution); resultQuestion.Add(new FeedbackQuestionsDto { QuestionType = questionType, Sort = sort, Question = question, Solution = solution, EndTime = null // 根据需求设置解决时间 }); } } } var feedbackQuestions = resultQuestion.ToArray(); //沟通时间/执行记录 var regex1 = new Regex(@"(.*?):(.*?)(?=;|$)", RegexOptions.Singleline); var record = new List(); foreach (Match match in regex1.Matches(imp.Record)) { record.Add(new RecordDto() { ExecutionTime = match.Groups[1].Value.ExtractDateTime(), ExecutionRecords = match.Groups[2].Value, }); } var business = new SchoolBusiness() { SchoolId = school.Id, SchoolName = school.Name, GradeLevel = gradeInfo.GradeLevel, GradeYear = gradeInfo.GradeYear, ClassMeeting = imp.ClassMeeting, Discussion = imp.Discussion, SchoolBusinessUser = adminArr, Remark = imp.Remark, StartTime = imp.StartTime, FeedbackQuestions = feedbackQuestions, SolutionRecord = new SolutionRecordDto() { EndRecord = imp.EndRecord, EndRecordTime = imp.EndRecord.ExtractDateTime(), Solution = imp.Solution, Record = record, } }; }; if (errorExcelInfo.Count != 0) return File(errorExcelInfo.ExportExcel(), "application/ms-excel"); //写入数据库 return Ok(); } /// /// 下载导入成绩模板 /// /// [HttpGet, ResultIgnore, AllowAnonymous] public IActionResult DwImportTemplate() { var resultList = new List() { new SchoolBusinessImport() { } }; return File(resultList.ExportExcel(), "application/ms-excel"); } } }