Преглед на файлове

fix:处理字段超长的数据库异常

caozixuan преди 2 години
родител
ревизия
6edc5ca29b

+ 28 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/ExamDetailServiceImpl.java

@@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.qmth.boot.api.exception.ApiException;
 import com.qmth.distributed.print.business.bean.dto.*;
 import com.qmth.distributed.print.business.bean.params.SerialNumberParams;
 import com.qmth.distributed.print.business.bean.result.ExaminationDetailResult;
@@ -30,6 +31,7 @@ import com.qmth.teachcloud.common.enums.*;
 import com.qmth.teachcloud.common.service.*;
 import com.qmth.teachcloud.common.util.ConvertUtil;
 import com.qmth.teachcloud.common.util.RedisUtil;
+import com.qmth.teachcloud.common.util.ResultUtil;
 import com.qmth.teachcloud.common.util.ServletUtil;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.ss.usermodel.HorizontalAlignment;
@@ -38,6 +40,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
 import org.springframework.context.annotation.Lazy;
+import org.springframework.dao.DataIntegrityViolationException;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -782,7 +785,31 @@ public class ExamDetailServiceImpl extends ServiceImpl<ExamDetailMapper, ExamDet
 //                    .eq(BasicClazz::getClazzName, examStudent.getClazzName())
 //                    .eq(BasicClazz::getSchoolId, examStudent.getSchoolId())).getId()));
 //        }
-        examStudentService.saveBatch(examStudentList);
+        try {
+            examStudentService.saveBatch(examStudentList);
+        } catch (Exception e) {
+            if (e instanceof DataIntegrityViolationException) {
+                String error = e.getCause().toString();
+                String tooLongColumn = error.substring(error.lastIndexOf("column") + 7);
+                tooLongColumn = tooLongColumn.substring(0, tooLongColumn.lastIndexOf("at") - 1);
+                tooLongColumn = tooLongColumn.replaceAll("'", "");
+                String columnName = "";
+                switch (tooLongColumn) {
+                    case "student_name":
+                        columnName = "考生名称";
+                        break;
+                    case "student_code":
+                        columnName = "考生编号";
+                        break;
+                }
+                String content = SystemConstant.strNotNull(columnName) ? columnName : tooLongColumn;
+                throw ExceptionResultEnum.SQL_ERROR.exception("[" + content + "]超过长度限制");
+            } else if (e instanceof ApiException) {
+                ResultUtil.error((ApiException) e, e.getMessage());
+            } else {
+                ResultUtil.error(e.getMessage());
+            }
+        }
     }
 
     @Override

+ 2 - 5
distributed-print-business/src/main/java/com/qmth/distributed/print/business/templete/service/impl/TaskLogicServiceImpl.java

@@ -1438,11 +1438,8 @@ public class TaskLogicServiceImpl implements TaskLogicService {
                         }
                     }
 
