Prechádzať zdrojové kódy

提交订单,提交成功推送

chao 3 rokov pred
rodič
commit
6ac7f3dab2

+ 4 - 0
pom.xml

@@ -55,6 +55,10 @@
             <groupId>org.springframework.cloud</groupId>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-loadbalancer</artifactId>
             <artifactId>spring-cloud-starter-loadbalancer</artifactId>
         </dependency>
         </dependency>
+<!--        <dependency>-->
+<!--            <groupId>org.springframework.cloud</groupId>-->
+<!--            <artifactId>spring-cloud-starter-openfeign</artifactId>-->
+<!--        </dependency>-->
         <!-- mysql -->
         <!-- mysql -->
         <dependency>
         <dependency>
             <groupId>mysql</groupId>
             <groupId>mysql</groupId>

+ 9 - 2
src/main/java/com/caimei365/order/OrderApplication.java

@@ -3,22 +3,29 @@ package com.caimei365.order;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.client.loadbalancer.LoadBalanced;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.reactive.function.client.WebClient;
 
 
 /**
 /**
  * `@EnableEurekaClient`: 声明一个Eureka客户端,只能注册到Eureka Server
  * `@EnableEurekaClient`: 声明一个Eureka客户端,只能注册到Eureka Server
  * `@EnableDiscoveryClient`: 声明一个可以被发现的客户端,可以是其他注册中心
  * `@EnableDiscoveryClient`: 声明一个可以被发现的客户端,可以是其他注册中心
- * `@EnableFeignClients`: 开启Feign
  *
  *
  * @author : Charles
  * @author : Charles
  * @date : 2021/2/22
  * @date : 2021/2/22
  */
  */
 @EnableDiscoveryClient
 @EnableDiscoveryClient
 @SpringBootApplication
 @SpringBootApplication
-//@EnableFeignClients
 public class OrderApplication {
 public class OrderApplication {
 
 
     public static void main(String[] args) {
     public static void main(String[] args) {
         SpringApplication.run(OrderApplication.class, args);
         SpringApplication.run(OrderApplication.class, args);
     }
     }
 
 
+    @Bean
+    @LoadBalanced
+    public WebClient.Builder webClientBuilder(){
+        return WebClient.builder();
+    }
+
 }
 }

+ 7 - 0
src/main/java/com/caimei365/order/mapper/CartSellerMapper.java

