Learn.Archives/Learn.Archives.API/Controllers/SchoolBusinessController.cs

301 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 Mapster;
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;
readonly LiveUserInfo userInfo;
public SchoolBusinessController(Repository<SchoolBusiness> baseService, Repository<School> schoolService, IHttpContextAccessor accessor, Repository<Core.Model.Admin> adminService, LiveUserInfo userInfo) : base(baseService)
{
this.baseService = baseService;
this.schoolService = schoolService;
this.accessor = accessor;
this.adminService = adminService;
this.userInfo = userInfo;
}
public class QueryPageDto
{
/// <summary>
/// 学校id
/// </summary>
public long SchoolId { get; set; }
/// <summary>
/// 年级
/// </summary>
public string? Grade { get; set; }
/// <summary>
/// 赴校人员ID
/// </summary>
public string? UserName{ 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);
}
public override async Task<dynamic> Info(long id)
{
return (await base.Info(id) as object).Adapt<SchoolBusinessDto>();
}
/// <summary>
/// 查询 赴校列表
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<PageResult<SchoolBusinessDto>> QueryPageList(QueryPageDto dto)
{
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(!string.IsNullOrEmpty(dto.UserName), s => s.SchoolBusinessUser != null && SqlFunc.JsonLike(s.SchoolBusinessUser, dto.UserName))
.OrderByDescending(s => s.Id)
.Select<SchoolBusinessDto>()
.ToPageListAsync(dto.PageIndex, dto.PageSize, total);
foreach (var item in resData)
item.CanOperate =userInfo.IsSa || (item.SchoolBusinessUser?.Contains(userInfo.Name) ?? false);
return new PageResult<SchoolBusinessDto>() { Data = resData, Total = total };
}
/// <summary>
/// 导入考试信息
/// </summary>
/// <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("导入失败:无有效数据");
//处理数据
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.Name).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;
if (string.IsNullOrEmpty(questions) || string.IsNullOrEmpty(solutions))
continue;
// 解析问题
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();
if(feedbackQuestions.Length==0)
{
imp.Error = $"未能识别到有效的赴校问题信息";
errorExcelInfo.Add(imp);
continue;
}
//沟通时间/执行记录
var regex1 = new Regex(@"(.*?)(.*?)(?=|$)", RegexOptions.Singleline);
var record = new List<RecordDto>();
if (!string.IsNullOrEmpty(imp.Record))
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,
}
};
business.SolutionEnd = business.SolutionRecord.EndRecordTime != null;
insertInfo.Add(business);
};
if (errorExcelInfo.Count != 0)
return File(errorExcelInfo.ExportExcel(), "application/ms-excel"
, $"错误赴校信息{DateTime.Now.ToString("MMddHHmm")}.xlsx");
//写入数据库
await baseService.InsertRangeAsync(insertInfo);
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", "导入赴校信息模板.xlsx");
}
/// <summary>
/// 下载导出模板
/// </summary>
/// <returns></returns>
[HttpGet, ResultIgnore, AllowAnonymous]
public async Task<IActionResult> ExportTemplate()
{
var qData = await baseService.AsQueryable()
.Where(s => true)
.ToArrayAsync();
//todo完善数据转换
var resultList = new List<SchoolBusinessImport>() { new SchoolBusinessImport() { } };
return File(resultList.ExportExcel(), "application/ms-excel", "导入赴校信息模板.xlsx");
}
}
}