Browse Source

Merge branch 'cpq-dev' of wangfan/adweb3-server into master

chenpeiqing 2 weeks ago
parent
commit
143301c449

+ 181 - 179
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/product/service/impl/AdwebProductServiceImpl.java

@@ -4,215 +4,217 @@ import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.codec.Base64Encoder;
 import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpResponse;
-
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import lombok.extern.slf4j.Slf4j;
-
+import org.apache.commons.lang3.StringUtils;
 import org.jeecg.modules.adweb.common.constant.AdwebConstant;
 import org.jeecg.modules.adweb.product.dto.ProductDTO;
 import org.jeecg.modules.adweb.product.entity.AdwebProduct;
 import org.jeecg.modules.adweb.product.mapper.AdwebProductMapper;
 import org.jeecg.modules.adweb.product.service.IAdwebProductService;
 import org.jeecg.modules.adweb.site.entity.AdwebSite;
+import org.jeecg.modules.adweb.site.entity.Sohoeb2bOrder;
+import org.jeecg.modules.adweb.site.service.Sohoeb2bOrderService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-
 /**
- * @Description: wp站点产品
- * @Author: jeecg-boot
- * @Date: 2024-12-06
- * @Version: V1.0
+ * @Description: wp站点产品 @Author: jeecg-boot @Date: 2024-12-06 @Version: V1.0
  */
 @Service
 @Slf4j
 public class AdwebProductServiceImpl extends ServiceImpl<AdwebProductMapper, AdwebProduct>