@@ -63,6 +63,13 @@ public interface CartSellerMapper {
      * @param cartIds 购物车Id集合
      * @param cartIds 购物车Id集合
      */
      */
     void deleteSellerCartByIds(Integer serviceProviderId, List<Integer> cartIds);
     void deleteSellerCartByIds(Integer serviceProviderId, List<Integer> cartIds);
+    /**
+     * 删除购物车
+     * @param serviceProviderId  协销Id
+     * @param clubId             机构Id
+     * @param productIds         商品Id集合
+     */
+    void deleteSellerCartByProductIds(Integer serviceProviderId, Integer clubId, List<String> productIds);
     /**
     /**
      * 根据商品Id获取供应商
      * 根据商品Id获取供应商
      * @param productId      商品Id
      * @param productId      商品Id

+ 32 - 0
src/main/java/com/caimei365/order/mapper/MessagePushMapper.java

@@ -0,0 +1,32 @@
+package com.caimei365.order.mapper;
+
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2021/7/15
+ */
+@Mapper
+public interface MessagePushMapper {
+    /**
+     * 查询短链接是否存在
+     */
+    Integer findIdByShortLink(String shortLink);
+    /**
+     * 保存短链接信息
+     *
+     * @param markId    短信类型
+     * @param shortLink 短链接
+     * @param url       跳转地址
+     */
+    void insertShortLink(int markId, String shortLink, String url);
+    /**
+     * 保存短信发送条数+count
+     *
+     * @param markId 短信类型
+     * @param count  增加条数
+     */
+    void updateSmsSendCount(int markId, int count);
+}

+ 29 - 0
src/main/java/com/caimei365/order/service/MessagePushService.java

@@ -0,0 +1,29 @@
+package com.caimei365.order.service;
+
+/**
+ * 订单推送服务
+ *
+ * @author : Charles
+ * @date : 2021/7/15
+ */
+public interface MessagePushService {
+    /**
+     * 生成短链接
+     *
+     * @param length 链接长度
+     * @param markId 跳转类型
+     * @param url    跳转地址
+     * @return
+     */
+    String getShortLink(int length, int markId, String url);
+
+    /**
+     * 请求发短信接口
+     *
+     * @param markId  短信类型
+     * @param mobile  手机号
+     * @param content 内容
+     * @return
+     */
+    boolean getSendSms(int markId, String mobile, String content);
+}

+ 111 - 0
src/main/java/com/caimei365/order/service/impl/MessagePushServiceImpl.java

@@ -0,0 +1,111 @@
+package com.caimei365.order.service.impl;
+
+import com.alibaba.fastjson.JSONObject;
+import com.caimei365.order.mapper.MessagePushMapper;
+import com.caimei365.order.service.MessagePushService;
+import com.caimei365.order.utils.CodeUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.MediaType;
+import org.springframework.stereotype.Service;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.reactive.function.BodyInserters;
+import org.springframework.web.reactive.function.client.WebClient;
+import reactor.core.publisher.Mono;
+
+import javax.annotation.Resource;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * 推送服务
+ *
+ * @author : Charles
+ * @date : 2021/7/15
+ */
+@Slf4j
+@Service
+public class MessagePushServiceImpl implements MessagePushService {
+    @Value("${spring.cloud.config.profile}")
+    private String profile;
+    @Resource
+    private MessagePushMapper messagePushMapper;
+    @Autowired
+    private WebClient.Builder webClientBuilder;
+
+    /**
+     * 生成短链接
+     *
+     * @param length 链接长度
+     * @param markId 跳转类型
+     * @param url    跳转地址
+     */
+    @Override
+    public String getShortLink(int length, int markId, String url) {
+        String shortLink = CodeUtil.generateShortLink(length);
+        Integer id = messagePushMapper.findIdByShortLink(shortLink);
+        if (id != null && id > 0) {
+            getShortLink(length, markId, url);
+        }
+        messagePushMapper.insertShortLink(markId, shortLink, url);
+        return shortLink;
+    }
+
+    /**
+     * 请求发短信接口
+     *
+     * @param markId  短信类型
+     * @param mobile  手机号
+     * @param content 内容
+     */
+    @Override
+    public boolean getSendSms(int markId, String mobile, String content) {
+        AtomicBoolean returnValue = new AtomicBoolean(false);
+        try {
+            //测试环境手机号允许发短信
+            List<String> list = new ArrayList<>();
+            list.add("15917362709");
+            list.add("15814011616");
+            list.add("13100721916");
+            list.add("15113936829");
+            if ("prod".equals(profile) || list.contains(mobile)) {
+                if (StringUtils.isNotBlank(mobile) && mobile.length() == 11) {
+                    String regex = "^(1[3-9]\\d{9}$)";
+                    Pattern pattern = Pattern.compile(regex);
+                    Matcher matcher = pattern.matcher(mobile);
+                    if (matcher.matches()) {
+                        //提交参数设置
+                        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
+                        map.add("mobile", mobile);
+                        map.add("content", content);
+                        // 发送服务间调用POST请求
+                        Mono<String> result = webClientBuilder.build()
+                                .post()
+                                .uri("http://CAIMEI365-CLOUD-TOOLS/tools/sms/send")
+                                .contentType(MediaType.APPLICATION_FORM_URLENCODED)
+                                .body(BodyInserters.fromFormData(map))
+                                .retrieve()
+                                .bodyToMono(String.class);
+                        result.subscribe(log::info);
+                        result.subscribe(jsonStr -> {
+                            JSONObject parseObject = JSONObject.parseObject(jsonStr);
+                            if (0 == parseObject.getInteger("code")){
+                                messagePushMapper.updateSmsSendCount(markId, 1);
+                                returnValue.set(true);
+                            }
+                        });
+                    }
+                }
+            }
+        } catch (Exception e) {
+            log.error("短信推送异常:", e);
+        }
+        return returnValue.get();
+    }
+}

+ 51 - 64
src/main/java/com/caimei365/order/service/impl/SubmitServiceImpl.java

@@ -4,12 +4,15 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONObject;
 import com.caimei365.order.components.ProductService;
 import com.caimei365.order.components.ProductService;
 import com.caimei365.order.mapper.BaseMapper;
 import com.caimei365.order.mapper.BaseMapper;
+import com.caimei365.order.mapper.CartClubMapper;
+import com.caimei365.order.mapper.CartSellerMapper;
 import com.caimei365.order.mapper.SubmitMapper;
 import com.caimei365.order.mapper.SubmitMapper;
 import com.caimei365.order.model.ResponseJson;
 import com.caimei365.order.model.ResponseJson;
 import com.caimei365.order.model.po.*;
 import com.caimei365.order.model.po.*;
 import com.caimei365.order.model.dto.SubmitDto;
 import com.caimei365.order.model.dto.SubmitDto;
 import com.caimei365.order.model.bo.OrderParamBo;
 import com.caimei365.order.model.bo.OrderParamBo;
 import com.caimei365.order.model.vo.*;
 import com.caimei365.order.model.vo.*;
+import com.caimei365.order.service.MessagePushService;
 import com.caimei365.order.service.SubmitService;
 import com.caimei365.order.service.SubmitService;
 import com.caimei365.order.utils.CodeUtil;
 import com.caimei365.order.utils.CodeUtil;
 import com.caimei365.order.utils.ImageUtil;
 import com.caimei365.order.utils.ImageUtil;
@@ -49,7 +52,12 @@ public class SubmitServiceImpl implements SubmitService {
     private String domain;
     private String domain;
     @Resource
     @Resource
     private ProductService productService;
     private ProductService productService;
-
+    @Resource
+    private CartClubMapper cartClubMapper;
+    @Resource
+    private CartSellerMapper cartSellerMapper;
+    @Resource
+    private MessagePushService messagePushService;
     /**
     /**
      * 生成订单
      * 生成订单
      *
      *
@@ -1266,74 +1274,53 @@ public class SubmitServiceImpl implements SubmitService {
         /*
         /*
          * 删除购物车
          * 删除购物车
          */
          */
-        // productIdList
-        
-
-
-
-
-
-
-
-//        log.info("******************** 提交订单逻辑处理 end *******************");
-//        /*
-//         * 构造返回参数
-//         */
-//        Map<String, String> info = new HashMap<>();
-//        info.put("orderId", String.valueOf(mainOrder.getOrderId()));
-//        info.put("orderNo", String.valueOf(mainOrder.getOrderNo()));
-//        info.put("orderMark", "#" + mainOrder.getOrderId() + "#");
-//        //应付订单金额
-//        info.put("payTotalFee", String.valueOf(mainOrder.getPayTotalFee()));
-//        //真实需要付款金额
-//        info.put("payableAmount", String.valueOf(mainOrder.getPayableAmount()));
-//
-//        //下单推送
-//        if (org.apache.commons.lang.StringUtils.isNotBlank(user.getBindMobile())) {
-//            String shortLink = orderPushService.getShortLink(8, 3, domain + "/user/mainOrder/detail.html?orderId=" + mainOrder.getOrderId());
-//            String name = productName.toString();
-//            if (name.length() > 10) {
-//                name = name.substring(0, 10);
-//            }
-//            String content = "您已成功下单“" + name + "...”等" + mainOrder.getProductCount() + "件商品,订单编号:" + mainOrder.getOrderNo() + ",订单等待支付,支付完成后采美将尽快安排发货。" +
-//                    "您可关注采美公众号或者访问采美微信小程序和网站查看并支付订单。平台公众号:微信搜索“采美365网”; 微信小程序:微信搜索“采美采购商城”;网址:www.caimei365.com/t/" + shortLink;
-//            boolean sendSms = orderPushService.getSendSms(3, user.getBindMobile(), content);
-//            if (!sendSms) {
-//                log.info("下单推送失败,orderId>>>>" + mainOrder.getOrderId());
-//            }
-//        }
-
-//        if (isPaySuccessFlag) {
-//            // 余额抵扣成功
-//            // 1提交成功[且支付完成]
-//            info.put("code", "1");
-//            info.put("msg", "提交成功且已支付");
-//            return JsonModel.newInstance().success(info);
-//        } else {
-//            info.put("code", "2");
-//            info.put("msg", "提交成功但未支付");
-//            return JsonModel.newInstance().success(info);
-//        }
-
-
-
-
-
-
-
-
+        if (3 == orderParamBo.getCartType()) {
+            // 清理当前机构的协销购物车数据
+            cartSellerMapper.deleteSellerCartByProductIds(orderParamBo.getBuyUserId(), orderParamBo.getClubId(), productIdList);
+            log.info("【提交订单】>>>>删除当前机构的 协销 购物车数据!");
+        } else {
+            // 清理用户购物车
+            cartClubMapper.deleteCartByProductIds(orderParamBo.getUserId(), productIdList);
+            log.info("【提交订单】>>>>删除当前机构 用户 购物车数据!");
+        }
 
 
+        log.info("******************** 提交订单逻辑处理 end *******************");
 
 
+        /*
+         * 构造返回参数
+         */
+        Map<String, Object> info = new HashMap<>();
+        info.put("orderId", mainOrder.getOrderId());
+        info.put("orderNo", mainOrder.getOrderNo());
+        info.put("orderMark", "#" + mainOrder.getOrderId() + "#");
+        //应付订单金额
+        info.put("payTotalFee", mainOrder.getPayTotalFee());
+        //真实需要付款金额
+        info.put("payableAmount", mainOrder.getPayableAmount());
 
 
+        //下单推送
+        if (StringUtils.isNotBlank(address.getMobile())) {
+            String shortLink = messagePushService.getShortLink(8, 3, domain + "/user/mainOrder/detail.html?orderId=" + mainOrder.getOrderId());
+            String name = orderProductList.get(0).getName();
+            if (name.length() > 10) {
+                name = name.substring(0, 10);
+            }
+            String content = "您已成功下单“" + name + "...”等" + mainOrder.getProductCount() + "件商品,订单编号:" + mainOrder.getOrderNo() + ",订单等待支付,支付完成后采美将尽快安排发货。" +
+                    "您可关注采美公众号或者访问采美微信小程序和网站查看并支付订单。平台公众号:微信搜索“采美365网”; 微信小程序:微信搜索“采美采购商城”;网址:www.caimei365.com/t/" + shortLink;
+            boolean sendSms = messagePushService.getSendSms(3, address.getMobile(), content);
+            if (!sendSms) {
+                log.info("下单推送失败,orderId>>>>" + mainOrder.getOrderId());
+            }
+        }
 
 
-        return null;
+        if (isPaySuccessFlag) {
+            // 1提交成功(支付完成)
+            return ResponseJson.success(1,"提交成功且已支付完成!", info);
+        } else {
+            //2提交成功(未支付)
+            return ResponseJson.success(2,"提交成功但未支付!", info);
+        }
     }
     }
 
 
 
 
