package com.template.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qcloud.cos.utils.IOUtils;
import com.seewo.open.sdk.DefaultSeewoClient;
import com.seewo.open.sdk.SeewoClient;
import com.seewo.open.sdk.auth.Account;
import com.template.annotation.DESRespondSecret;
import com.template.annotation.PassToken;
import com.template.api.SmartUserControllerAPI;
import com.template.common.utils.*;
import com.template.config.ControlConfig;
import com.template.config.SeewoConfig;
import com.template.model.enumModel.*;
import com.template.model.evaluate.teacher.SmartEvaluateTeacher;
import com.template.model.pojo.*;
import com.template.model.request.*;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.seewo.*;
import com.template.model.vo.*;
import com.template.services.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipInputStream;
import static com.template.common.utils.AesTestOne.decrypt;
/**
*
* 前端控制器
*
*
* @author ceshi
* @since 2023-12-04
*/
@RestController
//返回参数加密注解
@DESRespondSecret
public class SmartUserController implements SmartUserControllerAPI {
@Resource
private SeewoConfig seewoConfig;
@Resource
private ControlConfig controlConfig;
@Autowired
private SmartUserService smartUserService;
@Autowired
private SmartGradeService smartGradeService;
@Autowired
private SmartClassService smartClassService;
@Autowired
private SmartUploadService smartUploadService;
@Autowired
private SmartDutiesService smartDutiesService;
@Autowired
private SmartDepartmentService smartDepartmentService;
@Autowired
private SmartIdentityService smartIdentityService;
@Autowired
private SmartTimeGroupService smartTimeGroupService;
@Autowired
private SmartAuthorityService smartAuthorityService;
@Autowired
private SmartFamilyIndexService smartFamilyIndexService;
@Autowired
private SmartTeachingService smartTeachingService;
@Autowired
private SmartAuthorGroupService smartAuthorGroupService;
@Autowired
private SmartSemesterService smartSemesterService;
@Autowired
private SmartSubjectService smartSubjectService;
@Autowired
private SmartEvaluateTeacherService smartEvaluateTeacherService;
@Autowired
private SmartOperationUserService smartOperationUserService;
private static Logger logger = LoggerFactory.getLogger(SmartUserController.class);
//region 开发自己导数据用的接口
@Override
public CommonResult testDeleteUserById(int userId) throws Exception {
SmartUser user = smartUserService.getSmartById(userId);
bsDeleteStudent(user);
return CommonResult.ok();
}
@Override
public CommonResult insertSmartUserPhoto() throws JsonProcessingException {
Integer userType = eSeewoUserType.Student.getValue();
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
List students = smartUserService.queryStudentDatas();
List photoList = new ArrayList<>();
for (SmartUser student : students) {
PhotoServiceSavePhotosParam.ThirdSavePhotoQuery photo = new PhotoServiceSavePhotosParam.ThirdSavePhotoQuery();
photo.setPhotoUrl(student.getHeadImage());
photo.setUserCode(student.getCardNo());
photoList.add(photo);
}
int num = (int) Math.ceil((double) photoList.size() / 1000);
for (int count = 1; count <= num; count++) {
int startIndex = (count - 1) * 1000;
int endIndex = count * 1000;
if (count == num) {
endIndex = startIndex + (photoList.size() % 1000);
}
List currentPhotoList = photoList.subList(startIndex, endIndex);//结尾不包含下标1000
CommonResult result = SeewoInsertBatchPhoto(seewoClient, currentPhotoList, userType);
if (!result.isSuccess()) {
return result;
}
}
return CommonResult.ok("图片上传成功");
}
@Override
public CommonResult importExcelUserBsno(MultipartFile excelFile) throws IOException {
//先解析excel如果excel不满足格式就提示错误信息 避免提前占用带宽上传头像
if (excelFile.isEmpty() || excelFile.getSize() == 0) {
return CommonResult.fail("压缩包中的excel文件不能为空");
}
List result = new ArrayList<>();
String ContentType = excelFile.getContentType();
InputStream inputStream = excelFile.getInputStream();
if (ContentType.equals(eFileType.Xlsx.getValue())) {
CommonResult> resultData = readBsXlsx(inputStream);
if (!resultData.isSuccess()) {
return resultData;
}
result = resultData.getData();
if (result == null) {
return CommonResult.fail("文档内容为空,导入失败");
}
} else {
return CommonResult.fail("用户导入只支持Xlsx");
}
List gradeIds = result.stream().map(SmartUser::getGrade).distinct().collect(Collectors.toList());
if (gradeIds != null && gradeIds.size() > 0) {
List users = smartUserService.queryStudentsByGrade(gradeIds);
for (SmartUser user : users) {
Optional resultData = result.stream().filter(e -> e.getCardNo().equals(user.getCardNo()) && e.getName().equals(user.getName())).findFirst();
if (resultData != null && resultData.isPresent()) {
user.setBsStudentNo(resultData.get().getBsStudentNo());
}
}
if (users != null && users.size() > 0) {
boolean insertData = smartUserService.updateBatchById(users);
if (!insertData) {
return CommonResult.fail("导入失败");
}
}
}
return CommonResult.ok("导入成功");
}
/**
* Xlsx文件读取方法ss
*
* @param inputStream 文件流
* @return
* @throws IOException
*/
private CommonResult> readBsXlsx(InputStream inputStream) throws IOException {
List users = new ArrayList<>();
List idCards = new ArrayList<>();
List cardNos = new ArrayList<>();
XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
List grades = smartGradeService.list(null); //年级
//读取第一张sheet
XSSFSheet sheetAt = sheets.getSheetAt(0);
DataFormatter dataFormatter = new DataFormatter();
try {
for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
XSSFRow row = sheetAt.getRow(rowNum);
if (row != null) {
//使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
//所以先使用setCellType()方法先将该单元格的类型设置为STRING
//然后poi会根据字符串读取它
//标题 校验
if (rowNum == 0) {
String schoolClass = dataFormatter.formatCellValue(row.getCell(0));//年级
if (!schoolClass.equals("年级")) {
return CommonResult.fail("导入数据第一列为年级");
}
String cardNo = dataFormatter.formatCellValue(row.getCell(3));//学号
if (!cardNo.equals("学号")) {
return CommonResult.fail("导入数据第四列为学号");
}
String name = dataFormatter.formatCellValue(row.getCell(4));//姓名
if (!name.equals("姓名")) {
return CommonResult.fail("导入数据第五列为姓名");
}
String sex = dataFormatter.formatCellValue(row.getCell(5));//学生编码
if (!sex.equals("学生编码")) {
return CommonResult.fail("导入数据第六列为学生编码");
}
} else {
SmartUser user = new SmartUser();
String name = dataFormatter.formatCellValue(row.getCell(4));
if (ObjectUtils.isEmpty(name)) {
return CommonResult.fail("第" + (rowNum + 1) + "条数据的姓名不能为空");
}
String schoolGrade = dataFormatter.formatCellValue(row.getCell(0));//年级
if (ObjectUtils.isEmpty(schoolGrade)) {
return CommonResult.fail(name + "的年级不能为空");
}
String cardNo = dataFormatter.formatCellValue(row.getCell(3));//学号
if (ObjectUtils.isEmpty(cardNo)) {
return CommonResult.fail(name + "的学号不能为空");
}
//学号重复判断
if (!ObjectUtils.isEmpty(cardNo)) {
cardNos.add(cardNo);
}
if (cardNos.stream().distinct().count() != cardNos.size()) {
return CommonResult.fail("导入的Excel中," + name + "的学号:" + cardNo + "存在重复数据");
}
String studentNo = dataFormatter.formatCellValue(row.getCell(5));//学生编码
if (ObjectUtils.isEmpty(studentNo)) {
return CommonResult.fail(name + "的学生编码不能为空");
}
Optional oGrade = grades.stream().filter(e -> e.getName().equals(schoolGrade)).findFirst();
if (oGrade != null && oGrade.isPresent()) {
Integer gradeId = oGrade.get().getId();
user.setGrade(String.valueOf(gradeId));
} else {
return CommonResult.fail(name + "的年级数据无效,导入失败");
}
user.setName(name);
user.setBsStudentNo(studentNo);
user.setCardNo(cardNo);
users.add(user);
}
}
}
} catch (Exception e) {
return CommonResult.fail("请按模板格式导入数据");
}
return CommonResult.ok(users);
}
@Override
public CommonResult importExcelFamilys(MultipartFile excelFile) throws Exception {
//先解析excel如果excel不满足格式就提示错误信息 避免提前占用带宽上传头像
if (excelFile.isEmpty() || excelFile.getSize() == 0) {
return CommonResult.fail("压缩包中的excel文件不能为空");
}
String ContentType = excelFile.getContentType();
InputStream inputStream = excelFile.getInputStream();
List result = new ArrayList<>();
CommonResult> resultData = readFamilyXlsx(inputStream);
if (!resultData.isSuccess()) {
return resultData;
}
result = resultData.getData();
boolean insertBatch = smartFamilyIndexService.saveBatch(result);
return insertBatch ? CommonResult.ok("操作成功") : CommonResult.fail("操作失败");
}
/**
* Xlsx文件读取方法
*
* @param inputStream 文件流
* @return
* @throws IOException
*/
private CommonResult> readFamilyXlsx(InputStream inputStream) throws IOException {
List result = new ArrayList<>();
XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
//读取第一张sheet
XSSFSheet sheetAt = sheets.getSheetAt(0);
DataFormatter dataFormatter = new DataFormatter();
try {
for (int rowNum = 1; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
XSSFRow row = sheetAt.getRow(rowNum);
if (row != null) {
//使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
//所以先使用setCellType()方法先将该单元格的类型设置为STRING
//然后poi会根据字符串读取它
//标题 校验
if (rowNum < 3) {
//不做校验处理了
} else {
String cardNo = dataFormatter.formatCellValue(row.getCell(4));//学号
if (ObjectUtils.isEmpty(cardNo)) {
continue;
}
String phone1 = dataFormatter.formatCellValue(row.getCell(10));//手机号1
if (!ObjectUtils.isEmpty(phone1)) {
SmartFamilyIndex index = new SmartFamilyIndex();
index.setStudentNo(cardNo);
index.setParentPhone(phone1);
index.setIndexData(0);
result.add(index);
}
String phone2 = dataFormatter.formatCellValue(row.getCell(12));//手机号1
if (!ObjectUtils.isEmpty(phone1)) {
SmartFamilyIndex index = new SmartFamilyIndex();
index.setStudentNo(cardNo);
index.setParentPhone(phone1);
index.setIndexData(1);
result.add(index);
}
}
}
}
} catch (Exception e) {
return CommonResult.fail("请按模板格式导入数据");
}
return CommonResult.ok(result);
}
/**
* 批量导入压缩包用户信息
* 以身份证号作为判断依据 如果存在重复数据就提示存在重复数据
* 开发流程:建议先去拿到excel中的所有数据的身份证号去数据库中找一遍是否存在重复数据
* 如果不存在重复数据就把附件文件夹中的附件文件上传到cos服务器上
* 然后手动拼接一个人脸照片文件地址存入对应的数据中
*
* @param zipFile zip压缩包
* @return
*/
@Override
@DESRespondSecret(validated = true)
public CommonResult importZipUsers(MultipartFile zipFile) throws IOException {
List result = new ArrayList<>();
MultipartFile excelFile = null;
List multipartFileHashMap = new ArrayList<>();
String excelStr = null;
if (zipFile != null) {
//解压压缩文件并上传文件
ByteArrayOutputStream byteOut = null;
ZipInputStream zipIn = null;
try {
java.util.zip.ZipEntry zipEntry = null;
zipIn = new ZipInputStream(zipFile.getInputStream(), Charset.forName("GBK"));
while ((zipEntry = zipIn.getNextEntry()) != null) {
String fileName = zipEntry.getName();
if (fileName.contains("人脸照片") && (fileName.endsWith(".jpg") || fileName.endsWith(".png") || fileName.endsWith(".jpeg"))) {
byteOut = new ByteArrayOutputStream();
IOUtils.copy(zipIn, byteOut);
InputStream inputStream = new ByteArrayInputStream(byteOut.toByteArray());
MultipartFile multipartFile = FileUtils.getMultipartFile(inputStream, fileName);
multipartFileHashMap.add(multipartFile);
} else if (fileName.contains("人员信息采集表") && (fileName.endsWith(".xlsx") || fileName.endsWith(".xls"))) {
excelStr = fileName.endsWith(".xlsx") ? "xlsx" : "xls";
byteOut = new ByteArrayOutputStream();
IOUtils.copy(zipIn, byteOut);
InputStream inputStream = new ByteArrayInputStream(byteOut.toByteArray());
excelFile = FileUtils.getMultipartFile(inputStream, fileName);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (byteOut != null) {
byteOut.close();
}
if (zipIn != null) {
zipIn.close();
}
}
}
//先解析excel如果excel不满足格式就提示错误信息 避免提前占用带宽上传头像
if (excelFile.isEmpty() || excelFile.getSize() == 0) {
return CommonResult.fail("压缩包中的excel文件不能为空");
}
String ContentType = excelFile.getContentType();
InputStream inputStream = excelFile.getInputStream();
//xls格式文件
if (excelStr.equals("xls")) {
CommonResult> resultData = readXls(inputStream);
if (!resultData.isSuccess()) {
return resultData;
}
result = resultData.getData();
} else if (excelStr.equals("xlsx")) {
CommonResult> resultData = readXlsx(inputStream);
if (!resultData.isSuccess()) {
return resultData;
}
result = resultData.getData();
} else {
return CommonResult.fail("用户导入只支持Xls、Xlsx");
}
//cos上传 返回图片地址
String uploadResult = smartUploadService.upload(multipartFileHashMap.toArray(new MultipartFile[0]));
List uploadImages = Arrays.asList(uploadResult.split(","));
for (SmartUser user : result) {
Optional headImage = uploadImages == null ? null : uploadImages.stream().filter(e -> e.equals("https://wanzai-1306339220.cos.ap-shanghai.myqcloud.com/headImage/" + user.getHeadImage())).findFirst();
if (headImage != null && headImage.isPresent()) {
user.setHeadImage(headImage.get());
}
}
//批量存储用户信息
boolean resultBool = smartUserService.saveBatch(result);
return resultBool ? CommonResult.ok("导入成功") : CommonResult.fail("导入失败");
}
//endregion
//region 注销账号
@Override
@DESRespondSecret(validated = true)
public CommonResult logoffAccount(useridsRequest ur, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
String st = paramUtils.getParamError(bindingResult);
return CommonResult.fail(st);
}
int user = smartUserService.getSmartUserCountByIds(ur.getUserIds());
if (user != ur.getUserIds().size()) {
return CommonResult.fail("用户信息已失效");
}
return 0 > 0 ? CommonResult.ok("注销成功") : CommonResult.fail("注销失败");
}
//endregion
//region 移动多个用户到其他部门
@Override
@DESRespondSecret(validated = true)
public CommonResult changeDepartment(changeDepartmentRequest cdr, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
String st = paramUtils.getParamError(bindingResult);
return CommonResult.fail(st);
}
List users = smartUserService.getSmartUserByIds(cdr.getUserIds());
if (users == null) {
return CommonResult.fail("用户信息无效,移动失败!");
}
if (users.size() != cdr.getUserIds().size()) {
return CommonResult.fail("存在无效用户数据,移动失败!");
}
for (SmartUser user : users) {
user.setDepartmentId(cdr.getDepartmentId());
}
boolean result = smartUserService.updateBatchById(users);
return result ? CommonResult.ok("移动成功") : CommonResult.fail("移动失败");
}
//endregion
//region 批量新增用户
//region 批量新增用户方法
@Override
@DESRespondSecret(validated = true)
public CommonResult importExcelUsers(MultipartFile excelFile, String headImage) throws Exception {
List result = new ArrayList<>();
int useXw = 1;
int useBs = 1;
//先解析excel如果excel不满足格式就提示错误信息 避免提前占用带宽上传头像
if (excelFile.isEmpty() || excelFile.getSize() == 0) {
return CommonResult.fail("压缩包中的excel文件不能为空");
}
String ContentType = excelFile.getContentType();
InputStream inputStream = excelFile.getInputStream();
//xls格式文件
if (ContentType.equals(eFileType.Xls.getValue())) {
CommonResult> resultData = readXls(inputStream);
if (!resultData.isSuccess()) {
return resultData;
}
result = resultData.getData();
} else if (ContentType.equals(eFileType.Xlsx.getValue())) {
CommonResult> resultData = readXlsx(inputStream);
if (!resultData.isSuccess()) {
return resultData;
}
result = resultData.getData();
} else {
return CommonResult.fail("用户导入只支持Xls、Xlsx");
}
List uploadImages = Arrays.asList(headImage.split(","));
for (SmartUser user : result) {
Optional image = uploadImages == null ? null : uploadImages.stream().filter(e -> e.equals("https://wanzai-1306339220.cos.ap-shanghai.myqcloud.com/" + user.getHeadImage())).findFirst();
if (image != null && image.isPresent()) {
user.setHeadImage(image.get());
} else {
if (user.getIdentityId().equals(eIdentityStatu.Student.getValue())
|| user.getIdentityId().equals(eIdentityStatu.Teacher.getValue())) {
return CommonResult.fail(user.getName() + "头像不存在,导入失败");
}
}
}
//region 将数据加入第三方
List studentDatas = result.stream().filter(e -> e.getIdentityId().intValue() == eIdentityStatu.Student.getValue()).collect(Collectors.toList());
if (studentDatas != null && studentDatas.size() > 0) {
//判断是否存在重复数据
List cardNos = studentDatas.stream().map(SmartUser::getCardNo).collect(Collectors.toList());
if (cardNos == null) {
return CommonResult.fail("文档内容为空,导入失败");
}
if (cardNos.size() <= 0) {
return CommonResult.fail("文档内容为空,导入失败");
}
List existUsers = smartUserService.querySmartUserByCardNos(cardNos);
if (existUsers != null && existUsers.size() > 0) {
String names = StringUtils.join(existUsers.stream().map(SmartUser::getName).collect(Collectors.toList()), ",");
return CommonResult.fail("系统中已存在" + names + "的信息数据,请勿重复导入");
}
//获取班级Uid
List classDatas = smartClassService.getSmartClasss();
if (classDatas == null) {
return CommonResult.fail("班级数据无效,新增失败");
}
List gradeDatas = smartGradeService.getSmartGrades();
if (gradeDatas == null) {
return CommonResult.fail("年级数据无效,新增失败");
}
HashMap> seewoDatas = new HashMap<>();
List photoList = new ArrayList<>();
for (SmartUser student : studentDatas) {
PhotoServiceSavePhotosParam.ThirdSavePhotoQuery photo = new PhotoServiceSavePhotosParam.ThirdSavePhotoQuery();
photo.setPhotoUrl(student.getHeadImage());
photo.setUserCode(student.getCardNo());
photoList.add(photo);
//region 学生参数必填判断:年级、班级
if (student.getGrade() == null) {
return CommonResult.fail("学生年级不能为空");
}
if (student.getSchoolClass() == null) {
return CommonResult.fail("学生班级不能为空");
}
//endregion
Optional oClassData = classDatas.stream().filter(e -> e.getId().equals(student.getSchoolClass())).findFirst();
if (oClassData == null) {
return CommonResult.fail("班级数据无效,新增失败");
}
Optional oGradeData = gradeDatas.stream().filter(e -> e.getId().intValue() == Integer.valueOf(student.getGrade())).findFirst();
if (oGradeData == null) {
return CommonResult.fail("年级数据无效,新增失败");
}
SmartClass classData = oClassData.get();
SmartGrade gradeData = oGradeData.get();
//region 凑希沃参数
if (useXw == 1) {
if (!seewoDatas.containsKey(classData.getClassUid())) {
seewoDatas.put(classData.getClassUid(), new ArrayList<>());
}
StudentServiceBatchSaveClassStudentsParam.StudentInfo studentSeewo = StudentServiceBatchSaveClassStudentsParam.StudentInfo.builder()
.studentName(student.getName())
.studentCode(student.getCardNo())
.gender(student.getSexId())
.phone(student.getPhone() == null ? "" : student.getPhone())
.build();
seewoDatas.get(classData.getClassUid()).add(studentSeewo);
}
//endregion
//region 百胜新增学生信息
if (useBs == 1) {
/**
* 学生数据的有效期是到毕业年份的8月31日
*/
String startTime = TimeExchange.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
String endTime = queryGraduationYear(gradeData.getGradeNo());
CommonResult insertBsStudent = bsInsertStudent(student.getName(), student.getCardNo(), student.getSexId(), student.getHeadImage(), student.getTimeGroupId(), classData.getBsClassNo(), startTime, endTime, student.getIdCard());
if (!insertBsStudent.isSuccess()) {
return CommonResult.fail("百胜平台:" + insertBsStudent.getMessage());
}
student.setBsStudentNo(insertBsStudent.getData());
}
//endregion
}
if (useXw == 1) {
//初始化客户端
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
List seewoResultDatas = new ArrayList<>();
for (Map.Entry> entry : seewoDatas.entrySet()) {
String classUid = entry.getKey();
CommonResult> insertStudents = SeewoInsertBatchStudent(seewoClient, entry.getValue(), classUid);
if (!insertStudents.isSuccess()) {
return CommonResult.fail("希沃平台:" + insertStudents.getMessage());
}
seewoResultDatas.addAll(insertStudents.getData());
}
for (SmartUser student : studentDatas) {
Optional seewoResultData = seewoResultDatas.stream().filter(e -> e.getStudentCode().equals(student.getCardNo())).findFirst();
if (seewoResultData != null && seewoResultData.isPresent()) {
student.setXwStudentUid(seewoResultData.get().getUserUid());
}
}
}
if (studentDatas != null && studentDatas.size() > 0) {
//批量存储学生信息
boolean resultBool = smartUserService.saveBatch(studentDatas);
if (!resultBool) {
return CommonResult.fail("导入失败");
} else {
// 需添加到海康平台
ArrayList operationUsers = new ArrayList<>();
for (SmartUser studentData : studentDatas) {
Integer id = studentData.getId();
SmartOperationUser smartOperationUser = new SmartOperationUser();
smartOperationUser.setOperationId(id);
smartOperationUser.setOperationMode("1");
smartOperationUser.setStatus(1);
smartOperationUser.setType(studentData.getIdentityId());
operationUsers.add(smartOperationUser);
}
smartOperationUserService.saveBatch(operationUsers);
}
List familyIndexs = new ArrayList<>();
//region 希沃新增编辑学生家长信息
if (useXw == 1) {
int num = (int) Math.ceil((double) studentDatas.size() / 100);
for (int count = 1; count <= num; count++) {
int startIndex = (count - 1) * 100;
int endIndex = count * 100;
if (count == num) {
endIndex = startIndex + (studentDatas.size() % 100);
}
List currentStudentDatas = studentDatas.subList(startIndex, endIndex);//结尾不包含下标100
//学生与家长列表,最大100条
List studentParents = new ArrayList<>();
for (SmartUser student : currentStudentDatas) {
ParentServiceBatchSaveOrUpdateParentsParam.StudentParentsItem students = ParentServiceBatchSaveOrUpdateParentsParam.StudentParentsItem.builder()
.studentCode(student.getCardNo())
.build();
List parentDatas = result.stream().filter(e -> e.getIdentityId().intValue() == eIdentityStatu.Parent.getValue() && e.getAffiliate().equals(student.getCardNo())).collect(Collectors.toList());
List parents = new ArrayList<>();
int i = 0;
for (SmartUser parent : parentDatas) {
SmartFamilyIndex familyIndex = new SmartFamilyIndex();
familyIndex.setParentPhone(parent.getPhone());
familyIndex.setStudentNo(student.getCardNo());
familyIndex.setIndexData(i);
familyIndexs.add(familyIndex);
ParentServiceBatchSaveOrUpdateParentsParam.ParentsItem data = ParentServiceBatchSaveOrUpdateParentsParam.ParentsItem.builder()
.name(parent.getShip())
.phone(parent.getPhone())
.index(i)
.build();
parents.add(data);
++i;
}
if (parents != null && parents.size() > 0) {
studentParents.add(students);
//家长列表,最多4个
students.setParents(parents);
}
}
//初始化客户端
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
ParentServiceBatchSaveOrUpdateParentsParam param = new ParentServiceBatchSaveOrUpdateParentsParam();
//响应体,MimeType为 application/json
ParentServiceBatchSaveOrUpdateParentsParam.RequestBody requestBody = ParentServiceBatchSaveOrUpdateParentsParam.RequestBody.builder()
.build();
param.setRequestBody(requestBody);
//query
ParentServiceBatchSaveOrUpdateParentsParam.Query query = ParentServiceBatchSaveOrUpdateParentsParam.Query.builder()
.appId(seewoConfig.getAppId())
.schoolUid(seewoConfig.getSchoolId())
.build();
requestBody.setQuery(query);
query.setStudentParents(studentParents);
param.setRequestBody(requestBody);
ParentServiceBatchSaveOrUpdateParentsRequest request = new ParentServiceBatchSaveOrUpdateParentsRequest(param);
String jsonString = JSON.toJSONString(request);
logger.info("入参:" + request);
//如果想要调用沙箱环境,请通过设置 request 对象的 serverUrl 属性,如:
//request.setServerUrl("https://openapi.test.seewo.com")
//执行请求,如果想获取到com.seewo.open.sdk.HttpResponse对象,请调用 seewoClient.execute 方法
ParentServiceBatchSaveOrUpdateParentsResult parentResult = seewoClient.invoke(request);
logger.info("出参:" + parentResult);
if (parentResult == null) {
return CommonResult.fail("希沃学生家长数据添加失败!");
}
if (!parentResult.getResponseBody().getCode().equals("000000")) {
return CommonResult.fail("希沃平台:" + parentResult.getResponseBody().getMessage());
}
}
}
//endregion
if (familyIndexs != null && familyIndexs.size() > 0) {
boolean insertBatch = smartFamilyIndexService.saveBatch(familyIndexs);
if (!insertBatch) {
return CommonResult.fail("导入失败!");
}
}
}
//endregion
List parents = result.stream().filter(e -> e.getIdentityId().intValue() == eIdentityStatu.Parent.getValue()).collect(Collectors.toList());
//双胞胎家长
List newParents = new ArrayList<>();
if (cardNos != null && cardNos.size() > 0) {
List students = smartUserService.querySmartUserByCardNos(cardNos);
for (SmartUser parent : parents) {
Optional student = students.stream().filter(e -> e.getCardNo().equals(parent.getAffiliate())).findFirst();
if (student != null && student.isPresent()) {
//先查找是否存在
Optional newData = newParents.stream().filter(e -> e.getName().equals(parent.getName()) && e.getPhone().equals(parent.getPhone()) && e.getShip().equals(parent.getShip())).findFirst();
if (newData != null && newData.isPresent()) {
String affiliate = newData.get().getAffiliate() + "," + student.get().getId();
newData.get().setAffiliate(affiliate);
} else {
SmartUser newParent = new SmartUser();
newParent.setName(parent.getName());
newParent.setDepartmentId(parent.getDepartmentId());
newParent.setPhone(parent.getPhone());
newParent.setIdentityId(parent.getIdentityId());
newParent.setSexId(parent.getSexId());
newParent.setIsCancel(parent.getIsCancel());
newParent.setAffiliate(String.valueOf(student.get().getId()));
newParent.setShip(parent.getShip());
newParents.add(newParent);
}
}
}
if (newParents != null && newParents.size() > 0) {
//批量存储家长信息
boolean resultBool = smartUserService.saveBatch(newParents);
if (!resultBool) {
return CommonResult.fail("导入失败!");
}
}
//region 最后上传学生图片 这样能保证图片上传失败也不影响数据导入
if (useXw == 1) {
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
int num = (int) Math.ceil((double) photoList.size() / 1000);
for (int count = 1; count <= num; count++) {
int startIndex = (count - 1) * 1000;
int endIndex = count * 1000;
if (count == num) {
endIndex = startIndex + (photoList.size() % 1000);
}
List currentPhotoList = photoList.subList(startIndex, endIndex);//结尾不包含下标1000
CommonResult photoResult = SeewoInsertBatchPhoto(seewoClient, currentPhotoList, eSeewoUserType.Student.getValue());
if (!photoResult.isSuccess()) {
return photoResult;
}
}
}
//endregion
}
} else {
List teacherDatas = result.stream().filter(e -> e.getIdentityId().intValue() == eIdentityStatu.Teacher.getValue()).collect(Collectors.toList());
//判断是否存在重复数据
List cardNos = teacherDatas.stream().map(SmartUser::getCardNo).collect(Collectors.toList());
if (cardNos == null) {
return CommonResult.fail("文档内容为空,导入失败");
}
if (cardNos.size() <= 0) {
return CommonResult.fail("文档内容为空,导入失败");
}
List existUsers = smartUserService.querySmartUserByCardNos(cardNos);
if (existUsers != null && existUsers.size() > 0) {
String names = StringUtils.join(existUsers.stream().map(SmartUser::getName).collect(Collectors.toList()), ",");
return CommonResult.fail("系统中已存在" + names + "的信息数据,请勿重复导入");
}
//获取班级Uid
List classDatas = smartClassService.getSmartClasss();
List departmentDatas = smartDepartmentService.list(null);
//region
//region 希沃添加教师数据
if (useXw == 1) {
int num = (int) Math.ceil((double) teacherDatas.size() / 100);
for (int count = 1; count <= num; count++) {
int startIndex = (count - 1) * 100;
int endIndex = count * 100;
if (count == num) {
endIndex = startIndex + (teacherDatas.size() % 100);
}
List currentStudentDatas = teacherDatas.subList(startIndex, endIndex);//结尾不包含下标100
//初始化客户端
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
//上传图片
List photoList = new ArrayList<>();
// 老师列表
List teachers = new ArrayList<>();
for (SmartUser teacher : currentStudentDatas) {
TeacherServiceBatchSaveOrUpdateTeacherParam.SaveOrUpdateTeacherQuery teacherData = TeacherServiceBatchSaveOrUpdateTeacherParam.SaveOrUpdateTeacherQuery.builder()
.account(teacher.getPhone())//用户账号
.name(teacher.getName())//用户名字
.accountType("phone")//账号类型 phone:手机号 email:邮箱
.teacherCode(teacher.getCardNo())//教师工号
.photoUrl(teacher.getHeadImage())//图片链接
.build();
teachers.add(teacherData);
PhotoServiceSavePhotosParam.ThirdSavePhotoQuery photo = new PhotoServiceSavePhotosParam.ThirdSavePhotoQuery();
photo.setPhotoUrl(teacher.getHeadImage());
photo.setUserCode(teacher.getPhone());
photoList.add(photo);
}
CommonResult> insertTeacher = SeewoInsertBatchTeacher(seewoClient, teachers);
if (!insertTeacher.isSuccess()) {
return CommonResult.fail("希沃平台:" + insertTeacher.getMessage());
}
CommonResult insertPhotoResult = SeewoInsertBatchPhoto(seewoClient, photoList, eSeewoUserType.Teacher.getValue());
if (!insertPhotoResult.isSuccess()) {
return insertPhotoResult;
}
for (SmartUser teacher : teacherDatas) {
if (teacher.getDuties().intValue() == eDuties.ClassTeacher.getValue()) {
Optional oClass = classDatas.stream().filter(e -> e.getId().equals(teacher.getSchoolClass())).findFirst();
if (oClass != null && oClass.isPresent()) {
//获取班级Uid
//region 将班主任推送到希沃
if (useXw == 1) {
CommonResult pushMaster = SeewoPushMaster(seewoClient, teacher.getPhone(), oClass.get().getClassUid());
if (!pushMaster.isSuccess()) {
return CommonResult.fail("希沃平台:" + pushMaster.getMessage());
}
}
//endregion
} else {
return CommonResult.fail(teacher.getName() + "班级数据无效,导入失败");
}
}
}
}
}
//endregion
//region 百胜添加教师数据
if (useBs == 1) {
for (SmartUser teacher : teacherDatas) {
Optional oDepartment = departmentDatas.stream().filter(e -> e.getId().equals(teacher.getDepartmentId())).findFirst();
if (!(oDepartment != null && oDepartment.isPresent())) {
return CommonResult.fail(teacher.getName() + "的部门数据无效,导入失败");
}
/**
* 教师数据的有效期是20年
*/
String startTime = TimeExchange.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
String endTime = TimeExchange.addYear(20);
if (ObjectUtils.isEmpty(teacher.getBsStaffCode())) {
CommonResult insertBsTeacher = bsInsertTeacher(teacher, oDepartment.get().getBsDepartmentNo(), startTime, endTime);
if (!insertBsTeacher.isSuccess()) {
return CommonResult.fail("百胜平台:" + insertBsTeacher.getMessage());
}
teacher.setBsStaffCode(insertBsTeacher.getData());
} else {
CommonResult bsUpdateTeacher = updateBsTeacher(teacher, oDepartment.get().getBsDepartmentNo(), startTime, endTime);
if (!bsUpdateTeacher.isSuccess()) {
return CommonResult.fail("百胜平台:" + bsUpdateTeacher.getMessage());
}
teacher.setBsStaffCode(bsUpdateTeacher.getData());
}
}
}
//endregion
if (teacherDatas != null && teacherDatas.size() > 0) {
//批量存储老师信息
boolean resultBool = smartUserService.saveBatch(teacherDatas);
if (!resultBool) {
return CommonResult.fail("导入失败");
} else {
// 需添加到海康平台
ArrayList operationUsers = new ArrayList<>();
for (SmartUser teacherData : teacherDatas) {
Integer id = teacherData.getId();
SmartOperationUser smartOperationUser = new SmartOperationUser();
smartOperationUser.setOperationId(id);
smartOperationUser.setOperationMode("1");
smartOperationUser.setStatus(1);
smartOperationUser.setType(teacherData.getIdentityId());
operationUsers.add(smartOperationUser);
}
smartOperationUserService.saveBatch(operationUsers);
}
//region 批量存储综合评价
List insertETeachers = new ArrayList<>();
List semesters = smartSemesterService.list(null);
List classes = smartClassService.list(null);
List grades = smartGradeService.list(null);
for (SmartUser teacher : teacherDatas) {
for (SmartSemester semester : semesters) {
//<0 : A < B
//=0 : A = B
//>0 : A > B
if (semester.getEndTime().compareTo(teacher.getCreateTime()) >= 0) {//说明在半学期创建的老师
SmartEvaluateTeacher set = new SmartEvaluateTeacher();
set.setCardNo(teacher.getCardNo());
set.setName(teacher.getName());
set.setUserId(teacher.getId());
set.setTermId(semester.getId());
set.setTerm(semester.getName());
if (teacher.getDuties().intValue() == eDuties.ClassTeacher.getValue()) {//班主任才有年级和班级
set.setGradeId(Integer.valueOf(teacher.getGrade()));
Optional oGrade = grades.stream().filter(e -> e.getId().equals(set.getGradeId())).findFirst();
if (oGrade != null && oGrade.isPresent()) {
set.setGradeName(oGrade.get().getName());
}
set.setClassId(teacher.getSchoolClass());
Optional oClass = classes.stream().filter(e -> e.getId().equals(set.getClassId())).findFirst();
if (oClass != null && oClass.isPresent()) {
set.setClassName(oClass.get().getName());
}
}
set.setTotalScore(0.0);
set.setLowingScore(0.0);
insertETeachers.add(set);
}
}
}
if (insertETeachers != null && insertETeachers.size() > 0) {
boolean insertEteacher = smartEvaluateTeacherService.saveBatch(insertETeachers);
if (!insertEteacher) {
return CommonResult.fail("导入失败");
}
}
//endregion
}
}
return CommonResult.ok("导入成功");
}
//endregion
//region 批量新增Xlsx文件读取方法
/**
* Xlsx文件读取方法
*
* @param inputStream 文件流
* @return
* @throws IOException
*/
private CommonResult> readXlsx(InputStream inputStream) throws IOException {
List idCards = new ArrayList<>();
List cardNos = new ArrayList<>();
List result = new ArrayList<>();
XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
List departments = smartDepartmentService.list(null);
List grades = smartGradeService.list(null); //年级
List classs = smartClassService.list(null);//班级
List timeGroups = smartTimeGroupService.queryTimeGroups();//时间组
//读取第一张sheet
XSSFSheet sheetAt = sheets.getSheetAt(0);
DataFormatter dataFormatter = new DataFormatter();
//region 判断是老师还是学生导入
boolean isTeacher = false;
XSSFRow checkRow = sheetAt.getRow(0);
if (checkRow != null) {
String firstCow = dataFormatter.formatCellValue(checkRow.getCell(0));//第一列 学生是年级 老师是 学校名称
if (firstCow.equals("学校名称")) {
isTeacher = true;
}
}
//endregion
try {
for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
XSSFRow row = sheetAt.getRow(rowNum);
if (row != null) {
if (!isTeacher) {
//使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
//所以先使用setCellType()方法先将该单元格的类型设置为STRING
//然后poi会根据字符串读取它
//标题 校验
if (rowNum == 0) {
String schoolGrade = dataFormatter.formatCellValue(row.getCell(0));//年级
if (!schoolGrade.equals("年级")) {
return CommonResult.fail("导入数据第一列为年级");
}
String schoolClass = dataFormatter.formatCellValue(row.getCell(1));//班级
if (!schoolClass.equals("班级")) {
return CommonResult.fail("导入数据第一列为班级");
}
String departmentNo = dataFormatter.formatCellValue(row.getCell(2));//部门
if (!departmentNo.equals("部门")) {
return CommonResult.fail("导入数据第二列为部门");
}
String cardNo = dataFormatter.formatCellValue(row.getCell(3));//学号
if (!cardNo.equals("学号")) {
return CommonResult.fail("导入数据第二列为学号");
}
String name = dataFormatter.formatCellValue(row.getCell(4));//姓名
if (!name.equals("姓名")) {
return CommonResult.fail("导入数据第三列为姓名");
}
String sex = dataFormatter.formatCellValue(row.getCell(5));//性别
if (!sex.equals("性别")) {
return CommonResult.fail("导入数据第四列为性别");
}
String nation = dataFormatter.formatCellValue(row.getCell(6));//民族
if (!nation.equals("民族")) {
return CommonResult.fail("导入数据第五列为民族");
}
String cardId = dataFormatter.formatCellValue(row.getCell(7));//身份证
if (!cardId.equals("身份证")) {
return CommonResult.fail("导入数据第六列为身份证");
}
String headImage = dataFormatter.formatCellValue(row.getCell(8));//照片
if (!headImage.equals("照片")) {
return CommonResult.fail("导入数据第七列为照片");
}
String timeGroup = dataFormatter.formatCellValue(row.getCell(9));//常规时间组
if (!timeGroup.equals("常规时间组")) {
return CommonResult.fail("导入数据第八列为常规时间组");
}
String address = dataFormatter.formatCellValue(row.getCell(10));//住址
if (!address.equals("住址")) {
return CommonResult.fail("导入数据第九列为住址");
}
String phone = dataFormatter.formatCellValue(row.getCell(11));//联系电话
if (!phone.equals("联系电话")) {
return CommonResult.fail("导入数据第十列为联系电话");
}
String family = dataFormatter.formatCellValue(row.getCell(12));//家属
if (!family.equals("家属")) {
return CommonResult.fail("导入数据第十一列为家属");
}
String familyShip = dataFormatter.formatCellValue(row.getCell(13));//家属与本人关系
if (!familyShip.equals("家属与本人关系")) {
return CommonResult.fail("导入数据第十二列为家属与本人关系");
}
String phoneTwo = dataFormatter.formatCellValue(row.getCell(14));//联系电话2
if (!phoneTwo.equals("联系电话2")) {
return CommonResult.fail("导入数据第十三列为联系电话2");
}
String familyTwo = dataFormatter.formatCellValue(row.getCell(15));//家属2
if (!familyTwo.equals("家属2")) {
return CommonResult.fail("导入数据第十四列为家属2");
}
String familyShipTwo = dataFormatter.formatCellValue(row.getCell(16));//家属与本人关系2
if (!familyShipTwo.equals("家属与本人关系2")) {
return CommonResult.fail("导入数据第十五列为家属与本人关系2");
}
} else {
SmartUser user = new SmartUser();
String name = dataFormatter.formatCellValue(row.getCell(4));
// if (ObjectUtils.isEmpty(name)) {
// return CommonResult.fail("第" + (rowNum + 1) + "条数据的名称不能为空");
// }
if (ObjectUtils.isEmpty(name)) {
continue;
}
String schoolGrade = dataFormatter.formatCellValue(row.getCell(0));//年级
if (ObjectUtils.isEmpty(schoolGrade)) {
return CommonResult.fail(name + "的年级不能为空");
}
String schoolClass = dataFormatter.formatCellValue(row.getCell(1));//班级
if (ObjectUtils.isEmpty(schoolClass)) {
return CommonResult.fail(name + "的班级不能为空");
}
//不判断重复性 因为会出现双胞胎
String phone = dataFormatter.formatCellValue(row.getCell(11));
// if (!ObjectUtils.isEmpty(phone)) {
// phones.add(phone);
// }
// if (phones.stream().distinct().count() != phones.size()) {
// return CommonResult.fail("导入的Excel中,联系电话:" + phone + "存在重复数据");
// }
//性别是否为空判断
String sex = dataFormatter.formatCellValue(row.getCell(5));
if (ObjectUtils.isEmpty(sex)) {
return CommonResult.fail(name + "的性别不能为空");
}
//家庭住址是否为空判断
String address = dataFormatter.formatCellValue(row.getCell(10));
//民族是否为空判断
String nation = dataFormatter.formatCellValue(row.getCell(6));
if (ObjectUtils.isEmpty(nation)) {
return CommonResult.fail(name + "的民族不能为空");
}
//部门是否为空判断
String department = dataFormatter.formatCellValue(row.getCell(2));
if (ObjectUtils.isEmpty(nation)) {
return CommonResult.fail(name + "的部门不能为空");
}
Integer parentDid = null;
Integer departmentId = null;
String[] departmentStrs = department.split("/");
for (int i = 0; i < departmentStrs.length; i++) {
String departmentName = departmentStrs[i];
Optional oD = departments.stream().filter(e -> e.getName().equals(departmentName)).findFirst();
if (oD != null && oD.isPresent()) {
parentDid = oD.get().getId();
} else {
String bsDepartment = insertDepartmentToBs(departmentName);
if (bsDepartment == null) {
return CommonResult.fail(name + "的部门添加到百胜失败");
}
SmartDepartment sdParent = new SmartDepartment();
sdParent.setParentId(parentDid);
sdParent.setName(departmentName);
sdParent.setBsDepartmentNo(bsDepartment);
int departmentData = smartDepartmentService.insertSmartDepartment(sdParent);
departmentId = departmentData;
parentDid = departmentData;
}
}
//学号重复判断
String cardNo = dataFormatter.formatCellValue(row.getCell(3));
if (!ObjectUtils.isEmpty(cardNo)) {
cardNos.add(cardNo);
}
if (cardNos.stream().distinct().count() != cardNos.size()) {
return CommonResult.fail("导入的Excel中," + name + "的学号:" + cardNo + "存在重复数据");
}
//身份证重复判断
String idCard = dataFormatter.formatCellValue(row.getCell(7));
if (!ObjectUtils.isEmpty(idCard)) {
idCards.add(idCard);
}
if (idCards.stream().distinct().count() != idCards.size()) {
return CommonResult.fail("导入的Excel中," + name + "的身份证号:" + idCard + "存在重复数据");
}
user.setCardNo(cardNo == null ? "" : cardNo);
user.setName(name == null ? "" : name);
user.setIdentityId(eIdentityStatu.Student.getValue());
user.setIdCard(idCard == null ? "" : idCard);
user.setSexId(sex == null ? eSexStatu.Man.getValue() : eSexStatu.integerOf(sex));
user.setDepartmentId(departmentId);
String cellImage = dataFormatter.formatCellValue(row.getCell(8));
user.setHeadImage(cellImage);
user.setDormitoryNumber("");
String grade = schoolGrade == null ? "" : schoolGrade;
Optional oGrade = grades.stream().filter(e -> e.getName().equals(grade)).findFirst();
if (oGrade != null && oGrade.isPresent()) {
Integer gradeId = oGrade.get().getId();
user.setGrade(String.valueOf(gradeId));
Optional oClass = classs.stream().filter(e -> e.getName().equals(schoolClass) && e.getGradeId().equals(gradeId)).findFirst();
if (oClass != null && oClass.isPresent()) {
user.setSchoolClass(oClass.get().getId());
} else {
return CommonResult.fail(name + "的班级数据无效,导入失败");
}
} else {
return CommonResult.fail(name + "的年级数据无效,导入失败");
}
user.setCollege("");
user.setSpeciality("");
user.setCampus("");
user.setPhone("");
user.setAffiliate("");
user.setTitle("");
user.setAddress(address == null ? "" : address);
user.setNation(nation == null ? "" : nation);
user.setOfStudent("");
user.setGraduate("");
user.setDuties(null);
String timeGroup = dataFormatter.formatCellValue(row.getCell(9));
Optional groupData = timeGroups.stream().filter(e -> e.getName().equals(timeGroup)).findFirst();
if (groupData != null && groupData.isPresent()) {
user.setTimeGroupId(groupData.get().getId());
} else {
user.setTimeGroupId(0);
}
user.setIsCancel(eLogOff.Unlogout.getValue());
result.add(user);
//部门是否为空判断
String familyDepartment = dataFormatter.formatCellValue(row.getCell(1)).replace("年级", "") + "家长";
Integer familyDepartmentId = null;
Optional familyDepartModel = departments.stream().filter(e -> e.getName().equals(familyDepartment)).findFirst();
if (familyDepartModel != null && familyDepartModel.isPresent()) {
familyDepartmentId = familyDepartModel.get().getId();
} else {
familyDepartmentId = 16;
}
//region 家属
if (!ObjectUtils.isEmpty(phone)) {
if (phone.length() != 11) {
return CommonResult.fail(name + "的学生家长手机号长度不符合标准");
}
SmartUser familyOne = new SmartUser();
String family = dataFormatter.formatCellValue(row.getCell(12));//家属
familyOne.setName(!ObjectUtils.isEmpty(family) ? family : "家长");
familyOne.setDepartmentId(familyDepartmentId);
familyOne.setPhone(phone);
familyOne.setIdentityId(eIdentityStatu.Parent.getValue());
familyOne.setSexId(eSexStatu.Man.getValue());
familyOne.setIsCancel(eLogOff.Unlogout.getValue());
familyOne.setAffiliate(user.getCardNo());
String familyShip = dataFormatter.formatCellValue(row.getCell(13));//家属与本人关系
familyOne.setShip(familyShip == null ? "其他" : familyShip);
result.add(familyOne);
}
String phoneTwo = dataFormatter.formatCellValue(row.getCell(14));//联系电话2
if (!ObjectUtils.isEmpty(phoneTwo)) {
if (phoneTwo.length() != 11) {
return CommonResult.fail(name + "的学生家长手机号长度不符合标准");
}
SmartUser familyTwo = new SmartUser();
String familyNameTwo = dataFormatter.formatCellValue(row.getCell(15));//家属2
familyTwo.setName(!ObjectUtils.isEmpty(familyNameTwo) ? familyNameTwo : "家长");
familyTwo.setDepartmentId(familyDepartmentId);
familyTwo.setPhone(phoneTwo == null ? "" : phoneTwo);
familyTwo.setIdentityId(eIdentityStatu.Parent.getValue());
familyTwo.setSexId(eSexStatu.Man.getValue());
familyTwo.setIsCancel(eLogOff.Unlogout.getValue());
familyTwo.setAffiliate(user.getCardNo());
String familyShipTwo = dataFormatter.formatCellValue(row.getCell(16));//家属与本人关系2
familyTwo.setShip(familyShipTwo == null ? "其他" : familyShipTwo);
result.add(familyTwo);
}
//endregion
//希沃不允许一个学生家长的两个手机号重复 所以做一个重复性判断
if (!ObjectUtils.isEmpty(phone) && !ObjectUtils.isEmpty(dataFormatter.formatCellValue(row.getCell(14)))) {
if (phone.equals(dataFormatter.formatCellValue(row.getCell(14)))) {
return CommonResult.fail(name + "的学生家长手机号不可重复");
}
}
//endregion
}
} else {
//教师导入
//使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
//所以先使用setCellType()方法先将该单元格的类型设置为STRING
//然后poi会根据字符串读取它
//标题 校验
if (rowNum == 0) {
String schoolName = dataFormatter.formatCellValue(row.getCell(0));//学校名称
if (!schoolName.equals("学校名称")) {
return CommonResult.fail("导入数据第一列为学校名称");
}
String departmentNo = dataFormatter.formatCellValue(row.getCell(1));//部门编码
if (!departmentNo.equals("部门编码")) {
return CommonResult.fail("导入数据第二列为部门编码");
}
String departmentName = dataFormatter.formatCellValue(row.getCell(2));//部门名称
if (!departmentName.equals("部门名称")) {
return CommonResult.fail("导入数据第三列为部门名称");
}
String teacherNo = dataFormatter.formatCellValue(row.getCell(3));//教师编码
if (!teacherNo.equals("教师编码")) {
return CommonResult.fail("导入数据第四列为教师编码");
}
String name = dataFormatter.formatCellValue(row.getCell(4));//姓名
if (!name.equals("姓名")) {
return CommonResult.fail("导入数据第五列为姓名");
}
String sex = dataFormatter.formatCellValue(row.getCell(5));//性别
if (!sex.equals("性别")) {
return CommonResult.fail("导入数据第六列为性别");
}
String phone = dataFormatter.formatCellValue(row.getCell(6));//联系电话
if (!phone.equals("联系电话")) {
return CommonResult.fail("导入数据第七列为联系电话");
}
String schoolClass = dataFormatter.formatCellValue(row.getCell(7));//管理班级
if (!schoolClass.equals("管理班级")) {
return CommonResult.fail("导入数据第八列为身份证");
}
String nation = dataFormatter.formatCellValue(row.getCell(8));//民族
if (!nation.equals("民族")) {
return CommonResult.fail("导入数据第九列为民族");
}
String card = dataFormatter.formatCellValue(row.getCell(9));//身份证
if (!card.equals("身份证(允许为空)")) {
return CommonResult.fail("导入数据第十列为身份证");
}
String photo = dataFormatter.formatCellValue(row.getCell(10));//照片
if (!photo.equals("照片")) {
return CommonResult.fail("导入数据第十一列为照片");
}
String timeGroup = dataFormatter.formatCellValue(row.getCell(11));//常规时间组
if (!timeGroup.equals("常规时间组")) {
return CommonResult.fail("导入数据第十二列为常规时间组");
}
String address = dataFormatter.formatCellValue(row.getCell(12));//住址
if (!address.equals("住址")) {
return CommonResult.fail("导入数据第十三列为住址");
}
} else {
SmartUser user = new SmartUser();
user.setIdentityId(eIdentityStatu.Teacher.getValue());
String name = dataFormatter.formatCellValue(row.getCell(4));//姓名
if (ObjectUtils.isEmpty(name)) {
continue;
}
user.setName(name);
String sex = dataFormatter.formatCellValue(row.getCell(5));//性别
user.setSexId(sex == null ? eSexStatu.Man.getValue() : eSexStatu.integerOf(sex));
String departmentName = dataFormatter.formatCellValue(row.getCell(2));//部门名称
if (ObjectUtils.isEmpty(departmentName)) {
return CommonResult.fail(name + "的部门名称不能为空");
}
String departmentNo = dataFormatter.formatCellValue(row.getCell(1));//部门编码
if (ObjectUtils.isEmpty(departmentNo)) {
return CommonResult.fail(name + "的部门编码不能为空");
}
Optional oDepartment = departments.stream().filter(e -> e.getName().equals(departmentName)).findFirst();
if (!(oDepartment != null && oDepartment.isPresent())) {
return CommonResult.fail(departmentName + "部门数据在系统中不存在");
}
user.setDepartmentId(oDepartment.get().getId());
if (departmentName.equals("班主任")) {
String schoolClass = dataFormatter.formatCellValue(row.getCell(7));//班级
if (ObjectUtils.isEmpty(schoolClass)) {
return CommonResult.fail(name + "的管理班级不能为空");
}
//班级和年级
Optional oClass = classs.stream().filter(e -> e.getName().equals(schoolClass)).findFirst();
if (!(oClass != null && oClass.isPresent())) {
return CommonResult.fail(schoolClass + "的班级数据在系统中不存在");
}
user.setSchoolClass(oClass.get().getId());
user.setGrade(String.valueOf(oClass.get().getGradeId()));
user.setDuties(eDuties.ClassTeacher.getValue());
} else {
user.setDuties(5);//任课老师
}
String teacherNo = dataFormatter.formatCellValue(row.getCell(3));//教师编码
if (!ObjectUtils.isEmpty(teacherNo)) {
user.setCardNo(teacherNo);
}
String phone = dataFormatter.formatCellValue(row.getCell(6));//联系电话
if (ObjectUtils.isEmpty(phone)) {
return CommonResult.fail(name + "的手机号不能为空");
}
user.setPhone(phone);
user.setXwTeacherCode(phone);
String nation = dataFormatter.formatCellValue(row.getCell(8));//民族
if (!ObjectUtils.isEmpty(nation)) {
user.setNation(nation);
}
String card = dataFormatter.formatCellValue(row.getCell(9));//身份证
if (!ObjectUtils.isEmpty(card)) {
user.setIdCard(card);
}
String photo = dataFormatter.formatCellValue(row.getCell(10));//照片
if (!ObjectUtils.isEmpty(photo)) {
user.setHeadImage(photo);
}
String timeGroup = dataFormatter.formatCellValue(row.getCell(11));//常规时间组
if (!ObjectUtils.isEmpty(timeGroup)) {
Optional groupData = timeGroups.stream().filter(e -> e.getName().equals(timeGroup)).findFirst();
if (groupData != null && groupData.isPresent()) {
user.setTimeGroupId(groupData.get().getId());
}
}
String address = dataFormatter.formatCellValue(row.getCell(12));//住址
if (!ObjectUtils.isEmpty(address)) {
user.setAddress(address);
}
user.setIsCancel(eLogOff.Unlogout.getValue());
result.add(user);
}
}
}
}
} catch (Exception e) {
return CommonResult.fail("请按模板格式导入数据");
}
return CommonResult.ok(result);
}
//endregion
//region 批量新增Xls文件读取方法
/**
* Xls文件读取方法
*
* @param inputStream 文件流
* @return
* @throws IOException
*/
private CommonResult> readXls(InputStream inputStream) throws IOException {
List idCards = new ArrayList<>();
List cardNos = new ArrayList<>();
List result = new ArrayList<>();
HSSFWorkbook sheets = new HSSFWorkbook(inputStream);
List departments = smartDepartmentService.list(null);
List grades = smartGradeService.list(null); //年级
List classs = smartClassService.list(null);//班级
List timeGroups = smartTimeGroupService.queryTimeGroups();//时间组
//读取第一张sheet
HSSFSheet sheetAt = sheets.getSheetAt(0);
DataFormatter dataFormatter = new DataFormatter();
//region 判断是老师还是学生导入
boolean isTeacher = false;
HSSFRow checkRow = sheetAt.getRow(0);
if (checkRow != null) {
String firstCow = dataFormatter.formatCellValue(checkRow.getCell(0));//第一列 学生是年级 老师是 学校名称
if (firstCow.equals("学校名称")) {
isTeacher = true;
}
}
//endregion
try {
for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
HSSFRow row = sheetAt.getRow(rowNum);
if (row != null) {
if (!isTeacher) {
//使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
//所以先使用setCellType()方法先将该单元格的类型设置为STRING
//然后poi会根据字符串读取它
//标题 校验
if (rowNum == 0) {
String schoolGrade = dataFormatter.formatCellValue(row.getCell(0));//年级
if (!schoolGrade.equals("年级")) {
return CommonResult.fail("导入数据第一列为年级");
}
String schoolClass = dataFormatter.formatCellValue(row.getCell(1));//班级
if (!schoolClass.equals("班级")) {
return CommonResult.fail("导入数据第一列为班级");
}
String departmentNo = dataFormatter.formatCellValue(row.getCell(2));//部门
if (!departmentNo.equals("部门")) {
return CommonResult.fail("导入数据第二列为部门");
}
String cardNo = dataFormatter.formatCellValue(row.getCell(3));//学号
if (!cardNo.equals("学号")) {
return CommonResult.fail("导入数据第二列为学号");
}
String name = dataFormatter.formatCellValue(row.getCell(4));//姓名
if (!name.equals("姓名")) {
return CommonResult.fail("导入数据第三列为姓名");
}
String sex = dataFormatter.formatCellValue(row.getCell(5));//性别
if (!sex.equals("性别")) {
return CommonResult.fail("导入数据第四列为性别");
}
String nation = dataFormatter.formatCellValue(row.getCell(6));//民族
if (!nation.equals("民族")) {
return CommonResult.fail("导入数据第五列为民族");
}
String cardId = dataFormatter.formatCellValue(row.getCell(7));//身份证
if (!cardId.equals("身份证")) {
return CommonResult.fail("导入数据第六列为身份证");
}
String headImage = dataFormatter.formatCellValue(row.getCell(8));//照片
if (!headImage.equals("照片")) {
return CommonResult.fail("导入数据第七列为照片");
}
String timeGroup = dataFormatter.formatCellValue(row.getCell(9));//常规时间组
if (!timeGroup.equals("常规时间组")) {
return CommonResult.fail("导入数据第八列为常规时间组");
}
String address = dataFormatter.formatCellValue(row.getCell(10));//住址
if (!address.equals("住址")) {
return CommonResult.fail("导入数据第九列为住址");
}
String phone = dataFormatter.formatCellValue(row.getCell(11));//联系电话
if (!phone.equals("联系电话")) {
return CommonResult.fail("导入数据第十列为联系电话");
}
String family = dataFormatter.formatCellValue(row.getCell(12));//家属
if (!family.equals("家属")) {
return CommonResult.fail("导入数据第十一列为家属");
}
String familyShip = dataFormatter.formatCellValue(row.getCell(13));//家属与本人关系
if (!familyShip.equals("家属与本人关系")) {
return CommonResult.fail("导入数据第十二列为家属与本人关系");
}
String phoneTwo = dataFormatter.formatCellValue(row.getCell(14));//联系电话2
if (!phoneTwo.equals("联系电话2")) {
return CommonResult.fail("导入数据第十三列为联系电话2");
}
String familyTwo = dataFormatter.formatCellValue(row.getCell(15));//家属2
if (!familyTwo.equals("家属2")) {
return CommonResult.fail("导入数据第十四列为家属2");
}
String familyShipTwo = dataFormatter.formatCellValue(row.getCell(16));//家属与本人关系2
if (!familyShipTwo.equals("家属与本人关系2")) {
return CommonResult.fail("导入数据第十五列为家属与本人关系2");
}
} else {
SmartUser user = new SmartUser();
String name = dataFormatter.formatCellValue(row.getCell(4));
// if (ObjectUtils.isEmpty(name)) {
// return CommonResult.fail("第" + (rowNum + 1) + "条数据的名称不能为空");
// }
if (ObjectUtils.isEmpty(name)) {
continue;
}
String schoolGrade = dataFormatter.formatCellValue(row.getCell(0));//年级
if (ObjectUtils.isEmpty(schoolGrade)) {
return CommonResult.fail(name + "的年级不能为空");
}
String schoolClass = dataFormatter.formatCellValue(row.getCell(1));//班级
if (ObjectUtils.isEmpty(schoolClass)) {
return CommonResult.fail(name + "的班级不能为空");
}
//不判断重复性 因为会出现双胞胎
String phone = dataFormatter.formatCellValue(row.getCell(11));
// if (!ObjectUtils.isEmpty(phone)) {
// phones.add(phone);
// }
// if (phones.stream().distinct().count() != phones.size()) {
// return CommonResult.fail("导入的Excel中,联系电话:" + phone + "存在重复数据");
// }
//性别是否为空判断
String sex = dataFormatter.formatCellValue(row.getCell(5));
if (ObjectUtils.isEmpty(sex)) {
return CommonResult.fail(name + "的性别不能为空");
}
//家庭住址是否为空判断
String address = dataFormatter.formatCellValue(row.getCell(10));
//民族是否为空判断
String nation = dataFormatter.formatCellValue(row.getCell(6));
if (ObjectUtils.isEmpty(nation)) {
return CommonResult.fail(name + "的民族不能为空");
}
//部门是否为空判断
String department = dataFormatter.formatCellValue(row.getCell(2));
if (ObjectUtils.isEmpty(nation)) {
return CommonResult.fail(name + "的部门不能为空");
}
Integer parentDid = null;
Integer departmentId = null;
String[] departmentStrs = department.split("/");
for (int i = 0; i < departmentStrs.length; i++) {
String departmentName = departmentStrs[i];
Optional oD = departments.stream().filter(e -> e.getName().equals(departmentName)).findFirst();
if (oD != null && oD.isPresent()) {
parentDid = oD.get().getId();
} else {
String bsDepartment = insertDepartmentToBs(departmentName);
if (bsDepartment == null) {
return CommonResult.fail(name + "的部门添加到百胜失败");
}
SmartDepartment sdParent = new SmartDepartment();
sdParent.setParentId(parentDid);
sdParent.setName(departmentName);
sdParent.setBsDepartmentNo(bsDepartment);
int departmentData = smartDepartmentService.insertSmartDepartment(sdParent);
departmentId = departmentData;
parentDid = departmentData;
}
}
//学号重复判断
String cardNo = dataFormatter.formatCellValue(row.getCell(3));
if (!ObjectUtils.isEmpty(cardNo)) {
cardNos.add(cardNo);
}
if (cardNos.stream().distinct().count() != cardNos.size()) {
return CommonResult.fail("导入的Excel中," + name + "的学号:" + cardNo + "存在重复数据");
}
//身份证重复判断
String idCard = dataFormatter.formatCellValue(row.getCell(7));
if (!ObjectUtils.isEmpty(idCard)) {
idCards.add(idCard);
}
if (idCards.stream().distinct().count() != idCards.size()) {
return CommonResult.fail("导入的Excel中," + name + "的身份证号:" + idCard + "存在重复数据");
}
user.setCardNo(cardNo == null ? "" : cardNo);
user.setName(name == null ? "" : name);
user.setIdentityId(eIdentityStatu.Student.getValue());
user.setIdCard(idCard == null ? "" : idCard);
user.setSexId(sex == null ? eSexStatu.Man.getValue() : eSexStatu.integerOf(sex));
user.setDepartmentId(departmentId);
String cellImage = dataFormatter.formatCellValue(row.getCell(8));
user.setHeadImage(cellImage);
user.setDormitoryNumber("");
String grade = schoolGrade == null ? "" : schoolGrade;
Optional oGrade = grades.stream().filter(e -> e.getName().equals(grade)).findFirst();
if (oGrade != null && oGrade.isPresent()) {
Integer gradeId = oGrade.get().getId();
user.setGrade(String.valueOf(gradeId));
Optional oClass = classs.stream().filter(e -> e.getName().equals(schoolClass) && e.getGradeId().equals(gradeId)).findFirst();
if (oClass != null && oClass.isPresent()) {
user.setSchoolClass(oClass.get().getId());
} else {
return CommonResult.fail(name + "的班级数据无效,导入失败");
}
} else {
return CommonResult.fail(name + "的年级数据无效,导入失败");
}
user.setCollege("");
user.setSpeciality("");
user.setCampus("");
user.setPhone("");
user.setAffiliate("");
user.setTitle("");
user.setAddress(address == null ? "" : address);
user.setNation(nation == null ? "" : nation);
user.setOfStudent("");
user.setGraduate("");
user.setDuties(null);
String timeGroup = dataFormatter.formatCellValue(row.getCell(9));
Optional groupData = timeGroups.stream().filter(e -> e.getName().equals(timeGroup)).findFirst();
if (groupData != null && groupData.isPresent()) {
user.setTimeGroupId(groupData.get().getId());
} else {
user.setTimeGroupId(0);
}
user.setIsCancel(eLogOff.Unlogout.getValue());
result.add(user);
//部门是否为空判断
String familyDepartment = dataFormatter.formatCellValue(row.getCell(1)).replace("年级", "") + "家长";
Integer familyDepartmentId = null;
Optional familyDepartModel = departments.stream().filter(e -> e.getName().equals(familyDepartment)).findFirst();
if (familyDepartModel != null && familyDepartModel.isPresent()) {
familyDepartmentId = familyDepartModel.get().getId();
} else {
familyDepartmentId = 16;
}
//region 家属
if (!ObjectUtils.isEmpty(phone)) {
if (phone.length() != 11) {
return CommonResult.fail(name + "的学生家长手机号长度不符合标准");
}
SmartUser familyOne = new SmartUser();
String family = dataFormatter.formatCellValue(row.getCell(12));//家属
familyOne.setName(!ObjectUtils.isEmpty(family) ? family : "家长");
familyOne.setDepartmentId(familyDepartmentId);
familyOne.setPhone(phone);
familyOne.setIdentityId(eIdentityStatu.Parent.getValue());
familyOne.setSexId(eSexStatu.Man.getValue());
familyOne.setIsCancel(eLogOff.Unlogout.getValue());
familyOne.setAffiliate(user.getCardNo());
String familyShip = dataFormatter.formatCellValue(row.getCell(13));//家属与本人关系
familyOne.setShip(familyShip == null ? "其他" : familyShip);
result.add(familyOne);
}
String phoneTwo = dataFormatter.formatCellValue(row.getCell(14));//联系电话2
if (!ObjectUtils.isEmpty(phoneTwo)) {
if (phoneTwo.length() != 11) {
return CommonResult.fail(name + "的学生家长手机号长度不符合标准");
}
SmartUser familyTwo = new SmartUser();
String familyNameTwo = dataFormatter.formatCellValue(row.getCell(15));//家属2
familyTwo.setName(!ObjectUtils.isEmpty(familyNameTwo) ? familyNameTwo : "家长");
familyTwo.setDepartmentId(familyDepartmentId);
familyTwo.setPhone(phoneTwo == null ? "" : phoneTwo);
familyTwo.setIdentityId(eIdentityStatu.Parent.getValue());
familyTwo.setSexId(eSexStatu.Man.getValue());
familyTwo.setIsCancel(eLogOff.Unlogout.getValue());
familyTwo.setAffiliate(user.getCardNo());
String familyShipTwo = dataFormatter.formatCellValue(row.getCell(16));//家属与本人关系2
familyTwo.setShip(familyShipTwo == null ? "其他" : familyShipTwo);
result.add(familyTwo);
}
//endregion
//希沃不允许一个学生家长的两个手机号重复 所以做一个重复性判断
if (!ObjectUtils.isEmpty(phone) && !ObjectUtils.isEmpty(dataFormatter.formatCellValue(row.getCell(14)))) {
if (phone.equals(dataFormatter.formatCellValue(row.getCell(14)))) {
return CommonResult.fail(name + "的学生家长手机号不可重复");
}
}
//endregion
}
} else {
//教师导入
//使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
//所以先使用setCellType()方法先将该单元格的类型设置为STRING
//然后poi会根据字符串读取它
//标题 校验
if (rowNum == 0) {
String schoolName = dataFormatter.formatCellValue(row.getCell(0));//学校名称
if (!schoolName.equals("学校名称")) {
return CommonResult.fail("导入数据第一列为学校名称");
}
String departmentNo = dataFormatter.formatCellValue(row.getCell(1));//部门编码
if (!departmentNo.equals("部门编码")) {
return CommonResult.fail("导入数据第二列为部门编码");
}
String departmentName = dataFormatter.formatCellValue(row.getCell(2));//部门名称
if (!departmentName.equals("部门名称")) {
return CommonResult.fail("导入数据第三列为部门名称");
}
String teacherNo = dataFormatter.formatCellValue(row.getCell(3));//教师编码
if (!teacherNo.equals("教师编码")) {
return CommonResult.fail("导入数据第四列为教师编码");
}
String name = dataFormatter.formatCellValue(row.getCell(4));//姓名
if (!name.equals("姓名")) {
return CommonResult.fail("导入数据第五列为姓名");
}
String sex = dataFormatter.formatCellValue(row.getCell(5));//性别
if (!sex.equals("性别")) {
return CommonResult.fail("导入数据第六列为性别");
}
String phone = dataFormatter.formatCellValue(row.getCell(6));//联系电话
if (!phone.equals("联系电话")) {
return CommonResult.fail("导入数据第七列为联系电话");
}
String schoolClass = dataFormatter.formatCellValue(row.getCell(7));//管理班级
if (!schoolClass.equals("管理班级")) {
return CommonResult.fail("导入数据第八列为身份证");
}
String nation = dataFormatter.formatCellValue(row.getCell(8));//民族
if (!nation.equals("民族")) {
return CommonResult.fail("导入数据第九列为民族");
}
String card = dataFormatter.formatCellValue(row.getCell(9));//身份证
if (!card.equals("身份证(允许为空)")) {
return CommonResult.fail("导入数据第十列为身份证");
}
String photo = dataFormatter.formatCellValue(row.getCell(10));//照片
if (!photo.equals("照片")) {
return CommonResult.fail("导入数据第十一列为照片");
}
String timeGroup = dataFormatter.formatCellValue(row.getCell(11));//常规时间组
if (!timeGroup.equals("常规时间组")) {
return CommonResult.fail("导入数据第十二列为常规时间组");
}
String address = dataFormatter.formatCellValue(row.getCell(12));//住址
if (!address.equals("住址")) {
return CommonResult.fail("导入数据第十三列为住址");
}
} else {
SmartUser user = new SmartUser();
user.setIdentityId(eIdentityStatu.Teacher.getValue());
String name = dataFormatter.formatCellValue(row.getCell(4));//姓名
if (ObjectUtils.isEmpty(name)) {
continue;
}
user.setName(name);
String sex = dataFormatter.formatCellValue(row.getCell(5));//性别
user.setSexId(sex == null ? eSexStatu.Man.getValue() : eSexStatu.integerOf(sex));
String departmentName = dataFormatter.formatCellValue(row.getCell(2));//部门名称
if (ObjectUtils.isEmpty(departmentName)) {
return CommonResult.fail(name + "的部门名称不能为空");
}
String departmentNo = dataFormatter.formatCellValue(row.getCell(1));//部门编码
if (ObjectUtils.isEmpty(departmentNo)) {
return CommonResult.fail(name + "的部门编码不能为空");
}
Optional oDepartment = departments.stream().filter(e -> e.getName().equals(departmentName)).findFirst();
if (!(oDepartment != null && oDepartment.isPresent())) {
return CommonResult.fail(departmentName + "部门数据在系统中不存在");
}
user.setDepartmentId(oDepartment.get().getId());
if (departmentName.equals("班主任")) {
String schoolClass = dataFormatter.formatCellValue(row.getCell(7));//班级
if (ObjectUtils.isEmpty(schoolClass)) {
return CommonResult.fail(name + "的管理班级不能为空");
}
//班级和年级
Optional oClass = classs.stream().filter(e -> e.getName().equals(schoolClass)).findFirst();
if (!(oClass != null && oClass.isPresent())) {
return CommonResult.fail(schoolClass + "的班级数据在系统中不存在");
}
user.setSchoolClass(oClass.get().getId());
user.setGrade(String.valueOf(oClass.get().getGradeId()));
user.setDuties(eDuties.ClassTeacher.getValue());
} else {
user.setDuties(5);//任课老师
}
String teacherNo = dataFormatter.formatCellValue(row.getCell(3));//教师编码
if (!ObjectUtils.isEmpty(teacherNo)) {
//user.setBsStaffCode(teacherNo);
user.setCardNo(teacherNo);
}
String phone = dataFormatter.formatCellValue(row.getCell(6));//联系电话
if (ObjectUtils.isEmpty(phone)) {
return CommonResult.fail(name + "的手机号不能为空");
}
user.setPhone(phone);
user.setXwTeacherCode(phone);
String nation = dataFormatter.formatCellValue(row.getCell(8));//民族
if (!ObjectUtils.isEmpty(nation)) {
user.setNation(nation);
}
String card = dataFormatter.formatCellValue(row.getCell(9));//身份证
if (!ObjectUtils.isEmpty(card)) {
user.setIdCard(card);
}
String photo = dataFormatter.formatCellValue(row.getCell(10));//照片
if (!ObjectUtils.isEmpty(photo)) {
user.setHeadImage(photo);
}
String timeGroup = dataFormatter.formatCellValue(row.getCell(11));//常规时间组
if (!ObjectUtils.isEmpty(timeGroup)) {
Optional groupData = timeGroups.stream().filter(e -> e.getName().equals(timeGroup)).findFirst();
if (groupData != null && groupData.isPresent()) {
user.setTimeGroupId(groupData.get().getId());
}
}
String address = dataFormatter.formatCellValue(row.getCell(12));//住址
if (!ObjectUtils.isEmpty(address)) {
user.setAddress(address);
}
user.setIsCancel(eLogOff.Unlogout.getValue());
result.add(user);
}
}
}
}
} catch (Exception e) {
return CommonResult.fail("请按模板格式导入数据");
}
return CommonResult.ok(result);
}
//endregion
public String insertDepartmentToBs(String name) throws Exception {
//region 百胜添加部门数据
String appId = controlConfig.getAppId();
String schoolno = controlConfig.getSchoolCode();
String timestamp = TimeExchange.DateNowTimeStamo();
String appSecret = controlConfig.getAppSecret();
String url = controlConfig.getUrl() + "department/create";
JSONObject jsonobject = new JSONObject();
jsonobject.put("appid", appId);
String str = "{\"department_name\":\"" + name + "\"}";
String aesStr = URLEncoder.encode(AesTestOne.encrypt(str, controlConfig.getAppSecret()), "UTF-8");
jsonobject.put("data", aesStr);
jsonobject.put("schoolno", schoolno);
jsonobject.put("timestamp", timestamp);
String md5Str = "appid=" + appId + "&data={\"department_name\":\"" + name + "\"}" + "&schoolno=" + schoolno + "×tamp=" + timestamp + "&key=" + appSecret;
String sign = CommonUtil.MD5(md5Str);
//sign签名
jsonobject.put("sign", sign);
//返回的结果中 code为1表示成功
String bsResult = RequestUtils.httpPost(url, jsonobject.toJSONString());
if (bsResult.contains("添加成功")) {
ObjectMapper objectMapper = new ObjectMapper();
BsDepartmentVo department = objectMapper.readValue(bsResult, BsDepartmentVo.class);
// URL解码
String decodedUrl = URLDecoder.decode(department.getData(), "UTF-8");
BsDepartmentNoVo departmentData = objectMapper.readValue(decrypt(decodedUrl, controlConfig.getAppSecret()), BsDepartmentNoVo.class);
return departmentData.getDepartment_no();
}
//endregion
return null;
}
//endregion
//region 批量更新用户
//region 批量更新用户方法
/**
* 只更新 姓名 班级 时间组
*
* @param excelFile excel文件
* @return
* @throws IOException
*/
@Override
public CommonResult importExcelUpdateUsers(MultipartFile excelFile, String headImage) throws Exception {
List result = new ArrayList<>();
int useXw = 1;
int useBs = 1;
//先解析excel如果excel不满足格式就提示错误信息 避免提前占用带宽上传头像
if (excelFile.isEmpty() || excelFile.getSize() == 0) {
return CommonResult.fail("excel文件不能为空");
}
String ContentType = excelFile.getContentType();
InputStream inputStream = excelFile.getInputStream();
//xls格式文件
if (ContentType.equals(eFileType.Xls.getValue())) {
CommonResult> resultData = readUpdateXls(inputStream);
if (!resultData.isSuccess()) {
return resultData;
}
result = resultData.getData();
} else if (ContentType.equals(eFileType.Xlsx.getValue())) {
CommonResult> resultData = readUpdateXlsx(inputStream);
if (!resultData.isSuccess()) {
return resultData;
}
result = resultData.getData();
} else {
return CommonResult.fail("用户导入只支持Xls、Xlsx");
}
//获取导入学生的相关数据
List cardNos = result.stream().map(SmartUser::getCardNo).collect(Collectors.toList());
if (cardNos == null) {
return CommonResult.fail("文档内容为空,批量更新失败");
}
List grades = smartGradeService.list(null);
List classes = smartClassService.list(null);
List insertStudent = new ArrayList<>();
List updateStudent = new ArrayList<>();
List updateStudents = result.stream().filter(e -> e.getId() != null && e.getIdentityId().intValue() == eIdentityStatu.Student.getValue()).collect(Collectors.toList());
for (SmartUser studentData : updateStudents) {
studentData.setName(studentData.getName());//姓名
studentData.setSchoolClass(studentData.getSchoolClass());//班级
studentData.setGrade(studentData.getGrade());//可能会变年级
studentData.setTimeGroupId(studentData.getTimeGroupId());//时间组
updateStudent.add(studentData);
Optional oGrade = grades.stream().filter(e -> String.valueOf(e.getId()).equals(studentData.getGrade())).findFirst();
if (oGrade != null && oGrade.isPresent()) {
Optional oClass = classes.stream().filter(e -> e.getId().equals(studentData.getSchoolClass())).findFirst();
if (oClass != null && oClass.isPresent()) {
if (useXw == 1) {
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
CommonResult updateSwStudent = SeewoUpdateStudent(seewoClient, studentData);
if (!updateSwStudent.isSuccess()) {
return CommonResult.fail("希沃平台:" + updateSwStudent.getMessage());
}
if (!studentData.getOldClassUid().equals(oClass.get().getClassUid())) {
CommonResult changeClass = SeewoChangeClass(seewoClient, seewoConfig.getSchoolId(), studentData.getXwStudentUid(), studentData.getOldClassUid(), oClass.get().getClassUid());
if (!changeClass.isSuccess()) {
//return CommonResult.fail("希沃平台:" + changeClass.getMessage());
}
}
}
if (useBs == 1) {
/**
* 学生数据的有效期是到毕业年份的8月31日
*/
String startTime = TimeExchange.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
String endTime = queryGraduationYear(oGrade.get().getGradeNo());
CommonResult updateBsStudent = bsEUpdateStudent(studentData, oClass.get().getBsClassNo(), startTime, endTime);
if (!updateBsStudent.isSuccess()) {
return CommonResult.fail("百胜平台:" + updateBsStudent.getMessage());
}
}
} else {
return CommonResult.fail("学号为:" + studentData.getCardNo() + "的" + studentData.getName() + "的班级数据无效,无法进行更新操作");
}
} else {
return CommonResult.fail("学号为:" + studentData.getCardNo() + "的" + studentData.getName() + "的年级数据无效,无法进行更新操作");
}
}
//更新家长
List updateParents = result.stream().filter(e -> e.getId() != null && e.getIdentityId().intValue() == eIdentityStatu.Parent.getValue()).collect(Collectors.toList());
if(updateParents != null && updateParents.size() > 0){
boolean updateBatch = smartUserService.updateBatchById(updateParents);
if (!updateBatch) {
return CommonResult.fail("系统批量更新家长出错,导入失败");
}
}
//新增家长
List insertParentDatas = new ArrayList<>();
List insertParents = result.stream().filter(e -> e.getId() == null && e.getIdentityId().intValue() == eIdentityStatu.Parent.getValue()).collect(Collectors.toList());
if(insertParents != null && insertParents.size() > 0){
List familyCardNos = updateStudents.stream().map(SmartUser::getCardNo).distinct().collect(Collectors.toList());
List indexs = smartFamilyIndexService.querySmartFamilyByCardNo(familyCardNos);
List familyIndexs = new ArrayList<>();
//region 希沃新增编辑学生家长信息
if (useXw == 1) {
int num = (int) Math.ceil((double) updateStudents.size() / 100);
for (int count = 1; count <= num; count++) {
int startIndex = (count - 1) * 100;
int endIndex = count * 100;
if (count == num) {
endIndex = startIndex + (updateStudents.size() % 100);
}
List currentStudentDatas = updateStudents.subList(startIndex, endIndex);//结尾不包含下标100
//学生与家长列表,最大100条
List studentParents = new ArrayList<>();
for (SmartUser student : currentStudentDatas) {
ParentServiceBatchSaveOrUpdateParentsParam.StudentParentsItem students = ParentServiceBatchSaveOrUpdateParentsParam.StudentParentsItem.builder()
.studentCode(student.getCardNo())
.build();
List parentDatas = insertParents.stream().filter(e -> e.getIdentityId().intValue() == eIdentityStatu.Parent.getValue() && e.getAffiliate().equals(student.getCardNo())).collect(Collectors.toList());
List parents = new ArrayList<>();
long indexCount = indexs.stream().filter(e -> e.getStudentNo().equals(student.getCardNo())).count();
int i = (indexCount<= 0 ? 0 : (int)indexCount);
for (SmartUser parent : parentDatas) {
insertParentDatas.add(parent);
SmartFamilyIndex familyIndex = new SmartFamilyIndex();
familyIndex.setParentPhone(parent.getPhone());
familyIndex.setStudentNo(student.getCardNo());
familyIndex.setIndexData(i);
familyIndexs.add(familyIndex);
ParentServiceBatchSaveOrUpdateParentsParam.ParentsItem data = ParentServiceBatchSaveOrUpdateParentsParam.ParentsItem.builder()
.name(parent.getShip())
.phone(parent.getPhone())
.index(i)
.build();
parents.add(data);
++i;
}
if (parents != null && parents.size() > 0) {
studentParents.add(students);
//家长列表,最多4个
students.setParents(parents);
}
}
//初始化客户端
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
ParentServiceBatchSaveOrUpdateParentsParam param = new ParentServiceBatchSaveOrUpdateParentsParam();
//响应体,MimeType为 application/json
ParentServiceBatchSaveOrUpdateParentsParam.RequestBody requestBody = ParentServiceBatchSaveOrUpdateParentsParam.RequestBody.builder()
.build();
param.setRequestBody(requestBody);
//query
ParentServiceBatchSaveOrUpdateParentsParam.Query query = ParentServiceBatchSaveOrUpdateParentsParam.Query.builder()
.appId(seewoConfig.getAppId())
.schoolUid(seewoConfig.getSchoolId())
.build();
requestBody.setQuery(query);
query.setStudentParents(studentParents);
param.setRequestBody(requestBody);
ParentServiceBatchSaveOrUpdateParentsRequest request = new ParentServiceBatchSaveOrUpdateParentsRequest(param);
String jsonString = JSON.toJSONString(request);
logger.info("入参:" + request);
//如果想要调用沙箱环境,请通过设置 request 对象的 serverUrl 属性,如:
//request.setServerUrl("https://openapi.test.seewo.com")
//执行请求,如果想获取到com.seewo.open.sdk.HttpResponse对象,请调用 seewoClient.execute 方法
ParentServiceBatchSaveOrUpdateParentsResult parentResult = seewoClient.invoke(request);
logger.info("出参:" + parentResult);
if (parentResult == null) {
return CommonResult.fail("希沃学生家长数据添加失败!");
}
if (!parentResult.getResponseBody().getCode().equals("000000")) {
return CommonResult.fail("希沃平台:" + parentResult.getResponseBody().getMessage());
}
}
}
//endregion
if (familyIndexs != null && familyIndexs.size() > 0) {
boolean insertBatch = smartFamilyIndexService.saveBatch(familyIndexs);
if (!insertBatch) {
return CommonResult.fail("导入失败!");
}
}
List newParents = new ArrayList<>();
if(insertParentDatas != null && insertParentDatas.size() > 0){
for (SmartUser parent : insertParentDatas) {
Optional student = updateStudents.stream().filter(e -> e.getCardNo().equals(parent.getAffiliate())).findFirst();
if (student != null && student.isPresent()) {
//先查找是否存在
Optional newData = newParents.stream().filter(e -> e.getName().equals(parent.getName()) && e.getPhone().equals(parent.getPhone()) && e.getShip().equals(parent.getShip())).findFirst();
if (newData != null && newData.isPresent()) {
String affiliate = newData.get().getAffiliate() + "," + student.get().getId();
newData.get().setAffiliate(affiliate);
} else {
SmartUser newParent = new SmartUser();
newParent.setName(parent.getName());
newParent.setDepartmentId(parent.getDepartmentId());
newParent.setPhone(parent.getPhone());
newParent.setIdentityId(parent.getIdentityId());
newParent.setSexId(parent.getSexId());
newParent.setIsCancel(parent.getIsCancel());
newParent.setAffiliate(String.valueOf(student.get().getId()));
newParent.setShip(parent.getShip());
newParents.add(newParent);
}
}
}
boolean insertBatch = smartUserService.saveBatch(newParents);
if (!insertBatch) {
return CommonResult.fail("批量新增家长失败!");
}
}
}
if (updateStudent != null && updateStudent.size() > 0) {
boolean updateBatch = smartUserService.updateBatchById(updateStudent);
if (!updateBatch) {
return CommonResult.fail("系统批量更新出错,导入失败");
} else {
//修改需同步到海康平台
for (SmartUser su : updateStudent) {
Integer identityId = su.getIdentityId();
if (2 == identityId || identityId == 3) {
Integer id = su.getId();
SmartOperationUser smartOperationUser = new SmartOperationUser();
smartOperationUser.setOperationId(id);
smartOperationUser.setOperationMode("2");
smartOperationUser.setStatus(1);
smartOperationUser.setType(identityId);
smartOperationUserService.save(smartOperationUser);
}
}
}
}
List insertStudents = result.stream().filter(e -> e.getId() == null).collect(Collectors.toList());
if (insertStudents != null && insertStudents.size() > 0) {
if(headImage == null){
return CommonResult.fail("更新数据文档中存在新增数据,头像不能为空");
}
List uploadImages = Arrays.asList(headImage.split(","));
for (SmartUser user : insertStudents) {
Optional image = uploadImages == null ? null : uploadImages.stream().filter(e -> e.equals("https://wanzai-1306339220.cos.ap-shanghai.myqcloud.com/" + user.getHeadImage())).findFirst();
if (image != null && image.isPresent()) {
user.setHeadImage(image.get());
} else {
if (user.getIdentityId().equals(eIdentityStatu.Student.getValue())
|| user.getIdentityId().equals(eIdentityStatu.Teacher.getValue())) {
return CommonResult.fail(user.getName() + "头像不存在,导入失败");
}
}
}
//region 将数据加入第三方
List studentDatas = insertStudents.stream().filter(e -> e.getIdentityId().intValue() == eIdentityStatu.Student.getValue()).collect(Collectors.toList());
List newCardNos = studentDatas.stream().map(SmartUser::getCardNo).collect(Collectors.toList());
if (newCardNos == null) {
return CommonResult.fail("文档内容为空,批量更新失败");
}
if (studentDatas != null && studentDatas.size() > 0) {
//判断是否存在重复数据
List cardNoDatas = studentDatas.stream().map(SmartUser::getCardNo).collect(Collectors.toList());
if (cardNoDatas == null) {
return CommonResult.fail("文档内容为空,导入失败");
}
if (cardNoDatas.size() <= 0) {
return CommonResult.fail("文档内容为空,导入失败");
}
List existUsers = smartUserService.querySmartUserByCardNos(cardNoDatas);
if (existUsers != null && existUsers.size() > 0) {
String names = StringUtils.join(existUsers.stream().map(SmartUser::getName).collect(Collectors.toList()), ",");
return CommonResult.fail("系统中已存在" + names + "的信息数据,请勿重复导入");
}
//获取班级Uid
List classDatas = smartClassService.getSmartClasss();
if (classDatas == null) {
return CommonResult.fail("班级数据无效,新增失败");
}
List gradeDatas = smartGradeService.getSmartGrades();
if (gradeDatas == null) {
return CommonResult.fail("年级数据无效,新增失败");
}
HashMap> seewoDatas = new HashMap<>();
List photoList = new ArrayList<>();
for (SmartUser student : studentDatas) {
PhotoServiceSavePhotosParam.ThirdSavePhotoQuery photo = new PhotoServiceSavePhotosParam.ThirdSavePhotoQuery();
photo.setPhotoUrl(student.getHeadImage());
photo.setUserCode(student.getCardNo());
photoList.add(photo);
//region 学生参数必填判断:年级、班级
if (student.getGrade() == null) {
return CommonResult.fail("学生年级不能为空");
}
if (student.getSchoolClass() == null) {
return CommonResult.fail("学生班级不能为空");
}
//endregion
Optional oClassData = classDatas.stream().filter(e -> e.getId().equals(student.getSchoolClass())).findFirst();
if (oClassData == null) {
return CommonResult.fail("班级数据无效,新增失败");
}
Optional oGradeData = gradeDatas.stream().filter(e -> e.getId().intValue() == Integer.valueOf(student.getGrade())).findFirst();
if (oGradeData == null) {
return CommonResult.fail("年级数据无效,新增失败");
}
SmartClass classData = oClassData.get();
SmartGrade gradeData = oGradeData.get();
//region 凑希沃参数
if (useXw == 1) {
if (!seewoDatas.containsKey(classData.getClassUid())) {
seewoDatas.put(classData.getClassUid(), new ArrayList<>());
}
StudentServiceBatchSaveClassStudentsParam.StudentInfo studentSeewo = StudentServiceBatchSaveClassStudentsParam.StudentInfo.builder()
.studentName(student.getName())
.studentCode(student.getCardNo())
.gender(student.getSexId())
.phone(student.getPhone() == null ? "" : student.getPhone())
.build();
seewoDatas.get(classData.getClassUid()).add(studentSeewo);
}
//endregion
//region 百胜新增学生信息
if (useBs == 1) {
/**
* 学生数据的有效期是到毕业年份的8月31日
*/
String startTime = TimeExchange.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
String endTime = queryGraduationYear(gradeData.getGradeNo());
CommonResult insertBsStudent = bsInsertStudent(student.getName(), student.getCardNo(), student.getSexId(), student.getHeadImage(), student.getTimeGroupId(), classData.getBsClassNo(), startTime, endTime, student.getIdCard());
if (!insertBsStudent.isSuccess()) {
return CommonResult.fail("百胜平台:" + insertBsStudent.getMessage());
}
student.setBsStudentNo(insertBsStudent.getData());
}
//endregion
}
if (useXw == 1) {
//初始化客户端
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
List seewoResultDatas = new ArrayList<>();
for (Map.Entry> entry : seewoDatas.entrySet()) {
String classUid = entry.getKey();
CommonResult> insertStudentsws = SeewoInsertBatchStudent(seewoClient, entry.getValue(), classUid);
if (!insertStudentsws.isSuccess()) {
return CommonResult.fail("希沃平台:" + insertStudentsws.getMessage());
}
seewoResultDatas.addAll(insertStudentsws.getData());
}
for (SmartUser student : studentDatas) {
Optional seewoResultData = seewoResultDatas.stream().filter(e -> e.getStudentCode().equals(student.getCardNo())).findFirst();
if (seewoResultData != null && seewoResultData.isPresent()) {
student.setXwStudentUid(seewoResultData.get().getUserUid());
}
}
}
if (studentDatas != null && studentDatas.size() > 0) {
//批量存储学生信息
boolean resultBool = smartUserService.saveBatch(studentDatas);
if (!resultBool) {
return CommonResult.fail("导入失败");
} else {
// 需添加到海康平台
ArrayList operationUsers = new ArrayList<>();
for (SmartUser studentData1 : studentDatas) {
Integer id = studentData1.getId();
SmartOperationUser smartOperationUser = new SmartOperationUser();
smartOperationUser.setOperationId(id);
smartOperationUser.setOperationMode("1");
smartOperationUser.setStatus(1);
smartOperationUser.setType(studentData1.getIdentityId());
operationUsers.add(smartOperationUser);
}
smartOperationUserService.saveBatch(operationUsers);
}
List familyIndexs = new ArrayList<>();
//region 希沃新增编辑学生家长信息
if (useXw == 1) {
int num = (int) Math.ceil((double) studentDatas.size() / 100);
for (int count = 1; count <= num; count++) {
int startIndex = (count - 1) * 100;
int endIndex = count * 100;
if (count == num) {
endIndex = startIndex + (studentDatas.size() % 100);
}
List currentStudentDatas = studentDatas.subList(startIndex, endIndex);//结尾不包含下标100
//学生与家长列表,最大100条
List studentParents = new ArrayList<>();
for (SmartUser student : currentStudentDatas) {
ParentServiceBatchSaveOrUpdateParentsParam.StudentParentsItem students = ParentServiceBatchSaveOrUpdateParentsParam.StudentParentsItem.builder()
.studentCode(student.getCardNo())
.build();
List parentDatas = insertStudents.stream().filter(e -> e.getIdentityId().intValue() == eIdentityStatu.Parent.getValue() && e.getAffiliate().equals(student.getCardNo())).collect(Collectors.toList());
List parents = new ArrayList<>();
int i = 0;
for (SmartUser parent : parentDatas) {
SmartFamilyIndex familyIndex = new SmartFamilyIndex();
familyIndex.setParentPhone(parent.getPhone());
familyIndex.setStudentNo(student.getCardNo());
familyIndex.setIndexData(i);
familyIndexs.add(familyIndex);
ParentServiceBatchSaveOrUpdateParentsParam.ParentsItem data = ParentServiceBatchSaveOrUpdateParentsParam.ParentsItem.builder()
.name(parent.getShip())
.phone(parent.getPhone())
.index(i)
.build();
parents.add(data);
++i;
}
if (parents != null && parents.size() > 0) {
studentParents.add(students);
//家长列表,最多4个
students.setParents(parents);
}
}
//初始化客户端
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
ParentServiceBatchSaveOrUpdateParentsParam param = new ParentServiceBatchSaveOrUpdateParentsParam();
//响应体,MimeType为 application/json
ParentServiceBatchSaveOrUpdateParentsParam.RequestBody requestBody = ParentServiceBatchSaveOrUpdateParentsParam.RequestBody.builder()
.build();
param.setRequestBody(requestBody);
//query
ParentServiceBatchSaveOrUpdateParentsParam.Query query = ParentServiceBatchSaveOrUpdateParentsParam.Query.builder()
.appId(seewoConfig.getAppId())
.schoolUid(seewoConfig.getSchoolId())
.build();
requestBody.setQuery(query);
query.setStudentParents(studentParents);
param.setRequestBody(requestBody);
ParentServiceBatchSaveOrUpdateParentsRequest request = new ParentServiceBatchSaveOrUpdateParentsRequest(param);
String jsonString = JSON.toJSONString(request);
logger.info("入参:" + request);
//如果想要调用沙箱环境,请通过设置 request 对象的 serverUrl 属性,如:
//request.setServerUrl("https://openapi.test.seewo.com")
//执行请求,如果想获取到com.seewo.open.sdk.HttpResponse对象,请调用 seewoClient.execute 方法
ParentServiceBatchSaveOrUpdateParentsResult parentResult = seewoClient.invoke(request);
logger.info("出参:" + parentResult);
if (parentResult == null) {
return CommonResult.fail("希沃学生家长数据添加失败!");
}
if (!parentResult.getResponseBody().getCode().equals("000000")) {
return CommonResult.fail("希沃平台:" + parentResult.getResponseBody().getMessage());
}
}
}
//endregion
if (familyIndexs != null && familyIndexs.size() > 0) {
boolean insertBatch = smartFamilyIndexService.saveBatch(familyIndexs);
if (!insertBatch) {
return CommonResult.fail("导入失败!");
}
}
}
//endregion
List parents = insertStudents.stream().filter(e -> e.getIdentityId().intValue() == eIdentityStatu.Parent.getValue()).collect(Collectors.toList());
//双胞胎家长
List newParents = new ArrayList<>();
if (newCardNos != null && newCardNos.size() > 0) {
List students = smartUserService.querySmartUserByCardNos(newCardNos);
for (SmartUser parent : parents) {
Optional student = students.stream().filter(e -> e.getCardNo().equals(parent.getAffiliate())).findFirst();
if (student != null && student.isPresent()) {
//先查找是否存在
Optional newData = newParents.stream().filter(e -> e.getName().equals(parent.getName()) && e.getPhone().equals(parent.getPhone()) && e.getShip().equals(parent.getShip())).findFirst();
if (newData != null && newData.isPresent()) {
String affiliate = newData.get().getAffiliate() + "," + student.get().getId();
newData.get().setAffiliate(affiliate);
} else {
SmartUser newParent = new SmartUser();
newParent.setName(parent.getName());
newParent.setDepartmentId(parent.getDepartmentId());
newParent.setPhone(parent.getPhone());
newParent.setIdentityId(parent.getIdentityId());
newParent.setSexId(parent.getSexId());
newParent.setIsCancel(parent.getIsCancel());
newParent.setAffiliate(String.valueOf(student.get().getId()));
newParent.setShip(parent.getShip());
newParents.add(newParent);
}
}
}
if (newParents != null && newParents.size() > 0) {
//批量存储家长信息
boolean resultBool = smartUserService.saveBatch(newParents);
if (!resultBool) {
return CommonResult.fail("导入失败!");
}
}
//region 最后上传学生图片 这样能保证图片上传失败也不影响数据导入
if (useXw == 1) {
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
int num = (int) Math.ceil((double) photoList.size() / 1000);
for (int count = 1; count <= num; count++) {
int startIndex = (count - 1) * 1000;
int endIndex = count * 1000;
if (count == num) {
endIndex = startIndex + (photoList.size() % 1000);
}
List currentPhotoList = photoList.subList(startIndex, endIndex);//结尾不包含下标1000
CommonResult photoResult = SeewoInsertBatchPhoto(seewoClient, currentPhotoList, eSeewoUserType.Student.getValue());
if (!photoResult.isSuccess()) {
return photoResult;
}
}
}
//endregion
}
}
}
return CommonResult.ok("导入成功");
}
//endregion
//region Xls文件读取方法
/**
* Xls文件读取方法
*
* @param inputStream 文件流
* @return
* @throws IOException
*/
private CommonResult> readUpdateXls(InputStream inputStream) throws IOException {
List idCards = new ArrayList<>();
List cardNos = new ArrayList<>();
List result = new ArrayList<>();
HSSFWorkbook sheets = new HSSFWorkbook(inputStream);
List grades = smartGradeService.list(null);//年级
List classs = smartClassService.list(null);//班级
List timeGroups = smartTimeGroupService.queryTimeGroups();//时间组
List departments = smartDepartmentService.list(null);
List ids = new ArrayList<>();
for (int i = Year.now().getValue() - 2; i <= Year.now().getValue(); i++) {
String strName = (i + "级");
Optional oGrade = grades.stream().filter(e -> e.getName().equals(strName)).findFirst();
if (oGrade != null && oGrade.isPresent()) {
ids.add(oGrade.get().getId());
}
}
//读取第一张sheet
HSSFSheet sheetAt = sheets.getSheetAt(0);
DataFormatter dataFormatter = new DataFormatter();
List users = smartUserService.queryStudentDatas(eIdentityStatu.Student.getValue(), ids);
List parents = smartUserService.getSmartUserByIdentity(eIdentityStatu.Parent.getValue());
try {
for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
HSSFRow row = sheetAt.getRow(rowNum);
if (row != null) {
//使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
//所以先使用setCellType()方法先将该单元格的类型设置为STRING
//然后poi会根据字符串读取它
//标题 校验
if (rowNum == 0) {
String schoolGrade = dataFormatter.formatCellValue(row.getCell(0));//年级
if (!schoolGrade.equals("年级")) {
return CommonResult.fail("导入数据第一列为年级");
}
String schoolClass = dataFormatter.formatCellValue(row.getCell(1));//班级
if (!schoolClass.equals("班级")) {
return CommonResult.fail("导入数据第二列为班级");
}
String departmentNo = dataFormatter.formatCellValue(row.getCell(2));//部门
if (!departmentNo.equals("部门")) {
return CommonResult.fail("导入数据第三列为部门");
}
String cardNo = dataFormatter.formatCellValue(row.getCell(3));//学号
if (!cardNo.equals("学号")) {
return CommonResult.fail("导入数据第四列为学号");
}
String name = dataFormatter.formatCellValue(row.getCell(4));//姓名
if (!name.equals("姓名")) {
return CommonResult.fail("导入数据第五列为姓名");
}
String sex = dataFormatter.formatCellValue(row.getCell(5));//性别
if (!sex.equals("性别")) {
return CommonResult.fail("导入数据第六列为性别");
}
String nation = dataFormatter.formatCellValue(row.getCell(6));//民族
if (!nation.equals("民族")) {
return CommonResult.fail("导入数据第七列为民族");
}
String cardId = dataFormatter.formatCellValue(row.getCell(7));//身份证
if (!cardId.equals("身份证")) {
return CommonResult.fail("导入数据第八列为身份证");
}
String headImage = dataFormatter.formatCellValue(row.getCell(8));//照片
if (!headImage.equals("照片")) {
return CommonResult.fail("导入数据第九列为照片");
}
String timeGroup = dataFormatter.formatCellValue(row.getCell(9));//常规时间组
if (!timeGroup.equals("常规时间组")) {
return CommonResult.fail("导入数据第十列为常规时间组");
}
String address = dataFormatter.formatCellValue(row.getCell(10));//住址
if (!address.equals("住址")) {
return CommonResult.fail("导入数据第十一列为住址");
}
String phone = dataFormatter.formatCellValue(row.getCell(11));//联系电话
if (!phone.equals("联系电话")) {
return CommonResult.fail("导入数据第十二列为联系电话");
}
String family = dataFormatter.formatCellValue(row.getCell(12));//家属
if (!family.equals("家属")) {
return CommonResult.fail("导入数据第十三列为家属");
}
String familyShip = dataFormatter.formatCellValue(row.getCell(13));//家属与本人关系
if (!familyShip.equals("家属与本人关系")) {
return CommonResult.fail("导入数据第十四列为家属与本人关系");
}
String phoneTwo = dataFormatter.formatCellValue(row.getCell(14));//联系电话2
if (!phoneTwo.equals("联系电话2")) {
return CommonResult.fail("导入数据第十五列为联系电话2");
}
String familyTwo = dataFormatter.formatCellValue(row.getCell(15));//家属2
if (!familyTwo.equals("家属2")) {
return CommonResult.fail("导入数据第十六列为家属2");
}
String familyShipTwo = dataFormatter.formatCellValue(row.getCell(16));//家属与本人关系2
if (!familyShipTwo.equals("家属与本人关系2")) {
return CommonResult.fail("导入数据第十七列为家属与本人关系2");
}
} else {
SmartUser user = new SmartUser();
String cardNo = dataFormatter.formatCellValue(row.getCell(3));//学号
if (ObjectUtils.isEmpty(cardNo)) {
return CommonResult.fail("第" + (rowNum) + "条数据的学号不能为空");
}
Optional oUser = users.stream().filter(e -> e.getCardNo().equals(cardNo)).findFirst();
if (oUser != null && oUser.isPresent()) {
user = oUser.get();
Optional oldClass = classs.stream().filter(e -> e.getId().equals(oUser.get().getSchoolClass())).findFirst();
if (oldClass != null && oldClass.isPresent()) {
user.setOldClassUid(oldClass.get().getClassUid());
}
//学号重复判断
if (!ObjectUtils.isEmpty(cardNo)) {
cardNos.add(cardNo);
}
if (cardNos.stream().distinct().count() != cardNos.size()) {
return CommonResult.fail("导入的Excel中," + user.getName() + "的学号:" + cardNo + "存在重复数据");
}
//身份证重复判断
String idCard = dataFormatter.formatCellValue(row.getCell(7));
if (!ObjectUtils.isEmpty(idCard)) {
idCards.add(idCard);
}
if (idCards.stream().distinct().count() != idCards.size()) {
return CommonResult.fail("导入的Excel中," + user.getName() + "的身份证号:" + idCard + "存在重复数据");
}
String name = dataFormatter.formatCellValue(row.getCell(4));//姓名
if (ObjectUtils.isEmpty(name)) {
return CommonResult.fail("第" + (rowNum) + "条数据的姓名不能为空");
}
String department = dataFormatter.formatCellValue(row.getCell(2));//部门
if (ObjectUtils.isEmpty(department)) {
return CommonResult.fail(name + "的部门不能为空");
}
String schoolClass = dataFormatter.formatCellValue(row.getCell(1));//班级
if (ObjectUtils.isEmpty(schoolClass)) {
return CommonResult.fail(name + "的班级不能为空");
}
String group = dataFormatter.formatCellValue(row.getCell(9));//时间组
if (ObjectUtils.isEmpty(group)) {
return CommonResult.fail(name + "的时间组不能为空");
}
Integer parentDid = null;
Integer departmentId = null;
String[] departmentStrs = department.split("/");
for (int i = 0; i < departmentStrs.length; i++) {
String departmentName = departmentStrs[i];
Optional oD = departments.stream().filter(e -> e.getName().equals(departmentName)).findFirst();
if (oD != null && oD.isPresent()) {
parentDid = oD.get().getId();
} else {
String bsDepartment = insertDepartmentToBs(departmentName);
if (bsDepartment == null) {
return CommonResult.fail(name + "的部门添加到百胜失败");
}
SmartDepartment sdParent = new SmartDepartment();
sdParent.setParentId(parentDid);
sdParent.setName(departmentName);
sdParent.setBsDepartmentNo(bsDepartment);
int departmentData = smartDepartmentService.insertSmartDepartment(sdParent);
departmentId = departmentData;
parentDid = departmentData;
}
}
//性别是否为空判断
String sex = dataFormatter.formatCellValue(row.getCell(5));
if (ObjectUtils.isEmpty(sex)) {
return CommonResult.fail(name + "的性别不能为空");
}
user.setSexId(sex == null ? eSexStatu.Man.getValue() : eSexStatu.integerOf(sex));
user.setDepartmentId(departmentId == null ? parentDid : departmentId);
user.setCardNo(cardNo == null ? "" : cardNo);
user.setName(name == null ? "" : name);
user.setIdentityId(eIdentityStatu.Student.getValue());
String cardId = dataFormatter.formatCellValue(row.getCell(7));//身份证
user.setIdCard(cardId == null ? user.getIdCard() : cardId);
Optional oClass = classs.stream().filter(e -> e.getName().equals(schoolClass)).findFirst();
if (oClass != null && oClass.isPresent()) {
user.setSchoolClass(oClass.get().getId());
user.setGrade(String.valueOf(oClass.get().getGradeId()));
} else {
return CommonResult.fail(name + "的班级数据无效,导入失败");
}
Optional oGroup = timeGroups.stream().filter(e -> e.getName().equals(group)).findFirst();
if (oGroup != null && oGroup.isPresent()) {
user.setTimeGroupId(oGroup.get().getId());
}
result.add(user);
//不判断重复性 因为会出现双胞胎
String phone = dataFormatter.formatCellValue(row.getCell(11));
//部门是否为空判断
String familyDepartment = dataFormatter.formatCellValue(row.getCell(1)).replace("年级", "") + "家长";
Integer familyDepartmentId = null;
Optional familyDepartModel = departments.stream().filter(e -> e.getName().equals(familyDepartment)).findFirst();
if (familyDepartModel != null && familyDepartModel.isPresent()) {
familyDepartmentId = familyDepartModel.get().getId();
} else {
familyDepartmentId = 16;
}
//region 家属
if (!ObjectUtils.isEmpty(phone)) {
if (phone.length() != 11) {
return CommonResult.fail(name + "的学生家长手机号长度不符合标准");
}
SmartUser familyOne = new SmartUser();
Optional oneParent = parents.stream().filter(e -> e.getPhone().equals(phone)).findFirst();
if(oneParent != null && oneParent.isPresent()){
familyOne = oneParent.get();
}else{
familyOne.setAffiliate(user.getCardNo());
}
String family = dataFormatter.formatCellValue(row.getCell(12));//家属
familyOne.setName(!ObjectUtils.isEmpty(family) ? family : "家长");
familyOne.setDepartmentId(familyDepartmentId);
familyOne.setPhone(phone);
familyOne.setIdentityId(eIdentityStatu.Parent.getValue());
familyOne.setSexId(eSexStatu.Man.getValue());
familyOne.setIsCancel(eLogOff.Unlogout.getValue());
familyOne.setAffiliate(user.getCardNo());
String familyShip = dataFormatter.formatCellValue(row.getCell(13));//家属与本人关系
familyOne.setShip(familyShip == null ? "其他" : familyShip);
result.add(familyOne);
}
String phoneTwo = dataFormatter.formatCellValue(row.getCell(14));//联系电话2
if (!ObjectUtils.isEmpty(phoneTwo)) {
if (phoneTwo.length() != 11) {
return CommonResult.fail(name + "的学生家长手机号长度不符合标准");
}
SmartUser familyTwo = new SmartUser();
Optional oneParent = parents.stream().filter(e -> e.getPhone().equals(phoneTwo)).findFirst();
if(oneParent != null && oneParent.isPresent()){
familyTwo = oneParent.get();
}else{
familyTwo.setAffiliate(user.getCardNo());
}
String familyNameTwo = dataFormatter.formatCellValue(row.getCell(15));//家属2
familyTwo.setName(!ObjectUtils.isEmpty(familyNameTwo) ? familyNameTwo : "家长");
familyTwo.setDepartmentId(familyDepartmentId);
familyTwo.setPhone(phoneTwo == null ? "" : phoneTwo);
familyTwo.setIdentityId(eIdentityStatu.Parent.getValue());
familyTwo.setSexId(eSexStatu.Man.getValue());
familyTwo.setIsCancel(eLogOff.Unlogout.getValue());
familyTwo.setAffiliate(user.getCardNo());
String familyShipTwo = dataFormatter.formatCellValue(row.getCell(16));//家属与本人关系2
familyTwo.setShip(familyShipTwo == null ? "其他" : familyShipTwo);
result.add(familyTwo);
}
//endregion
} else {
String name = dataFormatter.formatCellValue(row.getCell(4));
// if (ObjectUtils.isEmpty(name)) {
// return CommonResult.fail("第" + (rowNum + 1) + "条数据的名称不能为空");
// }
if (ObjectUtils.isEmpty(name)) {
continue;
}
String schoolGrade = dataFormatter.formatCellValue(row.getCell(0));//年级
if (ObjectUtils.isEmpty(schoolGrade)) {
return CommonResult.fail(name + "的年级不能为空");
}
String schoolClass = dataFormatter.formatCellValue(row.getCell(1));//班级
if (ObjectUtils.isEmpty(schoolClass)) {
return CommonResult.fail(name + "的班级不能为空");
}
//不判断重复性 因为会出现双胞胎
String phone = dataFormatter.formatCellValue(row.getCell(11));
// if (!ObjectUtils.isEmpty(phone)) {
// phones.add(phone);
// }
// if (phones.stream().distinct().count() != phones.size()) {
// return CommonResult.fail("导入的Excel中,联系电话:" + phone + "存在重复数据");
// }
//性别是否为空判断
String sex = dataFormatter.formatCellValue(row.getCell(5));
if (ObjectUtils.isEmpty(sex)) {
return CommonResult.fail(name + "的性别不能为空");
}
//家庭住址是否为空判断
String address = dataFormatter.formatCellValue(row.getCell(10));
//民族是否为空判断
String nation = dataFormatter.formatCellValue(row.getCell(6));
if (ObjectUtils.isEmpty(nation)) {
return CommonResult.fail(name + "的民族不能为空");
}
//部门是否为空判断
String department = dataFormatter.formatCellValue(row.getCell(2));
if (ObjectUtils.isEmpty(nation)) {
return CommonResult.fail(name + "的部门不能为空");
}
Integer parentDid = null;
Integer departmentId = null;
String[] departmentStrs = department.split("/");
for (int i = 0; i < departmentStrs.length; i++) {
String departmentName = departmentStrs[i];
Optional oD = departments.stream().filter(e -> e.getName().equals(departmentName)).findFirst();
if (oD != null && oD.isPresent()) {
parentDid = oD.get().getId();
} else {
String bsDepartment = insertDepartmentToBs(departmentName);
if (bsDepartment == null) {
return CommonResult.fail(name + "的部门添加到百胜失败");
}
SmartDepartment sdParent = new SmartDepartment();
sdParent.setParentId(parentDid);
sdParent.setName(departmentName);
sdParent.setBsDepartmentNo(bsDepartment);
int departmentData = smartDepartmentService.insertSmartDepartment(sdParent);
departmentId = departmentData;
parentDid = departmentData;
}
}
//学号重复判断
if (!ObjectUtils.isEmpty(cardNo)) {
cardNos.add(cardNo);
}
if (cardNos.stream().distinct().count() != cardNos.size()) {
return CommonResult.fail("导入的Excel中," + name + "的学号:" + cardNo + "存在重复数据");
}
//身份证重复判断
String idCard = dataFormatter.formatCellValue(row.getCell(7));
if (!ObjectUtils.isEmpty(idCard)) {
idCards.add(idCard);
}
if (idCards.stream().distinct().count() != idCards.size()) {
return CommonResult.fail("导入的Excel中," + name + "的身份证号:" + idCard + "存在重复数据");
}
user.setCardNo(cardNo == null ? "" : cardNo);
user.setName(name == null ? "" : name);
user.setIdentityId(eIdentityStatu.Student.getValue());
user.setIdCard(idCard == null ? "" : idCard);
user.setSexId(sex == null ? eSexStatu.Man.getValue() : eSexStatu.integerOf(sex));
user.setDepartmentId(departmentId == null ? parentDid : departmentId);
String cellImage = dataFormatter.formatCellValue(row.getCell(8));
user.setHeadImage(cellImage);
user.setDormitoryNumber("");
String grade = schoolGrade == null ? "" : schoolGrade;
Optional oGrade = grades.stream().filter(e -> e.getName().equals(grade)).findFirst();
if (oGrade != null && oGrade.isPresent()) {
Integer gradeId = oGrade.get().getId();
user.setGrade(String.valueOf(gradeId));
Optional oClass = classs.stream().filter(e -> e.getName().equals(schoolClass) && e.getGradeId().equals(gradeId)).findFirst();
if (oClass != null && oClass.isPresent()) {
user.setSchoolClass(oClass.get().getId());
} else {
return CommonResult.fail(name + "的班级数据无效,导入失败");
}
} else {
return CommonResult.fail(name + "的年级数据无效,导入失败");
}
user.setCollege("");
user.setSpeciality("");
user.setCampus("");
user.setPhone("");
user.setAffiliate("");
user.setTitle("");
user.setAddress(address == null ? "" : address);
user.setNation(nation == null ? "" : nation);
user.setOfStudent("");
user.setGraduate("");
user.setDuties(null);
String timeGroup = dataFormatter.formatCellValue(row.getCell(9));
Optional groupData = timeGroups.stream().filter(e -> e.getName().equals(timeGroup)).findFirst();
if (groupData != null && groupData.isPresent()) {
user.setTimeGroupId(groupData.get().getId());
} else {
user.setTimeGroupId(0);
}
user.setIsCancel(eLogOff.Unlogout.getValue());
result.add(user);
//部门是否为空判断
String familyDepartment = dataFormatter.formatCellValue(row.getCell(1)).replace("年级", "") + "家长";
Integer familyDepartmentId = null;
Optional familyDepartModel = departments.stream().filter(e -> e.getName().equals(familyDepartment)).findFirst();
if (familyDepartModel != null && familyDepartModel.isPresent()) {
familyDepartmentId = familyDepartModel.get().getId();
} else {
familyDepartmentId = 16;
}
//region 家属
if (!ObjectUtils.isEmpty(phone)) {
if (phone.length() != 11) {
return CommonResult.fail(name + "的学生家长手机号长度不符合标准");
}
SmartUser familyOne = new SmartUser();
String family = dataFormatter.formatCellValue(row.getCell(12));//家属
familyOne.setName(!ObjectUtils.isEmpty(family) ? family : "家长");
familyOne.setDepartmentId(familyDepartmentId);
familyOne.setPhone(phone);
familyOne.setIdentityId(eIdentityStatu.Parent.getValue());
familyOne.setSexId(eSexStatu.Man.getValue());
familyOne.setIsCancel(eLogOff.Unlogout.getValue());
familyOne.setAffiliate(user.getCardNo());
String familyShip = dataFormatter.formatCellValue(row.getCell(13));//家属与本人关系
familyOne.setShip(familyShip == null ? "其他" : familyShip);
result.add(familyOne);
}
String phoneTwo = dataFormatter.formatCellValue(row.getCell(14));//联系电话2
if (!ObjectUtils.isEmpty(phoneTwo)) {
if (phoneTwo.length() != 11) {
return CommonResult.fail(name + "的学生家长手机号长度不符合标准");
}
SmartUser familyTwo = new SmartUser();
String familyNameTwo = dataFormatter.formatCellValue(row.getCell(15));//家属2
familyTwo.setName(!ObjectUtils.isEmpty(familyNameTwo) ? familyNameTwo : "家长");
familyTwo.setDepartmentId(familyDepartmentId);
familyTwo.setPhone(phoneTwo == null ? "" : phoneTwo);
familyTwo.setIdentityId(eIdentityStatu.Parent.getValue());
familyTwo.setSexId(eSexStatu.Man.getValue());
familyTwo.setIsCancel(eLogOff.Unlogout.getValue());
familyTwo.setAffiliate(user.getCardNo());
String familyShipTwo = dataFormatter.formatCellValue(row.getCell(16));//家属与本人关系2
familyTwo.setShip(familyShipTwo == null ? "其他" : familyShipTwo);
result.add(familyTwo);
}
//endregion
//希沃不允许一个学生家长的两个手机号重复 所以做一个重复性判断
if (!ObjectUtils.isEmpty(phone) && !ObjectUtils.isEmpty(dataFormatter.formatCellValue(row.getCell(14)))) {
if (phone.equals(dataFormatter.formatCellValue(row.getCell(14)))) {
return CommonResult.fail(name + "的学生家长手机号不可重复");
}
}
}
}
}
}
} catch (Exception e) {
return CommonResult.fail("请按模板格式导入数据");
}
return CommonResult.ok(result);
}
//endregion
//region Xlsx文件读取方法
/**
* Xlsx文件读取方法
*
* @param inputStream 文件流
* @return
* @throws IOException
*/
private CommonResult> readUpdateXlsx(InputStream inputStream) throws IOException {
List idCards = new ArrayList<>();
List cardNos = new ArrayList<>();
List result = new ArrayList<>();
XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
List grades = smartGradeService.list(null);//年级
List classs = smartClassService.list(null);//班级
List timeGroups = smartTimeGroupService.queryTimeGroups();//时间组
List departments = smartDepartmentService.list(null);
List ids = new ArrayList<>();
for (int i = Year.now().getValue() - 2; i <= Year.now().getValue(); i++) {
String strName = (i + "级");
Optional oGrade = grades.stream().filter(e -> e.getName().equals(strName)).findFirst();
if (oGrade != null && oGrade.isPresent()) {
ids.add(oGrade.get().getId());
}
}
//读取第一张sheet
XSSFSheet sheetAt = sheets.getSheetAt(0);
DataFormatter dataFormatter = new DataFormatter();
List users = smartUserService.queryStudentDatas(eIdentityStatu.Student.getValue(), ids);
List parents = smartUserService.getSmartUserByIdentity(eIdentityStatu.Parent.getValue());
try {
for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
XSSFRow row = sheetAt.getRow(rowNum);
if (row != null) {
//使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
//所以先使用setCellType()方法先将该单元格的类型设置为STRING
//然后poi会根据字符串读取它
//标题 校验
if (rowNum == 0) {
String schoolGrade = dataFormatter.formatCellValue(row.getCell(0));//年级
if (!schoolGrade.equals("年级")) {
return CommonResult.fail("导入数据第一列为年级");
}
String schoolClass = dataFormatter.formatCellValue(row.getCell(1));//班级
if (!schoolClass.equals("班级")) {
return CommonResult.fail("导入数据第二列为班级");
}
String departmentNo = dataFormatter.formatCellValue(row.getCell(2));//部门
if (!departmentNo.equals("部门")) {
return CommonResult.fail("导入数据第三列为部门");
}
String cardNo = dataFormatter.formatCellValue(row.getCell(3));//学号
if (!cardNo.equals("学号")) {
return CommonResult.fail("导入数据第四列为学号");
}
String name = dataFormatter.formatCellValue(row.getCell(4));//姓名
if (!name.equals("姓名")) {
return CommonResult.fail("导入数据第五列为姓名");
}
String sex = dataFormatter.formatCellValue(row.getCell(5));//性别
if (!sex.equals("性别")) {
return CommonResult.fail("导入数据第六列为性别");
}
String nation = dataFormatter.formatCellValue(row.getCell(6));//民族
if (!nation.equals("民族")) {
return CommonResult.fail("导入数据第七列为民族");
}
String cardId = dataFormatter.formatCellValue(row.getCell(7));//身份证
if (!cardId.equals("身份证")) {
return CommonResult.fail("导入数据第八列为身份证");
}
String headImage = dataFormatter.formatCellValue(row.getCell(8));//照片
if (!headImage.equals("照片")) {
return CommonResult.fail("导入数据第九列为照片");
}
String timeGroup = dataFormatter.formatCellValue(row.getCell(9));//常规时间组
if (!timeGroup.equals("常规时间组")) {
return CommonResult.fail("导入数据第十列为常规时间组");
}
String address = dataFormatter.formatCellValue(row.getCell(10));//住址
if (!address.equals("住址")) {
return CommonResult.fail("导入数据第十一列为住址");
}
String phone = dataFormatter.formatCellValue(row.getCell(11));//联系电话
if (!phone.equals("联系电话")) {
return CommonResult.fail("导入数据第十二列为联系电话");
}
String family = dataFormatter.formatCellValue(row.getCell(12));//家属
if (!family.equals("家属")) {
return CommonResult.fail("导入数据第十三列为家属");
}
String familyShip = dataFormatter.formatCellValue(row.getCell(13));//家属与本人关系
if (!familyShip.equals("家属与本人关系")) {
return CommonResult.fail("导入数据第十四列为家属与本人关系");
}
String phoneTwo = dataFormatter.formatCellValue(row.getCell(14));//联系电话2
if (!phoneTwo.equals("联系电话2")) {
return CommonResult.fail("导入数据第十五列为联系电话2");
}
String familyTwo = dataFormatter.formatCellValue(row.getCell(15));//家属2
if (!familyTwo.equals("家属2")) {
return CommonResult.fail("导入数据第十六列为家属2");
}
String familyShipTwo = dataFormatter.formatCellValue(row.getCell(16));//家属与本人关系2
if (!familyShipTwo.equals("家属与本人关系2")) {
return CommonResult.fail("导入数据第十七列为家属与本人关系2");
}
} else {
SmartUser user = new SmartUser();
String cardNo = dataFormatter.formatCellValue(row.getCell(3));//学号
if (ObjectUtils.isEmpty(cardNo)) {
return CommonResult.fail("第" + (rowNum) + "条数据的学号不能为空");
}
Optional oUser = users.stream().filter(e -> e.getCardNo().equals(cardNo)).findFirst();
if (oUser != null && oUser.isPresent()) {
user = oUser.get();
Optional oldClass = classs.stream().filter(e -> e.getId().equals(oUser.get().getSchoolClass())).findFirst();
if (oldClass != null && oldClass.isPresent()) {
user.setOldClassUid(oldClass.get().getClassUid());
}
//学号重复判断
if (!ObjectUtils.isEmpty(cardNo)) {
cardNos.add(cardNo);
}
if (cardNos.stream().distinct().count() != cardNos.size()) {
return CommonResult.fail("导入的Excel中," + user.getName() + "的学号:" + cardNo + "存在重复数据");
}
//身份证重复判断
String idCard = dataFormatter.formatCellValue(row.getCell(7));
if (!ObjectUtils.isEmpty(idCard)) {
idCards.add(idCard);
}
if (idCards.stream().distinct().count() != idCards.size()) {
return CommonResult.fail("导入的Excel中," + user.getName() + "的身份证号:" + idCard + "存在重复数据");
}
String name = dataFormatter.formatCellValue(row.getCell(4));//姓名
if (ObjectUtils.isEmpty(name)) {
return CommonResult.fail("第" + (rowNum) + "条数据的姓名不能为空");
}
String department = dataFormatter.formatCellValue(row.getCell(2));//部门
if (ObjectUtils.isEmpty(department)) {
return CommonResult.fail(name + "的部门不能为空");
}
String schoolClass = dataFormatter.formatCellValue(row.getCell(1));//班级
if (ObjectUtils.isEmpty(schoolClass)) {
return CommonResult.fail(name + "的班级不能为空");
}
String group = dataFormatter.formatCellValue(row.getCell(9));//时间组
if (ObjectUtils.isEmpty(group)) {
return CommonResult.fail(name + "的时间组不能为空");
}
Integer parentDid = null;
Integer departmentId = null;
String[] departmentStrs = department.split("/");
for (int i = 0; i < departmentStrs.length; i++) {
String departmentName = departmentStrs[i];
Optional oD = departments.stream().filter(e -> e.getName().equals(departmentName)).findFirst();
if (oD != null && oD.isPresent()) {
parentDid = oD.get().getId();
} else {
String bsDepartment = insertDepartmentToBs(departmentName);
if (bsDepartment == null) {
return CommonResult.fail(name + "的部门添加到百胜失败");
}
SmartDepartment sdParent = new SmartDepartment();
sdParent.setParentId(parentDid);
sdParent.setName(departmentName);
sdParent.setBsDepartmentNo(bsDepartment);
int departmentData = smartDepartmentService.insertSmartDepartment(sdParent);
departmentId = departmentData;
parentDid = departmentData;
}
}
//性别是否为空判断
String sex = dataFormatter.formatCellValue(row.getCell(5));
if (ObjectUtils.isEmpty(sex)) {
return CommonResult.fail(name + "的性别不能为空");
}
user.setSexId(sex == null ? eSexStatu.Man.getValue() : eSexStatu.integerOf(sex));
user.setDepartmentId(departmentId == null ? parentDid : departmentId);
user.setCardNo(cardNo == null ? "" : cardNo);
user.setName(name == null ? "" : name);
user.setIdentityId(eIdentityStatu.Student.getValue());
String cardId = dataFormatter.formatCellValue(row.getCell(7));//身份证
user.setIdCard(cardId == null ? user.getIdCard() : cardId);
String address = dataFormatter.formatCellValue(row.getCell(10));//住址
user.setAddress(address == null ? user.getAddress() : address);
Optional oClass = classs.stream().filter(e -> e.getName().equals(schoolClass)).findFirst();
if (oClass != null && oClass.isPresent()) {
user.setSchoolClass(oClass.get().getId());
user.setGrade(String.valueOf(oClass.get().getGradeId()));
} else {
return CommonResult.fail(name + "的班级数据无效,导入失败");
}
Optional oGroup = timeGroups.stream().filter(e -> e.getName().equals(group)).findFirst();
if (oGroup != null && oGroup.isPresent()) {
user.setTimeGroupId(oGroup.get().getId());
}
result.add(user);
//不判断重复性 因为会出现双胞胎
String phone = dataFormatter.formatCellValue(row.getCell(11));
//部门是否为空判断
String familyDepartment = dataFormatter.formatCellValue(row.getCell(1)).replace("年级", "") + "家长";
Integer familyDepartmentId = null;
Optional familyDepartModel = departments.stream().filter(e -> e.getName().equals(familyDepartment)).findFirst();
if (familyDepartModel != null && familyDepartModel.isPresent()) {
familyDepartmentId = familyDepartModel.get().getId();
} else {
familyDepartmentId = 16;
}
//region 家属
if(user.getName().equals("张宇霏")){
String sdsd ="";
}
if (!ObjectUtils.isEmpty(phone)) {
if (phone.length() != 11) {
return CommonResult.fail(name + "的学生家长手机号长度不符合标准");
}
SmartUser familyOne = new SmartUser();
Optional oneParent = parents.stream().filter(e -> e.getPhone().equals(phone)).findFirst();
if(oneParent != null && oneParent.isPresent()){
familyOne = oneParent.get();
}else{
familyOne.setAffiliate(user.getCardNo());
}
String family = dataFormatter.formatCellValue(row.getCell(12));//家属
familyOne.setName(!ObjectUtils.isEmpty(family) ? family : "家长");
familyOne.setDepartmentId(familyDepartmentId);
familyOne.setPhone(phone);
familyOne.setIdentityId(eIdentityStatu.Parent.getValue());
familyOne.setSexId(eSexStatu.Man.getValue());
familyOne.setIsCancel(eLogOff.Unlogout.getValue());
String familyShip = dataFormatter.formatCellValue(row.getCell(13));//家属与本人关系
familyOne.setShip(familyShip == null ? "其他" : familyShip);
result.add(familyOne);
}
String phoneTwo = dataFormatter.formatCellValue(row.getCell(14));//联系电话2
if (!ObjectUtils.isEmpty(phoneTwo)) {
if (phoneTwo.length() != 11) {
return CommonResult.fail(name + "的学生家长手机号长度不符合标准");
}
SmartUser familyTwo = new SmartUser();
Optional oneParent = parents.stream().filter(e -> e.getPhone().equals(phoneTwo)).findFirst();
if(oneParent != null && oneParent.isPresent()){
familyTwo = oneParent.get();
}else{
familyTwo.setAffiliate(user.getCardNo());
}
String familyNameTwo = dataFormatter.formatCellValue(row.getCell(15));//家属2
familyTwo.setName(!ObjectUtils.isEmpty(familyNameTwo) ? familyNameTwo : "家长");
familyTwo.setDepartmentId(familyDepartmentId);
familyTwo.setPhone(phoneTwo == null ? "" : phoneTwo);
familyTwo.setIdentityId(eIdentityStatu.Parent.getValue());
familyTwo.setSexId(eSexStatu.Man.getValue());
familyTwo.setIsCancel(eLogOff.Unlogout.getValue());
String familyShipTwo = dataFormatter.formatCellValue(row.getCell(16));//家属与本人关系2
familyTwo.setShip(familyShipTwo == null ? "其他" : familyShipTwo);
result.add(familyTwo);
}
//endregion
} else {
String name = dataFormatter.formatCellValue(row.getCell(4));
// if (ObjectUtils.isEmpty(name)) {
// return CommonResult.fail("第" + (rowNum + 1) + "条数据的名称不能为空");
// }
if (ObjectUtils.isEmpty(name)) {
continue;
}
String schoolGrade = dataFormatter.formatCellValue(row.getCell(0));//年级
if (ObjectUtils.isEmpty(schoolGrade)) {
return CommonResult.fail(name + "的年级不能为空");
}
String schoolClass = dataFormatter.formatCellValue(row.getCell(1));//班级
if (ObjectUtils.isEmpty(schoolClass)) {
return CommonResult.fail(name + "的班级不能为空");
}
//不判断重复性 因为会出现双胞胎
String phone = dataFormatter.formatCellValue(row.getCell(11));
// if (!ObjectUtils.isEmpty(phone)) {
// phones.add(phone);
// }
// if (phones.stream().distinct().count() != phones.size()) {
// return CommonResult.fail("导入的Excel中,联系电话:" + phone + "存在重复数据");
// }
//性别是否为空判断
String sex = dataFormatter.formatCellValue(row.getCell(5));
if (ObjectUtils.isEmpty(sex)) {
return CommonResult.fail(name + "的性别不能为空");
}
//家庭住址是否为空判断
String address = dataFormatter.formatCellValue(row.getCell(10));
//民族是否为空判断
String nation = dataFormatter.formatCellValue(row.getCell(6));
if (ObjectUtils.isEmpty(nation)) {
return CommonResult.fail(name + "的民族不能为空");
}
//部门是否为空判断
String department = dataFormatter.formatCellValue(row.getCell(2));
if (ObjectUtils.isEmpty(nation)) {
return CommonResult.fail(name + "的部门不能为空");
}
Integer parentDid = null;
Integer departmentId = null;
String[] departmentStrs = department.split("/");
for (int i = 0; i < departmentStrs.length; i++) {
String departmentName = departmentStrs[i];
Optional oD = departments.stream().filter(e -> e.getName().equals(departmentName)).findFirst();
if (oD != null && oD.isPresent()) {
parentDid = oD.get().getId();
} else {
String bsDepartment = insertDepartmentToBs(departmentName);
if (bsDepartment == null) {
return CommonResult.fail(name + "的部门添加到百胜失败");
}
SmartDepartment sdParent = new SmartDepartment();
sdParent.setParentId(parentDid);
sdParent.setName(departmentName);
sdParent.setBsDepartmentNo(bsDepartment);
int departmentData = smartDepartmentService.insertSmartDepartment(sdParent);
departmentId = departmentData;
parentDid = departmentData;
}
}
//学号重复判断
if (!ObjectUtils.isEmpty(cardNo)) {
cardNos.add(cardNo);
}
if (cardNos.stream().distinct().count() != cardNos.size()) {
return CommonResult.fail("导入的Excel中," + name + "的学号:" + cardNo + "存在重复数据");
}
//身份证重复判断
String idCard = dataFormatter.formatCellValue(row.getCell(7));
if (!ObjectUtils.isEmpty(idCard)) {
idCards.add(idCard);
}
if (idCards.stream().distinct().count() != idCards.size()) {
return CommonResult.fail("导入的Excel中," + name + "的身份证号:" + idCard + "存在重复数据");
}
user.setCardNo(cardNo == null ? "" : cardNo);
user.setName(name == null ? "" : name);
user.setIdentityId(eIdentityStatu.Student.getValue());
user.setIdCard(idCard == null ? "" : idCard);
user.setSexId(sex == null ? eSexStatu.Man.getValue() : eSexStatu.integerOf(sex));
user.setDepartmentId(departmentId == null ? parentDid : departmentId);
String cellImage = dataFormatter.formatCellValue(row.getCell(8));
user.setHeadImage(cellImage);
user.setDormitoryNumber("");
String grade = schoolGrade == null ? "" : schoolGrade;
Optional oGrade = grades.stream().filter(e -> e.getName().equals(grade)).findFirst();
if (oGrade != null && oGrade.isPresent()) {
Integer gradeId = oGrade.get().getId();
user.setGrade(String.valueOf(gradeId));
Optional oClass = classs.stream().filter(e -> e.getName().equals(schoolClass) && e.getGradeId().equals(gradeId)).findFirst();
if (oClass != null && oClass.isPresent()) {
user.setSchoolClass(oClass.get().getId());
} else {
return CommonResult.fail(name + "的班级数据无效,导入失败");
}
} else {
return CommonResult.fail(name + "的年级数据无效,导入失败");
}
user.setCollege("");
user.setSpeciality("");
user.setCampus("");
user.setPhone("");
user.setAffiliate("");
user.setTitle("");
user.setAddress(address == null ? "" : address);
user.setNation(nation == null ? "" : nation);
user.setOfStudent("");
user.setGraduate("");
user.setDuties(null);
String timeGroup = dataFormatter.formatCellValue(row.getCell(9));
Optional groupData = timeGroups.stream().filter(e -> e.getName().equals(timeGroup)).findFirst();
if (groupData != null && groupData.isPresent()) {
user.setTimeGroupId(groupData.get().getId());
} else {
user.setTimeGroupId(0);
}
user.setIsCancel(eLogOff.Unlogout.getValue());
result.add(user);
//部门是否为空判断
String familyDepartment = dataFormatter.formatCellValue(row.getCell(1)).replace("年级", "") + "家长";
Integer familyDepartmentId = null;
Optional familyDepartModel = departments.stream().filter(e -> e.getName().equals(familyDepartment)).findFirst();
if (familyDepartModel != null && familyDepartModel.isPresent()) {
familyDepartmentId = familyDepartModel.get().getId();
} else {
familyDepartmentId = 16;
}
//region 家属
if (!ObjectUtils.isEmpty(phone)) {
if (phone.length() != 11) {
return CommonResult.fail(name + "的学生家长手机号长度不符合标准");
}
SmartUser familyOne = new SmartUser();
String family = dataFormatter.formatCellValue(row.getCell(12));//家属
familyOne.setName(!ObjectUtils.isEmpty(family) ? family : "家长");
familyOne.setDepartmentId(familyDepartmentId);
familyOne.setPhone(phone);
familyOne.setIdentityId(eIdentityStatu.Parent.getValue());
familyOne.setSexId(eSexStatu.Man.getValue());
familyOne.setIsCancel(eLogOff.Unlogout.getValue());
familyOne.setAffiliate(user.getCardNo());
String familyShip = dataFormatter.formatCellValue(row.getCell(13));//家属与本人关系
familyOne.setShip(familyShip == null ? "其他" : familyShip);
result.add(familyOne);
}
String phoneTwo = dataFormatter.formatCellValue(row.getCell(14));//联系电话2
if (!ObjectUtils.isEmpty(phoneTwo)) {
if (phoneTwo.length() != 11) {
return CommonResult.fail(name + "的学生家长手机号长度不符合标准");
}
SmartUser familyTwo = new SmartUser();
String familyNameTwo = dataFormatter.formatCellValue(row.getCell(15));//家属2
familyTwo.setName(!ObjectUtils.isEmpty(familyNameTwo) ? familyNameTwo : "家长");
familyTwo.setDepartmentId(familyDepartmentId);
familyTwo.setPhone(phoneTwo == null ? "" : phoneTwo);
familyTwo.setIdentityId(eIdentityStatu.Parent.getValue());
familyTwo.setSexId(eSexStatu.Man.getValue());
familyTwo.setIsCancel(eLogOff.Unlogout.getValue());
familyTwo.setAffiliate(user.getCardNo());
String familyShipTwo = dataFormatter.formatCellValue(row.getCell(16));//家属与本人关系2
familyTwo.setShip(familyShipTwo == null ? "其他" : familyShipTwo);
result.add(familyTwo);
}
//endregion
//希沃不允许一个学生家长的两个手机号重复 所以做一个重复性判断
if (!ObjectUtils.isEmpty(phone) && !ObjectUtils.isEmpty(dataFormatter.formatCellValue(row.getCell(14)))) {
if (phone.equals(dataFormatter.formatCellValue(row.getCell(14)))) {
return CommonResult.fail(name + "的学生家长手机号不可重复");
}
}
}
}
}
}
} catch (Exception e) {
return CommonResult.fail("请按模板格式导入数据");
}
return CommonResult.ok(result);
}
//endregion
//endregion
//region 时间组数据集合
@Override
@DESRespondSecret(validated = true)
public CommonResult timeGroups() throws Exception {
List result = new ArrayList<>();
//region 获取百胜的时间组
String appId = controlConfig.getAppId();
String schoolno = controlConfig.getSchoolCode();
String timestamp = TimeExchange.DateNowTimeStamo();
String appSecret = controlConfig.getAppSecret();
String url = controlConfig.getUrl() + "timegroup/searchtimegroup";
JSONObject jsonobject = new JSONObject();
jsonobject.put("appid", appId);
String str = "{\"pageindex\":\"" + 1 + "\",\"pagesize\":\"" + 20 + "\"}";
String aesStr = URLEncoder.encode(AesTestOne.encrypt(str, controlConfig.getAppSecret()), "UTF-8");
jsonobject.put("data", aesStr);
jsonobject.put("schoolno", schoolno);
jsonobject.put("timestamp", timestamp);
String md5Str = "appid=" + appId + "&data={\"pageindex\":\"" + 1 + "\",\"pagesize\":\"" + 20 + "\"}" + "&schoolno=" + schoolno + "×tamp=" + timestamp + "&key=" + appSecret;
String sign = CommonUtil.MD5(md5Str);
//sign签名
jsonobject.put("sign", sign);
//返回的结果中 code为1表示成功
String bsResult = RequestUtils.httpPost(url, jsonobject.toJSONString());
ObjectMapper objectMapper = new ObjectMapper();
BsTimeGroupVo bsDataResult = objectMapper.readValue(bsResult, BsTimeGroupVo.class);
if (!bsResult.contains("查询成功")) {
return CommonResult.fail(bsDataResult.getMsg());
}
// URL解码
String decodedUrl = URLDecoder.decode(bsDataResult.getData(), "UTF-8");
BsTimeGroupNoVo timeGroupNos = objectMapper.readValue(decrypt(decodedUrl, controlConfig.getAppSecret()), BsTimeGroupNoVo.class);
//endregion
List groups = smartTimeGroupService.queryTimeGroups();
List newGroups = new ArrayList<>();
List oldGroups = new ArrayList<>();
if (timeGroupNos.getData() != null) {
for (BsTimeGroupNoListVo timeGroup : timeGroupNos.getData()) {
Optional oGroup = groups == null ? null : groups.stream().filter(e -> e.getBsno().equals(timeGroup.getTg_no())).findFirst();
if (oGroup != null && oGroup.isPresent()) {
SmartTimeGroup oldData = new SmartTimeGroup();
oldData.setId(oGroup.get().getId());
oldData.setBsno(timeGroup.getTg_no());
oldData.setName(timeGroup.getTg_name());
oldData.setRemark(timeGroup.getTg_remark());
oldGroups.add(oldData);
} else {
SmartTimeGroup newData = new SmartTimeGroup();
newData.setBsno(timeGroup.getTg_no());
newData.setName(timeGroup.getTg_name());
newData.setRemark(timeGroup.getTg_remark());
newGroups.add(newData);
}
}
}
if (oldGroups != null && oldGroups.size() > 0) {
boolean updateGroup = smartTimeGroupService.updateSmartTimeGroups(oldGroups);
if (!updateGroup) {
return CommonResult.fail("获取时间组失败");
}
}
if (newGroups != null && newGroups.size() > 0) {
boolean insertGroup = smartTimeGroupService.insertSmartTimeGroups(newGroups);
if (!insertGroup) {
return CommonResult.fail("获取时间组失败");
}
}
//oldData集合不在newDatas集合中的内容
List newDatas = oldGroups.stream().map(SmartTimeGroup::getId).collect(Collectors.toList());
List oldDatas = groups.stream().map(SmartTimeGroup::getId).collect(Collectors.toList());
List deleteIds = oldDatas.stream().filter(item -> !newDatas.contains(item)).collect(Collectors.toList());//需要删除的id
if (deleteIds.size() > 0) {
int deleted = smartTimeGroupService.deletedTimeGroupByIds(deleteIds);
if (deleted <= 0) {
throw new Exception("获取时间组失败!");
}
}
List groupDatas = smartTimeGroupService.queryTimeGroups();
for (SmartTimeGroup groupData : groupDatas) {
TimeGroupVo data = new TimeGroupVo();
data.setId(groupData.getId());
data.setName(groupData.getName());
data.setRemark(groupData.getRemark());
result.add(data);
}
return CommonResult.ok(result);
}
//endregion
//region 新增用户
/**
* 新增用户
*
* @param isur 用户数据
* @param bindingResult
* @return
*/
@Override
@DESRespondSecret(validated = true)
public CommonResult insertSmartUser(insertSmartUserRequest isur, BindingResult bindingResult) throws Exception {
if (bindingResult.hasErrors()) {
String st = paramUtils.getParamError(bindingResult);
return CommonResult.fail(st);
}
int useBs = 1;
int useXw = 1;
SmartGrade gradeData = null;
SmartClass classData = null;
if (org.springframework.util.StringUtils.hasText(isur.getCardNo())) {
//重复性判断
int existCount = smartUserService.querySmartUserByCardNo(isur.getCardNo());
if (existCount > 0) {
return CommonResult.fail("当前学号已存在,请勿重复添加");
}
}
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
SmartUser su = new SmartUser();
su.setTimeGroupId(isur.getTimeGroupId());
su.setCardNo(isur.getCardNo());
su.setName(isur.getName());
su.setIdentityId(isur.getIdentityId());
su.setIdCard(isur.getIdCard());
su.setSexId(isur.getSexId());
su.setDepartmentId(isur.getDepartmentId());
su.setHeadImage(isur.getHeadImage());
su.setGrade(isur.getGrade());
su.setCollege(isur.getCollege());
su.setSpeciality(isur.getSpeciality());
su.setSchoolClass(isur.getSchoolClass());
su.setCampus(isur.getCampus());
su.setDormitoryNumber(isur.getDormitoryNumber());
su.setPhone(isur.getPhone());
su.setAffiliate(StringUtils.join(isur.getAffiliate(), ","));
su.setTitle(isur.getTitle());
su.setAddress(isur.getAddress());
su.setNation(isur.getNation());
su.setOfStudent(isur.getOfStudent());
su.setGraduate(isur.getGraduate());
su.setDuties(isur.getDuties());
su.setIsCancel(eLogOff.Unlogout.getValue());
//region 人员信息加入到第三方api
//要将用户数据加入到希沃和百胜中
//希沃和百胜的老师、学生数据添加是不一样的,所以按身份添加
if (isur.getIdentityId().intValue() == eIdentityStatu.Parent.getValue()) {//家长
//region 家长参数必填判断:手机号
if (isur.getPhone() == null) {
return CommonResult.fail("家长手机号不能为空");
}
//endregion
//查找家长身份是否存在用了该手机号的用户
int existCount = smartUserService.queryParentByCount(isur.getPhone());
if (existCount > 0) {
return CommonResult.fail("当前手机号已被其他家长使用");
}
//拿到被关联学生的信息去获取对应的卡号
//有多个学生就循环学生
if (isur.getAffiliate() == null) {
return CommonResult.fail("被关联人不能为空");
}
if (isur.getAffiliate().size() <= 0) {
return CommonResult.fail("被关联人不能为空");
}
List studentDatas = smartUserService.getSmartUserIds(isur.getAffiliate());
//region 希沃新增编辑学生家长信息
if (useXw == 1) {
CommonResult insertOrUpdateStudent = insertOrUpdateStudentParent(seewoClient, studentDatas, "其他", isur.getPhone(), true);
if (!insertOrUpdateStudent.isSuccess()) {
return CommonResult.fail("希沃平台:" + insertOrUpdateStudent.getMessage());
}
}
//endregion
} else if (isur.getIdentityId().intValue() == eIdentityStatu.Student.getValue()) {//学生
//region 学生参数必填判断:年级、班级
if (isur.getGrade() == null) {
return CommonResult.fail("学生年级不能为空");
}
if (isur.getSchoolClass() == null) {
return CommonResult.fail("学生班级不能为空");
}
//endregion
//获取班级Uid
classData = smartClassService.getSmartClassById(isur.getSchoolClass());
if (classData == null) {
return CommonResult.fail("班级数据无效,新增失败");
}
gradeData = smartGradeService.querySmartGradeById(Integer.valueOf(isur.getGrade()));
if (gradeData == null) {
return CommonResult.fail("年级数据无效,新增失败");
}
//region 希沃新增学生信息
if (useXw == 1) {
CommonResult insertStudent = SeewoInsertStudent(seewoClient, isur.getName(), isur.getCardNo(), isur.getSexId(), isur.getPhone(), classData.getClassUid());
if (!insertStudent.isSuccess()) {
return CommonResult.fail("希沃平台:" + insertStudent.getMessage());
}
su.setXwStudentUid(insertStudent.getData());
//上传图片
List photoList = new ArrayList<>();
PhotoServiceSavePhotosParam.ThirdSavePhotoQuery photo = new PhotoServiceSavePhotosParam.ThirdSavePhotoQuery();
photo.setPhotoUrl(su.getHeadImage());
photo.setUserCode(su.getCardNo());
photoList.add(photo);
CommonResult result = SeewoInsertBatchPhoto(seewoClient, photoList, eSeewoUserType.Student.getValue());
if (!result.isSuccess()) {
return result;
}
}
//endregion
//region 百胜新增学生信息
if (useBs == 1) {
/**
* 学生数据的有效期是到毕业年份的8月31日
*/
String startTime = TimeExchange.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
String endTime = queryGraduationYear(gradeData.getGradeNo());
CommonResult insertBsStudent = bsInsertStudent(isur.getName(), isur.getCardNo(), isur.getSexId(), isur.getHeadImage(), isur.getTimeGroupId(), classData.getBsClassNo(), startTime, endTime, isur.getIdCard());
if (!insertBsStudent.isSuccess()) {
return CommonResult.fail("百胜平台:" + insertBsStudent.getMessage());
}
su.setBsStudentNo(insertBsStudent.getData());
}
//endregion
} else if (isur.getIdentityId().intValue() == eIdentityStatu.Teacher.getValue()) {//老师
//region 老师参数必填判断:职称、手机号
if (isur.getTitle() == null) {
return CommonResult.fail("老师职称不能为空");
}
if (isur.getPhone() == null) {
return CommonResult.fail("老师手机号不能为空");
}
//endregion
//region 希沃添加教师数据
if (useXw == 1) {
CommonResult insertTeacher = SeewoInsertTeacher(seewoClient, isur.getPhone(), isur.getName(), isur.getHeadImage());
if (!insertTeacher.isSuccess()) {
return CommonResult.fail("希沃平台:" + insertTeacher.getMessage());
}
su.setXwTeacherCode(insertTeacher.getData());
//上传图片
List photoList = new ArrayList<>();
PhotoServiceSavePhotosParam.ThirdSavePhotoQuery photo = new PhotoServiceSavePhotosParam.ThirdSavePhotoQuery();
photo.setPhotoUrl(su.getHeadImage());
photo.setUserCode(su.getPhone());
photoList.add(photo);
CommonResult result = SeewoInsertBatchPhoto(seewoClient, photoList, eSeewoUserType.Teacher.getValue());
if (!result.isSuccess()) {
return result;
}
}
//endregion
if (isur.getDuties().intValue() == eDuties.ClassTeacher.getValue()) {
//获取班级Uid
classData = smartClassService.getSmartClassById(isur.getSchoolClass());
if (classData == null) {
return CommonResult.fail("班级数据无效,新增失败");
}
//region 将班主任推送到希沃
if (useXw == 1) {
CommonResult pushMaster = SeewoPushMaster(seewoClient, isur.getPhone(), classData.getClassUid());
if (!pushMaster.isSuccess()) {
return CommonResult.fail("希沃平台:" + pushMaster.getMessage());
}
}
//endregion
}
SmartDepartment departmentData = smartDepartmentService.getSmartById(isur.getDepartmentId());
if (departmentData == null) {
return CommonResult.fail("部门数据无效,新增教师失败");
}
String departmentNo = departmentData.getBsDepartmentNo();//"DT1701845086538710";
if (departmentNo == null) {
return CommonResult.fail("百胜部门编号为空,新增教师失败");
}
//region 百胜添加教师数据
if (useBs == 1) {
/**
* 教师数据的有效期是20年
*/
String startTime = TimeExchange.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
String endTime = TimeExchange.addYear(20);
CommonResult insertBsTeacher = bsInsertTeacher(su, departmentNo, startTime, endTime);
if (!insertBsTeacher.isSuccess()) {
return CommonResult.fail("百胜平台:" + insertBsTeacher.getMessage());
}
su.setBsStaffCode(insertBsTeacher.getData());
}
//endregion
}
//endregion
//最后都要把数据加入到数据库中
if (org.springframework.util.StringUtils.hasText(isur.getPhone())) {
//多身份
List users = smartUserService.getPhoneUsers(isur.getPhone());
if (users != null && users.size() > 0) {
Optional ownerUser = users.stream().filter(e -> org.springframework.util.StringUtils.hasText(e.getXOpenId())).findFirst();
if (ownerUser != null && ownerUser.isPresent()) {
su.setXOpenId(ownerUser.get().getXOpenId());
}
}
}
int result = smartUserService.insertSmartUser(su);
if (result > 0) {
// 将添加的用户同步到海康平台
Integer identityId = su.getIdentityId();
if (2 == identityId || identityId == 3) {
Integer id = su.getId();
SmartOperationUser smartOperationUser = new SmartOperationUser();
smartOperationUser.setOperationId(id);
smartOperationUser.setOperationMode("1");
smartOperationUser.setStatus(1);
smartOperationUser.setType(identityId);
smartOperationUserService.save(smartOperationUser);
}
if (isur.getIdentityId().intValue() == eIdentityStatu.Teacher.getValue()) {
if (isur.getIsr() != null && isur.getIsr().size() > 0) {
List teachings = new ArrayList<>();
for (isrRequest sv : isur.getIsr()) {
if (sv.getIds() != null && sv.getIds().size() > 0) {
SmartTeaching st = new SmartTeaching();
st.setUserId(result);
st.setSubjectId(sv.getIds().size() > 0 ? sv.getIds().get(0) : 0);
st.setGradeId(sv.getIds().size() > 1 ? sv.getIds().get(1) : 0);
st.setClassId(sv.getIds().size() > 2 ? sv.getIds().get(2) : 0);
teachings.add(st);
}
}
if (teachings != null && teachings.size() > 0) {
boolean insertBatch = smartTeachingService.saveBatch(teachings);
if (!insertBatch) {
return CommonResult.fail("任课数据添加失败");
}
}
}
//region 新增用户的时候同步新增教师综合评价数据
List semesters = smartSemesterService.querySemesterByTime(new Date());
gradeData = smartGradeService.querySmartGradeById(Integer.valueOf(isur.getGrade()));
if (semesters != null && semesters.size() > 0) {
List eTeachers = new ArrayList<>();
for (SmartSemester semester : semesters) {
SmartEvaluateTeacher set = new SmartEvaluateTeacher();
set.setCardNo(su.getCardNo());
set.setName(su.getName());
set.setUserId(su.getId());
set.setTermId(semester.getId());
set.setTerm(semester.getName());
if (su.getDuties().intValue() == eDuties.ClassTeacher.getValue()) {//班主任才有年级和班级
set.setGradeId(Integer.valueOf(su.getGrade()));
if (gradeData != null) {
set.setGradeName(gradeData.getName());
}
set.setClassId(su.getSchoolClass());
if (classData != null) {
set.setClassName(classData.getName());
}
}
set.setTotalScore(0.0);
set.setLowingScore(0.0);
eTeachers.add(set);
}
if (eTeachers != null && eTeachers.size() > 0) {
boolean insertEteacher = smartEvaluateTeacherService.saveBatch(eTeachers);
if (!insertEteacher) {
return CommonResult.fail("添加失败");
}
}
}
//endregion
}
}
return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
}
//endregion
@Override
@DESRespondSecret(validated = true)
@PassToken
public CommonResult updateTest(int departmentId) throws Exception {
List departmentIds = new ArrayList<>();
departmentIds.add(81);
departmentIds.add(82);
departmentIds.add(83);
departmentIds.add(84);
departmentIds.add(85);
departmentIds.add(86);
departmentIds.add(87);
departmentIds.add(88);
departmentIds.add(89);
departmentIds.add(90);
departmentIds.add(91);
departmentIds.add(92);
departmentIds.add(93);
departmentIds.add(94);
departmentIds.add(95);
departmentIds.add(96);
departmentIds.add(97);
departmentIds.add(98);
List users = smartUserService.queryDatasByDepartments(departmentIds);
for (SmartUser usur:users) {
if(usur.getName().equals("张玉玲")){
String sdsd = "";
}
int useXw = 1;
int useBs = 1;
//更新的同时将百胜用户信息同步过去或者同步过来?
SmartUser su = smartUserService.getSmartById(usur.getId());
if (su == null) {
return CommonResult.fail("用户数据已失效,修改失败!");
}
//是否转换身份
boolean changeIdentity = false;
Integer oldIdentity = null;
String oldAffiliate = su.getAffiliate();
Integer oldSchoolClass = su.getSchoolClass();
String oldStaffNo = su.getBsStaffCode();
String oldCardNo = su.getCardNo();
if (usur.getIdentityId().intValue() != su.getIdentityId().intValue()) {
changeIdentity = true;
oldIdentity = su.getIdentityId().intValue();
}
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
su.setTimeGroupId(usur.getTimeGroupId());
su.setCardNo(usur.getCardNo());
su.setName(usur.getName());
su.setIdentityId(usur.getIdentityId());
su.setIdCard(usur.getIdCard());
su.setSexId(usur.getSexId());
su.setDepartmentId(usur.getDepartmentId());
su.setHeadImage(usur.getHeadImage());
su.setGrade(usur.getGrade());
su.setCollege(usur.getCollege());
su.setSpeciality(usur.getSpeciality());
su.setSchoolClass(usur.getSchoolClass());
su.setCampus(usur.getCampus());
su.setDormitoryNumber(usur.getDormitoryNumber());
su.setPhone(usur.getPhone());
su.setAffiliate(StringUtils.join(usur.getAffiliate(), ","));
su.setTitle(usur.getTitle());
su.setAddress(usur.getAddress());
su.setNation(usur.getNation());
su.setOfStudent(usur.getOfStudent());
su.setGraduate(usur.getGraduate());
su.setDuties(usur.getDuties());
su.setIsCancel(eLogOff.Unlogout.getValue());
if (usur.getIdentityId().intValue() == eIdentityStatu.Parent.getValue()) {//家长
//拿到被关联学生的信息去获取对应的卡号
//有多个学生就循环学生
if (usur.getAffiliate() == null) {
return CommonResult.fail("被关联人不能为空");
}
List affiliates = Arrays.asList(usur.getAffiliate().split(","));
if (affiliates.size() <= 0) {
return CommonResult.fail("被关联人不能为空");
}
SmartUser oldUser = smartUserService.queryParentByPhone(usur.getPhone());
if (oldUser != null && oldUser.getId().intValue() != usur.getId().intValue()) {
//return CommonResult.fail("当前手机号已被其他家长使用");
}
List studentDatas = smartUserService.getSmartUserIds(affiliates);
if(studentDatas.size() <= 0){
smartUserService.deleteSmartUserById(usur.getId());
continue;
}
if (!changeIdentity) {
//region 希沃新增编辑学生家长信息
if (useXw == 1) {
//学生与家长列表,最大100条
CommonResult insertOrUpdateResult = insertOrUpdateStudentParent(seewoClient, studentDatas, "其他", usur.getPhone(), false);
if (!insertOrUpdateResult.isSuccess()) {
return CommonResult.fail("希沃平台:" + insertOrUpdateResult.getMessage());
}
}
//endregion
} else {
if (oldIdentity.intValue() == eIdentityStatu.Student.getValue()) {
//region 希沃删除学生
//获取班级Uid
SmartClass classData = smartClassService.getSmartClassById(oldSchoolClass);
if (classData == null) {
return CommonResult.fail("班级数据无效,更新失败");
}
if (useXw == 1) {
CommonResult deleteStudent = SeewoDeleteStudent(seewoClient, classData.getClassUid(), oldCardNo);
if (!deleteStudent.isSuccess()) {
return CommonResult.fail("希沃平台:" + deleteStudent.getMessage());
}
}
//endregion
//region 百胜删除学生
if (useBs == 1) {
CommonResult deleteBsStudent = bsDeleteStudent(su);
if (!deleteBsStudent.isSuccess()) {
return CommonResult.fail("百胜平台:" + deleteBsStudent.getMessage());
}
}
//endregion
} else if (oldIdentity.intValue() == eIdentityStatu.Teacher.getValue()) {
//获取班级Uid
SmartClass classData = smartClassService.getSmartClassById(oldSchoolClass);
if (classData == null) {
return CommonResult.fail("班级数据无效,更新失败");
}
//region 希沃删除教师
if (useXw == 1) {
CommonResult deleteTeacher = SeewoDeleteTeacher(seewoClient, su.getPhone());
if (!deleteTeacher.isSuccess()) {
return CommonResult.fail("希沃平台:" + deleteTeacher.getMessage());
}
}
//endregion
//region 希沃删除班主任
if (useXw == 1) {
CommonResult deleteTeacherMaster = SeewoDeleteTeacherMaster(seewoClient, classData.getClassUid(), su.getPhone());
if (deleteTeacherMaster.isSuccess()) {
return CommonResult.fail("希沃平台:" + deleteTeacherMaster.getMessage());
}
}
//endregion
//region 百胜删除教师
if (useBs == 1) {
CommonResult bsDeleteTeacher = bsDeleteTeacher(oldStaffNo);
if (!bsDeleteTeacher.isSuccess()) {
return CommonResult.fail("百胜平台:" + bsDeleteTeacher.getMessage());
}
}
//endregion
}
//region 希沃新增编辑学生家长信息
if (useXw == 1) {
List indexs = smartFamilyIndexService.querySmartFamilyByCardNo(su.getCardNo());
if (indexs != null && indexs.size() >= 4) {
return CommonResult.fail("绑定失败,希沃学生家长最多绑定四个家长");
}
CommonResult insertOrUpdate = insertOrUpdateStudentParent(seewoClient, studentDatas, su.getShip(), su.getPhone(), true);
if (!insertOrUpdate.isSuccess()) {
return CommonResult.fail("希沃平台:" + insertOrUpdate.getMessage());
}
}
//endregion
}
}
}
return CommonResult.ok();
}
//region 更新用户
/**
* 更新用户
*
* @param usur 更新用户数据
* @param bindingResult
* @return
*/
@Override
@DESRespondSecret(validated = true)
public CommonResult updateSmartUserById(updateSmartUserRequest usur, BindingResult bindingResult) throws Exception {
if (bindingResult.hasErrors()) {
String st = paramUtils.getParamError(bindingResult);
return CommonResult.fail(st);
}
//region 参数判断
if (usur.getIdentityId().intValue() == eIdentityStatu.Student.getValue()) {
if (usur.getTimeGroupId() == null) {
return CommonResult.fail("学生时间组不能为空");
}
if (usur.getCardNo() == null) {
return CommonResult.fail("学生编号不能为空");
}
if (usur.getHeadImage() == null) {
return CommonResult.fail("学生人脸照片不能为空");
}
}
//endregion
int useXw = 1;
int useBs = 1;
//更新的同时将百胜用户信息同步过去或者同步过来?
SmartUser su = smartUserService.getSmartById(usur.getId());
if (su == null) {
return CommonResult.fail("用户数据已失效,修改失败!");
}
//是否转换身份
boolean changeIdentity = false;
Integer oldIdentity = null;
String oldAffiliate = su.getAffiliate();
Integer oldSchoolClass = su.getSchoolClass();
String oldStaffNo = su.getBsStaffCode();
String oldCardNo = su.getCardNo();
if (usur.getIdentityId().intValue() != su.getIdentityId().intValue()) {
changeIdentity = true;
oldIdentity = su.getIdentityId().intValue();
}
SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
su.setTimeGroupId(usur.getTimeGroupId());
su.setCardNo(usur.getCardNo());
su.setName(usur.getName());
su.setIdentityId(usur.getIdentityId());
su.setIdCard(usur.getIdCard());
su.setSexId(usur.getSexId());
su.setDepartmentId(usur.getDepartmentId());
su.setHeadImage(usur.getHeadImage());
su.setGrade(usur.getGrade());
su.setCollege(usur.getCollege());
su.setSpeciality(usur.getSpeciality());
su.setSchoolClass(usur.getSchoolClass());
su.setCampus(usur.getCampus());
su.setDormitoryNumber(usur.getDormitoryNumber());
su.setPhone(usur.getPhone());
su.setAffiliate(StringUtils.join(usur.getAffiliate(), ","));
su.setTitle(usur.getTitle());
su.setAddress(usur.getAddress());
su.setNation(usur.getNation());
su.setOfStudent(usur.getOfStudent());
su.setGraduate(usur.getGraduate());
su.setDuties(usur.getDuties());
su.setIsCancel(eLogOff.Unlogout.getValue());
if (usur.getIdentityId().intValue() == eIdentityStatu.Parent.getValue()) {//家长
//拿到被关联学生的信息去获取对应的卡号
//有多个学生就循环学生
if (usur.getAffiliate() == null) {
return CommonResult.fail("被关联人不能为空");
}
if (usur.getAffiliate().size() <= 0) {
return CommonResult.fail("被关联人不能为空");
}
SmartUser oldUser = smartUserService.queryParentByPhone(usur.getPhone());
if (oldUser != null && oldUser.getId().intValue() != usur.getId().intValue()) {
return CommonResult.fail("当前手机号已被其他家长使用");
}
List studentDatas = smartUserService.getSmartUserIds(usur.getAffiliate());
if (!changeIdentity) {
//region 希沃新增编辑学生家长信息
if (useXw == 1) {
//学生与家长列表,最大100条
CommonResult insertOrUpdateResult = insertOrUpdateStudentParent(seewoClient, studentDatas, "其他", usur.getPhone(), false);
if (!insertOrUpdateResult.isSuccess()) {
return CommonResult.fail("希沃平台:" + insertOrUpdateResult.getMessage());
}
}
//endregion
} else {
if (oldIdentity.intValue() == eIdentityStatu.Student.getValue()) {
//region 希沃删除学生
//获取班级Uid
SmartClass classData = smartClassService.getSmartClassById(oldSchoolClass);
if (classData == null) {
return CommonResult.fail("班级数据无效,更新失败");
}
if (useXw == 1) {
CommonResult deleteStudent = SeewoDeleteStudent(seewoClient, classData.getClassUid(), oldCardNo);
if (!deleteStudent.isSuccess()) {
return CommonResult.fail("希沃平台:" + deleteStudent.getMessage());
}
}
//endregion
//region 百胜删除学生
if (useBs == 1) {
CommonResult deleteBsStudent = bsDeleteStudent(su);
if (!deleteBsStudent.isSuccess()) {
return CommonResult.fail("百胜平台:" + deleteBsStudent.getMessage());
}
}
//endregion
} else if (oldIdentity.intValue() == eIdentityStatu.Teacher.getValue()) {
//获取班级Uid
SmartClass classData = smartClassService.getSmartClassById(oldSchoolClass);
if (classData == null) {
return CommonResult.fail("班级数据无效,更新失败");
}
//region 希沃删除教师
if (useXw == 1) {
CommonResult deleteTeacher = SeewoDeleteTeacher(seewoClient, su.getPhone());
if (!deleteTeacher.isSuccess()) {
return CommonResult.fail("希沃平台:" + deleteTeacher.getMessage());
}
}
//endregion
//region 希沃删除班主任
if (useXw == 1) {
CommonResult deleteTeacherMaster = SeewoDeleteTeacherMaster(seewoClient, classData.getClassUid(), su.getPhone());
if (deleteTeacherMaster.isSuccess()) {
return CommonResult.fail("希沃平台:" + deleteTeacherMaster.getMessage());
}
}
//endregion
//region 百胜删除教师
if (useBs == 1) {
CommonResult bsDeleteTeacher = bsDeleteTeacher(oldStaffNo);
if (!bsDeleteTeacher.isSuccess()) {
return CommonResult.fail("百胜平台:" + bsDeleteTeacher.getMessage());
}
}
//endregion
}
//region 希沃新增编辑学生家长信息
if (useXw == 1) {
List indexs = smartFamilyIndexService.querySmartFamilyByCardNo(su.getCardNo());
if (indexs != null && indexs.size() >= 4) {
return CommonResult.fail("绑定失败,希沃学生家长最多绑定四个家长");
}
CommonResult insertOrUpdate = insertOrUpdateStudentParent(seewoClient, studentDatas, su.getShip(), su.getPhone(), true);
if (!insertOrUpdate.isSuccess()) {
return CommonResult.fail("希沃平台:" + insertOrUpdate.getMessage());
}
}
//endregion
}
} else if (usur.getIdentityId().intValue() == eIdentityStatu.Student.getValue()) {//学生
//region 年级和班级不能为空
if (usur.getGrade() == null) {
return CommonResult.fail("学生年级不能为空");
}
if (usur.getSchoolClass() == null) {
return CommonResult.fail("学生班级不能为空");
}
//endregion
//获取班级Uid
SmartClass classData = smartClassService.getSmartClassById(su.getSchoolClass());
if (classData == null) {
return CommonResult.fail("班级数据无效,更新失败");
}
//获取年级Uid
SmartGrade gradeData = smartGradeService.querySmartGradeById(Integer.valueOf(su.getGrade()));
if (gradeData == null) {
return CommonResult.fail("年级数据无效,新增失败");
}
if (!changeIdentity) {
//region 更新希沃学生信息
if (useXw == 1) {
CommonResult updateStudent = SeewoUpdateStudent(seewoClient, su);
if (!updateStudent.isSuccess()) {
if (updateStudent.getMessage().equals("学生不存在")) {
CommonResult insertStudent = SeewoInsertStudent(seewoClient, su.getName(), su.getCardNo(), su.getSexId(), su.getPhone(), classData.getClassUid());
if (!insertStudent.isSuccess()) {
return CommonResult.fail("希沃平台:" + insertStudent.getMessage());
}
}
} else {
if (!su.getSchoolClass().equals(oldSchoolClass)) {
//获取班级Uid
SmartClass oldClassData = smartClassService.getSmartClassById(oldSchoolClass);
if (oldClassData == null) {
return CommonResult.fail("班级数据无效,更新失败");
}
CommonResult changeClass = SeewoChangeClass(seewoClient, seewoConfig.getSchoolId(), su.getXwStudentUid(), oldClassData.getClassUid(), classData.getClassUid());
if(!changeClass.isSuccess()){
return CommonResult.fail("希沃平台:" + changeClass.getMessage());
}
}
}
//上传图片
List photoList = new ArrayList<>();
PhotoServiceSavePhotosParam.ThirdSavePhotoQuery photo = new PhotoServiceSavePhotosParam.ThirdSavePhotoQuery();
photo.setPhotoUrl(su.getHeadImage());
photo.setUserCode(su.getCardNo());
photoList.add(photo);
CommonResult result = SeewoInsertBatchPhoto(seewoClient, photoList, eSeewoUserType.Student.getValue());
if (!result.isSuccess()) {
return result;
}
}
//endregion
//region 更新百胜学生信息
if (useBs == 1) {
/**
* 学生数据的有效期是到毕业年份的8月31日
*/
String startTime = TimeExchange.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
String endTime = queryGraduationYear(gradeData.getGradeNo());
CommonResult updateBsStudent = bsUpdateStudent(su, classData.getBsClassNo() ,startTime, endTime);
if (!updateBsStudent.isSuccess()) {
if (updateBsStudent.getMessage().equals("人员不存在或已删除")) {
//region 百胜新增学生信息
CommonResult insertBsStudent = bsInsertStudent(usur.getName(), usur.getCardNo(), usur.getSexId(), usur.getHeadImage(), usur.getTimeGroupId(), classData.getBsClassNo(), startTime, endTime, usur.getIdCard());
if (!insertBsStudent.isSuccess()) {
return CommonResult.fail("百胜平台:" + insertBsStudent.getMessage());
}
su.setBsStudentNo(insertBsStudent.getData());
//endregion
} else {
return CommonResult.fail("百胜平台:" + updateBsStudent.getMessage());
}
}
}
//endregion
} else {
if (oldIdentity.intValue() == eIdentityStatu.Parent.getValue()) {
List studentDatas = smartUserService.getSmartUserIds(Arrays.asList(oldAffiliate.split(",")));
//region 希沃删除原有的家长关系
if (useXw == 1) {
CommonResult deleteResult = deleteOldParentShip(seewoClient, studentDatas, su.getPhone());
if (!deleteResult.isSuccess()) {
return CommonResult.fail("希沃平台:" + deleteResult.getMessage());
}
smartFamilyIndexService.deleteSmartFamilyByPhone(su.getPhone());
}
//endregion
} else if (oldIdentity.intValue() == eIdentityStatu.Teacher.getValue()) {
//获取班级Uid
SmartClass oldClassData = smartClassService.getSmartClassById(oldSchoolClass);
if (oldClassData == null) {
return CommonResult.fail("班级数据无效,更新失败");
}
//region 希沃删除教师
if (useXw == 1) {
CommonResult deleteTeacher = SeewoDeleteTeacher(seewoClient, su.getPhone());
if (!deleteTeacher.isSuccess()) {
return CommonResult.fail("希沃平台:" + deleteTeacher.getMessage());
}
}
//endregion
//region 希沃删除班主任
if (useXw == 1) {
CommonResult deleteMaster = SeewoDeleteTeacherMaster(seewoClient, oldClassData.getClassUid(), su.getPhone());
if (!deleteMaster.isSuccess()) {
return CommonResult.fail("希沃平台:" + deleteMaster.getMessage());
}
}
//endregion
//region 百胜删除教师
if (useBs == 1) {
CommonResult deleteBsTeacher = bsDeleteTeacher(oldStaffNo);
if (!deleteBsTeacher.isSuccess()) {
return CommonResult.fail("百胜平台:" + deleteBsTeacher.getMessage());
}
}
//endregion
}
//region 希沃新增学生信息
if (useXw == 1) {
CommonResult insertStudent = SeewoInsertStudent(seewoClient, su.getName(), su.getCardNo(), su.getSexId(), su.getPhone(), classData.getClassUid());
if (!insertStudent.isSuccess()) {
return CommonResult.fail("希沃平台:" + insertStudent.getMessage());
} else {
su.setXwStudentUid(insertStudent.getData());
}
//上传图片
List photoList = new ArrayList<>();
PhotoServiceSavePhotosParam.ThirdSavePhotoQuery photo = new PhotoServiceSavePhotosParam.ThirdSavePhotoQuery();
photo.setPhotoUrl(su.getHeadImage());
photo.setUserCode(su.getCardNo());
photoList.add(photo);
CommonResult result = SeewoInsertBatchPhoto(seewoClient, photoList, eSeewoUserType.Student.getValue());
if (!result.isSuccess()) {
return result;
}
}
//endregion
//region 百胜新增学生信息
if (useBs == 1) {
/**
* 学生数据的有效期是到毕业年份的8月31日
*/
String startTime = TimeExchange.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
String endTime = queryGraduationYear(gradeData.getGradeNo());
CommonResult insertBsStudent = bsInsertStudent(su.getName(), su.getCardNo(), su.getSexId(), su.getHeadImage(), su.getTimeGroupId(), classData.getBsClassNo(), startTime, endTime, su.getIdCard());
if (insertBsStudent.isSuccess()) {
su.setBsStudentNo(insertBsStudent.getData());
} else {
//region 希沃删除学生
//百胜数据插入失败后需要把已插入到希沃的数据删除
CommonResult deleteStudent = SeewoDeleteStudent(seewoClient, classData.getClassUid(), oldCardNo);
if (!deleteStudent.isSuccess()) {
return CommonResult.fail("百胜平台:" + deleteStudent.getMessage());
}
//endregion
return CommonResult.fail("切换身份后," + insertBsStudent.getMessage() + ",百胜无法插入学生数据");
}
}
//endregion
}
} else if (usur.getIdentityId().intValue() == eIdentityStatu.Teacher.getValue()) {//教师
if (!changeIdentity) {
//region 希沃更新教师数据
if (useXw == 1) {
CommonResult updateTeacher = SeewoUpdateTeacher(seewoClient, su.getPhone(), su.getName(), su.getHeadImage());
if (!updateTeacher.isSuccess()) {
return CommonResult.fail("希沃平台:" + updateTeacher.getMessage());
}
su.setXwTeacherCode(updateTeacher.getData());
//上传图片
List photoList = new ArrayList<>();
PhotoServiceSavePhotosParam.ThirdSavePhotoQuery photo = new PhotoServiceSavePhotosParam.ThirdSavePhotoQuery();
photo.setPhotoUrl(su.getHeadImage());
photo.setUserCode(su.getPhone());
photoList.add(photo);
CommonResult result = SeewoInsertBatchPhoto(seewoClient, photoList, eSeewoUserType.Teacher.getValue());
if (!result.isSuccess()) {
return result;
}
}
if (su.getDuties().intValue() == eDuties.ClassTeacher.getValue()) {
//获取班级Uid
SmartClass classData = smartClassService.getSmartClassById(su.getSchoolClass());
if (classData == null) {
return CommonResult.fail("班级数据无效,新增失败");
}
if (useXw == 1) {
//获取班级Uid
SmartClass oldClassData = smartClassService.getSmartClassById(oldSchoolClass);
if (oldClassData == null) {
return CommonResult.fail("班级数据无效,更新失败");
}
CommonResult deleteMaster = SeewoDeleteTeacherMaster(seewoClient, oldClassData.getClassUid(), su.getPhone());
if (!deleteMaster.isSuccess()) {
return CommonResult.fail("希沃平台:" + deleteMaster.getMessage());
}
}
//region 将班主任推送到希沃
if (useXw == 1) {
CommonResult pushMaster = SeewoPushMaster(seewoClient, su.getPhone(), classData.getClassUid());
if (!pushMaster.isSuccess()) {
return CommonResult.fail("希沃平台:" + pushMaster.getMessage());
}
}
//endregion
}
//endregion
SmartDepartment departmentData = smartDepartmentService.getSmartById(su.getDepartmentId());
if (departmentData == null) {
return CommonResult.fail("部门数据无效,更新教师失败");
}
String departmentNo = departmentData.getBsDepartmentNo();
if (departmentNo == null) {
return CommonResult.fail("百胜部门编号为空,新增教师失败");
}
//region 百胜更新教师数据
if (useBs == 1) {
/**
* 教师数据的有效期是20年
*/
String startTime = TimeExchange.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
String endTime = TimeExchange.addYear(20);
CommonResult bsUpdateTeacher = updateBsTeacher(su, departmentNo, startTime, endTime);
if (!bsUpdateTeacher.isSuccess()) {
return CommonResult.fail("百胜平台:" + bsUpdateTeacher.getMessage());
}
su.setBsStaffCode(bsUpdateTeacher.getData());
}
//endregion
} else {
if (oldIdentity.intValue() == eIdentityStatu.Parent.getValue()) {
List studentDatas = smartUserService.getSmartUserIds(Arrays.asList(oldAffiliate.split(",")));
//region 希沃删除原有的家长关系
if (useXw == 1) {
CommonResult deleteOldShip = deleteOldParentShip(seewoClient, studentDatas, su.getPhone());
if (!deleteOldShip.isSuccess()) {
return CommonResult.fail("希沃平台:" + deleteOldShip.getMessage());
}
smartFamilyIndexService.deleteSmartFamilyByPhone(su.getPhone());
}
//endregion
} else if (oldIdentity.intValue() == eIdentityStatu.Student.getValue()) {
//获取班级Uid
SmartClass classData = smartClassService.getSmartClassById(oldSchoolClass);
if (classData == null) {
return CommonResult.fail("班级数据无效,更新失败");
}
//region 希沃删除学生
if (useXw == 1) {
CommonResult deleteStudent = SeewoDeleteStudent(seewoClient, classData.getClassUid(), oldCardNo);
if (!deleteStudent.isSuccess()) {
return CommonResult.fail("希沃平台:" + deleteStudent.getMessage());
}
}
//endregion
//region 百胜删除学生
if (useBs == 1) {
CommonResult deleteBsStudent = bsDeleteStudent(su);
if (!deleteBsStudent.isSuccess()) {
return CommonResult.fail("百胜平台:" + deleteBsStudent.getMessage());
}
}
//endregion
}
//region 希沃添加教师数据
if (useXw == 1) {
CommonResult insertTeacher = SeewoInsertTeacher(seewoClient, su.getPhone(), su.getName(), su.getHeadImage());
if (!insertTeacher.isSuccess()) {
return CommonResult.fail("希沃平台:" + insertTeacher.getMessage());
}
su.setXwTeacherCode(insertTeacher.getData());
//上传图片
List