新增 导入excel 更新学生信息接口
This commit is contained in:
parent
3d3ba31391
commit
7d5f8984df
|
|
@ -31,13 +31,20 @@ namespace Learn.Archives.API.Controllers
|
||||||
readonly Repository<Position> positionService;
|
readonly Repository<Position> positionService;
|
||||||
readonly UserCenterService _userCenterService;
|
readonly UserCenterService _userCenterService;
|
||||||
readonly LiveUserInfo userInfo;
|
readonly LiveUserInfo userInfo;
|
||||||
public StudentController(Repository<Student> baseService, LiveUserInfo userInfo, IHttpContextAccessor httpContextAccessor, UserCenterService userCenterService, Repository<Position> positionService) : base(baseService)
|
readonly Repository<School> schoolService;
|
||||||
|
readonly Repository<Classes> classesService;
|
||||||
|
readonly Repository<User> userService;
|
||||||
|
|
||||||
|
public StudentController(Repository<Student> baseService, LiveUserInfo userInfo, IHttpContextAccessor httpContextAccessor, UserCenterService userCenterService, Repository<Position> positionService, Repository<School> schoolService, Repository<Classes> classesService, Repository<User> userService) : base(baseService)
|
||||||
{
|
{
|
||||||
this.baseService = baseService;
|
this.baseService = baseService;
|
||||||
this.userInfo = userInfo;
|
this.userInfo = userInfo;
|
||||||
_httpContextAccessor = httpContextAccessor;
|
_httpContextAccessor = httpContextAccessor;
|
||||||
_userCenterService = userCenterService;
|
_userCenterService = userCenterService;
|
||||||
this.positionService = positionService;
|
this.positionService = positionService;
|
||||||
|
this.schoolService = schoolService;
|
||||||
|
this.classesService = classesService;
|
||||||
|
this.userService = userService;
|
||||||
}
|
}
|
||||||
|
|
||||||
[NonAction]
|
[NonAction]
|
||||||
|
|
@ -66,7 +73,7 @@ namespace Learn.Archives.API.Controllers
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<long[]> PosititonIds(PositionIdsReq[] data)
|
public async Task<long[]> PosititonIds(PositionIdsReq[] data)
|
||||||
{
|
{
|
||||||
if (data == null || data.Count()==0)
|
if (data == null || data.Count() == 0)
|
||||||
Oh.ModelError("无效数据");
|
Oh.ModelError("无效数据");
|
||||||
var query = new Expressionable<Position>();
|
var query = new Expressionable<Position>();
|
||||||
foreach (var pos in data)
|
foreach (var pos in data)
|
||||||
|
|
@ -101,7 +108,7 @@ namespace Learn.Archives.API.Controllers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<long> EditInfo(Student e)
|
public async Task<long> EditInfo(Student e)
|
||||||
{
|
{
|
||||||
if (e == null || e.UserCenterId == 0)
|
if (e == null || e.UserCenterId == 0)
|
||||||
Oh.ModelError("无效数据");
|
Oh.ModelError("无效数据");
|
||||||
|
|
@ -113,7 +120,7 @@ namespace Learn.Archives.API.Controllers
|
||||||
{
|
{
|
||||||
e.Id = has.Id;
|
e.Id = has.Id;
|
||||||
e.UserCenterId = has.UserCenterId;
|
e.UserCenterId = has.UserCenterId;
|
||||||
await baseService.UpdateAsync(e);
|
await baseService.UpdateAsync(e);
|
||||||
}
|
}
|
||||||
return e.Id;
|
return e.Id;
|
||||||
}
|
}
|
||||||
|
|
@ -124,9 +131,9 @@ namespace Learn.Archives.API.Controllers
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public new async Task<PageResult<StudentInfoRes>> PageList(object data)
|
public new async Task<PageResult<StudentInfoRes>> PageList(object data)
|
||||||
{
|
{
|
||||||
var apiRes =await _userCenterService.CallAPI_GetPageUserList(_httpContextAccessor.HttpContext);
|
var apiRes = await _userCenterService.CallAPI_GetPageUserList(_httpContextAccessor.HttpContext);
|
||||||
var res= new PageResult<StudentInfoRes>() { Data = new List<StudentInfoRes>() };
|
var res = new PageResult<StudentInfoRes>() { Data = new List<StudentInfoRes>() };
|
||||||
if (apiRes == null|| apiRes.Data.Count == 0) return res;
|
if (apiRes == null || apiRes.Data.Count == 0) return res;
|
||||||
res.Total = apiRes.Total;
|
res.Total = apiRes.Total;
|
||||||
var uids = apiRes.Data.Select(s => s.Id).ToArray();
|
var uids = apiRes.Data.Select(s => s.Id).ToArray();
|
||||||
var exData = await baseService.AsQueryable()
|
var exData = await baseService.AsQueryable()
|
||||||
|
|
@ -138,8 +145,8 @@ namespace Learn.Archives.API.Controllers
|
||||||
res.Data.Add(item);
|
res.Data.Add(item);
|
||||||
if (!exDataDic.ContainsKey(item.Id)) continue;
|
if (!exDataDic.ContainsKey(item.Id)) continue;
|
||||||
var ex = exDataDic[item.Id].First();
|
var ex = exDataDic[item.Id].First();
|
||||||
item.Status = ex.Status??UserStatusEnum.未录入;
|
item.Status = ex.Status ?? UserStatusEnum.未录入;
|
||||||
item.ExitTime = ex.ExitTime==null? string.Empty : ex.ExitTime?.ToString("yyyy-MM-dd");
|
item.ExitTime = ex.ExitTime == null ? string.Empty : ex.ExitTime?.ToString("yyyy-MM-dd");
|
||||||
item.JoinTime = ex.JoinTime == null ? string.Empty : ex.JoinTime?.ToString("yyyy-MM-dd");
|
item.JoinTime = ex.JoinTime == null ? string.Empty : ex.JoinTime?.ToString("yyyy-MM-dd");
|
||||||
|
|
||||||
item.AmountRelief = ex.AmountRelief;
|
item.AmountRelief = ex.AmountRelief;
|
||||||
|
|
@ -196,9 +203,9 @@ namespace Learn.Archives.API.Controllers
|
||||||
}
|
}
|
||||||
//基础信息校验
|
//基础信息校验
|
||||||
if (string.IsNullOrEmpty(s.UserType) ||
|
if (string.IsNullOrEmpty(s.UserType) ||
|
||||||
string.IsNullOrEmpty(s.School)||
|
string.IsNullOrEmpty(s.School) ||
|
||||||
(s.UserType != "年级主任" && string.IsNullOrEmpty(s.Class)) ||
|
(s.UserType != "年级主任" && string.IsNullOrEmpty(s.Class)) ||
|
||||||
string.IsNullOrEmpty(s.Phone)||
|
string.IsNullOrEmpty(s.Phone) ||
|
||||||
string.IsNullOrEmpty(s.RealName)
|
string.IsNullOrEmpty(s.RealName)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|
@ -207,7 +214,7 @@ namespace Learn.Archives.API.Controllers
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//无效的任教学科
|
//无效的任教学科
|
||||||
if (!string.IsNullOrEmpty(s.Subject) && s.Subject.ToEnum<SubjectEnum>()==null)
|
if (!string.IsNullOrEmpty(s.Subject) && s.Subject.ToEnum<SubjectEnum>() == null)
|
||||||
{
|
{
|
||||||
s.ErrorMsg = "无效的任教学科";
|
s.ErrorMsg = "无效的任教学科";
|
||||||
impError.Add(s);
|
impError.Add(s);
|
||||||
|
|
@ -275,13 +282,13 @@ namespace Learn.Archives.API.Controllers
|
||||||
foreach (var s in dataList)
|
foreach (var s in dataList)
|
||||||
{
|
{
|
||||||
var ginfo = GradeHelper.GetStudentGradeBaseByGrade(s.Grade);
|
var ginfo = GradeHelper.GetStudentGradeBaseByGrade(s.Grade);
|
||||||
if(ginfo is null)
|
if (ginfo is null)
|
||||||
{
|
{
|
||||||
s.ErrorMsg = "未能识别的[初一/初2028] [年级范围应当是当前有效的就读年级]";
|
s.ErrorMsg = "未能识别的[初一/初2028] [年级范围应当是当前有效的就读年级]";
|
||||||
impError.Add(s);
|
impError.Add(s);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
var gStr = GradeHelper.GetGrade(ginfo.GradeLevel, ginfo.GradeYear);
|
var gStr = GradeHelper.GetGrade(ginfo.GradeLevel, ginfo.GradeYear);
|
||||||
if (gStr.ToEnum<GradeEnum>() == null) //无效的传入年级
|
if (gStr.ToEnum<GradeEnum>() == null) //无效的传入年级
|
||||||
{
|
{
|
||||||
s.ErrorMsg = "无效的年级 例如[初一/初2028] [年级范围应当是当前有效的就读年级]";
|
s.ErrorMsg = "无效的年级 例如[初一/初2028] [年级范围应当是当前有效的就读年级]";
|
||||||
|
|
@ -290,16 +297,16 @@ namespace Learn.Archives.API.Controllers
|
||||||
}
|
}
|
||||||
insertInfo.Add(new Student()
|
insertInfo.Add(new Student()
|
||||||
{
|
{
|
||||||
AmountRelief = decimal.TryParse(s.AmountRelief,out decimal v)?v:0,
|
AmountRelief = decimal.TryParse(s.AmountRelief, out decimal v) ? v : 0,
|
||||||
ExitTime = s.ExitTime,
|
ExitTime = s.ExitTime,
|
||||||
Remark = s.Remark,
|
Remark = s.Remark,
|
||||||
JoinTime = s.JoinTime,
|
JoinTime = s.JoinTime,
|
||||||
Status = s.Status.ToEnum<UserStatusEnum>()??default,
|
Status = s.Status.ToEnum<UserStatusEnum>() ?? default,
|
||||||
UserCenterId = s.Id,
|
UserCenterId = s.Id,
|
||||||
ReliefApplication=s.ReliefApplication?.Contains("已申请")??false,
|
ReliefApplication = s.ReliefApplication?.Contains("已申请") ?? false,
|
||||||
ReliefSubTime = s.ReliefSubTime,
|
ReliefSubTime = s.ReliefSubTime,
|
||||||
ReliefType =s.ReliefType,
|
ReliefType = s.ReliefType,
|
||||||
StudentType =s.StudentType.ToEnum<StudentTypeEnum>(),
|
StudentType = s.StudentType.ToEnum<StudentTypeEnum>(),
|
||||||
});
|
});
|
||||||
var addExcelUser = new UserExcelExportData()
|
var addExcelUser = new UserExcelExportData()
|
||||||
{
|
{
|
||||||
|
|
@ -314,7 +321,7 @@ namespace Learn.Archives.API.Controllers
|
||||||
Stages = s.Grade.Contains("初") ? StudentStagesEnum.初中.ToString() : StudentStagesEnum.高中.ToString()
|
Stages = s.Grade.Contains("初") ? StudentStagesEnum.初中.ToString() : StudentStagesEnum.高中.ToString()
|
||||||
};
|
};
|
||||||
userCenterImp.Add(addExcelUser);
|
userCenterImp.Add(addExcelUser);
|
||||||
if (!string.IsNullOrWhiteSpace(s.高考选科))
|
if (!string.IsNullOrWhiteSpace(s.高考选科))
|
||||||
addExcelUser.GKSubject = s.高考选科;
|
addExcelUser.GKSubject = s.高考选科;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -322,12 +329,12 @@ namespace Learn.Archives.API.Controllers
|
||||||
if (impError.Count() != 0)
|
if (impError.Count() != 0)
|
||||||
return File(impError.ExportExcel(), "application/ms-excel", $"导入错误学生{DateTime.Now.ToString("MMddHHmm")}.xlsx");
|
return File(impError.ExportExcel(), "application/ms-excel", $"导入错误学生{DateTime.Now.ToString("MMddHHmm")}.xlsx");
|
||||||
//调用 用户中心 导入接口
|
//调用 用户中心 导入接口
|
||||||
var importRes = await _userCenterService.CallAPI_ImportJsonData(_httpContextAccessor.HttpContext, userCenterImp);
|
var importRes = await _userCenterService.CallAPI_ImportJsonData(_httpContextAccessor.HttpContext, userCenterImp);
|
||||||
|
|
||||||
var hUAccount = importRes.InsertUsers.ToDictionary(s=>s.Account,s=>s.Id);
|
var hUAccount = importRes.InsertUsers.ToDictionary(s => s.Account, s => s.Id);
|
||||||
//基于结果判断添加成功`
|
//基于结果判断添加成功`
|
||||||
insertInfo = insertInfo
|
insertInfo = insertInfo
|
||||||
.Where(s => hUAccount.ContainsKey(s.UserCenterId.ToString())).Select(s =>
|
.Where(s => hUAccount.ContainsKey(s.UserCenterId.ToString())).Select(s =>
|
||||||
{
|
{
|
||||||
s.UserCenterId = hUAccount[s.UserCenterId.ToString()];
|
s.UserCenterId = hUAccount[s.UserCenterId.ToString()];
|
||||||
return s;
|
return s;
|
||||||
|
|
@ -352,37 +359,135 @@ namespace Learn.Archives.API.Controllers
|
||||||
[HttpLogEnable]
|
[HttpLogEnable]
|
||||||
public async Task<IActionResult> ImportUpdateStudent(IFormFile? file)
|
public async Task<IActionResult> ImportUpdateStudent(IFormFile? file)
|
||||||
{
|
{
|
||||||
var dataList = await _httpContextAccessor
|
var dataList = await _httpContextAccessor.ParsingExcelAsync<StudentInfoImportError>();
|
||||||
.ParsingExcelAsync<StudentInfoImportError>();
|
if (dataList == null || dataList.Length == 0)
|
||||||
if (dataList == null || dataList.Count() == 0)
|
|
||||||
Oh.ModelError("导入失败:无有效数据");
|
Oh.ModelError("导入失败:无有效数据");
|
||||||
var insertInfo = new List<Student>();
|
|
||||||
|
var updateArr = new List<Student>();
|
||||||
var impError = new List<StudentInfoImportError>();
|
var impError = new List<StudentInfoImportError>();
|
||||||
var userCenterImp = new List<UserExcelExportData>();
|
var userCenterUpdate = new List<User>();
|
||||||
|
|
||||||
|
var db = schoolService.Context;
|
||||||
|
var schoolDic = await schoolService.AsQueryable().ToDictionaryAsync(s => s.Name, s => s.Id);
|
||||||
|
|
||||||
//如果有错误数据则返回excel
|
foreach (var schoolGroup in dataList.GroupBy(s => s.School))
|
||||||
if (impError.Count() != 0)
|
{
|
||||||
return File(impError.ExportExcel(), "application/ms-excel", $"导入错误学生{DateTime.Now.ToString("MMddHHmm")}.xlsx");
|
if (!schoolDic.ContainsKey(schoolGroup.Key))
|
||||||
//调用 用户中心 导入接口
|
Oh.ModelError($"未能识别的学校[{schoolGroup.Key}]");
|
||||||
var importRes = await _userCenterService.CallAPI_ImportJsonData(_httpContextAccessor.HttpContext, userCenterImp);
|
var schoolId = (long)schoolDic[schoolGroup.Key];
|
||||||
|
var names = schoolGroup.Select(r => r.RealName)
|
||||||
var hUAccount = importRes.InsertUsers.ToDictionary(s => s.Account, s => s.Id);
|
.Where(n => !string.IsNullOrWhiteSpace(n)).Distinct().ToList();
|
||||||
//基于结果判断添加成功`
|
var userDic = new Dictionary<string, long>();
|
||||||
insertInfo = insertInfo
|
for (int i = 0; i < names.Count; i += 500)
|
||||||
.Where(s => hUAccount.ContainsKey(s.UserCenterId.ToString())).Select(s =>
|
|
||||||
{
|
{
|
||||||
s.UserCenterId = hUAccount[s.UserCenterId.ToString()];
|
var batch = names.Skip(i).Take(500).ToList();
|
||||||
return s;
|
var part = await db.Queryable<School>()
|
||||||
}).ToList();
|
.LeftJoin<Classes>((s, c) => c.SchoolId == s.Id)
|
||||||
//部分成功的数据写入数据库
|
.LeftJoin<Position>((s, c, p) => p.ClassId == c.Id)
|
||||||
await baseService.InsertRangeAsync(insertInfo);
|
.LeftJoin<PositionRelation>((s, c, p, pr) => pr.PositionId == p.Id)
|
||||||
//处理数据
|
.LeftJoin<User>((s, c, p, pr, u) => u.Id == pr.UserId)
|
||||||
var errorExcelInfo = importRes.ErrorExcelExport.Select(s => s.Adapt<StudentInfoImportError>());
|
.Where((s, c, p, pr, u) =>
|
||||||
//如果有错误数据则返回excel
|
s.Id == schoolId &&
|
||||||
if (errorExcelInfo.Count() != 0)
|
u.UserType == UserTypeEnum.学生.GetHashCode() &&
|
||||||
return File(errorExcelInfo.ExportExcel(), "application/ms-excel", $"导入错误学生{DateTime.Now.ToString("MMddHHmm")}.xlsx");
|
batch.Contains(u.RealName) &&
|
||||||
|
s.Enable && p.Enable && pr.Enable &&
|
||||||
|
!p.DeleteState && !c.DeleteState && !u.DeleteState && !s.DeleteState)
|
||||||
|
.Select((s, c, p, pr, u) => new
|
||||||
|
{
|
||||||
|
Name = c.GradeLevel.ToString() + c.GraduationYear.ToString() + c.Name + u.RealName,
|
||||||
|
u.Id,
|
||||||
|
}).ToListAsync();
|
||||||
|
foreach (var kv in part)
|
||||||
|
userDic[kv.Name] = (long)kv.Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var row in schoolGroup)
|
||||||
|
{
|
||||||
|
var ginfo = GradeHelper.GetStudentGradeBaseByGrade(row.Grade);
|
||||||
|
var key = (ginfo?.GradeLevel.ToString() ?? string.Empty) + (ginfo?.GradeYear.ToString() ?? string.Empty) + row.Class + row.RealName;
|
||||||
|
if (!userDic.ContainsKey(key))
|
||||||
|
{
|
||||||
|
row.ErrorMsg = "未能匹配到学校下对应的班级学生";
|
||||||
|
impError.Add(row);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var uid = (long)userDic[key];
|
||||||
|
var u = new Student()
|
||||||
|
{
|
||||||
|
UserCenterId = uid,
|
||||||
|
AmountRelief = decimal.TryParse(row.AmountRelief, out var v) ? v : 0,
|
||||||
|
ReliefApplication = row.ReliefApplication?.Contains("已申请") ?? false,
|
||||||
|
ReliefType = row.ReliefType,
|
||||||
|
ReliefSubTime = row.ReliefSubTime,
|
||||||
|
StudentType = row.StudentType.ToEnum<StudentTypeEnum>(),
|
||||||
|
Status = row.Status.ToEnum<UserStatusEnum>(),
|
||||||
|
JoinTime = row.JoinTime,
|
||||||
|
ExitTime = row.ExitTime,
|
||||||
|
Remark = row.Remark,
|
||||||
|
};
|
||||||
|
updateArr.Add(u);
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(row.高考选科))
|
||||||
|
{
|
||||||
|
var gradeStd = ginfo is null ? row.Grade : GradeHelper.GetGrade(ginfo.GradeLevel, ginfo.GradeYear);
|
||||||
|
var subjectArr = row.高考选科.Split(",")
|
||||||
|
.Select(s => s.ToEnum<SubjectEnum>())
|
||||||
|
.ToArray();
|
||||||
|
userCenterUpdate.Add(new User
|
||||||
|
{
|
||||||
|
Id = uid,
|
||||||
|
Phone = row.Phone,
|
||||||
|
GLSubject = (int?)subjectArr[0],
|
||||||
|
GSubject1 = (int?)subjectArr[1],
|
||||||
|
GSubject2 = (int?)subjectArr[2],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (impError.Count != 0)
|
||||||
|
return File(impError.ExportExcel(), "application/ms-excel", $"导入错误学生{DateTime.Now.ToString("MMddHHmm")}.xlsx");
|
||||||
|
|
||||||
|
if (updateArr.Count > 0)
|
||||||
|
{
|
||||||
|
await baseService.AsUpdateable(updateArr)
|
||||||
|
.UpdateColumns(s => new
|
||||||
|
{
|
||||||
|
s.AmountRelief,
|
||||||
|
s.ReliefApplication,
|
||||||
|
s.ReliefType,
|
||||||
|
s.ReliefSubTime,
|
||||||
|
s.StudentType,
|
||||||
|
s.Status,
|
||||||
|
s.JoinTime,
|
||||||
|
s.ExitTime,
|
||||||
|
s.Remark
|
||||||
|
})
|
||||||
|
//更新忽略null并且忽略默认值 (比如int默认值是0就不更新)
|
||||||
|
.IgnoreColumns(ignoreAllNullColumns: true, ignoreAllDefaultValue: true)
|
||||||
|
.WhereColumns(it => new { it.UserCenterId })
|
||||||
|
.ExecuteCommandAsync();
|
||||||
|
}
|
||||||
|
if (userCenterUpdate.Count > 0)
|
||||||
|
await userService.AsUpdateable(userCenterUpdate)
|
||||||
|
.UpdateColumns(s => new
|
||||||
|
{
|
||||||
|
s.Phone,
|
||||||
|
s.GLSubject,
|
||||||
|
s.GSubject1,
|
||||||
|
s.GSubject2,
|
||||||
|
})
|
||||||
|
//更新忽略null并且忽略默认值 (比如int默认值是0就不更新)
|
||||||
|
.IgnoreColumns(ignoreAllNullColumns: true, ignoreAllDefaultValue: true)
|
||||||
|
.WhereColumns(it => new { it.Id })
|
||||||
|
.ExecuteCommandAsync();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Oh.Error("需要测试");
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -411,7 +516,7 @@ namespace Learn.Archives.API.Controllers
|
||||||
Remark="选填",
|
Remark="选填",
|
||||||
高考选科 = "填写学科\r\n 例如 物理 \r\n多学科情况下 物理,化学,生物"
|
高考选科 = "填写学科\r\n 例如 物理 \r\n多学科情况下 物理,化学,生物"
|
||||||
} };
|
} };
|
||||||
return File(resultList.ExportExcel(), "application/ms-excel",
|
return File(resultList.ExportExcel(), "application/ms-excel",
|
||||||
$"导入学生模板{DateTime.Now.ToString("MMddHHmm")}.xlsx");
|
$"导入学生模板{DateTime.Now.ToString("MMddHHmm")}.xlsx");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -425,7 +530,7 @@ namespace Learn.Archives.API.Controllers
|
||||||
[HttpGet, ResultIgnore, AllowAnonymous]
|
[HttpGet, ResultIgnore, AllowAnonymous]
|
||||||
public IActionResult DwImportTeacherTemplate()
|
public IActionResult DwImportTeacherTemplate()
|
||||||
{
|
{
|
||||||
var resultList = new List<TeacherInfoImport>() {
|
var resultList = new List<TeacherInfoImport>() {
|
||||||
new TeacherInfoImport()
|
new TeacherInfoImport()
|
||||||
{
|
{
|
||||||
Phone="必填",
|
Phone="必填",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue