Explorar el Código

优化-11

(cherry picked from commit 83542054707288a1481d4516a847a605eb56e56d)
MOKASZ\lw12420 hace 2 años
padre
commit
c5a161d4f0

+ 88 - 0
launch-admin/src/main/java/com/webflux/launchadmin/global/BaseException.java

@@ -0,0 +1,88 @@
+package com.webflux.launchadmin.global;
+
+
+import org.springframework.util.StringUtils;
+
+/**
+ * 基础异常
+ * 
+ * @author launch
+ */
+public class BaseException extends RuntimeException
+{
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 所属模块
+     */
+    private String module;
+
+    /**
+     * 错误码
+     */
+    private String code;
+
+    /**
+     * 错误码对应的参数
+     */
+    private Object[] args;
+
+    /**
+     * 错误消息
+     */
+    private String defaultMessage;
+
+    public BaseException(String module, String code, Object[] args, String defaultMessage)
+    {
+        this.module = module;
+        this.code = code;
+        this.args = args;
+        this.defaultMessage = defaultMessage;
+
+    }
+
+    public BaseException(String module, String code, Object[] args)
+    {
+        this(module, code, args, null);
+    }
+
+    public BaseException(String module, String defaultMessage)
+    {
+        this(module, null, null, defaultMessage);
+    }
+
+    public BaseException(String code, Object[] args)
+    {
+        this(null, code, args, null);
+    }
+
+    public BaseException(String defaultMessage)
+    {
+        this(null, null, null, defaultMessage);
+    }
+
+    public String getModule()
+    {
+        return module;
+    }
+
+    public String getCode()
+    {
+        return code;
+    }
+
+    @Override
+    public String getMessage() {
+        return StringUtils.isEmpty(this.defaultMessage)?super.getMessage():this.defaultMessage;
+    }
+
+    public Object[] getArgs()
+    {
+        return args;
+    }
+
+    public String getDefaultMessage()
+    {
+        return defaultMessage;
+    }
+}

+ 4 - 2
launch-admin/src/main/java/com/webflux/launchadmin/global/GlobalExceptionHandler.java

@@ -1,12 +1,14 @@
 package com.webflux.launchadmin.global;
 
 import com.alibaba.cola.dto.Response;
+import com.webflux.launchcommon.returnObj.RStatus;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.ResponseStatus;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
+import reactor.core.publisher.Mono;
 
 @RestControllerAdvice
 @Slf4j
@@ -15,7 +17,7 @@ public class GlobalExceptionHandler {
     @ResponseStatus(HttpStatus.BAD_REQUEST)
     @ExceptionHandler(Exception.class)
     public Response handleException(Exception e) {
-      //  log.error(e.toString());
-        return Response.buildFailure("未知异常", e.getMessage());
+        log.error(e.toString());
+        return Response.buildFailure("异常", e.getMessage());
     }
 }

+ 18 - 7
launch-admin/src/main/java/com/webflux/launchadmin/mysql/controller/lbOut/HomePageOutController.java

@@ -1,5 +1,6 @@
 package com.webflux.launchadmin.mysql.controller.lbOut;
 
+import com.webflux.launchadmin.global.BaseException;
 import com.webflux.launchadmin.mysql.controller.lbIn.request.HomePageItemRequest;
 import com.webflux.launchadmin.mysql.controller.lbOut.request.HomePageItemOutRequest;
 import com.webflux.launchadmin.mysql.controller.lbOut.resp.DataList;
@@ -14,6 +15,7 @@ import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
 import org.springframework.data.relational.core.query.Criteria;
 import org.springframework.data.relational.core.query.Query;
 import org.springframework.web.bind.annotation.*;
+import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
 import java.util.*;
@@ -46,6 +48,14 @@ public class HomePageOutController {
         return RStatus.success(cc);
     }
 
+    @PostMapping("pageTempItem1")
+    public Mono<RStatus<List<HomePageTemp>>> pageTempItem1(@RequestBody HomePageItemOutRequest request)   {
+        String code = request.getCode();
+        Mono<List<HomePageTemp>> type_id = template.select(Query.query(Criteria.where("type_id").is(4)), HomePageTemp.class).collectList();
+        // Mono<Plan> code1 = template.selectOne(Query.query(Criteria.where("code").is(code)), Plan.class);
+        return RStatus.success(type_id);
+
+    }
     /**
      * H5 短剧查询
      * @param request
@@ -55,10 +65,11 @@ public class HomePageOutController {
     public Mono<RStatus<DataList<HomePageTempItem>>> pageTempItem(@RequestBody HomePageItemOutRequest request)   {
         String code = request.getCode();
         Mono<Plan> code1 = template.selectOne(Query.query(Criteria.where("code").is(code)), Plan.class);
-        code1.switchIfEmpty(Mono.error(new Exception("code is match failed")));
-        Mono<DataList<HomePageTempItem>> dataListMono = code1.flatMap(plan ->
+        code1.switchIfEmpty(Mono.error(new BaseException("code is match failed")));
+        Mono<DataList<HomePageTempItem>> dataListMono = code1.filter(f->Objects.nonNull(f.groupTypeIdSc())&& f.groupTypeIdSc()>0).flatMap(plan ->
                 template.select(Query.query(Criteria.where("type_id").is(plan.groupTypeIdSc())), HomePageTemp.class)
                         .collectList()
+                        .filter(f->!f.isEmpty())
                 .flatMap(fm -> Mono.just(selectRandomDataOne(fm)))
                 .flatMap(homePageTemp -> template.select(Query.query(Criteria.where("home_page_id").is(homePageTemp.homePageId())), HomePageTempItem.class)
                         .collectList()
@@ -91,10 +102,11 @@ public class HomePageOutController {
                             }
                         }
                     });
-                    return new DataList<HomePageTempItem>(bannerList, hotList, recommendList);
-                }).switchIfEmpty(Mono.error(new Exception(" match failed")))
-                ).switchIfEmpty(Mono.error(new Exception(" match failed"))))
-                .switchIfEmpty(Mono.error(new Exception(" code match failed")));
+                    return new DataList<HomePageTempItem>("成功",bannerList, hotList, recommendList);
+                }).switchIfEmpty(Mono.error(new BaseException(" 首页模版查询结果无关联子模版")))
+                ).switchIfEmpty(Mono.error(new BaseException(" 根据模版类型匹配结果为空"))))
+                .switchIfEmpty(Mono.error(new BaseException(" 通过code查询结果为空")))
+                .onErrorResume(throwable ->Mono.just(new DataList<HomePageTempItem>(throwable.getMessage(),List.of(), List.of(), List.of())) );
         return RStatus.successList(dataListMono);
     }
 
@@ -114,7 +126,6 @@ public class HomePageOutController {
     }
 
     public static <T> T selectRandomDataOne(List<T> dataList) {
-        List<T> selectedData = new ArrayList<>();
         Random random = new Random();
         int randomIndex = random.nextInt(dataList.size());
         T data = dataList.get(randomIndex);

+ 1 - 0
launch-admin/src/main/java/com/webflux/launchadmin/mysql/controller/lbOut/resp/DataList.java

@@ -3,6 +3,7 @@ package com.webflux.launchadmin.mysql.controller.lbOut.resp;
 import java.util.List;
 
 public record DataList<T>(
+        String messaeg,
         List<T> bannerlists,
         List<T> hotLists,
         List<T> recommendLists) {