فهرست منبع

修复重复名称bug,新增全局异常返回值处理

yangsj 1 سال پیش
والد
کامیت
080a45ce87

+ 7 - 1
fhKeeper/formulahousekeeper/management-workshop/pom.xml

@@ -117,6 +117,12 @@
             <groupId>com.baidu.aip</groupId>
             <artifactId>java-sdk</artifactId>
         </dependency>
+        <!-- hutool工具包 -->
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.8.16</version>
+        </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>
@@ -281,4 +287,4 @@
             </snapshots>
         </pluginRepository>
     </pluginRepositories>
-</project>
+</project>

+ 19 - 0
fhKeeper/formulahousekeeper/management-workshop/src/main/java/com/management/platform/config/exception/CustomServiceException.java

@@ -0,0 +1,19 @@
+package com.management.platform.config.exception;
+
+import lombok.Getter;
+
+
+/**
+ * 自定义业务异常
+ *
+ * @author 杨圣君
+ * @date 2023/08/24
+ */
+@Getter
+public class CustomServiceException extends RuntimeException {
+
+    public CustomServiceException(String msg) {
+        super(msg);
+    }
+
+}

+ 60 - 0
fhKeeper/formulahousekeeper/management-workshop/src/main/java/com/management/platform/config/exception/GlobalExceptionHandler.java

@@ -0,0 +1,60 @@
+package com.management.platform.config.exception;
+
+import com.management.platform.util.HttpRespMsg;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+/**
+ * 全局异常处理程序
+ *
+ * @author 杨圣君
+ * @date 2023/08/24
+ */
+@Slf4j
+@RestControllerAdvice
+public class GlobalExceptionHandler {
+    private static String CODE="error";
+
+    /**
+     * 返回错误结果
+     *
+     * @param code   错误码
+     * @param msg    返回信息
+     * @param result 返回参数对象
+     * @return 错误结果集
+     */
+    private HttpRespMsg error(String code, String msg, Object result) {
+        HttpRespMsg resp = new HttpRespMsg();
+        resp.setCode(code);
+        resp.setMsg(msg);
+        resp.setData(result);
+        return resp;
+    }
+
+
+    /**
+     * 其他异常统一处理
+     *
+     * @param e 异常类型
+     * @return 异常结果
+     */
+    @ExceptionHandler(Throwable.class)
+    public HttpRespMsg handleException(Throwable e) {
+        log.error(e.getMessage(), e);
+        return error(CODE, "服务器异常", "服务器异常");
+    }
+
+    /**
+     * 自定义业务异常处理
+     *
+     * @param e 异常类型
+     * @return 异常结果
+     */
+    @ExceptionHandler(CustomServiceException.class)
+    public HttpRespMsg CustomServiceException(Throwable e) {
+        log.error(e.getMessage(), e);
+        return error(CODE, e.getMessage(), "业务异常");
+    }
+
+}

+ 28 - 22
fhKeeper/formulahousekeeper/management-workshop/src/main/java/com/management/platform/service/impl/ProdCategoryServiceImpl.java

@@ -1,6 +1,9 @@
 package com.management.platform.service.impl;
 
+import cn.hutool.core.util.ObjectUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.management.platform.config.exception.CustomServiceException;
 import com.management.platform.entity.ProdCategory;
 import com.management.platform.entity.Product;
 import com.management.platform.entity.User;
@@ -8,17 +11,17 @@ import com.management.platform.mapper.ProdCategoryMapper;
 import com.management.platform.mapper.ProductMapper;
 import com.management.platform.mapper.UserMapper;
 import com.management.platform.service.ProdCategoryService;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.management.platform.util.HttpRespMsg;
 import io.micrometer.core.instrument.util.StringUtils;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 
 /**
  * <p>
- *  服务实现类
+ * 服务实现类
  * </p>
  *
  * @author Seyason
@@ -34,52 +37,55 @@ public class ProdCategoryServiceImpl extends ServiceImpl<ProdCategoryMapper, Pro
     private ProductMapper productMapper;
 
     @Override
-    public HttpRespMsg saveOrUpdateInfo(ProdCategory prodCategory, HttpServletRequest request) {
+    public HttpRespMsg saveOrUpdateInfo(ProdCategory prodCategory, HttpServletRequest request){
         HttpRespMsg msg = new HttpRespMsg();
 
         String token = request.getHeader("Token");
         User user = userMapper.selectById(token);
         prodCategory.setCompanyId(user.getCompanyId());
 
-        Integer count = prodCategoryMapper.selectCount(new LambdaQueryWrapper<ProdCategory>().eq(StringUtils.isNotBlank(prodCategory.getName()), ProdCategory::getName, prodCategory.getName()));
+        ProdCategory prodCategory1 = prodCategoryMapper
+                .selectOne(new LambdaQueryWrapper<ProdCategory>()
+                        .eq(StringUtils.isNotBlank(prodCategory.getName()), ProdCategory::getName, prodCategory.getName()));
 
-        if(count==0){
-            if(prodCategory.getId()==null){
-                prodCategoryMapper.insert(prodCategory);
-            }else{
-                prodCategoryMapper.updateById(prodCategory);
-            }
+        if (ObjectUtil.isNotNull(prodCategory1) && !prodCategory1.getId().equals(prodCategory.getId())) {
+            throw new CustomServiceException("分类已存在");
+        }
 
-            msg.setData(prodCategory);
-        }else{
-          msg.setError("name is repeat !");
+        if (prodCategory.getId() == null) {
+            prodCategoryMapper.insert(prodCategory);
+        } else {
+            prodCategoryMapper.updateById(prodCategory);
         }
 
+        msg.setData(prodCategory);
+
+
         return msg;
     }
 
     @Override
     public HttpRespMsg delete(Integer id, HttpServletRequest request) {
-        HttpRespMsg msg=new HttpRespMsg();
+        HttpRespMsg msg = new HttpRespMsg();
         String token = request.getHeader("Token");
         User user = userMapper.selectById(token);
 
         Integer count = productMapper.selectCount(
                 new LambdaQueryWrapper<Product>()
                         .eq(id != null, Product::getCategoryId, id)
-                .eq(user.getCompanyId()!=null,Product::getCompanyId,user.getCompanyId())
+                        .eq(user.getCompanyId() != null, Product::getCompanyId, user.getCompanyId())
         );
 
 
-        if(count==0){
+        if (count == 0) {
 
-             if(prodCategoryMapper.selectCount(new LambdaQueryWrapper<ProdCategory>().eq(user.getCompanyId()!=null,ProdCategory::getCompanyId,user.getCompanyId()))==1){
-                 msg.setError("产品分类数量不能低于1");
-             }else{
-                 prodCategoryMapper.deleteById(id);
-             }
+            if (prodCategoryMapper.selectCount(new LambdaQueryWrapper<ProdCategory>().eq(user.getCompanyId() != null, ProdCategory::getCompanyId, user.getCompanyId())) == 1) {
+                msg.setError("产品分类数量不能低于1");
+            } else {
+                prodCategoryMapper.deleteById(id);
+            }
 
-        }else{
+        } else {
             msg.setError("已有产品为此分类,无法删除");
         }