소스 검색

修改异步处理同步场景接口调用

xiewenjie 3 년 전
부모
커밋
4df8dcff77

+ 131 - 0
sxz-core/src/main/java/com/fdkk/sxz/other/listener/SnycBuild.java

@@ -0,0 +1,131 @@
+package com.fdkk.sxz.other.listener;
+
+
+import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.fastjson.JSONObject;
+import com.fdkk.sxz.entity.RenovationPartsDetailEntity;
+import com.fdkk.sxz.entity.custuom.CustomComponentEntity;
+import com.fdkk.sxz.entity.custuom.CustomProductEntity;
+import com.fdkk.sxz.other.mq.TopicRabbitConfig;
+import com.fdkk.sxz.util.OkHttpUtils;
+import com.fdkk.sxz.webApi.service.IRenovationPartsDetailService;
+import com.fdkk.sxz.webApi.service.custom.ICustomComponentService;
+import com.fdkk.sxz.webApi.service.custom.ICustomProductService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.annotation.RabbitHandler;
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+
+/**
+ * @author Xiewj
+ * @date 2021/11/12
+ */
+
+@Component
+@Slf4j
+public class SnycBuild {
+
+    @Value("${build.url}")
+    private String buildUrl;
+
+    @Autowired
+    private IRenovationPartsDetailService renovationPartsDetailService;
+    @Autowired
+    private ICustomProductService customProductService;
+
+    @Autowired
+    private ICustomComponentService customComponentService;
+
+
+    @RabbitHandler
+    @RabbitListener(queues = TopicRabbitConfig.SYNCMODEL)
+    public void syncModel(String str) {
+        SnycBuild.log.info("模型syncmodel:" + str + ",开始同步");
+        syncModelHandler(str);
+    }
+
+    public void syncModelHandler(String str) {
+        String[] datas = str.split(":;");
+        String id = datas[0].trim();
+        String type = datas[1].trim();
+        String json = datas[2].trim();
+        RenovationPartsDetailEntity renovationPartsDetailEntity = null;
+        CustomProductEntity customProductEntity = null;
+        CustomComponentEntity componentEntity = null;
+        try {
+            SnycBuild.log.info("更新模型库,syncmodel开始:" + json);
+            String resultData = OkHttpUtils.httpPostJson(buildUrl + "syncmodel", json);
+            JSONObject resultJson = JSONObject.parseObject(resultData);
+            SnycBuild.log.info("更新模型库,syncmodel结束:" + resultData.toString());
+            if ("error".equals(resultJson.getString("state"))) {
+                SnycBuild.log.info("更新模型库,syncmodel接口失败:" + resultData);
+                throw new RuntimeException(id + ":更新模型库,syncmodel接口失败");
+            }
+            switch (type) {
+                case "1":
+                    //普通
+                    renovationPartsDetailEntity = renovationPartsDetailService.findById(id);
+                    if (ObjectUtil.isNotNull(renovationPartsDetailEntity)) {
+                        renovationPartsDetailEntity.setExamine(1);
+                        renovationPartsDetailEntity.setExamineTime(new Date());
+                        renovationPartsDetailService.updateById(renovationPartsDetailEntity);
+                    }
+                    break;
+                case "2":
+                    //定制
+                    customProductEntity = customProductService.findById(id);
+                    if (ObjectUtil.isNotNull(customProductEntity)) {
+                        customProductEntity.setExamine(1);
+                        customProductEntity.setExamineTime(new Date());
+                        customProductService.updateById(customProductEntity);
+                    }
+                    break;
+                case "3":
+                    //组件
+                    componentEntity = customComponentService.findById(id);
+                    if (ObjectUtil.isNotNull(componentEntity)) {
+                        componentEntity.setExamine(1);
+                        componentEntity.setExamineTime(new Date());
+                        customComponentService.updateById(componentEntity);
+                    }
+                    break;
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            SnycBuild.log.error("同步模型报错-{}", e.getMessage());
+            switch (type) {
+                case "1":
+                    //普通
+                    renovationPartsDetailEntity = renovationPartsDetailService.findById(id);
+                    if (ObjectUtil.isNotNull(renovationPartsDetailEntity)) {
+                        renovationPartsDetailEntity.setExamine(-1);
+                        renovationPartsDetailEntity.setExamineTime(new Date());
+                        renovationPartsDetailService.updateById(renovationPartsDetailEntity);
+                    }
+                    break;
+                case "2":
+                    //定制
+                    customProductEntity = customProductService.findById(id);
+                    if (ObjectUtil.isNotNull(customProductEntity)) {
+                        customProductEntity.setExamine(-1);
+                        customProductEntity.setExamineTime(new Date());
+                        customProductService.updateById(customProductEntity);
+                    }
+                    break;
+                case "3":
+                    //组件
+                    componentEntity = customComponentService.findById(id);
+                    if (ObjectUtil.isNotNull(componentEntity)) {
+                        componentEntity.setExamine(-1);
+                        componentEntity.setExamineTime(new Date());
+                        customComponentService.updateById(componentEntity);
+                    }
+                    break;
+            }
+        }
+    }
+}

+ 19 - 6
sxz-core/src/main/java/com/fdkk/sxz/other/mq/TopicRabbitConfig.java

@@ -23,20 +23,23 @@ public class TopicRabbitConfig {
 //
 //    public final static String TOPICE = "topicExchangeCopy";
 
-    public final static String CHANGE = "topic.change.dev";
+    public final static String CHANGE = "topic.chang";
     //灯光预览
-    public final static String LIGHT = "topic.light.dev";
+    public final static String LIGHT = "topic.light";
 
-    public final static String MODEL = "topic.model.dev";
+    public final static String MODEL = "topic.model";
+
+    //"更新模型库,syncmodel
+    public final static String SYNCMODEL = "topic.syncmodel";
 
     //处理
-    public final static String MVIEW = "delay.mview.dev";
+    public final static String MVIEW = "delay.mview";
 
-    public final static String TOPICE = "topicExchange.dev";
+    public final static String TOPICE = "topicExchange";
     /**
      * 延迟消息交换机
      */
-    public final static String DELAY_EXCHANGE = "delay.exchange.dev";
+    public final static String DELAY_EXCHANGE = "delay.exchange";
 
     @Bean
     public CustomExchange delayMessageExchange() {
@@ -62,6 +65,11 @@ public class TopicRabbitConfig {
     }
 
     @Bean
+    public Queue syncModelQueue() {
+        return new Queue(TopicRabbitConfig.SYNCMODEL);
+    }
+
+    @Bean
     public Queue mviewQueue() {
         return new Queue(TopicRabbitConfig.MVIEW);
     }
@@ -95,4 +103,9 @@ public class TopicRabbitConfig {
         return BindingBuilder.bind(mviewQueue()).to(delayMessageExchange()).with(TopicRabbitConfig.MVIEW).noargs();
     }
 
+    @Bean
+    Binding bindingExchangeMessage5() {
+        return BindingBuilder.bind(syncModelQueue()).to(exchange()).with(TopicRabbitConfig.SYNCMODEL);
+    }
+
 }

+ 8 - 11
sxz-core/src/main/java/com/fdkk/sxz/webApi/controller/ManagerController.java

@@ -38,7 +38,10 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.servlet.http.HttpServletRequest;
 import java.io.File;
 import java.io.IOException;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * Created by Hb_zzZ on 2020/12/1.
@@ -765,8 +768,6 @@ public class ManagerController extends BaseController {
         renovationPartsDetailEntity.setId(param.getId());
 
         if (param.getExamine().intValue() == 1) {
-            renovationPartsDetailEntity.setExamine(param.getExamine());
-            renovationPartsDetailEntity.setExamineTime(new Date());
             ModelUploadEntity modelUploadEntity = modelUploadService.findByPartsDetailId(param.getId());
             if (ObjectUtil.isNotNull(modelUploadEntity)) {
                 //入库前先提交模型到模型库,失败返回提示
@@ -785,17 +786,13 @@ public class ManagerController extends BaseController {
                 array.add(object);
                 jsonObject.put("version", version);
 
+
+                //  TODO 使用队列操作
                 ManagerController.log.info("更新模型库,syncmodel接口参数:" + jsonObject.toJSONString());
                 uploadToOssUtil.uploadTo4dTjw(array.toJSONString().getBytes(), "domain/eHome/furniture/syncdata/" +
                         version + "/SyncData.json");
-
-                String resultData = OkHttpUtils.httpPostJson(buildUrl + "syncmodel", jsonObject.toJSONString());
-                JSONObject resultJson = JSONObject.parseObject(resultData);
-                if ("error".equals(resultJson.getString("state"))) {
-                    ManagerController.log.info("更新模型库,syncmodel接口失败:" + resultData);
-                    renovationPartsDetailEntity.setExamine(0);
-                    return Result.failure(CodeConstant.FAILURE_CODE_4015, CodeConstant.FAILURE_MSG_4015);
-                }
+                String message = param.getId() + ":;" + "1" + ":;" + jsonObject.toString();
+                rabbitTemplate.convertAndSend(TopicRabbitConfig.TOPICE, TopicRabbitConfig.SYNCMODEL, message);
 
                 uploadToOssUtil.copyTo4dTjw(renovationPartsDetailEntity.getImg().replace(prefix, ""),
                         renovationPartsDetailEntity.getImg().replace("/temp/", "/models/").replace(prefix, ""));

+ 17 - 13
sxz-core/src/main/java/com/fdkk/sxz/webApi/service/custom/impl/CustomComponentServiceImpl.java

@@ -14,7 +14,7 @@ import com.fdkk.sxz.entity.ComponentModelUploadEntity;
 import com.fdkk.sxz.entity.custuom.CustomComponentClassifyEntity;
 import com.fdkk.sxz.entity.custuom.CustomComponentEntity;
 import com.fdkk.sxz.exception.BusinessException;
-import com.fdkk.sxz.util.OkHttpUtils;
+import com.fdkk.sxz.other.mq.TopicRabbitConfig;
 import com.fdkk.sxz.util.SnowFlakeUUidUtils;
 import com.fdkk.sxz.util.UploadToOssUtil;
 import com.fdkk.sxz.vo.request.RequestCustom;
@@ -28,6 +28,7 @@ import com.fdkk.sxz.webApi.service.custom.ICustomComponentClassifyService;
 import com.fdkk.sxz.webApi.service.custom.ICustomComponentService;
 import com.github.pagehelper.PageInfo;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
@@ -36,7 +37,6 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.io.IOException;
-import java.util.Date;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -65,6 +65,9 @@ public class CustomComponentServiceImpl extends BaseServiceImpl<ICustomComponent
     @Value("${build.url}")
     private String buildUrl;
 
+    @Autowired
+    private RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方
+
     @Override
     public PageInfo<CustomComponentEntity> listByPage(RequestCustomComponent param) {
         LambdaQueryWrapper<CustomComponentEntity> wrapper = Wrappers.lambdaQuery();
@@ -74,7 +77,15 @@ public class CustomComponentServiceImpl extends BaseServiceImpl<ICustomComponent
         if (ObjectUtil.isNotNull(param.getUserId())) {
             wrapper.eq(CustomComponentEntity::getUserId, param.getUserId());
         }
-        param.setOrderBy("create_time");
+        if (ObjectUtil.isNotNull(param.getUserId())) {
+            wrapper.eq(CustomComponentEntity::getUserId, param.getUserId());
+        }
+        if (ObjectUtil.isNotNull(param.getPartsId())) {
+            wrapper.eq(CustomComponentEntity::getComponentTypeId, param.getPartsId());
+        }
+        if (ObjectUtil.isNotNull(param.getIsShow())) {
+            wrapper.eq(CustomComponentEntity::getIsShow, param.getIsShow());
+        }
         param.setSortBy("desc");
         return super.listByPage(param, wrapper);
     }
@@ -127,8 +138,6 @@ public class CustomComponentServiceImpl extends BaseServiceImpl<ICustomComponent
     public Result updatePartsDetailExamine(RequestRenovationPartsDetailManager param) throws IOException {
         CustomComponentEntity componentEntity = findById(param.getId());
         if (ObjectUtil.isNotNull(componentEntity)) {
-            componentEntity.setExamine(param.getExamine());
-            componentEntity.setExamineTime(new Date());
             ComponentModelUploadEntity modelUploadEntity = componentModelUploadService.findByPartsDetailId(componentEntity.getId());
             if (ObjectUtil.isNotNull(modelUploadEntity)) {
                 //入库前先提交模型到模型库,失败返回提示
@@ -147,14 +156,9 @@ public class CustomComponentServiceImpl extends BaseServiceImpl<ICustomComponent
                         version + "/SyncData.json");
                 CustomComponentServiceImpl.log.info("上传完成SyncData.jsn:" + version);
                 jsonObject.put("version", version);
-                synchronized (componentEntity.getId()) {
-                    String resultData = OkHttpUtils.httpPostJson(buildUrl + "syncmodel", jsonObject.toJSONString());
-                    JSONObject resultJson = JSONObject.parseObject(resultData);
-                    if ("error".equals(resultJson.getString("state"))) {
-                        CustomComponentServiceImpl.log.info("更新模型库,syncmodel接口失败:" + resultData);
-                        throw new BusinessException("更新模型库,syncmodel接口失败:" + resultData);
-                    }
-                }
+                String message = param.getId() + ":;" + "3" + ":;" + jsonObject.toString();
+                rabbitTemplate.convertAndSend(TopicRabbitConfig.TOPICE, TopicRabbitConfig.SYNCMODEL, message);
+
             }
             return Result.success(updateById(componentEntity));
         }

+ 16 - 10
sxz-core/src/main/java/com/fdkk/sxz/webApi/service/custom/impl/CustomProductServiceImpl.java

@@ -16,7 +16,7 @@ import com.fdkk.sxz.entity.custuom.CustomComponentClassifyEntity;
 import com.fdkk.sxz.entity.custuom.CustomComponentEntity;
 import com.fdkk.sxz.entity.custuom.CustomProductEntity;
 import com.fdkk.sxz.exception.BusinessException;
-import com.fdkk.sxz.util.OkHttpUtils;
+import com.fdkk.sxz.other.mq.TopicRabbitConfig;
 import com.fdkk.sxz.util.SnowFlakeUUidUtils;
 import com.fdkk.sxz.util.UploadToOssUtil;
 import com.fdkk.sxz.vo.request.RequestCustom;
@@ -33,6 +33,7 @@ import com.fdkk.sxz.webApi.service.custom.ICustomComponentService;
 import com.fdkk.sxz.webApi.service.custom.ICustomProductService;
 import com.github.pagehelper.PageInfo;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
@@ -74,6 +75,8 @@ public class CustomProductServiceImpl extends BaseServiceImpl<ICustomProductMapp
     private IModelUploadService modelUploadService;
     @Autowired
     private UploadToOssUtil uploadToOssUtil;
+    @Autowired
+    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方
 
     @Override
     public PageInfo<CustomProductEntity> listByPage(RequestCustomProduct param) {
@@ -84,6 +87,15 @@ public class CustomProductServiceImpl extends BaseServiceImpl<ICustomProductMapp
         if (ObjectUtil.isNotNull(param.getUserId())) {
             wrapper.eq(CustomProductEntity::getUserId, param.getUserId());
         }
+        if (ObjectUtil.isNotNull(param.getUserId())) {
+            wrapper.eq(CustomProductEntity::getUserId, param.getUserId());
+        }
+        if (ObjectUtil.isNotNull(param.getPartsId())) {
+            wrapper.eq(CustomProductEntity::getParentId, param.getPartsId());
+        }
+        if (ObjectUtil.isNotNull(param.getIsShow())) {
+            wrapper.eq(CustomProductEntity::getIsShow, param.getIsShow());
+        }
         param.setOrderBy("create_time");
         param.setSortBy("desc");
         return listByPage(param, wrapper);
@@ -180,15 +192,9 @@ public class CustomProductServiceImpl extends BaseServiceImpl<ICustomProductMapp
                     version + "/SyncData.json");
             CustomProductServiceImpl.log.info("上传完成SyncData.jsn:" + version);
             jsonObject.put("version", version);
-            synchronized (customProductEntity.getId()) {
-                //链式构建请求
-                String resultData = OkHttpUtils.httpPostJson(buildUrl + "syncmodel", jsonObject.toJSONString());
-                JSONObject resultJson = JSONObject.parseObject(resultData);
-                if ("error".equals(resultJson.getString("state"))) {
-                    CustomProductServiceImpl.log.info("更新模型库,syncmodel接口失败:" + resultData);
-                    throw new BusinessException("更新模型库,syncmodel接口失败:" + resultData);
-                }
-            }
+
+            String message = param.getId() + ":;" + "2" + ":;" + jsonObject.toString();
+            rabbitTemplate.convertAndSend(TopicRabbitConfig.TOPICE, TopicRabbitConfig.SYNCMODEL, message);
         }
         return Result.success();
     }

+ 3 - 1
sxz-modules/src/main/java/com/fdkk/sxz/vo/request/RequestCustomComponent.java

@@ -1,8 +1,8 @@
 package com.fdkk.sxz.vo.request;
 
+import com.fdkk.sxz.base.RequestBase;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
-import com.fdkk.sxz.base.RequestBase;
 
 /**
  * @description: 定制家具——组件 Model
@@ -33,4 +33,6 @@ public class RequestCustomComponent extends RequestBase {
 
     private String recStatus;
 
+    private Long partsId;
+    
 }

+ 3 - 2
sxz-modules/src/main/java/com/fdkk/sxz/vo/request/RequestCustomProduct.java

@@ -1,8 +1,8 @@
 package com.fdkk.sxz.vo.request;
 
+import com.fdkk.sxz.base.RequestBase;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
-import com.fdkk.sxz.base.RequestBase;
 
 /**
  * @description: 定制家具——成品 Model
@@ -26,6 +26,7 @@ public class RequestCustomProduct extends RequestBase {
     @ApiModelProperty(value = "0不显示,1显示", name = "isShow")
     private Integer isShow;
 
-
+    private Long partsId;
+    
     private String recStatus;
 }