Bläddra i källkod

fix: 添加返回异常code

fangxinjian 4 år sedan
förälder
incheckning
0fb29a0933

+ 37 - 32
abi-cloud-qr-platform-common/src/main/java/com/abi/task/common/excel/ExcelInputFactory.java

@@ -3,6 +3,7 @@ package com.abi.task.common.excel;
 import cn.hutool.poi.excel.ExcelReader;
 import cn.hutool.poi.excel.ExcelUtil;
 import com.abi.task.common.api.exception.BusinessException;
+import com.abi.task.common.api.exception.ErrorCodeEnum;
 import com.abi.task.common.excel.common.AbstractExcelProperty;
 import com.abi.task.common.excel.common.ExcelHeadAlias;
 import lombok.extern.slf4j.Slf4j;
@@ -20,6 +21,7 @@ import java.util.Map;
 
 /**
  * excel导出工厂类
+ *
  * @Author AndyTan
  * @Date 2021/2/24
  */
@@ -28,17 +30,18 @@ public class ExcelInputFactory {
 
     /**
      * 导入excel
+     *
      * @param file
      * @param t
      * @param <T>
      * @return
      */
-    public static <T extends AbstractExcelProperty> List<T> getExcelPojoList(MultipartFile file, Class<T> t){
+    public static <T extends AbstractExcelProperty> List<T> getExcelPojoList(MultipartFile file, Class<T> t) {
         //1-校验入参文件
         checkExcelFile(file);
 
         //2-file转map
-        List<Map<String,Object>> mapList= importExcel(file,t);
+        List<Map<String, Object>> mapList = importExcel(file, t);
 
         //3-map反射转pojoList
         List<T> pojoList = mapListToPojoList(mapList, t);
@@ -48,42 +51,45 @@ public class ExcelInputFactory {
 
     /**
      * 导出excel空模板
+     *
      * @param excelFileName
      * @param response
      * @param clz
      * @param <T>
      */
     public static <T extends AbstractExcelProperty> void downloadExcelModel(String excelFileName,
-            HttpServletResponse response,Class<T> clz){
+                                                                            HttpServletResponse response, Class<T> clz) {
         //调用导出方法
-        ExcelOutputFactory.outputStreamExcel(excelFileName,response,null,clz);
+        ExcelOutputFactory.outputStreamExcel(excelFileName, response, null, clz);
     }
 
 
     /**
      * 校验excel文件后缀
+     *
      * @param file
      */
-    private static void checkExcelFile(MultipartFile file){
+    private static void checkExcelFile(MultipartFile file) {
         if (file != null && file.getSize() > 0) {
             String fileName = file.getOriginalFilename();
             String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
-            if(!"xlsx".equals(suffix)&&!"xls".equals(suffix)){
-                throw new BusinessException("上传文件格式不正确");
+            if (!"xlsx".equals(suffix) && !"xls".equals(suffix)) {
+                throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "上传文件格式不正确");
             }
         } else {
-            throw new BusinessException("上传文件不存在");
+            throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "上传文件不存在");
         }
     }
 
     /**
      * 读取excel表格内容返回List<Map>
+     *
      * @param file
      * @param t
      * @param <T>
      * @return
      */
-    public static <T extends AbstractExcelProperty> List<Map<String,Object>> importExcel(MultipartFile file,Class<T> t){
+    public static <T extends AbstractExcelProperty> List<Map<String, Object>> importExcel(MultipartFile file, Class<T> t) {
         InputStream inputStream = null;
         try {
             inputStream = file.getInputStream();
@@ -92,7 +98,7 @@ public class ExcelInputFactory {
         }
 
         ExcelReader reader = ExcelUtil.getReader(inputStream);
-        List<Object> header=reader.readRow(0);
+        List<Object> header = reader.readRow(0);
 
         //字段数组
         //表头数组 e.g. "单位名称","单位类型"
@@ -101,7 +107,7 @@ public class ExcelInputFactory {
         List<String> headerAliaList = new ArrayList<>();
         //循环获取模板的字段和别名数组
         Field[] feilds = t.getDeclaredFields();
-        for(Field field:feilds){
+        for (Field field : feilds) {
             try {
                 String fieldName = field.getName();
                 ExcelHeadAlias headAlias = field.getAnnotation(ExcelHeadAlias.class);
@@ -109,39 +115,39 @@ public class ExcelInputFactory {
                 //放入列表
                 headList.add(fieldDesc);
                 headerAliaList.add(fieldName);
-            }catch (Exception e){
-                log.info("toMapList error",e);
+            } catch (Exception e) {
+                log.info("toMapList error", e);
             }
         }
 
         //替换表头关键字
-        if(header.size()==0){
-            throw new BusinessException("导入文件内容样式错误,请参考下载模板填写");
+        if (header.size() == 0) {
+            throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "导入文件内容样式错误,请参考下载模板填写");
         }
-        for(int i=0;i<headList.size();i++){
-            if(headList.get(i).equals(header.get(i))){
-                reader.addHeaderAlias(headList.get(i),headerAliaList.get(i));
-            }else{
-                throw new BusinessException("导入文件内容样式错误,请参考下载模板填写");
+        for (int i = 0; i < headList.size(); i++) {
+            if (headList.get(i).equals(header.get(i))) {
+                reader.addHeaderAlias(headList.get(i), headerAliaList.get(i));
+            } else {
+                throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "导入文件内容样式错误,请参考下载模板填写");
             }
         }
 
         //读取excel,获得map
         //1:表头所在行数  2:数据开始读取位置   Integer.MAX_VALUE:数据读取结束行位置
-        List<Map<String,Object>> excelContentMap = reader.read(0,0,Integer.MAX_VALUE);
+        List<Map<String, Object>> excelContentMap = reader.read(0, 0, Integer.MAX_VALUE);
 
         return excelContentMap;
     }
 
-    private static <T extends AbstractExcelProperty> List<T> mapListToPojoList(List<Map<String,Object>> mapList,Class<T> tClass){
+    private static <T extends AbstractExcelProperty> List<T> mapListToPojoList(List<Map<String, Object>> mapList, Class<T> tClass) {
         List<T> excelPojoList = new ArrayList<>();
-        for(Map<String,Object> mapTemp:mapList){
+        for (Map<String, Object> mapTemp : mapList) {
             try {
                 T t = tClass.newInstance();
                 //方法map缓存
-                Map<String,Method> methodCache = new HashMap<>();
+                Map<String, Method> methodCache = new HashMap<>();
                 //循环每一个字段,反射放入数据
-                for(Map.Entry<String, Object> entry:mapTemp.entrySet()){
+                for (Map.Entry<String, Object> entry : mapTemp.entrySet()) {
                     String fieldName = entry.getKey();
                     Object value = entry.getValue();
 
@@ -149,8 +155,8 @@ public class ExcelInputFactory {
                     if (!methodCache.containsKey(fieldName)) {
                         String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                         Method[] methods = tClass.getMethods();
-                        for(Method method:methods){
-                            if(methodName.equals(method.getName())){
+                        for (Method method : methods) {
+                            if (methodName.equals(method.getName())) {
                                 methodCache.put(fieldName, method);
                                 break;
                             }
@@ -158,14 +164,14 @@ public class ExcelInputFactory {
                         }
                     }
                     //调用method
-                    if(value!=null){
+                    if (value != null) {
                         Method method = methodCache.get(fieldName);
-                        method.invoke(t,String.valueOf(value));
+                        method.invoke(t, String.valueOf(value));
                     }
                 }
                 excelPojoList.add(t);
-            }catch(Exception e){
-                log.error("mapListToPojoList error",e);
+            } catch (Exception e) {
+                log.error("mapListToPojoList error", e);
             }
         }
 
@@ -173,5 +179,4 @@ public class ExcelInputFactory {
     }
 
 
-
 }

+ 47 - 37
abi-cloud-qr-platform-common/src/main/java/com/abi/task/common/tablestore/TableStorePlusUtils.java

@@ -3,6 +3,7 @@ package com.abi.task.common.tablestore;
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.date.DateUtil;
 import com.abi.task.common.api.exception.BusinessException;
+import com.abi.task.common.api.exception.ErrorCodeEnum;
 import com.abi.task.common.tablestore.common.TableStore;
 import com.abi.task.common.tablestore.common.TableStoreEntity;
 import com.abi.task.common.utils.IStringUtil;
@@ -18,7 +19,12 @@ import org.springframework.stereotype.Component;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.time.LocalDateTime;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * 进一步封装的tablestore工具类
@@ -32,19 +38,21 @@ public class TableStorePlusUtils {
 
     /**
      * 存储单条 重构方法
+     *
      * @param entity
      */
-    public void putRow(TableStoreEntity entity){
+    public void putRow(TableStoreEntity entity) {
         putRow(Arrays.asList(entity));
     }
 
     /**
      * 存储多行数据(如果主键key相同,则覆盖)
+     *
      * @param entityList
      */
-    public void putRow(List<TableStoreEntity> entityList){
-        if(CollectionUtil.isEmpty(entityList)){
-            throw new BusinessException("入参列表为空");
+    public void putRow(List<TableStoreEntity> entityList) {
+        if (CollectionUtil.isEmpty(entityList)) {
+            throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "入参列表为空");
         }
         try {
             String primaryKeyName = null;
@@ -52,18 +60,18 @@ public class TableStorePlusUtils {
             List<String> pkValueList = new ArrayList<>();
             List<List<Column>> columnsList = new ArrayList<>();
 
-            for(TableStoreEntity entity:entityList){
+            for (TableStoreEntity entity : entityList) {
                 List<Column> columns = new ArrayList<>();
                 //表名
                 TableStore tableStore = entity.getClass().getAnnotation(TableStore.class);
                 tableName = tableStore.tableName();
                 if (StringUtils.isBlank(tableName)) {
-                    throw new BusinessException("存储表名为空");
+                    throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "存储表名为空");
                 }
                 //主键
                 primaryKeyName = tableStore.primaryKeyName();
                 if (StringUtils.isBlank(primaryKeyName)) {
-                    throw new BusinessException("存储主键为空");
+                    throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "存储主键为空");
                 }
                 //主键的驼峰转下划线
                 primaryKeyName = IStringUtil.camelToUnderline(primaryKeyName);
@@ -77,7 +85,7 @@ public class TableStorePlusUtils {
                     String underlineFieldName = IStringUtil.camelToUnderline(fieldName);
                     //获取method
                     Method method = entity.getClass().getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));
-                    if (method == null){
+                    if (method == null) {
                         throw new BusinessException("get方法不存在");
                     }
                     //反射调用拿到结果
@@ -87,7 +95,7 @@ public class TableStorePlusUtils {
                         primaryKeyVal = (String) fieldValue;
                         continue;
                     }
-                    if(fieldValue==null){
+                    if (fieldValue == null) {
                         continue;
                     }
                     //放入table的column
@@ -118,29 +126,30 @@ public class TableStorePlusUtils {
             }
 
             //真实去调用
-            if(pkValueList.size()>1){
+            if (pkValueList.size() > 1) {
                 tableStoreUtils.batchPutRow(primaryKeyName, pkValueList, tableName, columnsList);
-            }else{
+            } else {
                 tableStoreUtils.putRow(primaryKeyName, pkValueList.get(0), tableName, columnsList.get(0));
             }
 
-        }catch (Exception e){
-            log.info("setRow error",e);
+        } catch (Exception e) {
+            log.info("setRow error", e);
             throw new BusinessException("存储table异常");
         }
     }
 
     /**
      * 读取一行数据
+     *
      * @param clz
      * @param primaryKeyVal
      * @param <T>
      * @return
      */
-    public <T extends TableStoreEntity> T getRow(Class<T> clz,String primaryKeyVal){
+    public <T extends TableStoreEntity> T getRow(Class<T> clz, String primaryKeyVal) {
         T entity = null;
 
-        try{
+        try {
             //表名
             TableStore tableStore = clz.getAnnotation(TableStore.class);
             String tableName = tableStore.tableName();
@@ -160,9 +169,9 @@ public class TableStorePlusUtils {
             //获取字段的值
             Column[] columns = row.getColumns();
             //转化为map
-            Map<String,Object> rowMap = new HashMap<>();
-            for(Column column:columns){
-                rowMap.put(column.getName(),column.getValue().getValue());
+            Map<String, Object> rowMap = new HashMap<>();
+            for (Column column : columns) {
+                rowMap.put(column.getName(), column.getValue().getValue());
             }
 
             //转为对象
@@ -178,27 +187,27 @@ public class TableStorePlusUtils {
                 //转化完成的值
                 Object fieldValue = null;
                 //获取对象的set方法
-                Method method = clz.getMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1),fieldType);
+                Method method = clz.getMethod("set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), fieldType);
                 //赛主键
                 if (underlineFieldName.equals(primaryKeyName)) {
                     fieldValue = primaryKeyVal;
-                }else{
+                } else {
                     //塞其他值
-                    if(columnVal!=null){
+                    if (columnVal != null) {
                         if (fieldType == String.class) {
-                            fieldValue = (String)columnVal;
+                            fieldValue = (String) columnVal;
                         } else if (fieldType == Integer.class) {
                             fieldValue = Integer.valueOf(String.valueOf(columnVal));
                         } else if (fieldType == Long.class) {
-                            fieldValue = (Long)columnVal;
+                            fieldValue = (Long) columnVal;
                         } else if (fieldType == Double.class) {
-                            fieldValue = (Double)columnVal;
+                            fieldValue = (Double) columnVal;
                         } else if (fieldType == Boolean.class) {
-                            fieldValue = (Boolean)columnVal;
+                            fieldValue = (Boolean) columnVal;
                         } else if (fieldType == Date.class) {
-                            fieldValue = DateUtil.parse((String)columnVal,"yyyy-MM-dd HH:mm:ss");
+                            fieldValue = DateUtil.parse((String) columnVal, "yyyy-MM-dd HH:mm:ss");
                         } else if (fieldType == LocalDateTime.class) {
-                            fieldValue = DateUtil.parseLocalDateTime((String)columnVal,"yyyy-MM-dd HH:mm:ss");
+                            fieldValue = DateUtil.parseLocalDateTime((String) columnVal, "yyyy-MM-dd HH:mm:ss");
                         } else {
                             log.info("不识别的字段类型 fieldName=" + fieldName);
                             continue;
@@ -207,11 +216,11 @@ public class TableStorePlusUtils {
                 }
 
                 //反射调用set
-                method.invoke(entity,fieldValue);
+                method.invoke(entity, fieldValue);
             }
 
-        }catch (Exception e){
-            log.info("获取table store异常",e);
+        } catch (Exception e) {
+            log.info("获取table store异常", e);
             throw new BusinessException("获取table store异常");
         }
 
@@ -220,31 +229,32 @@ public class TableStorePlusUtils {
 
     /**
      * 删除行数据
+     *
      * @param clz
      * @param primaryKeyVal
      * @param <T>
      */
-    public <T extends TableStoreEntity> void deleteRow(Class<T> clz,String primaryKeyVal){
+    public <T extends TableStoreEntity> void deleteRow(Class<T> clz, String primaryKeyVal) {
         try {
             //表名
             TableStore tableStore = clz.getAnnotation(TableStore.class);
             String tableName = tableStore.tableName();
             if (StringUtils.isBlank(tableName)) {
-                throw new BusinessException("存储表名为空");
+                throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "存储表名为空");
             }
             //主键
             String primaryKeyName = tableStore.primaryKeyName();
             if (StringUtils.isBlank(primaryKeyName)) {
-                throw new BusinessException("存储主键为空");
+                throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "存储主键为空");
             }
             //主键的驼峰转下划线
             primaryKeyName = IStringUtil.camelToUnderline(primaryKeyName);
 
             //调用删除
-            tableStoreUtils.deleteRow(primaryKeyName,primaryKeyVal,tableName);
+            tableStoreUtils.deleteRow(primaryKeyName, primaryKeyVal, tableName);
 
-        }catch (Exception e){
-            log.info("删除table store异常",e);
+        } catch (Exception e) {
+            log.info("删除table store异常", e);
             throw new BusinessException("删除table store异常");
         }
 

+ 11 - 8
abi-cloud-qr-platform-common/src/main/java/com/abi/task/common/utils/TableStoreUtils.java

@@ -2,6 +2,7 @@ package com.abi.task.common.utils;
 
 import cn.hutool.core.collection.CollectionUtil;
 import com.abi.task.common.api.exception.BusinessException;
+import com.abi.task.common.api.exception.ErrorCodeEnum;
 import com.alicloud.openservices.tablestore.SyncClient;
 import com.alicloud.openservices.tablestore.model.BatchWriteRowRequest;
 import com.alicloud.openservices.tablestore.model.BatchWriteRowResponse;
@@ -110,25 +111,26 @@ public class TableStoreUtils {
 
     /**
      * 批量插入
+     *
      * @param primaryKeyName
      * @param pkValueList
      * @param tableName
      * @param columnsList
      */
     public void batchPutRow(String primaryKeyName, List<String> pkValueList, String tableName, List<List<Column>> columnsList) {
-        if(CollectionUtil.isEmpty(pkValueList)){
-            throw new BusinessException("主键内容为空");
+        if (CollectionUtil.isEmpty(pkValueList)) {
+            throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "主键内容为空");
         }
-        if(CollectionUtil.isEmpty(columnsList)){
-            throw new BusinessException("字段列表为空");
+        if (CollectionUtil.isEmpty(columnsList)) {
+            throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "字段列表为空");
         }
-        if(pkValueList.size() != columnsList.size()){
-            throw new BusinessException("主键与字段数量不匹配");
+        if (pkValueList.size() != columnsList.size()) {
+            throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "主键与字段数量不匹配");
         }
 
         BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest();
         //构造rowPutChange
-        for(int i=0;i<pkValueList.size();i++){
+        for (int i = 0; i < pkValueList.size(); i++) {
             RowPutChange rowPutChange = buildRowPutChange(primaryKeyName, pkValueList.get(i), tableName, columnsList.get(i));
             //添加到batch操作中。
             batchWriteRowRequest.addRowChange(rowPutChange);
@@ -152,13 +154,14 @@ public class TableStoreUtils {
 
     /**
      * 构造RowPutChange
+     *
      * @param primaryKeyName
      * @param pkValue
      * @param tableName
      * @param columns
      * @return
      */
-    private RowPutChange buildRowPutChange(String primaryKeyName, String pkValue, String tableName, List<Column> columns){
+    private RowPutChange buildRowPutChange(String primaryKeyName, String pkValue, String tableName, List<Column> columns) {
         //构造主键。
         PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
         //设置主键名称和主键值

+ 2 - 1
abi-cloud-qr-platform-server/src/main/java/com/abi/qms/platform/service/impl/BoxCodeFormatImpl.java

@@ -16,6 +16,7 @@ import com.abi.qms.platform.infrastructure.util.AssertUtil;
 import com.abi.qms.platform.infrastructure.util.PageUtil;
 import com.abi.qms.platform.service.BoxCodeFormatService;
 import com.abi.task.common.api.exception.BusinessException;
+import com.abi.task.common.api.exception.ErrorCodeEnum;
 import com.abi.task.common.utils.PojoConverterUtils;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import lombok.extern.slf4j.Slf4j;
@@ -143,7 +144,7 @@ public class BoxCodeFormatImpl implements BoxCodeFormatService {
         //查询箱码幅面
         BoxCodeFormat boxCodeFormat = boxCodeFormatMapper.selectById(req.getId());
         if (boxCodeFormat == null) {
-            throw new BusinessException("箱码幅面不存在");
+            throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(),"箱码幅面不存在");
         }
 
         //逻辑删除

+ 2 - 1
abi-cloud-qr-platform-server/src/main/java/com/abi/qms/platform/service/impl/DepartmentServiceImpl.java

@@ -16,6 +16,7 @@ import com.abi.qms.platform.infrastructure.util.AssertUtil;
 import com.abi.qms.platform.infrastructure.util.PageUtil;
 import com.abi.qms.platform.service.DepartmentService;
 import com.abi.task.common.api.exception.BusinessException;
+import com.abi.task.common.api.exception.ErrorCodeEnum;
 import com.abi.task.common.excel.preperties.DepartmentProperty;
 import com.abi.task.common.utils.PojoConverterUtils;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -55,7 +56,7 @@ public class DepartmentServiceImpl implements DepartmentService {
         }
         List<BaseDepartment> checkDeptList = baseDepartmentMapper.selectList(deptQw);
         if (CollectionUtil.isNotEmpty(checkDeptList)) {
-            throw new BusinessException("部门ID已存在");
+            throw new BusinessException(ErrorCodeEnum.ERROR_PARAM.getCode(), "部门ID已存在");
         }
 
         //1-声明对象