-                    if (Objects.isNull(studentName) || studentName.length() > 30) {
-                        errorStringJoiner.add("[姓名]最长30个字符");
-                    }
-                    if (Objects.isNull(studentCode) || studentCode.length() > 30 || !studentCode.matches(SystemConstant.REGULAR_EXPRESSION_OF_CODE)) {
-                        errorStringJoiner.add("[学号]最长30个字符");
+                    if (Objects.isNull(studentCode) || !studentCode.matches(SystemConstant.REGULAR_EXPRESSION_OF_CODE)) {
+                        errorStringJoiner.add("[学号]不符合规范");
                     }
                     if (Objects.nonNull(phoneNumber) && !phoneNumber.matches(SystemConstant.REGULAR_EXPRESSION_OF_PHONE)) {
                         errorStringJoiner.add("[手机号]不符合规范");

+ 53 - 2
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/BasicCourseServiceImpl.java

@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.google.common.collect.Lists;
+import com.qmth.boot.api.exception.ApiException;
 import com.qmth.teachcloud.common.base.BaseEntity;
 import com.qmth.teachcloud.common.bean.dto.BasicCourseExportDto;
 import com.qmth.teachcloud.common.bean.dto.DataPermissionRule;
@@ -23,9 +24,11 @@ import com.qmth.teachcloud.common.enums.RoleTypeEnum;
 import com.qmth.teachcloud.common.mapper.BasicCourseMapper;
 import com.qmth.teachcloud.common.service.*;
 import com.qmth.teachcloud.common.util.ExcelUtil;
+import com.qmth.teachcloud.common.util.ResultUtil;
 import com.qmth.teachcloud.common.util.ServletUtil;
 import com.qmth.teachcloud.common.util.excel.ExcelError;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.dao.DataIntegrityViolationException;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
@@ -171,7 +174,31 @@ public class BasicCourseServiceImpl extends ServiceImpl<BasicCourseMapper, Basic
         List<BasicCourseParams> basicCourseParamsList = new ArrayList<>();
         basicCourseParamsList.add(basicCourseParams);
         BasicCourse editBasicCourse = this.editEntityHelp(basicCourseParamsList, sysUser).get(0);
-        this.saveOrUpdate(editBasicCourse);
+        try {
+            this.saveOrUpdate(editBasicCourse);
+        } catch (Exception e) {
+            if (e instanceof DataIntegrityViolationException) {
+                String error = e.getCause().toString();
+                String tooLongColumn = error.substring(error.lastIndexOf("column") + 7);
+                tooLongColumn = tooLongColumn.substring(0, tooLongColumn.lastIndexOf("at") - 1);
+                tooLongColumn = tooLongColumn.replaceAll("'", "");
+                String columnName = "";
+                switch (tooLongColumn) {
+                    case "name":
+                        columnName = "基础课程名称";
+                        break;
+                    case "code":
+                        columnName = "基础课程编号";
+                        break;
+                }
+                String content = SystemConstant.strNotNull(columnName) ? columnName : tooLongColumn;
+                throw ExceptionResultEnum.SQL_ERROR.exception("[" + content + "]超过长度限制");
+            } else if (e instanceof ApiException) {
+                ResultUtil.error((ApiException) e, e.getMessage());
+            } else {
+                ResultUtil.error(e.getMessage());
+            }
+        }
         return editBasicCourse.getId();
     }
 
@@ -385,7 +412,31 @@ public class BasicCourseServiceImpl extends ServiceImpl<BasicCourseMapper, Basic
                 addBasicCourseParamsList.add(basicCourseParams);
             }
             List<BasicCourse> editBasicCourse = this.editEntityHelp(addBasicCourseParamsList, sysUser);
-            this.saveOrUpdateBatch(editBasicCourse);
+            try {
+                this.saveOrUpdateBatch(editBasicCourse);
+            } catch (Exception e) {
+                if (e instanceof DataIntegrityViolationException) {
+                    String error = e.getCause().toString();
+                    String tooLongColumn = error.substring(error.lastIndexOf("column") + 7);
+                    tooLongColumn = tooLongColumn.substring(0, tooLongColumn.lastIndexOf("at") - 1);
+                    tooLongColumn = tooLongColumn.replaceAll("'", "");
+                    String columnName = "";
+                    switch (tooLongColumn) {
+                        case "name":
+                            columnName = "基础课程名称";
+                            break;
+                        case "code":
+                            columnName = "基础课程编号";
+                            break;
+                    }
+                    String content = SystemConstant.strNotNull(columnName) ? columnName : tooLongColumn;
+                    throw ExceptionResultEnum.SQL_ERROR.exception("[" + content + "]超过长度限制");
+                } else if (e instanceof ApiException) {
+                    ResultUtil.error((ApiException) e, e.getMessage());
+                } else {
+                    ResultUtil.error(e.getMessage());
+                }
+            }
         }
         return addBasicCourseParamsList;
     }

+ 28 - 1
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/BasicStudentServiceImpl.java

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.qmth.boot.api.exception.ApiException;
 import com.qmth.teachcloud.common.base.BaseEntity;
 import com.qmth.teachcloud.common.bean.dto.BasicStudentExportDto;
 import com.qmth.teachcloud.common.bean.dto.DataPermissionRule;