-
-
-
-
-
-
 }
 }

+ 32 - 0
src/main/java/com/caimei365/order/utils/CodeUtil.java

@@ -32,6 +32,17 @@ public class CodeUtil {
             '8', '9'
             '8', '9'
     };
     };
 
 
+
+    private static final char[] SHORT_LINK_SEQUENCE = {
+            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
+            'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'O',
+            'W', 'X', 'Y', 'Z',
+            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
+            'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'o',
+            'w', 'x', 'y', 'z' ,
+            '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
+    };
+
     /**
     /**
      * 获取随机数字
      * 获取随机数字
      * @param size 位数
      * @param size 位数
@@ -101,4 +112,25 @@ public class CodeUtil {
         }
         }
         return platform + System.currentTimeMillis() + randomInt(2);
         return platform + System.currentTimeMillis() + randomInt(2);
     }
     }
+
+    /**
+     * 生成短链接
+     * @param size 长度
+     * @return 短链接
+     */
+    public static String generateShortLink(int size) {
+        StringBuilder sb = new StringBuilder();
+        Random random = new Random();
+        for (int i = 0; i < SHORT_LINK_SEQUENCE.length && i < size; ++i) {
+            sb.append(SHORT_LINK_SEQUENCE[random.nextInt(SHORT_LINK_SEQUENCE.length)]);
+        }
+        return sb.toString();
+    }
+
+
+
+
+
+
+
 }
 }

