package com.xjrsoft.module.student.service.impl; 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.xjrsoft.common.enums.DeleteMark; import com.xjrsoft.common.enums.GenderEnum; import com.xjrsoft.common.utils.VoToColumnUtil; import com.xjrsoft.module.base.entity.BaseSemester; import com.xjrsoft.module.base.service.IBaseSemesterService; import com.xjrsoft.module.organization.entity.User; import com.xjrsoft.module.organization.service.IUserService; import com.xjrsoft.module.student.dto.BaseStudentScholarshipApplicantCategoryPageDto; import com.xjrsoft.module.student.entity.BaseStudentScholarshipApplicant; import com.xjrsoft.module.student.entity.BaseStudentScholarshipCategory; import com.xjrsoft.module.student.entity.BaseStudentScholarshipRelease; import com.xjrsoft.module.student.mapper.BaseStudentMapper; import com.xjrsoft.module.student.mapper.BaseStudentScholarshipApplicantMapper; import com.xjrsoft.module.student.mapper.BaseStudentScholarshipReleaseMapper; import com.xjrsoft.module.student.service.IBaseStudentScholarshipApplicantService; import com.xjrsoft.module.student.service.IBaseStudentScholarshipCategoryService; import com.xjrsoft.module.student.vo.BaseStudentScholarshipApplicantCategoryPageVo; import com.xjrsoft.module.student.vo.BaseStudentScholarshipApplicantPageVo; import com.xjrsoft.module.student.vo.ScholarshipApplicantImportVo; import com.xjrsoft.module.student.vo.StudentInfoVo; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; /** * @title: 奖学金申请 * @Author dzx * @Date: 2023-11-23 * @Version 1.0 */ @Service @AllArgsConstructor public class BaseStudentScholarshipApplicantServiceImpl extends MPJBaseServiceImpl implements IBaseStudentScholarshipApplicantService { private final BaseStudentScholarshipApplicantMapper applicantMapper; private final IBaseSemesterService semesterService; private final IBaseStudentScholarshipCategoryService categoryService; private final IUserService userService; private final BaseStudentScholarshipApplicantMapper scholarshipApplicantMapper; private final BaseStudentMapper studentMapper; private final BaseStudentScholarshipReleaseMapper releaseMapper; @Override public Page getScholarshiPage(Page page, BaseStudentScholarshipApplicantCategoryPageDto dto) { Page result = applicantMapper.getScholarshiPage(page, dto); return result; } @Override public Boolean processDataHandler(Long dataId) { if (dataId == null) { return false; } // 原始数据 BaseStudentScholarshipApplicant scholarshipApplicant = scholarshipApplicantMapper.selectById(dataId); // 获取学生信息 StudentInfoVo studentInfo = studentMapper.getStudentInfo(scholarshipApplicant.getApplicantUserId()); scholarshipApplicant.setGradeName(studentInfo.getGradeName()); scholarshipApplicant.setClassName(studentInfo.getClassName()); scholarshipApplicant.setName(studentInfo.getStudentName()); scholarshipApplicant.setStudentId(studentInfo.getStudentId()); scholarshipApplicant.setGenderName(GenderEnum.getValue(studentInfo.getGender())); scholarshipApplicant.setEnrollTypeCn(studentInfo.getEnrollmentType()); scholarshipApplicant.setMajorName(studentInfo.getMajorName()); return updateById(scholarshipApplicant); } @Override public List> importData(MultipartFile file) throws IOException { List dataList = EasyExcel.read(file.getInputStream()).headRowNumber(3).head(ScholarshipApplicantImportVo.class).sheet().doReadSync(); List applicantList = this.list( new QueryWrapper().lambda() .select(BaseStudentScholarshipApplicant::getId) .select(BaseStudentScholarshipApplicant.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentScholarshipApplicantPageVo.class).contains(x.getProperty())) .eq(BaseStudentScholarshipApplicant::getStatus, 1) ); List userIds = applicantList.stream().map(BaseStudentScholarshipApplicant::getApplicantUserId).collect(Collectors.toList()); Map userMap = userService.list(new QueryWrapper().lambda().in(User::getId, userIds)).stream().collect(Collectors.toMap(x -> x.getId(), x -> x)); List semesterList = semesterService.list( new QueryWrapper().lambda() .select(BaseSemester::getId) .select(BaseSemester.class, x -> VoToColumnUtil.fieldsToColumns(BaseSemester.class).contains(x.getProperty())) .eq(BaseSemester::getDeleteMark, DeleteMark.NODELETE.getCode()) ); Map semesterMap = semesterList.stream().collect(Collectors.toMap(BaseSemester::getName, BaseSemester::getId)); List categoryList = categoryService.list( new QueryWrapper().lambda() .select(BaseStudentScholarshipCategory::getId) .select(BaseStudentScholarshipCategory.class, x -> VoToColumnUtil.fieldsToColumns(BaseStudentScholarshipCategory.class).contains(x.getProperty())) .eq(BaseStudentScholarshipCategory::getDeleteMark, DeleteMark.NODELETE.getCode()) ); Map categoryMap = categoryList.stream().collect(Collectors.toMap(BaseStudentScholarshipCategory::getName, x -> x)); Map categorySemeterMap = categoryList.stream().collect(Collectors.toMap(BaseStudentScholarshipCategory::getName, BaseStudentScholarshipCategory::getBaseSemesterId)); List> errorList = new ArrayList<>(); List updateList = new ArrayList<>(); for (ScholarshipApplicantImportVo importVo : dataList) { Set errorMsg = new HashSet<>(); if (StrUtil.isEmpty(importVo.getSemesterName()) || StrUtil.isEmpty(importVo.getName()) || StrUtil.isEmpty(importVo.getCredentialNumber()) || StrUtil.isEmpty(importVo.getScholarshipCategoryName()) || importVo.getScholarshipLevel() == null) { errorMsg.add("有未填写的列"); } if (semesterMap.get(importVo.getSemesterName()) == null) { errorMsg.add("学期填写不正确"); } Long semesterId = semesterMap.get(importVo.getSemesterName()); if (categoryMap.get(importVo.getScholarshipCategoryName()) == null) { errorMsg.add("奖学金名称填写不正确"); } BaseStudentScholarshipCategory category = categoryMap.get(importVo.getScholarshipCategoryName()); for (BaseStudentScholarshipApplicant applicant : applicantList) { User user = userMap.get(applicant.getApplicantUserId()); Long baseSemesterId = categorySemeterMap.get(importVo.getScholarshipCategoryName()); if (baseSemesterId.equals(semesterId) && importVo.getName().equals(user.getName()) && importVo.getCredentialNumber().equals(user.getCredentialNumber())) { applicant.setBaseStudentScholarshipCategoryId(category.getId()); applicant.setReviewStatus(1); applicant.setAmount(category.getTotalAmount()); applicant.setScholarshipLevel(importVo.getScholarshipLevel()); updateList.add(applicant); }else{ errorMsg.add("身份信息匹配不上"); } } if (!errorMsg.isEmpty()) { LinkedHashMap map = new LinkedHashMap<>(); map.put("学期", importVo.getSemesterName()); map.put("姓名", importVo.getName()); map.put("身份证号", importVo.getCredentialNumber()); map.put("奖学金名称", importVo.getScholarshipCategoryName()); map.put("获奖等级", importVo.getScholarshipLevel() + ""); map.put("错误信息", errorMsg.toString().replace("[", "").replace("]", "")); errorList.add(map); } } if (!updateList.isEmpty()) { this.updateBatchById(updateList); } return errorList; } /** * 每次发放时根据已经发放的金额判断这次申请的发放状态 * @param scholarshipApplicant * @return */ @Override @Transactional public Boolean updateReleaseStatus(BaseStudentScholarshipApplicant scholarshipApplicant) { List releaseList = releaseMapper.selectList( new QueryWrapper().lambda() .eq(BaseStudentScholarshipRelease::getDeleteMark, DeleteMark.NODELETE.getCode()) .eq(BaseStudentScholarshipRelease::getBaseStudentScholarshipApplicantId, scholarshipApplicant.getId()) ); if(releaseList.isEmpty()){ scholarshipApplicant.setReleaseStatus(0); } double sum = releaseList.stream().filter(x -> x.getAmount() != null).mapToDouble(BaseStudentScholarshipRelease::getAmount).sum(); if(sum == 0){ scholarshipApplicant.setReleaseStatus(0); }else if(sum < scholarshipApplicant.getAmount()){ scholarshipApplicant.setReleaseStatus(1); }else if(sum == scholarshipApplicant.getAmount()){ scholarshipApplicant.setReleaseStatus(2); } this.updateById(scholarshipApplicant); return true; } }