@@ -20,8 +21,10 @@ import com.qmth.teachcloud.common.enums.TeachBasicEnum;
 import com.qmth.teachcloud.common.mapper.BasicStudentMapper;
 import com.qmth.teachcloud.common.service.*;
 import com.qmth.teachcloud.common.util.ExcelUtil;
+import com.qmth.teachcloud.common.util.ResultUtil;
 import com.qmth.teachcloud.common.util.ServletUtil;
 import org.springframework.beans.BeanUtils;
+import org.springframework.dao.DataIntegrityViolationException;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
@@ -238,7 +241,31 @@ public class BasicStudentServiceImpl extends ServiceImpl<BasicStudentMapper, Bas
         }
         if (!CollectionUtils.isEmpty(addBasicStudentParamsList)) {
             List<BasicStudent> basicStudentList = this.editEntityHelp(addBasicStudentParamsList, requestUser);
-            this.saveOrUpdateBatch(basicStudentList);
+            try {
+                this.saveOrUpdateBatch(basicStudentList);
+            } catch (Exception e) {
+                if (e instanceof DataIntegrityViolationException) {
+                    String error = e.getCause().toString();
+                    String tooLongColumn = error.substring(error.lastIndexOf("column") + 7);
+                    tooLongColumn = tooLongColumn.substring(0, tooLongColumn.lastIndexOf("at") - 1);
+                    tooLongColumn = tooLongColumn.replaceAll("'", "");
+                    String columnName = "";
+                    switch (tooLongColumn) {
+                        case "student_name":
+                            columnName = "基础学生姓名";
+                            break;
+                        case "student_code":
+                            columnName = "基础学生编号";
+                            break;
+                    }
+                    String content = SystemConstant.strNotNull(columnName) ? columnName : tooLongColumn;
+                    throw ExceptionResultEnum.SQL_ERROR.exception("[" + content + "]超过长度限制");
+                } else if (e instanceof ApiException) {
+                    ResultUtil.error((ApiException) e, e.getMessage());
+                } else {
+                    ResultUtil.error(e.getMessage());
+                }
+            }
         }
     }
 

+ 49 - 23
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/SysOrgServiceImpl.java

@@ -28,6 +28,7 @@ import com.qmth.teachcloud.common.util.ServletUtil;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
+import org.springframework.dao.DataIntegrityViolationException;
 import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -540,33 +541,58 @@ public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> impleme
     }
 
     @Override
