|
@@ -0,0 +1,286 @@
|
|
|
+package cn.iocoder.yudao.module.as.service.aidingstudentsimportlist;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
+import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
+import cn.iocoder.yudao.module.as.controller.admin.aidingstudentsimportlist.vo.AidingStudentsImportExcelRespVO;
|
|
|
+import cn.iocoder.yudao.module.as.controller.admin.aidingstudentsimportlist.vo.AidingStudentsImportExcelVO;
|
|
|
+import cn.iocoder.yudao.module.as.controller.admin.aidingstudentsimportlist.vo.AidingStudentsImportListPageResqVO;
|
|
|
+import cn.iocoder.yudao.module.as.controller.admin.aidingstudentsimportlist.vo.AidingStudentsImportListSaveReqVO;
|
|
|
+import cn.iocoder.yudao.module.as.dal.dataobject.aidingstudentsimportlist.AidingStudentsImportExcelListDO;
|
|
|
+import cn.iocoder.yudao.module.as.dal.mysql.aidingstudentsimportlist.AidingStudentsImportListMapper;
|
|
|
+import cn.iocoder.yudao.module.as.dal.redis.aidingstudentsimportlist.AidingStudentsImportListRedisDAO;
|
|
|
+
|
|
|
+import cn.iocoder.yudao.module.system.api.dept.DeptApi;
|
|
|
+import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
|
|
+import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
|
|
|
+import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
|
|
+import cn.iocoder.yudao.module.system.dal.mysql.dept.DeptMapper;
|
|
|
+import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.validation.ValidationException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+
|
|
|
+import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static cn.iocoder.yudao.module.as.enums.ErrorCodeConstants.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Validated
|
|
|
+@Slf4j
|
|
|
+public class AidingStudentsImportListServiceImpl implements AidingStudentsImportListService {
|
|
|
+ @Resource
|
|
|
+ private AidingStudentsImportListMapper aidingStudentsImportListMapper;
|
|
|
+ @Resource
|
|
|
+ private AidingStudentsImportListRedisDAO aidingStudentsImportListRedisDAO;
|
|
|
+ @Resource
|
|
|
+ private AdminUserMapper userMapper;
|
|
|
+ @Resource
|
|
|
+ private DeptApi deptApi;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Long createAidingStudentsImportList(AidingStudentsImportListSaveReqVO createReqVO, Long aidingStudentsManageId) {
|
|
|
+ // 转换为DO
|
|
|
+ AidingStudentsImportExcelListDO aidingStudentsImportListDO = BeanUtils.toBean(createReqVO, AidingStudentsImportExcelListDO.class);
|
|
|
+ //set项目id和学院id
|
|
|
+ aidingStudentsImportListDO.setAsAidingStudentsManageId(aidingStudentsManageId);
|
|
|
+ aidingStudentsImportListDO.setCollegeId(getCollegeIdByStudentCode(aidingStudentsImportListDO.getStudentCode()));
|
|
|
+
|
|
|
+ if (checkStudentCodeExists(aidingStudentsImportListDO.getStudentCode(),aidingStudentsManageId)) {
|
|
|
+ throw exception(AIDING_STUDENTS_IMPORT_LIST_STUDENT_EXISTS);
|
|
|
+ }
|
|
|
+ aidingStudentsImportListMapper.insert(aidingStudentsImportListDO);
|
|
|
+ // 返回
|
|
|
+ return aidingStudentsImportListDO.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateAidingStudentsImportList(AidingStudentsImportListSaveReqVO updateReqVO) {
|
|
|
+ // 校验存在
|
|
|
+ validateAidingStudentsImportListExists(updateReqVO.getId());
|
|
|
+ // 更新
|
|
|
+ AidingStudentsImportExcelListDO updateObj = BeanUtils.toBean(updateReqVO, AidingStudentsImportExcelListDO.class);
|
|
|
+ aidingStudentsImportListMapper.updateById(updateObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteAidingStudentsImportList(Long id) {
|
|
|
+ // 校验存在
|
|
|
+ validateAidingStudentsImportListExists(id);
|
|
|
+ // 删除
|
|
|
+ aidingStudentsImportListMapper.deleteById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 根据id校验是否存在
|
|
|
+ private void validateAidingStudentsImportListExists(Long id) {
|
|
|
+ if (aidingStudentsImportListMapper.selectById(id) == null) {
|
|
|
+ throw exception(AIDING_STUDENTS_IMPORT_LIST_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据学号校验这次项目该学生是否已经导入
|
|
|
+ private boolean checkStudentCodeExists(String studentCode, Long asAidingStudentsManageId) {
|
|
|
+ //存在返回true,不存在返回false
|
|
|
+ return aidingStudentsImportListMapper.selectCount(
|
|
|
+ Wrappers.lambdaQuery(AidingStudentsImportExcelListDO.class)
|
|
|
+ .eq(AidingStudentsImportExcelListDO::getStudentCode, studentCode)
|
|
|
+ .eq(AidingStudentsImportExcelListDO::getAsAidingStudentsManageId, asAidingStudentsManageId)
|
|
|
+ ) > 0;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public AidingStudentsImportExcelListDO getAidingStudentsImportList(Long id) {
|
|
|
+ return aidingStudentsImportListMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageResult<AidingStudentsImportExcelListDO> getAidingStudentsImportListPage(AidingStudentsImportListPageResqVO pageReqVO) {
|
|
|
+ return aidingStudentsImportListMapper.selectPage(pageReqVO);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //校验excel导入的内容
|
|
|
+ public String validateImportListForCreateOrUpdate(String studentName, String studentIdNumber, String studentCode, int studentAge, int studentSex) throws ValidationException {
|
|
|
+ String message = null;
|
|
|
+ // 验证学生姓名
|
|
|
+ if (studentName == null || studentName.isEmpty()) {
|
|
|
+ message="姓名不能为空";
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证学生学号
|
|
|
+ if (studentCode == null || studentCode.isEmpty()) {
|
|
|
+ message="学号不能为空";
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证学生性别
|
|
|
+ if (studentSex != 1 && studentSex != 2 && studentSex != 3) {
|
|
|
+ message="性别必须为 1(男)、2(女)或 3(未知)";
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证学生年龄
|
|
|
+ if (studentAge <= 0 || studentAge > 150) {
|
|
|
+ message="年龄必须大于0且不能超过150岁";
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证学生身份证号
|
|
|
+ if (studentIdNumber == null || studentIdNumber.length() != 18) {
|
|
|
+ message="身份证号长度必须为18位";
|
|
|
+
|
|
|
+ }
|
|
|
+ return message;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+//导入到数据库当中
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入
|
|
|
+ public AidingStudentsImportExcelRespVO aidingStudentsImportExcelList( Long asAidingStudentsManageId,String redisUUID) {
|
|
|
+ List<AidingStudentsImportExcelListDO> list = aidingStudentsImportListRedisDAO.getAllMatchingValues(redisUUID);
|
|
|
+ if (CollUtil.isEmpty(list)) {
|
|
|
+ throw exception(AIDING_STUDENTS_IMPORT_LIST_IS_NULL);
|
|
|
+ }
|
|
|
+ AidingStudentsImportExcelRespVO respVO = AidingStudentsImportExcelRespVO.builder().createStudentNames(new ArrayList<>())
|
|
|
+ .failureStudentNames(new LinkedHashMap<>()).build();
|
|
|
+ List<AidingStudentsImportExcelListDO> aidingStudentsImportEecleListDOS = BeanUtils.toBean(list, AidingStudentsImportExcelListDO.class);
|
|
|
+ aidingStudentsImportEecleListDOS.forEach(aidingStudentsImportEecleListDO -> {
|
|
|
+
|
|
|
+ // 校验,判断是否有不符合的原因
|
|
|
+
|
|
|
+ String result = validateImportListForCreateOrUpdate(aidingStudentsImportEecleListDO.getStudentName(), aidingStudentsImportEecleListDO.getStudentIdNumber(),
|
|
|
+ aidingStudentsImportEecleListDO.getStudentCode(), aidingStudentsImportEecleListDO.getStudentAge(), aidingStudentsImportEecleListDO.getStudentSex());
|
|
|
+ if (result != null){
|
|
|
+ respVO.getFailureStudentNames().put(aidingStudentsImportEecleListDO.getStudentName(), result);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //赋值给外键
|
|
|
+ aidingStudentsImportEecleListDO.setAsAidingStudentsManageId(asAidingStudentsManageId);
|
|
|
+ aidingStudentsImportEecleListDO.setCollegeId(getCollegeIdByStudentCode(aidingStudentsImportEecleListDO.getStudentCode()));
|
|
|
+
|
|
|
+ if (checkStudentCodeExists(aidingStudentsImportEecleListDO.getStudentCode(),asAidingStudentsManageId)) {
|
|
|
+ respVO.getFailureStudentNames().put(aidingStudentsImportEecleListDO.getStudentName(), "该学生已导入过一次");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ aidingStudentsImportEecleListDO.setAsAidingStudentsManageId(asAidingStudentsManageId);
|
|
|
+ aidingStudentsImportListMapper.insert(aidingStudentsImportEecleListDO);
|
|
|
+ respVO.getCreateStudentNames().add(aidingStudentsImportEecleListDO.getStudentName());
|
|
|
+ });
|
|
|
+ return respVO;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //导入到redis中
|
|
|
+ @Override
|
|
|
+ public String aidingStudentsImportExcelToRedis(List<AidingStudentsImportExcelVO> list) {
|
|
|
+ if (CollUtil.isEmpty(list)) {
|
|
|
+ throw exception(AIDING_STUDENTS_IMPORT_LIST_IS_NULL);
|
|
|
+ }
|
|
|
+ String redisUUID = String.valueOf(UUID.randomUUID());
|
|
|
+ try {
|
|
|
+ List<AidingStudentsImportExcelListDO> aidingStudentsImportEecleListDOS = BeanUtils.toBean(list, AidingStudentsImportExcelListDO.class);
|
|
|
+ aidingStudentsImportEecleListDOS.forEach(aidingStudentsImportEecleListDO -> {
|
|
|
+ aidingStudentsImportListRedisDAO.setWithredisUUID(redisUUID,aidingStudentsImportEecleListDO);
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("导入redis失败", e);
|
|
|
+ throw new RuntimeException(e);
|
|
|
+
|
|
|
+ }
|
|
|
+ return redisUUID;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //重新导入
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void aidingStudentsImportExcelReImport(List<AidingStudentsImportExcelVO> list, Long aidingStudentsManageId, Integer strategyId) {
|
|
|
+ if (CollUtil.isEmpty(list)) {
|
|
|
+ throw exception(AIDING_STUDENTS_IMPORT_LIST_IS_NULL);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ List<AidingStudentsImportExcelListDO> aidingStudentsImportEecleListDOS = BeanUtils.toBean(list, AidingStudentsImportExcelListDO.class);
|
|
|
+ aidingStudentsImportEecleListDOS.forEach(aidingStudentsImportEecleListDO -> {
|
|
|
+ // 校验,判断是否有不符合的原因
|
|
|
+
|
|
|
+ String result = validateImportListForCreateOrUpdate(aidingStudentsImportEecleListDO.getStudentName(), aidingStudentsImportEecleListDO.getStudentIdNumber(),
|
|
|
+ aidingStudentsImportEecleListDO.getStudentCode(), aidingStudentsImportEecleListDO.getStudentAge(), aidingStudentsImportEecleListDO.getStudentSex());
|
|
|
+ if (result != null){
|
|
|
+ throw new ValidationException(aidingStudentsImportEecleListDO.getStudentName()+result);
|
|
|
+ }
|
|
|
+ // 赋值给外键
|
|
|
+ aidingStudentsImportEecleListDO.setAsAidingStudentsManageId(aidingStudentsManageId);
|
|
|
+ aidingStudentsImportEecleListDO.setCollegeId(getCollegeIdByStudentCode(aidingStudentsImportEecleListDO.getStudentCode()));
|
|
|
+
|
|
|
+ //数据库不存在,直接插入
|
|
|
+ if(!checkStudentCodeExists(aidingStudentsImportEecleListDO.getStudentCode(),aidingStudentsManageId)){
|
|
|
+ aidingStudentsImportListMapper.insert(aidingStudentsImportEecleListDO);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 数据库存在,判断策略,1表示覆盖
|
|
|
+ if(strategyId == 1){
|
|
|
+ aidingStudentsImportListMapper.update(aidingStudentsImportEecleListDO,
|
|
|
+ Wrappers.lambdaQuery(AidingStudentsImportExcelListDO.class)
|
|
|
+ .eq(AidingStudentsImportExcelListDO::getStudentCode, aidingStudentsImportEecleListDO.getStudentCode())
|
|
|
+ .eq(AidingStudentsImportExcelListDO::getAsAidingStudentsManageId, aidingStudentsManageId)
|
|
|
+ );
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ //数据库存在,判断策略,2表示不改变
|
|
|
+ if (strategyId == 2){
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ throw new ValidationException("导入失败");
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("导入失败", e);
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据学号查学院id
|
|
|
+ public Long getCollegeIdByStudentCode(String studentCode){
|
|
|
+ AdminUserDO adminUserDO = userMapper.selectByUsername(studentCode);
|
|
|
+ if (adminUserDO == null){
|
|
|
+ throw exception(AIDING_STUDENTS_IMPORT_LIST_STUDENT_CODE_NOT_EXISTS);
|
|
|
+ }
|
|
|
+
|
|
|
+ return deptApi.getCollegeId(adminUserDO.getDeptId());
|
|
|
+
|
|
|
+ //循环查询父级部门
|
|
|
+ /*DeptDO deptDO = new DeptDO();
|
|
|
+ Long parentId = deptMapper.selectById(adminUserDO.getDeptId()).getParentId();
|
|
|
+ while (parentId != 0) {
|
|
|
+ deptDO = deptMapper.selectById(parentId);
|
|
|
+ parentId= deptDO.getParentId();
|
|
|
+ }
|
|
|
+ return deptDO.getId();*/
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|