-        implements IAdwebProductService {
-
-    private final String SOHOEB2B_PUSH_PRODUCT_API = "https://admin.etcpu.com/api/product/product/";
-
-    /**
-     * @param productDTO
-     * @param adwebSites
-     */
-    @Override
-    public void addOrUpdateProduct(ProductDTO productDTO, AdwebSite adwebSites) {
-        // 产品同步时,update 有可能是新增或者更新,根据数据库中记录进行再次判断
-        if ("update".equals(productDTO.getActionType())) {
-            handleUpdate(productDTO, adwebSites);
-        } else if ("delete".equals(productDTO.getActionType())) {
-            handleDelete(productDTO, adwebSites);
-        } else {
-            handleAdd(productDTO, adwebSites);
-        }
-
-        pushSohoeB2BProduct(productDTO, adwebSites);
+    implements IAdwebProductService {
+
+  private final String SOHOEB2B_PUSH_PRODUCT_API = "https://admin.etcpu.com/api/product/product/";
+
+  @Autowired private Sohoeb2bOrderService sohoeb2bOrderService;
+
+  /**
+   * @param productDTO
+   * @param adwebSites
+   */
+  @Override
+  public void addOrUpdateProduct(ProductDTO productDTO, AdwebSite adwebSites) {
+    // 产品同步时,update 有可能是新增或者更新,根据数据库中记录进行再次判断
+    if ("update".equals(productDTO.getActionType())) {
+      handleUpdate(productDTO, adwebSites);
+    } else if ("delete".equals(productDTO.getActionType())) {
+      handleDelete(productDTO, adwebSites);
+    } else {
+      handleAdd(productDTO, adwebSites);
     }
 
-    private void handleUpdate(ProductDTO productDTO, AdwebSite adwebSites) {
-        LambdaQueryWrapper<AdwebProduct> productQuery = new LambdaQueryWrapper<>();
-        productQuery.eq(AdwebProduct::getProductId, productDTO.getProductId());
-        productQuery.eq(AdwebProduct::getSiteCode, adwebSites.getCode());
-
-        AdwebProduct product = this.getOne(productQuery);
-        // 执行更新操作
-        if (product != null) {
-            updateProduct(productDTO, product);
-        } else {
-            addProduct(productDTO, adwebSites);
-        }
+    Sohoeb2bOrder siteOrderPost = sohoeb2bOrderService.getBySiteCode(adwebSites.getCode());
+    // 属于苏豪租户的站点 向苏豪接口发送产品信息
+    if ("江苏苏豪纺织集团有限公司".equals(adwebSites.getChannelProviderName())
+        && StringUtils.isNotBlank(siteOrderPost.getOrderUrl())) {
+      pushSohoeB2BProduct(productDTO, adwebSites);
     }
-
-    private void handleDelete(ProductDTO productDTO, AdwebSite adwebSites) {
-        LambdaUpdateWrapper<AdwebProduct> productQuery = new LambdaUpdateWrapper<>();
-        productQuery.eq(AdwebProduct::getProductId, productDTO.getProductId());
-        productQuery.eq(AdwebProduct::getSiteCode, adwebSites.getCode());
-        productQuery.set(AdwebProduct::getActionType, "delete");
-        productQuery.set(AdwebProduct::getRequestTime, productDTO.getRequestTime());
-        productQuery.set(AdwebProduct::getAuthorName, productDTO.getAuthorName());
-
-        try {
-            boolean isUpdated = this.update(productQuery);
-            log.info(
-                    "MQ消息删除产品成功, 网站:{}, 产品ID:{}, 是否更新成功:{}",
-                    adwebSites.getCode(),
-                    productDTO.getProductId(),
-                    isUpdated);
-        } catch (Exception e) {
-            log.error("MQ消息删除产品失败:{}", e.getMessage(), e);
-        }
+  }
+
+  private void handleUpdate(ProductDTO productDTO, AdwebSite adwebSites) {
+    LambdaQueryWrapper<AdwebProduct> productQuery = new LambdaQueryWrapper<>();
+    productQuery.eq(AdwebProduct::getProductId, productDTO.getProductId());
+    productQuery.eq(AdwebProduct::getSiteCode, adwebSites.getCode());
+
+    AdwebProduct product = this.getOne(productQuery);
+    // 执行更新操作
+    if (product != null) {
+      updateProduct(productDTO, product);
+    } else {
+      addProduct(productDTO, adwebSites);
     }
-
-    private void handleAdd(ProductDTO productDTO, AdwebSite adwebSites) {
-        AdwebProduct product = new AdwebProduct();
-        BeanUtil.copyProperties(productDTO, product);
-        product.setSiteCode(adwebSites.getCode());
-        this.saveProduct(product);
+  }
+
+  private void handleDelete(ProductDTO productDTO, AdwebSite adwebSites) {
+    LambdaUpdateWrapper<AdwebProduct> productQuery = new LambdaUpdateWrapper<>();
+    productQuery.eq(AdwebProduct::getProductId, productDTO.getProductId());
+    productQuery.eq(AdwebProduct::getSiteCode, adwebSites.getCode());
+    productQuery.set(AdwebProduct::getActionType, "delete");
+    productQuery.set(AdwebProduct::getRequestTime, productDTO.getRequestTime());
+    productQuery.set(AdwebProduct::getAuthorName, productDTO.getAuthorName());
+
+    try {
+      boolean isUpdated = this.update(productQuery);
+      log.info(
+          "MQ消息删除产品成功, 网站:{}, 产品ID:{}, 是否更新成功:{}",
+          adwebSites.getCode(),
+          productDTO.getProductId(),
+          isUpdated);
+    } catch (Exception e) {
+      log.error("MQ消息删除产品失败:{}", e.getMessage(), e);
     }
-
-    private void updateProduct(ProductDTO productDTO, AdwebProduct product) {
-        BeanUtil.copyProperties(productDTO, product);
-
-        try {
-            this.updateById(product);
-            log.info("MQ消息更新产品成功:{}", product);
-        } catch (Exception e) {
-            log.error("MQ消息更新产品失败:{}", e.getMessage(), e);
-        }
+  }
+
+  private void handleAdd(ProductDTO productDTO, AdwebSite adwebSites) {
+    AdwebProduct product = new AdwebProduct();
+    BeanUtil.copyProperties(productDTO, product);
+    product.setSiteCode(adwebSites.getCode());
+    this.saveProduct(product);
+  }
+
+  private void updateProduct(ProductDTO productDTO, AdwebProduct product) {
+    BeanUtil.copyProperties(productDTO, product);
+
+    try {
+      this.updateById(product);
+      log.info("MQ消息更新产品成功:{}", product);
+    } catch (Exception e) {
+      log.error("MQ消息更新产品失败:{}", e.getMessage(), e);
     }
-
-    private void addProduct(ProductDTO productDTO, AdwebSite adwebSites) {
-        AdwebProduct product = new AdwebProduct();
-        BeanUtil.copyProperties(productDTO, product);
-        product.setSiteCode(adwebSites.getCode()).setActionType("add");
-        this.saveProduct(product);
+  }
+
+  private void addProduct(ProductDTO productDTO, AdwebSite adwebSites) {
+    AdwebProduct product = new AdwebProduct();
+    BeanUtil.copyProperties(productDTO, product);
+    product.setSiteCode(adwebSites.getCode()).setActionType("add");
+    this.saveProduct(product);
+  }
+
+  private void saveProduct(AdwebProduct product) {
+    try {
+      this.save(product);
+
+      log.info("MQ消息新增产品成功:{}", product);
+    } catch (Exception e) {
+      log.error("MQ消息新增产品失败:{}", e.getMessage());
     }
-
-    private void saveProduct(AdwebProduct product) {
-        try {
-            this.save(product);
-
-            log.info("MQ消息新增产品成功:{}", product);
-        } catch (Exception e) {
-            log.error("MQ消息新增产品失败:{}", e.getMessage());
+  }
+
+  /**
+   * 异步向苏豪通推送产品消息队列
+   *
+   * <p>TODO 待确定,是否在V3产品中加入推送到苏豪是否成功的状态
+   */
+  @Async
+  protected void pushSohoeB2BProduct(ProductDTO productDTO, AdwebSite adwebSites) {
+    // 站点必须处于上线状态,才向苏豪推送产品
+    if (AdwebConstant.STATUS.equals(adwebSites.getStatus())) {
+      // 向苏豪推送产品添加和更新消息
+      String imageBase64 = imageBase64(productDTO.getProductImage());
+      JSONObject params = new JSONObject();
+      params.put("domain", productDTO.getSiteHost());
+      params.put("product_id", productDTO.getProductId());
+      params.put("name", productDTO.getProductName());
+      params.put("cn_name", productDTO.getProductNameZh());
+      params.put("adduser", productDTO.getAuthorName());
+      params.put("image", imageBase64);
+
+      log.info("向苏豪推送产品消息,api地址:{}, 参数:{}", SOHOEB2B_PUSH_PRODUCT_API, params);
+      try {
+        HttpResponse response =
+            HttpRequest.post(SOHOEB2B_PUSH_PRODUCT_API)
+                .header("Content-Type", "application/json")
+                .header("Authorization", "Token f354b8a705c862abeae1f4f96dc55999fb9b3579")
+                .body(params.toString())
+                .execute(true);
+
+        if (response != null && response.getStatus() == 200) {
+          log.info("推送产品到苏豪成功:{}", response.body());
+        } else {
+          log.info("推送产品到苏豪失败:{}", response);
         }
+      } catch (Exception e) {
+        log.info("推送产品到苏豪失败:{}", e.getMessage());
+      }
     }
-
-    /**
-     * 异步向苏豪通推送产品消息队列
-     *
-     * <p>TODO 待确定,是否在V3产品中加入推送到苏豪是否成功的状态
-     */
-    @Async
-    protected void pushSohoeB2BProduct(ProductDTO productDTO, AdwebSite adwebSites) {
-        // 站点必须处于上线状态,才向苏豪推送产品
-        if (AdwebConstant.STATUS.equals(adwebSites.getStatus())) {
-            // 向苏豪推送产品添加和更新消息
-            String imageBase64 = imageBase64(productDTO.getProductImage());
-            JSONObject params = new JSONObject();
-            params.put("domain", productDTO.getSiteHost());
-            params.put("product_id", productDTO.getProductId());
-            params.put("name", productDTO.getProductName());
-            params.put("cn_name", productDTO.getProductNameZh());
-            params.put("adduser", productDTO.getAuthorName());
-            params.put("image", imageBase64);
-
-            log.info("向苏豪推送产品消息,api地址:{}, 参数:{}", SOHOEB2B_PUSH_PRODUCT_API, params);
-            try {
-                HttpResponse response =
-                        HttpRequest.post(SOHOEB2B_PUSH_PRODUCT_API)
-                                .header("Content-Type", "application/json")
-                                .header(
-                                        "Authorization",
-                                        "Token f354b8a705c862abeae1f4f96dc55999fb9b3579")
-                                .body(params.toString())
-                                .execute(true);
-
-                if (response != null && response.getStatus() == 200) {
-                    log.info("推送产品到苏豪成功:{}", response.body());
-                } else {
-                    log.info("推送产品到苏豪失败:{}", response);
-                }
-            } catch (Exception e) {
-                log.info("推送产品到苏豪失败:{}", e.getMessage());
-            }
+  }
+
+  private String imageBase64(String imgUrl) {
+    URL url = null;
+    InputStream is = null;
+    ByteArrayOutputStream outStream = null;
+    HttpURLConnection httpUrl = null;
+    try {
+      url = new URL(imgUrl);
+      httpUrl = (HttpURLConnection) url.openConnection();
+      httpUrl.connect();
+      httpUrl.getInputStream();
+      is = httpUrl.getInputStream();
+
+      outStream = new ByteArrayOutputStream();
+      // 创建一个Buffer字符串
+      byte[] buffer = new byte[1024];
+      // 每次读取的字符串长度,如果为-1,代表全部读取完毕
+      int len = 0;
+      // 使用一个输入流从buffer里把数据读取出来
+      while ((len = is.read(buffer)) != -1) {
+        // 用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
+        outStream.write(buffer, 0, len);
+      }
+      // 对字节数组Base64编码
+      return Base64Encoder.encode(outStream.toByteArray());
+    } catch (Exception e) {
+      log.error("图片地址:{}, 下载图片并转base64失败:{}", imgUrl, e.getMessage());
+    } finally {
+      if (is != null) {
+        try {
+          is.close();
+        } catch (IOException e) {
+          log.error("远程下载图片资源失败:{}", e.getMessage());
         }
-    }
-
-    private String imageBase64(String imgUrl) {
-        URL url = null;
-        InputStream is = null;
-        ByteArrayOutputStream outStream = null;
-        HttpURLConnection httpUrl = null;
+      }
+      if (outStream != null) {
         try {
-            url = new URL(imgUrl);
-            httpUrl = (HttpURLConnection) url.openConnection();
-            httpUrl.connect();
-            httpUrl.getInputStream();
-            is = httpUrl.getInputStream();
-
-            outStream = new ByteArrayOutputStream();
-            // 创建一个Buffer字符串
-            byte[] buffer = new byte[1024];
-            // 每次读取的字符串长度,如果为-1,代表全部读取完毕
-            int len = 0;
-            // 使用一个输入流从buffer里把数据读取出来
-            while ((len = is.read(buffer)) != -1) {
-                // 用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
-                outStream.write(buffer, 0, len);
-            }
-            // 对字节数组Base64编码
-            return Base64Encoder.encode(outStream.toByteArray());
-        } catch (Exception e) {
-            log.error("图片地址:{}, 下载图片并转base64失败:{}", imgUrl, e.getMessage());
-        } finally {
-            if (is != null) {
-                try {
-                    is.close();
-                } catch (IOException e) {
-                    log.error("远程下载图片资源失败:{}", e.getMessage());
-                }
-            }
-            if (outStream != null) {
-                try {
-                    outStream.close();
-                } catch (IOException e) {
-                    log.error("图片转base64失败:{}", e.getMessage());
-                }
-            }
-            if (httpUrl != null) {
-                httpUrl.disconnect();
-            }
+          outStream.close();
+        } catch (IOException e) {
+          log.error("图片转base64失败:{}", e.getMessage());
         }
-        return imgUrl;
+      }
+      if (httpUrl != null) {
+        httpUrl.disconnect();
+      }
     }
+    return imgUrl;
+  }
 }

+ 36 - 27
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/mq/ProductReceiver.java

@@ -2,6 +2,7 @@ package org.jeecg.modules.mq;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.rabbitmq.client.Channel;
+import java.util.List;
 import lombok.extern.slf4j.Slf4j;
 import org.jeecg.boot.starter.rabbitmq.core.BaseRabbiMqHandler;
 import org.jeecg.boot.starter.rabbitmq.listenter.MqListener;
@@ -10,14 +11,13 @@ import org.jeecg.modules.adweb.product.dto.ProductDTO;
 import org.jeecg.modules.adweb.product.service.IAdwebProductService;
 import org.jeecg.modules.adweb.site.entity.AdwebSite;
 import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
+import org.jeecg.modules.adweb.site.service.Sohoeb2bOrderService;
 import org.springframework.amqp.rabbit.annotation.RabbitHandler;
 import org.springframework.amqp.rabbit.annotation.RabbitListener;
 import org.springframework.amqp.support.AmqpHeaders;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.messaging.handler.annotation.Header;
 
-import java.util.List;
-
 /**
  * Rabbit MQ - 产品消息接收
  *
@@ -27,35 +27,44 @@ import java.util.List;
 @RabbitComponent(value = "productReceiver")
 public class ProductReceiver extends BaseRabbiMqHandler<List<ProductDTO>> {
 
-    @Autowired
-    private IAdwebSiteService adwebSiteService;
+  @Autowired private IAdwebSiteService adwebSiteService;
 
-    @Autowired
-    private IAdwebProductService adwebProductService;
+  @Autowired private IAdwebProductService adwebProductService;
 
-    @RabbitHandler
-    // ackMode需要设置为MANUAL - BaseRabbiMqHandler.onMessage()有channel.basicAck()操作
-    @RabbitListener(queues = "site_product", ackMode = "MANUAL")
-    public void onMessage(List<ProductDTO> products, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
-        super.onMessage(products, deliveryTag, channel, new MqListener<List<ProductDTO>>() {
-            @Override
-            public void handler(List<ProductDTO> products, Channel channel) {
-                // 业务处理
-                log.info("收到产品MQ消息 {}", products);
-                for (ProductDTO product : products) {
-                    // 查看发布当前消息的站点是否是 adweb3 平台中绑定的是否是有效站点, 如果不是则直接确认消息,不做业务处理
-                    List<AdwebSite> adwebSites = adwebSiteService.list(new LambdaQueryWrapper<AdwebSite>().like(AdwebSite::getDomain, product.getSiteHost()).ne(AdwebSite::getStatus, 0));
+  @RabbitHandler
+  // ackMode需要设置为MANUAL - BaseRabbiMqHandler.onMessage()有channel.basicAck()操作
+  @RabbitListener(queues = "site_product", ackMode = "MANUAL")
+  public void onMessage(
+      List<ProductDTO> products,
+      Channel channel,
+      @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
+    super.onMessage(
+        products,
+        deliveryTag,
+        channel,
+        new MqListener<List<ProductDTO>>() {
+          @Override
+          public void handler(List<ProductDTO> products, Channel channel) {
+            // 业务处理
+            log.info("收到产品MQ消息 {}", products);
+            for (ProductDTO product : products) {
+              // 查看发布当前消息的站点是否是 adweb3 平台中绑定的是否是有效站点, 如果不是则直接确认消息,不做业务处理
+              List<AdwebSite> adwebSites =
+                  adwebSiteService.list(
+                      new LambdaQueryWrapper<AdwebSite>()
+                          .like(AdwebSite::getDomain, product.getSiteHost())
+                          .ne(AdwebSite::getStatus, 0));
 
-                    // 一个消息中的产品所属站点相同,一条不符合,则认为全部不符合
-                    if (adwebSites.isEmpty()) {
-                        log.error("站点:{} 不是adweb3的有效绑定站点,MQ消息确认失败", product.getSiteHost());
-                        return;
-                    }
+              // 一个消息中的产品所属站点相同,一条不符合,则认为全部不符合
+              if (adwebSites.isEmpty()) {
+                log.error("站点:{} 不是adweb3的有效绑定站点,MQ消息确认失败", product.getSiteHost());
+                return;
+              }
 
-                    // 产品入库
-                    adwebProductService.addOrUpdateProduct(product, adwebSites.get(0));
-                }
+              // 产品入库
+              adwebProductService.addOrUpdateProduct(product, adwebSites.get(0));
             }
+          }
         });
-    }
+  }
 }

+ 2 - 2
jeecg-module-system/jeecg-system-start/src/main/resources/logback-spring.xml

@@ -11,7 +11,7 @@
 
     <springProperty scope="context" name="maxHistory"
 
-                    source="logging.file.max-history" defaultValue="15"/>
+                    source="logging.file.max-history" defaultValue="10"/>
 
 
     <!--定义日志文件的存储地址 -->
@@ -55,7 +55,7 @@
             <!--日志文件输出的文件名 -->
             <FileNamePattern>${LOG_HOME}/adweb3-%d{yyyy-MM-dd}.%i.log</FileNamePattern>
             <!--日志文件保留天数 -->
-            <MaxHistory>30</MaxHistory>
+            <MaxHistory>${maxHistory}</MaxHistory>
             <maxFileSize>10MB</maxFileSize>
         </rollingPolicy>
         <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">