-    public void executeImportSysOrgLogic(MultipartFile file) throws Exception {
-        InputStream inputStream = file.getInputStream();
-        List<LinkedMultiValueMap<Integer, Object>> finalList = ExcelUtil.excelReader(inputStream, Lists.newArrayList(SysOrgImportDto.class, DescribeImportDto.class), (finalExcelList, finalColumnNameList, finalExcelErrorList) -> finalExcelList);
-        List<String> orgInfoList = new ArrayList<>();
-        for (int i = 0; i < finalList.size(); i++) {
-            LinkedMultiValueMap<Integer, Object> excelMap = finalList.get(i);
-            List<Object> sysOrgImportDtoList = excelMap.get(i);
-            assert sysOrgImportDtoList != null;
-            if (sysOrgImportDtoList.get(0) instanceof DescribeImportDto) {
-                continue;
+    public void executeImportSysOrgLogic(MultipartFile file) {
+        try {
+            InputStream inputStream = file.getInputStream();
+            List<LinkedMultiValueMap<Integer, Object>> finalList = ExcelUtil.excelReader(inputStream, Lists.newArrayList(SysOrgImportDto.class, DescribeImportDto.class), (finalExcelList, finalColumnNameList, finalExcelErrorList) -> finalExcelList);
+            List<String> orgInfoList = new ArrayList<>();
+            for (int i = 0; i < finalList.size(); i++) {
+                LinkedMultiValueMap<Integer, Object> excelMap = finalList.get(i);
+                List<Object> sysOrgImportDtoList = excelMap.get(i);
+                assert sysOrgImportDtoList != null;
+                if (sysOrgImportDtoList.get(0) instanceof DescribeImportDto) {
+                    continue;
+                }
+                //  处理机构
+                if (sysOrgImportDtoList.get(0) instanceof SysOrgImportDto) {
+                    List<SysOrgImportDto> datasource = sysOrgImportDtoList.stream().map(e -> {
+                        SysOrgImportDto sysOrgImportDto = new SysOrgImportDto();
+                        BeanUtils.copyProperties(e, sysOrgImportDto);
+                        return sysOrgImportDto;
+                    }).collect(Collectors.toList());
+                    if (datasource.size() > 0) {
+                        datasource.forEach(e -> orgInfoList.add(e.getName()));
+                    }
+                }
+            }
+            if (orgInfoList.size() > 0) {
+                SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
+                this.createOrGetOrgByOrgInfo(orgInfoList, requestUser);
             }
-            //  处理机构
-            if (sysOrgImportDtoList.get(0) instanceof SysOrgImportDto) {
-                List<SysOrgImportDto> datasource = sysOrgImportDtoList.stream().map(e -> {
-                    SysOrgImportDto sysOrgImportDto = new SysOrgImportDto();
-                    BeanUtils.copyProperties(e, sysOrgImportDto);
-                    return sysOrgImportDto;
-                }).collect(Collectors.toList());
-                if (datasource.size() > 0) {
-                    datasource.forEach(e -> orgInfoList.add(e.getName()));
+        } catch (Exception e) {
+            log.error(SystemConstant.LOG_ERROR, e);
+            if (e instanceof DataIntegrityViolationException) {
+                String error = e.getCause().toString();
+                String tooLongColumn = error.substring(error.lastIndexOf("column") + 7);
+                tooLongColumn = tooLongColumn.substring(0, tooLongColumn.lastIndexOf("at") - 1);
+                tooLongColumn = tooLongColumn.replaceAll("'", "");
+                String columnName = "";
+                switch (tooLongColumn) {
+                    case "name":
+                        columnName = "机构名称";
+                        break;
+                    case "code":
+                        columnName = "机构编号";
+                        break;
                 }
+                String content = SystemConstant.strNotNull(columnName) ? columnName : tooLongColumn;
+                throw ExceptionResultEnum.SQL_ERROR.exception("[" + content + "]超过长度限制");
+            } else if (e instanceof ApiException) {
+                ResultUtil.error((ApiException) e, e.getMessage());
+            } else {
+                ResultUtil.error(e.getMessage());
             }
         }
-        if (orgInfoList.size() > 0) {
-            SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
-            this.createOrGetOrgByOrgInfo(orgInfoList, requestUser);
-        }
     }
 
     @Override

+ 20 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/SysUserServiceImpl.java

@@ -30,6 +30,7 @@ import com.qmth.teachcloud.common.util.ServletUtil;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
+import org.springframework.dao.DataIntegrityViolationException;
 import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -603,6 +604,25 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
                 String errorColumn = e.getCause().toString();
                 String columnStr = errorColumn.substring(errorColumn.lastIndexOf("key") + 3).replaceAll("'", "");
                 throw ExceptionResultEnum.SQL_ERROR.exception("[" + FieldUniqueEnum.convertToTitle(columnStr) + "]数据不允许重复插入");
+            } else if (e instanceof DataIntegrityViolationException) {
+                String error = e.getCause().toString();
+                String tooLongColumn = error.substring(error.lastIndexOf("column") + 7);
+                tooLongColumn = tooLongColumn.substring(0, tooLongColumn.lastIndexOf("at") - 1);
+                tooLongColumn = tooLongColumn.replaceAll("'", "");
+                String columnName = "";
+                switch (tooLongColumn) {
+                    case "login_name":
+                        columnName = "登录名";
+                        break;
+                    case "real_name":
+                        columnName = "真实姓名";
+                        break;
+                    case "code":
+                        columnName = "工号";
+                        break;
+                }
+                String content = SystemConstant.strNotNull(columnName) ? columnName : tooLongColumn;
+                throw ExceptionResultEnum.SQL_ERROR.exception("[" + content + "]超过长度限制");
             } else if (e instanceof ApiException) {
                 ResultUtil.error((ApiException) e, e.getMessage());
             } else {