浏览代码

消息推送

Duan_xu 2 年之前
父节点
当前提交
b05dac4f09

+ 10 - 0
src/main/java/com/caimei365/commodity/feign/ToolsFeign.java

@@ -20,4 +20,14 @@ public interface ToolsFeign {
     @PostMapping("/tools/mq/send")
     String sendCommonMessage(@RequestParam String topic, @RequestParam String content, @RequestParam String tag, @RequestParam Integer sort, @RequestParam Integer async, @RequestParam Integer oneway, @RequestParam Integer delay);
 
+
+    /**
+     * 请求发短信接口
+     *
+     * @param mobile  手机号
+     * @param content 内容
+     * @return str
+     */
+    @PostMapping("/tools/sms/send")
+    String getSendSms(@RequestParam String mobile, @RequestParam String content);
 }

+ 3 - 0
src/main/java/com/caimei365/commodity/mapper/MessageCenterMapper.java

@@ -23,4 +23,7 @@ public interface MessageCenterMapper {
     UserVo clubList(Integer userId);
 
     CouponVo CouponList(Integer couponType);
+
+    String contractMobile(Integer userID);
+
 }

+ 219 - 95
src/main/java/com/caimei365/commodity/service/impl/CouponServiceImpl.java

@@ -1,6 +1,8 @@
 package com.caimei365.commodity.service.impl;
 
+import com.alibaba.fastjson.JSONObject;
 import com.caimei365.commodity.components.PriceUtilService;
+import com.caimei365.commodity.feign.ToolsFeign;
 import com.caimei365.commodity.mapper.*;
 import com.caimei365.commodity.model.ResponseJson;
 import com.caimei365.commodity.model.dto.CollarCouponsDto;
@@ -11,8 +13,10 @@ import com.caimei365.commodity.model.vo.*;
 import com.caimei365.commodity.service.CouponService;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
