|
|
@@ -0,0 +1,322 @@
|
|
|
+package com.xjrsoft.module.xycxedu.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.github.yulichang.base.MPJBaseServiceImpl;
|
|
|
+import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
|
|
+import com.xjrsoft.common.enums.ArchivesStatusEnum;
|
|
|
+import com.xjrsoft.common.enums.DeleteMark;
|
|
|
+import com.xjrsoft.common.utils.VoToColumnUtil;
|
|
|
+import com.xjrsoft.module.base.entity.BaseCourseSubject;
|
|
|
+import com.xjrsoft.module.base.service.IBaseCourseSubjectService;
|
|
|
+import com.xjrsoft.module.generator.entity.ImportConfig;
|
|
|
+import com.xjrsoft.module.organization.entity.User;
|
|
|
+import com.xjrsoft.module.organization.service.IUserService;
|
|
|
+import com.xjrsoft.module.student.entity.BaseStudent;
|
|
|
+import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
|
|
|
+import com.xjrsoft.module.veb.util.ImportExcelUtil;
|
|
|
+import com.xjrsoft.module.xycxedu.dto.AddExamSubjectScoreEnterDto;
|
|
|
+import com.xjrsoft.module.xycxedu.dto.ExamSubjectScoreEnterDto;
|
|
|
+import com.xjrsoft.module.xycxedu.dto.ExamSubjectScoreEnterImportDto;
|
|
|
+import com.xjrsoft.module.xycxedu.dto.ExamSubjectScoreEnterPageDto;
|
|
|
+import com.xjrsoft.module.xycxedu.entity.ExamSubjectScore;
|
|
|
+import com.xjrsoft.module.xycxedu.entity.ExamSubjectScoreEnter;
|
|
|
+import com.xjrsoft.module.xycxedu.mapper.ExamSubjectScoreEnterMapper;
|
|
|
+import com.xjrsoft.module.xycxedu.service.IExamSubjectScoreEnterService;
|
|
|
+import com.xjrsoft.module.xycxedu.service.IExamSubjectScoreService;
|
|
|
+import com.xjrsoft.module.xycxedu.vo.ExamSubjectScoreEnterImportVo;
|
|
|
+import com.xjrsoft.module.xycxedu.vo.ExamSubjectScoreEnterPageVo;
|
|
|
+import com.xjrsoft.module.xycxedu.vo.ExamSubjectScoreEnterVo;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
+import org.apache.poi.ss.usermodel.FillPatternType;
|
|
|
+import org.apache.poi.ss.usermodel.Font;
|
|
|
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
|
+import org.apache.poi.ss.usermodel.IndexedColors;
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+* @title: 成绩录入
|
|
|
+* @Author dzx
|
|
|
+* @Date: 2025-01-06
|
|
|
+* @Version 1.0
|
|
|
+*/
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class ExamSubjectScoreEnterServiceImpl extends MPJBaseServiceImpl<ExamSubjectScoreEnterMapper, ExamSubjectScoreEnter> implements IExamSubjectScoreEnterService {
|
|
|
+
|
|
|
+ private final IBaseCourseSubjectService courseSubjectService;
|
|
|
+ private final IUserService userService;
|
|
|
+ private final IExamSubjectScoreService scoreService;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Boolean importData(Long id, MultipartFile file) throws IOException {
|
|
|
+ ExamSubjectScoreEnter enter = this.getById(id);
|
|
|
+ List<ExamSubjectScoreEnterImportDto> dataList = EasyExcel.read(file.getInputStream()).headRowNumber(3).head(ExamSubjectScoreEnterImportDto.class).sheet().doReadSync();
|
|
|
+ List<BaseCourseSubject> classList = courseSubjectService.list(
|
|
|
+ new QueryWrapper<BaseCourseSubject>().lambda()
|
|
|
+ .eq(BaseCourseSubject::getDeleteMark, DeleteMark.NODELETE.getCode())
|
|
|
+ );
|
|
|
+ Map<Long, String> courseSubjectMaps = classList.stream().collect(Collectors.toMap(BaseCourseSubject::getId, BaseCourseSubject::getName));
|
|
|
+
|
|
|
+ List<String> credentialNumbers = dataList.stream().filter(x -> StrUtil.isNotEmpty(x.getCredentialNumber())).map(ExamSubjectScoreEnterImportDto::getCredentialNumber).collect(Collectors.toList());
|
|
|
+ if(credentialNumbers.isEmpty()){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ List<User> userList = userService.list(
|
|
|
+ new QueryWrapper<User>().lambda()
|
|
|
+ .eq(User::getDeleteMark, DeleteMark.NODELETE.getCode())
|
|
|
+ .in(User::getCredentialNumber, credentialNumbers)
|
|
|
+ );
|
|
|
+ Map<String, Long> userMaps = userList.stream().collect(Collectors.toMap(User::getCredentialNumber, User::getId));
|
|
|
+
|
|
|
+ List<ExamSubjectScore> scoreList = scoreService.list(
|
|
|
+ new QueryWrapper<ExamSubjectScore>().lambda()
|
|
|
+ .eq(ExamSubjectScore::getExamSubjectScoreEnterId, enter.getId())
|
|
|
+ .eq(ExamSubjectScore::getDeleteMark, DeleteMark.NODELETE.getCode())
|
|
|
+ );
|
|
|
+
|
|
|
+ Map<Long, ExamSubjectScore> listMap = scoreList.stream().collect(Collectors.toMap(ExamSubjectScore::getUserId, x -> x));
|
|
|
+
|
|
|
+ List<ExamSubjectScore> updateList = new ArrayList<>();
|
|
|
+ for (ExamSubjectScoreEnterImportDto importVo : dataList) {
|
|
|
+ Long userId = userMaps.get(importVo.getCredentialNumber());
|
|
|
+ ExamSubjectScore examSubjectScores = listMap.get(userId);
|
|
|
+ examSubjectScores.setCourseSubjectId(enter.getCourseSubjectId());
|
|
|
+ examSubjectScores.setScore(importVo.getScore()==null?null:Float.parseFloat(importVo.getScore()));
|
|
|
+ examSubjectScores.setSemesterId(enter.getSemesterId());
|
|
|
+ examSubjectScores.setExamSubjectScoreEnterId(enter.getId());
|
|
|
+ examSubjectScores.setSemesterId(enter.getSemesterId());
|
|
|
+ examSubjectScores.setClassRanking(importVo.getClassRanking());
|
|
|
+ examSubjectScores.setGradeRanking(importVo.getGradeRanking());
|
|
|
+ examSubjectScores.setCoursename(courseSubjectMaps.get(enter.getCourseSubjectId()));
|
|
|
+ examSubjectScores.setMilexamid(enter.getExamPlanId());
|
|
|
+ updateList.add(examSubjectScores);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!updateList.isEmpty()){
|
|
|
+ scoreService.updateBatchById(updateList);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean save(AddExamSubjectScoreEnterDto dto) {
|
|
|
+ ExamSubjectScoreEnter examSubjectScoreEnter = BeanUtil.toBean(dto, ExamSubjectScoreEnter.class);
|
|
|
+ examSubjectScoreEnter.setCreateDate(new Date());
|
|
|
+ String classIds = dto.getClassIds().toString().replace("[", "").replace("]", "").replace(" ", "");
|
|
|
+ examSubjectScoreEnter.setClassIds(classIds);
|
|
|
+ BaseCourseSubject courseSubject = courseSubjectService.getById(examSubjectScoreEnter.getCourseSubjectId());
|
|
|
+ examSubjectScoreEnter.setCoursename(courseSubject.getName());
|
|
|
+ boolean isSuccess = this.save(examSubjectScoreEnter);
|
|
|
+ List<User> userList = userService.list(
|
|
|
+ new MPJLambdaWrapper<User>()
|
|
|
+ .select(User::getId)
|
|
|
+ .select(User.class, x -> VoToColumnUtil.fieldsToColumns(User.class).contains(x.getProperty()))
|
|
|
+ .innerJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, User::getId)
|
|
|
+ .innerJoin(BaseStudent.class, BaseStudent::getUserId, User::getId)
|
|
|
+ .eq(BaseStudentSchoolRoll::getArchivesStatus, ArchivesStatusEnum.FB2901.getCode())
|
|
|
+ .eq(BaseStudent::getIsNormal, 1)
|
|
|
+ .in(BaseStudentSchoolRoll::getClassId, dto.getClassIds())
|
|
|
+ );
|
|
|
+ List<ExamSubjectScore> insertList = new ArrayList<>();
|
|
|
+ for (User importVo : userList) {
|
|
|
+ insertList.add(
|
|
|
+ new ExamSubjectScore(){{
|
|
|
+ setCreateDate(new Date());
|
|
|
+ setExamSubjectScoreEnterId(examSubjectScoreEnter.getId());
|
|
|
+ setUserId(importVo.getId());
|
|
|
+ setSemesterId(examSubjectScoreEnter.getSemesterId());
|
|
|
+ setCourseSubjectId(examSubjectScoreEnter.getCourseSubjectId());
|
|
|
+ setCoursename(examSubjectScoreEnter.getCoursename());
|
|
|
+ setExamPlanId(examSubjectScoreEnter.getExamPlanId());
|
|
|
+ }}
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!insertList.isEmpty()){
|
|
|
+ scoreService.remove(new QueryWrapper<ExamSubjectScore>().lambda().eq(ExamSubjectScore::getExamSubjectScoreEnterId, examSubjectScoreEnter.getId()));
|
|
|
+
|
|
|
+ scoreService.saveBatch(insertList);
|
|
|
+ }
|
|
|
+ return isSuccess;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<ExamSubjectScoreEnterPageVo> getPage(Page<ExamSubjectScoreEnterPageVo> page, ExamSubjectScoreEnterPageDto dto) {
|
|
|
+ return this.baseMapper.getPage(page, dto);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ExamSubjectScoreEnterVo getInfo(Long id) {
|
|
|
+ return this.baseMapper.getInfo(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public byte[] downloadTemplate(ExamSubjectScoreEnterDto dto) throws IOException {
|
|
|
+ ExamSubjectScoreEnter scoreEnter = this.getById(dto.getExamSubjectScoreEnterId());
|
|
|
+ BaseCourseSubject courseSubject = courseSubjectService.getById(scoreEnter.getCourseSubjectId());
|
|
|
+
|
|
|
+ List<ExamSubjectScoreEnterImportVo> result = scoreService.getEnterScoreStudentList(new ExamSubjectScoreEnterDto(){{
|
|
|
+ setExamSubjectScoreEnterId(dto.getExamSubjectScoreEnterId());
|
|
|
+ }});
|
|
|
+ List<ArrayList<String>> dataList = new ArrayList<>();
|
|
|
+ for (ExamSubjectScoreEnterImportVo recordVo : result) {
|
|
|
+ ArrayList<String> data = new ArrayList<>();
|
|
|
+ data.add(recordVo.getClassName());
|
|
|
+ data.add(recordVo.getStudentName());
|
|
|
+ data.add(recordVo.getCredentialNumber());
|
|
|
+ data.add(courseSubject.getName());
|
|
|
+ data.add(recordVo.getScore());
|
|
|
+ data.add(recordVo.getClassRanking()==null?"":recordVo.getClassRanking() + "");
|
|
|
+ data.add(recordVo.getGradeRanking()==null?"":recordVo.getGradeRanking() + "");
|
|
|
+ dataList.add(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ Workbook workbook = new XSSFWorkbook();
|
|
|
+ // 创建一个工作表(sheet)
|
|
|
+ String sheetName = "数据";
|
|
|
+ Sheet sheet = workbook.createSheet(sheetName);
|
|
|
+
|
|
|
+ createFirstTitle(workbook, sheet);
|
|
|
+ createSecondTitle(workbook, sheet);
|
|
|
+ createThirdTitle(workbook, sheet, ExamSubjectScoreEnterImportVo.class);
|
|
|
+
|
|
|
+ int dataRowNumber = 3;
|
|
|
+
|
|
|
+ for (ArrayList<String> rowData : dataList) {
|
|
|
+ Row dataRow = sheet.createRow(dataRowNumber);
|
|
|
+ for (int i = 0; i < rowData.size(); i ++){
|
|
|
+ String content = rowData.get(i);
|
|
|
+ Font font = workbook.createFont();
|
|
|
+ font.setBold(false);// 设置为粗体
|
|
|
+ font.setFontName("宋体");
|
|
|
+ font.setFontHeightInPoints((short)12);
|
|
|
+
|
|
|
+ CellStyle cellStyle = workbook.createCellStyle();
|
|
|
+ cellStyle.setFont(font); // 将字体应用到样式
|
|
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ Cell row1cell2 = dataRow.createCell(i);
|
|
|
+ row1cell2.setCellValue(content);
|
|
|
+ row1cell2.setCellStyle(cellStyle);
|
|
|
+ }
|
|
|
+
|
|
|
+ dataRowNumber ++;
|
|
|
+ }
|
|
|
+
|
|
|
+ ByteArrayOutputStream bot = new ByteArrayOutputStream();
|
|
|
+ workbook.write(bot);
|
|
|
+ return bot.toByteArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ void createFirstTitle(Workbook workbook, Sheet sheet) {
|
|
|
+ int rowNumber = 0;
|
|
|
+ Font font = workbook.createFont();
|
|
|
+ font.setBold(true);// 设置为粗体
|
|
|
+ font.setFontName("宋体");
|
|
|
+// font.setColor(IndexedColors.RED.getIndex()); // 设置字体颜色为红色
|
|
|
+ font.setFontHeightInPoints((short)24);
|
|
|
+
|
|
|
+ CellStyle cellStyle = workbook.createCellStyle();
|
|
|
+ cellStyle.setFont(font); // 将字体应用到样式
|
|
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+
|
|
|
+ Row row = sheet.createRow(rowNumber);
|
|
|
+ //合并第一行的列
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowNumber, 0, 6));
|
|
|
+
|
|
|
+ //3、处理表头
|
|
|
+ String title1 = "学生成绩导入模板";
|
|
|
+ // 创建单元格并设置值
|
|
|
+ Cell cell = row.createCell(0);
|
|
|
+ cell.setCellValue(title1);
|
|
|
+ cell.setCellStyle(cellStyle);
|
|
|
+ }
|
|
|
+
|
|
|
+ void createSecondTitle(Workbook workbook, Sheet sheet) {
|
|
|
+ int rowNumber = 1;
|
|
|
+ Font font = workbook.createFont();
|
|
|
+ font.setBold(true);// 设置为粗体
|
|
|
+ font.setFontName("宋体");
|
|
|
+ font.setColor(IndexedColors.RED.getIndex()); // 设置字体颜色为红色
|
|
|
+ font.setFontHeightInPoints((short)12);
|
|
|
+
|
|
|
+ CellStyle cellStyle = workbook.createCellStyle();
|
|
|
+ cellStyle.setFont(font); // 将字体应用到样式
|
|
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+
|
|
|
+ Row row = sheet.createRow(rowNumber);
|
|
|
+ //合并第一行的列
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowNumber, 0, 6));
|
|
|
+
|
|
|
+ //3、处理表头
|
|
|
+ String title1 = "说明:导入的学生信息必须保证与系统现有信息一致,导入时请将示例数据删除,避免导入失败!黄底红字为必填项,白底黑字为非必填项;";
|
|
|
+ // 创建单元格并设置值
|
|
|
+ Cell cell = row.createCell(0);
|
|
|
+ cell.setCellValue(title1);
|
|
|
+ cell.setCellStyle(cellStyle);
|
|
|
+ }
|
|
|
+
|
|
|
+ void createThirdTitle(Workbook workbook, Sheet sheet, Class<?> clazz) {
|
|
|
+ int rowNumber = 2;
|
|
|
+ Font font = workbook.createFont();
|
|
|
+ font.setFontName("宋体");
|
|
|
+ font.setFontHeightInPoints((short)12);
|
|
|
+
|
|
|
+ CellStyle cellStyle = workbook.createCellStyle();
|
|
|
+ cellStyle.setFont(font); // 将字体应用到样式
|
|
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+
|
|
|
+ Font font2 = workbook.createFont();
|
|
|
+ font2.setFontName("宋体");
|
|
|
+ font2.setFontHeightInPoints((short)12);
|
|
|
+ font2.setColor(IndexedColors.RED.getIndex()); // 设置字体颜色为红色
|
|
|
+ CellStyle cellStyle2 = workbook.createCellStyle();
|
|
|
+ cellStyle2.setFont(font2); // 将字体应用到样式
|
|
|
+ cellStyle2.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ cellStyle2.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ cellStyle2.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
|
|
|
+ cellStyle2.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置填充模式
|
|
|
+
|
|
|
+ Row row1 = sheet.createRow(rowNumber);
|
|
|
+ List<ImportConfig> importConfigs = ImportExcelUtil.getAllFieldCN(clazz);
|
|
|
+ List<ImportConfig> list = importConfigs.stream().filter(x -> x.getLabel() != null).collect(Collectors.toList());
|
|
|
+ for(int i = 0; i < list.size(); i ++){
|
|
|
+ sheet.setColumnWidth(i, 24 * 256);
|
|
|
+ ImportConfig importConfig = list.get(i);
|
|
|
+ Cell rowcell = row1.createCell(i);
|
|
|
+ rowcell.setCellValue(importConfig.getLabel());
|
|
|
+ if(importConfig.getRequired() != null && importConfig.getRequired()){
|
|
|
+ rowcell.setCellStyle(cellStyle2);
|
|
|
+ }else{
|
|
|
+ rowcell.setCellStyle(cellStyle);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|