+ 9 - 0
src/main/resources/mapper/CartSellerMapper.xml

@@ -70,6 +70,15 @@
             #{id}
             #{id}
         </foreach>
         </foreach>
     </delete>
     </delete>
+    <delete id="deleteSellerCartByProductIds">
+        DELETE FROM bp_order_product_cart
+        WHERE serviceProviderId = #{serviceProviderId}
+        AND clubId = #{clubId}
+        AND productId in
+        <foreach collection="productIds" open="(" separator="," close=")" item="productId">
+            #{productId}
+        </foreach>
+    </delete>
     <select id="getShopByProductId" resultType="com.caimei365.order.model.vo.CartShopVo">
     <select id="getShopByProductId" resultType="com.caimei365.order.model.vo.CartShopVo">
         SELECT
         SELECT
             p.shopID AS shopId,
             p.shopID AS shopId,

+ 14 - 0
src/main/resources/mapper/MessagePushMapper.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.caimei365.order.mapper.MessagePushMapper">
+    <insert id="insertShortLink">
+        INSERT INTO cm_short_link (markId, shortLink, jumpLink, createTime)
+        VALUES (#{markId}, #{shortLink}, #{url},NOW())
+    </insert>
+    <update id="updateSmsSendCount">
+        UPDATE cm_sms_statistics SET sendNum = (sendNum + #{count}) WHERE markId = #{markId}
+    </update>
+    <select id="findIdByShortLink" resultType="java.lang.Integer">
+        SELECT id FROM cm_short_link WHERE shortLink = #{shortLink}
+    </select>
+</mapper>

+ 11 - 11
src/test/java/com/caimei365/order/OrderApplicationTests.java

@@ -14,16 +14,16 @@ import java.util.Random;
 @SpringBootTest
 @SpringBootTest
 class OrderApplicationTests {
 class OrderApplicationTests {
 
 
-    @Test
-    void contextLoads() {
-        Random rand = new Random();
-        String code = "";
-        for (int j = 0; j < 5; j++) {
-            code += rand.nextInt(10) + "";
-        }
-        System.out.println(code);
-        System.out.println(System.currentTimeMillis());
-        System.out.println(System.currentTimeMillis()/1000);
-    }
+//    @Test
+//    void contextLoads() {
+//        Random rand = new Random();
+//        String code = "";
+//        for (int j = 0; j < 5; j++) {
+//            code += rand.nextInt(10) + "";
+//        }
+//        System.out.println(code);
+//        System.out.println(System.currentTimeMillis());
+//        System.out.println(System.currentTimeMillis()/1000);
+//    }
 
 
 }
 }