+import lombok.SneakyThrows;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -21,6 +25,12 @@ import javax.annotation.Resource;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * Description
@@ -30,6 +40,10 @@ import java.util.*;
  */
 @Service
 public class CouponServiceImpl implements CouponService {
+    @Value("${spring.cloud.config.profile}")
+    private String profile;
+    @Resource
+    private ToolsFeign toolsFeign;
     @Resource
     private CouponMapper couponMapper;
     @Resource
@@ -156,118 +170,188 @@ public class CouponServiceImpl implements CouponService {
     }
 
     /**
-     * 每天12点判断
+     * 每天12点推送
      */
-    @Scheduled(cron = "0 0 12 * * ?")
     private Void Timerw(Integer userId) throws ParseException {
-        if(userId!=null){
-           UserVo user= messageCenterMapper.clubList(userId);
-           CouponVo coupon= messageCenterMapper.CouponList(userId);
-           Date date=new Date();
-            Date d=coupon.getStartDate();
-            //1.日期格式
-            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
-            String endTime = sdf.format(coupon.getEndDate());
-            //2.某天的日期
-            Date da1=sdf.parse(endTime);
-            Long s=(da1.getTime()-d.getTime())/24/60/60/1000;
-            Long t=(date.getTime()-da1.getTime())/24/60/60/1000;
-            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-            String current = dateFormat.format(new Date());
-            MessageCenter messageCenter=new MessageCenter();
-            //推送信息中心-账户通知
-            if(s>15){
-                if(t==7){
-                    messageCenter.setShopID(null);
-                    messageCenter.setClubID(user.getClubId());
-                    messageCenter.setUserType(1);
-                    messageCenter.setMessageType(4);
-                    messageCenter.setCouponType(2);
-                    messageCenter.setContent(t+"天后过期");
-                    messageCenter.setCouponFee(coupon.getCouponAmount().doubleValue());
-                    messageCenter.setTime(current);
-                    messageCenterMapper.addMessageCenter(messageCenter);
+        //1.获取下午3点的时间
+        SimpleDateFormat dnf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+        Calendar time = Calendar.getInstance();
+        time.set(Calendar.HOUR_OF_DAY, 12);
+        time.set(Calendar.MINUTE, 00);
+        time.set(Calendar.SECOND, 00);
+
+        String times = dnf.format(time.getTime());
+        System.out.println(times);
+        //2.某天的日期
+        Date date = dnf.parse(times);
+        //与当前时间的毫秒数相减得到相差的毫秒用于定时
+        Long h = (date.getTime() - date.getTime());
+        //这里如果当前时间大于下午三点会出现负数所以就不推送
+        if (h > 0) {
+            ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
+            executorService.schedule(new Runnable() {
+                @SneakyThrows
+                @Override
+                public void run() {
+                    if (userId != null) {
+                        UserVo user = messageCenterMapper.clubList(userId);
+                        CouponVo coupon = messageCenterMapper.CouponList(userId);
+                        String contractMobile=messageCenterMapper.contractMobile(userId);
+                        Date date = new Date();
+                        Date d = coupon.getStartDate();
+                        //1.日期格式
+                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
+                        String endTime = sdf.format(coupon.getEndDate());
+                        //2.某天的日期
+                        Date da1 = sdf.parse(endTime);
+                        Long s = (da1.getTime() - d.getTime()) / 24 / 60 / 60 / 1000;
+                        Long t = (date.getTime() - da1.getTime()) / 24 / 60 / 60 / 1000;
+                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                        String current = dateFormat.format(new Date());
+                        MessageCenter messageCenter = new MessageCenter();
+                        //推送信息中心-账户通知
+                        if (s > 15) {
+                            if (t == 7) {
+                                messageCenter.setShopID(null);
+                                messageCenter.setClubID(user.getClubId());
+                                messageCenter.setUserType(1);
+                                messageCenter.setMessageType(4);
+                                messageCenter.setCouponType(2);
+                                messageCenter.setContent(t + "天后过期");
+                                messageCenter.setCouponFee(coupon.getCouponAmount().doubleValue());
+                                messageCenter.setTime(current);
+                                messageCenterMapper.addMessageCenter(messageCenter);
+                                String content = "短信模板:【采美365】您有"+coupon.getCouponAmount()+"元优惠券将于7日后过期,快戳采美网站链接www.caimei365.com/M2Tr98CG 或微信搜索“采美采购商城”小程序登录采美平台查看使用吧。关注公众号“采美网”获取更多优惠和精彩资讯。";
+                                boolean sendSms = getSendSms(2, contractMobile, content);
+                            }
+                        }
+                    }
                 }
-            }
+            }, h, TimeUnit.MILLISECONDS);
         }
         return null;
     }
 
     /**
-     * 每天下午3点判断
+     * 每天10点判断
      */
-    @Scheduled(cron = "0 0 10 * * ?")
     private Void Timeto(Integer userId) throws ParseException {
-        if(userId!=null){
-            UserVo user= messageCenterMapper.clubList(userId);
-            CouponVo coupon= messageCenterMapper.CouponList(userId);
-            Date d=new Date();
-            //1.日期格式
-            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
-            String endTime = sdf.format(coupon.getEndDate());
-            //2.某天的日期
-            Date da1=sdf.parse(endTime);
-            Long s=(da1.getTime()-d.getTime())/24/60/60/1000;
-            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-            String current = dateFormat.format(new Date());
-            MessageCenter messageCenter=new MessageCenter();
-            //推送信息中心-账户通知
-            if(s==1){
+        //1.获取下午3点的时间
+        SimpleDateFormat dnf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+        Calendar time = Calendar.getInstance();
+        time.set(Calendar.HOUR_OF_DAY, 10);
+        time.set(Calendar.MINUTE, 00);
+        time.set(Calendar.SECOND, 00);
+        String contractMobile=messageCenterMapper.contractMobile(userId);
+        String times = dnf.format(time.getTime());
+        System.out.println(times);
+        //2.某天的日期
+        Date date = dnf.parse(times);
+        //与当前时间的毫秒数相减得到相差的毫秒用于定时
+        Long h = (date.getTime() - date.getTime());
+        //这里如果当前时间大于下午三点会出现负数所以就不推送
+        if (h > 0) {
+            ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
+            executorService.schedule(new Runnable() {
+                @SneakyThrows
+                @Override
+                public void run() {
+                    if (userId != null) {
+                        UserVo user = messageCenterMapper.clubList(userId);
+                        CouponVo coupon = messageCenterMapper.CouponList(userId);
+                        Date d = new Date();
+                        //1.日期格式
+                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
+                        String endTime = sdf.format(coupon.getEndDate());
+                        //2.某天的日期
+                        Date da1 = sdf.parse(endTime);
+                        Long s = (da1.getTime() - d.getTime()) / 24 / 60 / 60 / 1000;
+                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                        String current = dateFormat.format(new Date());
+                        MessageCenter messageCenter = new MessageCenter();
+                        //推送信息中心-账户通知
+                        if (s == 1) {
 
-                    messageCenter.setShopID(null);
-                    messageCenter.setClubID(user.getClubId());
-                    messageCenter.setUserType(1);
-                    messageCenter.setMessageType(4);
-                    messageCenter.setCouponType(2);
-                    messageCenter.setContent(null);
-                    messageCenter.setCouponFee(coupon.getCouponAmount().doubleValue());
-                    messageCenter.setTime(current);
-                    messageCenterMapper.addMessageCenter(messageCenter);
-
-            }
+                            messageCenter.setShopID(null);
+                            messageCenter.setClubID(user.getClubId());
+                            messageCenter.setUserType(1);
+                            messageCenter.setMessageType(4);
+                            messageCenter.setCouponType(2);
+                            messageCenter.setContent(null);
+                            messageCenter.setCouponFee(coupon.getCouponAmount().doubleValue());
+                            messageCenter.setTime(current);
+                            messageCenterMapper.addMessageCenter(messageCenter);
+                            String content = "短信模板:【采美365】您有"+coupon.getCouponAmount()+"元优惠券今日过期,快戳采美网站链接www.caimei365.com/M2Tr98CG 或微信搜索“采美采购商城”小程序登录采美平台查看使用吧。关注公众号“采美网”获取更多优惠和精彩资讯。";
+                            boolean sendSms = getSendSms(2, contractMobile, content);
+                        }
+                    }
+                }
+            }, h, TimeUnit.MILLISECONDS);
         }
         return null;
+
     }
 
 
-    @Scheduled(cron = "0 0 15 * * ?")
     private Void Timeth(Integer userId) throws ParseException {
-        if(userId!=null){
-            UserVo user= messageCenterMapper.clubList(userId);
-            CouponVo coupon= messageCenterMapper.CouponList(userId);
-            Date date=new Date();
-            Date d=coupon.getStartDate();
-            //1.日期格式
-            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
-            String endTime = sdf.format(coupon.getEndDate());
-            //2.某天的日期
-            Date da1=sdf.parse(endTime);
-            Long s=(da1.getTime()-d.getTime())/24/60/60/1000;
-            Long t=(date.getTime()-da1.getTime())/24/60/60/1000;
-            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-            String current = dateFormat.format(new Date());
-            MessageCenter messageCenter=new MessageCenter();
-            //推送信息中心-账户通知
+        //1.获取下午3点的时间
+        SimpleDateFormat dnf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+        Calendar time = Calendar.getInstance();
+        time.set(Calendar.HOUR_OF_DAY, 15);
+        time.set(Calendar.MINUTE, 00);
+        time.set(Calendar.SECOND, 00);
+        String contractMobile=messageCenterMapper.contractMobile(userId);
+        String times = dnf.format(time.getTime());
+        System.out.println(times);
+        //2.某天的日期
+        Date date = dnf.parse(times);
+        //与当前时间的毫秒数相减得到相差的毫秒用于定时
+        Long h = (date.getTime() - date.getTime());
+        //这里如果当前时间大于下午三点会出现负数所以就不推送
+        if (h > 0) {
+            ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
+            executorService.schedule(new Runnable() {
+                @SneakyThrows
+                @Override
+                public void run() {
+                    if (userId != null) {
+                        UserVo user = messageCenterMapper.clubList(userId);
+                        CouponVo coupon = messageCenterMapper.CouponList(userId);
+                        Date date = new Date();
+                        Date d = coupon.getStartDate();
+                        //1.日期格式
+                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
+                        String endTime = sdf.format(coupon.getEndDate());
+                        //2.某天的日期
+                        Date da1 = sdf.parse(endTime);
+                        Long s = (da1.getTime() - d.getTime()) / 24 / 60 / 60 / 1000;
+                        Long t = (date.getTime() - da1.getTime()) / 24 / 60 / 60 / 1000;
+                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                        String current = dateFormat.format(new Date());
+                        MessageCenter messageCenter = new MessageCenter();
+                        //推送信息中心-账户通知
+                        if (t == 3) {
+                            messageCenter.setShopID(null);
+                            messageCenter.setClubID(user.getClubId());
+                            messageCenter.setUserType(1);
+                            messageCenter.setMessageType(4);
+                            messageCenter.setCouponType(2);
+                            messageCenter.setContent(t + "天后下架");
+                            messageCenter.setCouponFee(coupon.getCouponAmount().doubleValue());
+                            messageCenter.setTime(current);
+                            messageCenterMapper.addMessageCenter(messageCenter);
+                            String content = "短信模板:【采美365】您有"+coupon.getCouponAmount()+"元优惠券3日后过期,快戳采美网站链接www.caimei365.com/M2Tr98CG 或微信搜索“采美采购商城”小程序登录采美平台查看使用吧。关注公众号“采美网”获取更多优惠和精彩资讯。";
+                            boolean sendSms = getSendSms(2, contractMobile, content);
+                        }
 
-                if(t==3){
-                    messageCenter.setShopID(null);
-                    messageCenter.setClubID(user.getClubId());
-                    messageCenter.setUserType(1);
-                    messageCenter.setMessageType(4);
-                    messageCenter.setCouponType(2);
-                    messageCenter.setContent(t+"天后下架");
-                    messageCenter.setCouponFee(coupon.getCouponAmount().doubleValue());
-                    messageCenter.setTime(current);
-                    messageCenterMapper.addMessageCenter(messageCenter);
+                    }
                 }
-
+            }, h, TimeUnit.MILLISECONDS);
         }
         return null;
     }
 
 
-
-
     @Transactional(rollbackFor = Exception.class)
     @Override
     public ResponseJson<CouponVo> redeemCoupons(RedeemCouponsDto redeemCouponsDto) {
@@ -337,7 +421,7 @@ public class CouponServiceImpl implements CouponService {
         //未领取
         List<CouponVo> notCouponList = couponMapper.findCouponList(userId, registerTime);
         filterCoupon(source, product, notCouponList);
-        notCouponList.forEach(n->n.setCouponBtnType(0));
+        notCouponList.forEach(n -> n.setCouponBtnType(0));
         //超级会员优惠券移除
         if (notCouponList != null && notCouponList.size() > 0) {
             notCouponList.removeIf(couponVo -> vipCoupon.contains(couponVo.getCouponId()));
@@ -345,7 +429,7 @@ public class CouponServiceImpl implements CouponService {
         map.put("notCouponNum", notCouponList == null ? 0 : notCouponList.size());
         //已领取
         List<CouponVo> couponList = couponMapper.findCouponClub(userId, 1);
-        couponList.forEach(c->c.setCouponBtnType(1));
+        couponList.forEach(c -> c.setCouponBtnType(1));
         //超级会员优惠券移除
         if (couponList != null && couponList.size() > 0) {
             couponList.removeIf(couponVo -> vipCoupon.contains(couponVo.getCouponId()));
@@ -373,32 +457,33 @@ public class CouponServiceImpl implements CouponService {
 
     /**
      * 用户可查看的价值优惠券
+     *
      * @param userId
      * @param pageNum
      * @param pageSize
      * @return
      */
     @Override
-    public ResponseJson<PageInfo<CouponVo>> findMoneyCoupons(Integer userId,int pageNum, int pageSize) {
+    public ResponseJson<PageInfo<CouponVo>> findMoneyCoupons(Integer userId, int pageNum, int pageSize) {
         Date registerTime = couponMapper.findUserRegisterTime(userId);
         PageHelper.startPage(pageNum, pageSize);
-        List<CouponVo> cop = couponMapper.findMoneyCoupons(userId,registerTime);
+        List<CouponVo> cop = couponMapper.findMoneyCoupons(userId, registerTime);
         PageInfo<CouponVo> pageInfo = new PageInfo<>(cop);
         return ResponseJson.success(pageInfo);
     }
 
     @Override
     public ResponseJson<CouponVo> findCouponDetail(Integer couponId) {
-        CouponVo couponVo=couponMapper.findCouponDetail(couponId);
+        CouponVo couponVo = couponMapper.findCouponDetail(couponId);
         return ResponseJson.success(couponVo);
     }
 
     @Override
     public ResponseJson<PageInfo<CouponVo>> collarCenterList(Integer userId, int pageNum, int pageSize) {
         Date registerTime = couponMapper.findUserRegisterTime(userId);
-        Integer permission=couponMapper.findUserPermission(userId);
+        Integer permission = couponMapper.findUserPermission(userId);
         //如果是协销,userid置为0查所有
-        if(1==permission){
+        if (1 == permission) {
             userId = 0;
         }
         PageHelper.startPage(pageNum, pageSize);
@@ -440,4 +525,43 @@ public class CouponServiceImpl implements CouponService {
             }
         }
     }
+
+    /**
+     * 请求发短信接口
+     *
+     * @param markId  跳转类型
+     * @param mobile  手机号
+     * @param content 内容
+     */
+    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");
+            list.add("18175515644");
+            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()) {
+                        // 调用 ToolsFeign 发送短信
+                        String jsonStr = toolsFeign.getSendSms(mobile, content);
+                        JSONObject parseObject = JSONObject.parseObject(jsonStr);
+                        if (0 == parseObject.getInteger("code")){
+                            returnValue.set(true);
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+
+        }
+        return returnValue.get();
+    }
+
 }

+ 1 - 1
src/main/java/com/caimei365/commodity/service/impl/ShopServiceImpl.java

@@ -126,7 +126,7 @@ public class ShopServiceImpl implements ShopService {
         Integer listingfeeExpireCount= shopMapper.listingfeeExpire(shopId);
         Integer receStatctCount=shopMapper.receStatct(shopId);
         Integer listingFee=0;
-        if(listingFeeCount>0 || listingfeeExpireCount<0){
+        if(listingFeeCount>0 || listingfeeExpireCount>0){
             if(receStatctCount<=0){
                 listingFee=1;
             }

+ 4 - 0
src/main/resources/mapper/MessageCenter.xml

@@ -18,4 +18,8 @@
         SELECT * FROM cm_coupon WHERE couponType=#{couponType} AND  NOW()>= startDate AND endDate >= NOW() AND status=1 AND delFlag=0
     </select>
 
+    <select id="contractMobile" resultType="java.lang.String">
+        SELECT contractMobile FROM club WHERE userID=#{UserID}
+    </select>
+
 </mapper>

+ 3 - 3
src/main/resources/mapper/ShopMapper.xml

@@ -706,15 +706,15 @@
     </select>
 
     <select id="listingFee" resultType="java.lang.Integer">
-        SELECT COUNT(1) FROM `cm_discern_receipt` WHERE newReceiptType=1 AND shopID=#{shopID}
+        SELECT COUNT(*) FROM `cm_discern_receipt` WHERE newReceiptType=1 AND shopID=#{shopID}
     </select>
 
     <select id="listingfeeExpire" resultType="java.lang.Integer">
-        SELECT COUNT(1) FROM `cm_discern_receipt` WHERE newReceiptType=1 AND shopID=#{shopID} AND NOW()> DATE_ADD(receiptDate, INTERVAL 1 YEAR)
+        SELECT COUNT(*) FROM `cm_discern_receipt` WHERE newReceiptType=1 AND shopID=#{shopID} AND NOW()> DATE_ADD(receiptDate, INTERVAL 1 YEAR)
     </select>
 
     <select id="receStatct" resultType="java.lang.Integer">
-        SELECT COUNT(1) FROM `cm_discern_receipt` WHERE newReceiptType=1 AND shopID=#{shopID} AND receStatct=2
+        SELECT COUNT(*) FROM `cm_discern_receipt` WHERE newReceiptType=1 AND shopID=#{shopID} AND receStatct=2
     </select>
 
 </mapper>