Ver Fonte

定时推送短信方法修改

dongbw há 7 anos atrás
pai
commit
63fe7606f4

+ 5 - 13
src/main/java/com/uas/ps/inquiry/SchedulingConfig.java

@@ -2,7 +2,6 @@ package com.uas.ps.inquiry;
 
 import com.uas.ps.core.util.CollectionUtils;
 import com.uas.ps.inquiry.dao.CronDao;
-import com.uas.ps.inquiry.entity.Inquiry;
 import com.uas.ps.inquiry.model.Cron;
 import com.uas.ps.inquiry.model.InquiryRemind;
 import com.uas.ps.inquiry.service.impl.InquiryServiceImpl;
@@ -17,7 +16,6 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
 import org.springframework.scheduling.support.CronTrigger;
 import org.springframework.util.StringUtils;
 
-import javax.annotation.Resource;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
@@ -52,21 +50,15 @@ public class SchedulingConfig implements SchedulingConfigurer {
             @Override
             public void run() {
                 Cron cron = cronDao.findOne(1L);
-                List<InquiryRemind> reminds = new ArrayList<InquiryRemind>();
+                List<InquiryRemind> reminds = new ArrayList<>();
                 logger.info("动态修改定时任务cron参数,当前时间:" + DATE_FORMAT.format(new Date()));
-                if(cron.getStartTime() != null){
-                    if(cron.getEndTime() != null){
-                        if(cron.getEnuu() != null){
-                            if(cron.getUseruu() != null){
-                                reminds = service.testMessage(cron.getStartTime(), cron.getEndTime(), cron.getUseruu(),cron.getEnuu());
-                            }
-                        }
-                    }
-
+                if(null != cron.getCron() && cron.getStartTime() != null && cron.getEndTime() != null && cron.getEnuu() != null && cron.getUseruu() != null){
+//                    reminds = service.testMessage(cron.getStartTime(), cron.getEndTime(), cron.getUseruu(),cron.getEnuu());
+                    reminds = service.findInquiryRemindGroupByVendUUAndVendUserUU(5);
                 }
 
                 if(!CollectionUtils.isEmpty(reminds)){
-                    service.sendMessage(reminds.size(),reminds);
+                    service.sendMessage(reminds);
                 }
             }
         }, new Trigger() {

+ 84 - 13
src/main/java/com/uas/ps/inquiry/controller/DeadlineTask.java

@@ -7,8 +7,9 @@ import com.uas.ps.inquiry.dao.EnterpriseDao;
 import com.uas.ps.inquiry.dao.PurcInquiryItemDao;
 import com.uas.ps.inquiry.entity.MessageModel;
 import com.uas.ps.inquiry.model.Enterprise;
+import com.uas.ps.inquiry.model.InquiryRemind;
 import com.uas.ps.inquiry.model.PublicInquiryItem;
-import com.uas.ps.inquiry.model.PurcInquiryItem;
+import com.uas.ps.inquiry.service.InquiryService;
 import com.uas.ps.inquiry.util.FlexJsonUtils;
 import com.uas.ps.inquiry.util.HttpUtil;
 import com.uas.ps.inquiry.util.ThreadUtils;
@@ -20,15 +21,14 @@ import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
-import java.util.Date;
+import java.util.Calendar;
 import java.util.List;
 
 /**
  * 求购截止,每日晚上轮询一次
  *
- * @param
+ * @version 2018年6月12日 15:00  询价短信定时发送的任务移到这里  dongbw
 
  */
 @Component
@@ -41,17 +41,41 @@ public class DeadlineTask {
     @Autowired
     private EnterpriseDao enterpriseDao;
 
+    @Autowired
+    private InquiryService inquiryService;
+
     private static final Logger log = Logger.getLogger(Logger.class);
 
-    /**
-     * 公共消息访问地址
-     */
-    private final String PS_MESSAGE_URL = ContextUtils.getBean(AccessConfiguration.class).getPsMessageUrl();
     /**
      * 消息类型,公共询价的采纳结果
      */
     private final String INQUIRY_DECIDE_TYPE_MALL = "商城公共询价采纳结果";
 
+    /**
+     * 接收应用
+     */
+    private final String CONSUMERAPP_B2B_MALL = "B2B,MALL";
+
+    /**
+     * 应用来源,主要是为了平台公共询价做处理
+     */
+    private String SOURCEAPP_MALL = "MALL";
+
+    /**
+     * 消息类型 (在B2B消息中拼接跳转单据详情url请求时需要)
+     */
+    private final String INQUIRY_TYPE = "公共询价";
+
+    /**
+     * 消息推送短信模板id
+     */
+    private final String SMS_TEMP_ID = "e6320a3c-89ac-4c77-a75f-62a727bce654";
+
+    /**
+     * 消息推送方式:邮件、短信、im
+     */
+    private final String SMS_TYPE = "MAIL_AND_SM_AND_IM";
+
     /**
      * 消费类型: 多个,MULTI
      */
@@ -60,12 +84,27 @@ public class DeadlineTask {
     /**
      * 接收应用
      */
-    private final String CONSUMERAPP = "B2B,MALL";
+    private final String CONSUMERAPP_HIDE = "HIDE";
 
     /**
-     * 应用来源,主要是为了平台公共询价做处理
+     * 消息类型 (在MALL消息中拼接求购询价待报价)
+     */
+    private final String INQUIRY_TYPE_SELLER_MALL = "MALL跳转卖家待报价页面";
+
+    /**
+     * 公共消息访问地址
+     */
+    private final String PS_MESSAGE_URL = ContextUtils.getBean(AccessConfiguration.class).getPsMessageUrl();
+
+    /**
+     * 询价统计通知发送人UU
+     */
+    private final Long MESSAGE_SENDERUU = ContextUtils.getBean(AccessConfiguration.class).getSenderuu();
+
+    /**
+     * 询价统计通知发送企业UU
      */
-    private String sourceApp = "MALL";
+    private final Long MESSAGE_SENDERENUU = ContextUtils.getBean(AccessConfiguration.class).getSenderEnuu();
 
     @Scheduled(cron =  "0 30 23 ? * *" )//每天23:30分执行
     public void execute(){
@@ -78,9 +117,9 @@ public class DeadlineTask {
                 String content= param.getInquiry().getEnName()+"关于“型号:"+ cmp +"“的求购【已截止】,您的报价尚未被采纳。";
                 MessageModel model = new MessageModel();
                 model.setType(INQUIRY_DECIDE_TYPE_MALL);
-                model.setProducerApp(sourceApp);
+                model.setProducerApp(SOURCEAPP_MALL);
                 model.setConsumerType(CUST_TYPE);
-                model.setConsumerApp(CONSUMERAPP);
+                model.setConsumerApp(CONSUMERAPP_B2B_MALL);
                 model.setContent(content);
                 model.setRemark(String.valueOf(param.getInquiry().getId()));
                 model.setSourceId(param.getId());
@@ -101,6 +140,7 @@ public class DeadlineTask {
 
         }
     }
+
     /**
      * 发送消息推送
      *
@@ -120,4 +160,35 @@ public class DeadlineTask {
             }
         }).run();
     }
+
+    /**
+     * 每早9点、14点、17点发送询价通知短信
+     */
+    @Scheduled(cron = "0 0 9,14,17 * * ?" )
+    private void sendMessageTiming() throws Exception {
+        Calendar now = Calendar.getInstance();
+        Integer hour = now.get(Calendar.HOUR_OF_DAY);
+        log.info(hour + "点定时统计询价单总数服务开启");
+        List<InquiryRemind> reminds = new ArrayList<>();
+        switch (hour) {
+            case 9 :
+                reminds = inquiryService. findInquiryRemindGroupByVendUUAndVendUserUU(16);
+                break;
+            case 14 :
+                reminds = inquiryService.findInquiryRemindGroupByVendUUAndVendUserUU(5);
+                break;
+            case 17 :
+                reminds = inquiryService.findInquiryRemindGroupByVendUUAndVendUserUU(3);
+                break;
+            default:
+                reminds = inquiryService.findInquiryRemindGroupByVendUUAndVendUserUU(5);
+                break;
+        }
+
+        if(!CollectionUtils.isEmpty(reminds)){
+            inquiryService.sendMessage(reminds);
+        }
+        log.info(hour + "点定时统计询价单总数服务结束");
+    }
+
 }

+ 2 - 1
src/main/java/com/uas/ps/inquiry/dao/CronDao.java

@@ -2,6 +2,7 @@ package com.uas.ps.inquiry.dao;
 
 import com.uas.ps.inquiry.model.Cron;
 import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 import org.springframework.stereotype.Repository;
 
 
@@ -9,6 +10,6 @@ import org.springframework.stereotype.Repository;
  * created by shicr on 2018/6/8
  **/
 @Repository
-public interface CronDao extends JpaRepository<Cron,Long>{
+public interface CronDao extends JpaRepository<Cron,Long>,JpaSpecificationExecutor<Cron> {
 
 }

+ 14 - 0
src/main/java/com/uas/ps/inquiry/model/InquiryRemind.java

@@ -180,6 +180,12 @@ public class InquiryRemind implements Serializable {
     @Transient
     private PublicInquiryItem quotation;
 
+    /**
+     * 发送消息时,记录条数
+     */
+    @Transient
+    private Integer counts;
+
     public String getInbrand() {
         return inbrand;
     }
@@ -374,4 +380,12 @@ public class InquiryRemind implements Serializable {
     public void setQuotation(PublicInquiryItem quotation) {
         this.quotation = quotation;
     }
+
+    public Integer getCounts() {
+        return counts;
+    }
+
+    public void setCounts(Integer counts) {
+        this.counts = counts;
+    }
 }

+ 14 - 1
src/main/java/com/uas/ps/inquiry/service/InquiryService.java

@@ -111,5 +111,18 @@ public interface InquiryService {
      * @param enuu
      * @return
      */
-    public List<InquiryRemind> testMessage(Date startTime, Date endTime, Long useruu, Long enuu);
+    List<InquiryRemind> testMessage(Date startTime, Date endTime, Long useruu, Long enuu);
+
+    /**
+     * 根据商机发送消息
+     * @param reminds 商机List
+     */
+    void sendMessage(List<InquiryRemind> reminds);
+
+    /**
+     * 以供应商企业UU和用户UU分组查询商机
+     * @param hours  查之前多少小时内的单据
+     * @return 商机List
+     */
+    List<InquiryRemind> findInquiryRemindGroupByVendUUAndVendUserUU(int hours);
 }

+ 117 - 132
src/main/java/com/uas/ps/inquiry/service/impl/InquiryServiceImpl.java

@@ -43,12 +43,13 @@ import com.uas.ps.inquiry.util.FlexJsonUtils;
 import com.uas.ps.inquiry.util.HttpUtil;
 import com.uas.ps.inquiry.util.ThreadUtils;
 import javassist.NotFoundException;
+import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Sort;
 import org.springframework.data.jpa.domain.Specification;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
 import org.springframework.jdbc.core.JdbcTemplate;
-import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 
@@ -56,7 +57,6 @@ import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Predicate;
 import javax.persistence.criteria.Root;
-import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashSet;
@@ -77,22 +77,6 @@ public class InquiryServiceImpl implements InquiryService {
      */
     private final String INQUIRY_TYPE = "公共询价";
 
-    /**
-     * 应用来源
-     */
-    private final String SOURCERAPP = "MALL";
-
-    /**
-     * 消息类型,公共询价的采纳结果
-     */
-    private final String INQUIRY_DECIDE_TYPE = "公共询价采纳结果";
-
-    /**
-     * 询价种类
-     */
-    private final String INQUIRYKIND = "publicInquiry";
-
-
     /**
      * 消息推送短信模板id
      */
@@ -104,19 +88,19 @@ public class InquiryServiceImpl implements InquiryService {
     private final String SMS_TYPE = "MAIL_AND_SM_AND_IM";
 
     /**
-     * 来源应用,ERP
+     * 消费类型: 多个,MULTI
      */
-    private final String PRODUCERAPP = "ERP";
+    private final String CUST_TYPE = "MULTI";
 
     /**
-     * 消费类型: 多个,MULTI
+     * 应用来源,主要是为了平台公共询价做处理
      */
-    private final String CUST_TYPE = "MULTI";
+    private String SOURCEAPP_MALL = "MALL";
 
     /**
      * 接收应用
      */
-    private final String CONSUMERAPP = "HIDE";
+    private final String CONSUMERAPP_HIDE = "HIDE";
 
     /**
      * 消息类型 (在MALL消息中拼接求购询价待报价)
@@ -185,6 +169,8 @@ public class InquiryServiceImpl implements InquiryService {
      */
     private final static InquiryBufferedLogger logger = BufferedLoggerManager.getLogger(InquiryBufferedLogger.class);
 
+    private static final Logger log = Logger.getLogger(Logger.class);
+
     /**
      * 查询公共询价列表信息
      *
@@ -345,121 +331,14 @@ public class InquiryServiceImpl implements InquiryService {
     /**
      * 可配置的定时任务
      */
+    @Override
     public List<InquiryRemind> testMessage(Date startTime, Date endTime, Long useruu, Long enuu){
         logger.log("公共询价","9点定时统计询价单总数服务开启");
         List<InquiryRemind> list = inquiryRemindDao.findTestInfo(startTime,endTime,useruu,enuu);
+        logger.log("公共询价","9点定时统计询价单总数服务结束");
         return list;
     }
 
-    /**
-     * 每早9点发送询价通知短信
-     */
-    @Scheduled(cron = "0 0 9 * * ?" )
-    private void sendMessage() throws Exception {
-            logger.log("公共询价","9点定时统计询价单总数服务开启");
-            List<BigInteger> list1 = inquiryRemindDao.findvenduseruu();
-            //找到询价推荐表中的所有公司
-            List<BigInteger> list2 = inquiryRemindDao.findvenduu();
-            for(BigInteger vendUserUU : list1){
-                for(BigInteger enuu : list2){
-                    //查出昨晚17点到今早九点的询价信息
-                    List<InquiryRemind> reminds = inquiryRemindDao.findInquiryRemind(vendUserUU,enuu);
-                    if(!CollectionUtils.isEmpty(reminds)){
-                            sendMessage(reminds.size(),reminds);
-                    }
-                }
-            }
-            logger.log("公共询价","9点定时统计询价单总数服务结束");
-    }
-
-
-    /**
-     * 发送消息推送
-     *
-     */
-    public void sendMessage(final Integer count, final List<InquiryRemind> reminds) {
-        ThreadUtils.task(new Runnable() {
-            @Override
-            public void run() {
-                try {
-                    List<MessageModel> models = new ArrayList<>();
-                    MessageModel model = new MessageModel();
-                    model.setType(INQUIRY_TYPE);
-                    model.setType(INQUIRY_TYPE_SELLER_MALL);
-                    model.setProducerApp("MALl");
-                    model.setConsumerType(CUST_TYPE);
-                    model.setConsumerApp(CONSUMERAPP);
-                    model.setRemark(String.valueOf(count));
-                    for(InquiryRemind remind : reminds){
-                        model.setReceiverEnuu(remind.getVendUU());
-                        model.setReceiverUu(remind.getVendUserUU());
-                        model.setSenderEnuu(MESSAGE_SENDERENUU);
-                        model.setSenderUu(MESSAGE_SENDERUU);
-                    }
-                    String company = "";
-                    Enterprise enterprise = enterpriseDao.findOne(reminds.get(0).getVendUU());
-                    if(null != enterprise){
-                        company = enterprise.getEnName();
-                    }
-                    String content = company + "新增了"+ count +"张公共询价单,快登录优软商城查看详情吧!https://www.usoftmall.com/vendor#/seekPurchase";
-                    model.setContent(content);
-                    model.setSmsType(SMS_TYPE);
-                    model.setSmTemplate(SMS_TEMP_ID);
-                    models.add(model);
-                    String res = HttpUtil.doPost(PS_MESSAGE_URL + "/messages", FlexJsonUtils.toJsonDeep(models));
-                    System.out.println(FlexJsonUtils.toJsonDeep(models));
-                    System.out.println(res);
-                    logger.log("公共询价","此次"+company+"公司新增"+count+"张公共询价");
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
-            }
-        }).run();
-    }
-
-
-    /**
-     *  每天下午2点发送询价通知短信
-     */
-    @Scheduled(cron = "0 0 14 * * ?" )
-    private void sendMessage2(){
-        logger.log("公共询价","14点定时统计询价单总数服务开始");
-        List<BigInteger> list1 = inquiryRemindDao.findvenduseruu();
-        //找到询价推荐表中的所有公司
-        List<BigInteger> list2 = inquiryRemindDao.findvenduu();
-        for(BigInteger vendUserUU : list1){
-            for(BigInteger enuu : list2){
-                //查出昨晚17点到今早九点的询价信息
-                List<InquiryRemind> reminds = inquiryRemindDao.findInquiryRemind1(vendUserUU,enuu);
-                if(!CollectionUtils.isEmpty(reminds)){
-                    sendMessage(reminds.size(),reminds);
-                }
-            }
-        }
-        logger.log("公共询价","14点定时统计询价单总数服务结束");
-    }
-
-    /**
-     * 每天下午5点发送询价通知短信
-     */
-    @Scheduled(cron = "0 0 17 * * ?" )
-    private void sendMessage3(){
-        logger.log("公共询价","17点定时统计询价单总数服务结束");
-        List<BigInteger> list1 = inquiryRemindDao.findvenduseruu();
-        //找到询价推荐表中的所有公司
-        List<BigInteger> list2 = inquiryRemindDao.findvenduu();
-        for(BigInteger vendUserUU : list1){
-            for(BigInteger enuu : list2){
-                //查出昨晚17点到今早九点的询价信息
-                List<InquiryRemind> reminds = inquiryRemindDao.findInquiryRemind2(vendUserUU,enuu);
-                if(!CollectionUtils.isEmpty(reminds)){
-                    sendMessage(reminds.size(),reminds);
-                }
-            }
-        }
-        logger.log("公共询价","17点定时统计询价单总数服务结束");
-    }
-
     /**
      * 发布询价成功后加入当前用户的个人物料库
      * @param inquiryItems
@@ -888,4 +767,110 @@ public class InquiryServiceImpl implements InquiryService {
         }
         return itemInfo;
     }
+
+    /**
+     * 发送消息推送
+     *
+     */
+    public void sendMessage(final Integer count, final List<InquiryRemind> reminds) {
+        ThreadUtils.task(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    List<MessageModel> models = new ArrayList<>();
+                    MessageModel model = new MessageModel();
+                    model.setType(INQUIRY_TYPE);
+                    model.setType(INQUIRY_TYPE_SELLER_MALL);
+                    model.setProducerApp("MALl");
+                    model.setConsumerType(CUST_TYPE);
+                    model.setConsumerApp(CONSUMERAPP_HIDE);
+                    model.setRemark(String.valueOf(count));
+                    for(InquiryRemind remind : reminds){
+                        model.setReceiverEnuu(remind.getVendUU());
+                        model.setReceiverUu(remind.getVendUserUU());
+                        model.setSenderEnuu(MESSAGE_SENDERENUU);
+                        model.setSenderUu(MESSAGE_SENDERUU);
+                    }
+                    String company = "";
+                    Enterprise enterprise = enterpriseDao.findOne(reminds.get(0).getVendUU());
+                    if(null != enterprise){
+                        company = enterprise.getEnName();
+                    }
+                    String content = company + "新增了"+ count +"张公共询价单,快登录优软商城查看详情吧!https://www.usoftmall.com/vendor#/seekPurchase";
+                    model.setContent(content);
+                    model.setSmsType(SMS_TYPE);
+                    model.setSmTemplate(SMS_TEMP_ID);
+                    models.add(model);
+                    String res = HttpUtil.doPost(PS_MESSAGE_URL + "/messages", FlexJsonUtils.toJsonDeep(models));
+                    System.out.println(FlexJsonUtils.toJsonDeep(models));
+                    System.out.println(res);
+                    logger.log("公共询价", "此次"+company+"公司新增"+count+"张公共询价");
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        }).run();
+    }
+
+    /**
+     * 发送消息推送
+     *
+     */
+    @Override
+    public void sendMessage(final List<InquiryRemind> reminds) {
+        ThreadUtils.task(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    log.info("发送消息开始");
+                    List<MessageModel> models = new ArrayList<>();
+                    for (InquiryRemind remind : reminds) {
+                        if (null != remind.getVendUU() && null != remind.getVendUserUU()) {
+                            Integer count = remind.getCounts();
+                            MessageModel model = new MessageModel();
+                            model.setType(INQUIRY_TYPE);
+                            model.setType(INQUIRY_TYPE_SELLER_MALL);
+                            model.setProducerApp(SOURCEAPP_MALL);
+                            model.setConsumerType(CUST_TYPE);
+                            model.setConsumerApp(CONSUMERAPP_HIDE);
+                            model.setRemark(String.valueOf(count));
+                            model.setReceiverEnuu(remind.getVendUU());
+                            model.setReceiverUu(remind.getVendUserUU());
+                            model.setSenderEnuu(MESSAGE_SENDERENUU);
+                            model.setSenderUu(MESSAGE_SENDERUU);
+                            String company = "";
+                            Enterprise enterprise = enterpriseDao.findOne(remind.getVendUU());
+                            if(null != enterprise){
+                                company = enterprise.getEnName();
+                            }
+                            String content = company + "新增了"+ count +"张公共询价单,快登录优软商城查看详情吧!https://www.usoftmall.com/vendor#/seekPurchase";
+                            model.setContent(content);
+                            model.setSmsType(SMS_TYPE);
+                            model.setSmTemplate(SMS_TEMP_ID);
+                            models.add(model);
+                            log.info("此次" + company + "公司新增" + remind.getCounts() + "张公共询价(发送信息前)");
+                        }
+                    }
+                    if (!org.springframework.util.CollectionUtils.isEmpty(models)) {
+                        String res = HttpUtil.doPost(PS_MESSAGE_URL + "/messages", FlexJsonUtils.toJsonDeep(models));
+                    }
+                    log.info("发送消息完成");
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        }).run();
+    }
+
+    /**
+     * 以供应商企业UU和用户UU分组查询商机
+     * @param hours  查之前多少小时内的单据
+     * @return 商机List
+     */
+    @Override
+    public List<InquiryRemind> findInquiryRemindGroupByVendUUAndVendUserUU(int hours) {
+        String sql = "select ir_venduu vendUU,ir_venduseruu vendUserUU, count(1) counts from purc$inquiry$remind where ir_date between date_sub(now(), interval "
+                + hours + " hour) and now() group by ir_venduu,ir_venduseruu";
+        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<InquiryRemind>(InquiryRemind.class));
+    }
 }