265 lines
11 KiB
C#
265 lines
11 KiB
C#
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<SchoolBusiness>
|
||
{
|
||
readonly Repository<SchoolBusiness> baseService;
|
||
readonly Repository<School> schoolService;
|
||
readonly Repository<Core.Model.Admin> adminService;
|
||
readonly IHttpContextAccessor accessor;
|
||
public SchoolBusinessController(Repository<SchoolBusiness> baseService, Repository<School> schoolService, IHttpContextAccessor accessor, Repository<Core.Model.Admin> adminService) : base(baseService)
|
||
{
|
||
this.baseService = baseService;
|
||
this.schoolService = schoolService;
|
||
this.accessor = accessor;
|
||
this.adminService = adminService;
|
||
}
|
||
|
||
public class QueryPageDto
|
||
{
|
||
/// <summary>
|
||
/// 学校id
|
||
/// </summary>
|
||
public long SchoolId { get; set; }
|
||
/// <summary>
|
||
/// 年级
|
||
/// </summary>
|
||
public string? Grade { get; set; }
|
||
/// <summary>
|
||
/// 赴校人员ID
|
||
/// </summary>
|
||
public long? UserId { get; set; }
|
||
/// <summary>
|
||
/// 是否查询完结
|
||
/// </summary>
|
||
public bool? SolutionEnd { get; set; }
|
||
public DateTime? StartTime { get; set; }
|
||
public DateTime? EndTime { get; set; }
|
||
|
||
/// <summary>
|
||
/// 页面索引
|
||
/// </summary>
|
||
public int PageIndex { get; set; } = 0;
|
||
/// <summary>
|
||
/// 页面数量
|
||
/// </summary>
|
||
public int PageSize { get; set; } = 20;
|
||
|
||
}
|
||
|
||
public override Task<bool> 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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询 赴校列表
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public async Task<PageResult<SchoolBusiness>> QueryPageList(QueryPageDto dto)
|
||
{
|
||
|
||
var uid = dto.UserId.ToString();
|
||
var gInfo = GradeHelper.GetStudentGradeBaseByGrade(dto.Grade);
|
||
RefAsync<int> 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<SchoolBusiness>() { Data = resData, Total = total };
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 导入考试信息
|
||
/// </summary>
|
||
/// <param name="dto"></param>
|
||
/// <returns></returns>
|
||
[HttpPost, ResultIgnore]
|
||
[HttpLogEnable]
|
||
public async Task<IActionResult> 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<SchoolBusinessImportError> dataList;
|
||
using var stream = new MemoryStream();
|
||
{
|
||
await fl.CopyToAsync(stream);
|
||
dataList = stream.Query<SchoolBusinessImportError>()
|
||
.Where(s => !string.IsNullOrEmpty(s.School));
|
||
}
|
||
if (dataList == null || dataList.Count() == 0)
|
||
Oh.ModelError("导入失败:无有效数据");
|
||
|
||
//todo
|
||
//处理数据
|
||
var errorExcelInfo = new List<SchoolBusinessImportError>();
|
||
var insertInfo = new List<SchoolBusiness>();
|
||
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, (string Questions, string Solutions)>()
|
||
{
|
||
{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<FeedbackQuestionsDto>();
|
||
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<string, string>();
|
||
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<RecordDto>();
|
||
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();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下载导入成绩模板
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpGet, ResultIgnore, AllowAnonymous]
|
||
public IActionResult DwImportTemplate()
|
||
{
|
||
var resultList = new List<SchoolBusinessImport>() { new SchoolBusinessImport() { } };
|
||
return File(resultList.ExportExcel(), "application/ms-excel");
|
||
}
|
||
|
||
|
||
|
||
}
|
||
}
|