Browse Source

增加一个短信定时可配置接口

scr 7 years ago
parent
commit
b005122b5f

+ 88 - 0
src/main/java/com/uas/ps/inquiry/SchedulingConfig.java

@@ -0,0 +1,88 @@
+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;
+import org.apache.log4j.Logger;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.Trigger;
+import org.springframework.scheduling.TriggerContext;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.scheduling.annotation.SchedulingConfigurer;
+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;
+import java.util.List;
+
+/**
+ * created by shicr on 2018/6/8
+ **/
+@Configuration
+@EnableScheduling
+public class SchedulingConfig implements SchedulingConfigurer {
+
+    private final Logger logger = Logger.getLogger(Logger.class);
+
+    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss");
+
+    @Autowired
+    private CronDao cronDao;
+
+    private String cornStr = "";
+
+    @Autowired
+    private InquiryServiceImpl service;
+
+    @Override
+    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
+
+
+        scheduledTaskRegistrar.addTriggerTask(new Runnable() {
+
+
+            @Override
+            public void run() {
+                Cron cron = cronDao.findOne(1L);
+                List<InquiryRemind> reminds = new ArrayList<InquiryRemind>();
+                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(!CollectionUtils.isEmpty(reminds)){
+                    service.sendMessage(reminds.size(),reminds);
+                }
+            }
+        }, new Trigger() {
+            @Override
+            public Date nextExecutionTime(TriggerContext triggerContext) {
+                Cron cron = cronDao.findOne(1L);
+                if (cron != null && !StringUtils.isEmpty(cron.getCron())) {
+                    cornStr = cron.getCron();
+                }
+                CronTrigger trigger = new CronTrigger(cron.getCron());
+                Date nextExecDate = trigger.nextExecutionTime(triggerContext);
+                logger.info("下次执行自动推送时间: " + DATE_FORMAT.format(nextExecDate));
+                return nextExecDate;
+            }
+        });
+    }
+
+
+}

+ 14 - 0
src/main/java/com/uas/ps/inquiry/dao/CronDao.java

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

+ 34 - 19
src/main/java/com/uas/ps/inquiry/dao/InquiryRemindDao.java

@@ -10,6 +10,7 @@ import org.springframework.stereotype.Repository;
 
 import javax.transaction.Transactional;
 import java.math.BigInteger;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -24,15 +25,15 @@ public interface InquiryRemindDao extends JpaRepository<InquiryRemind, Long>, Jp
      * 通过vendUU和公共询价明细id更新该单据的状态
      *
      * @param sourceId 公共询价明细id
-     * @param value 单据状态
-     *              <note>
-     *                  {@code 100 在录入}
-     *                  {@code 101 已提交}
-     *                  {@code 102 已审核}
-     *                  {@code 103 未通过}
-     *                  {@code 104 已通过}
-     *              </note>
-     * @param vendUU 被推荐的企业
+     * @param value    单据状态
+     *                 <note>
+     *                 {@code 100 在录入}
+     *                 {@code 101 已提交}
+     *                 {@code 102 已审核}
+     *                 {@code 103 未通过}
+     *                 {@code 104 已通过}
+     *                 </note>
+     * @param vendUU   被推荐的企业
      */
     @Transactional
     @Modifying
@@ -43,45 +44,59 @@ public interface InquiryRemindDao extends JpaRepository<InquiryRemind, Long>, Jp
      * 通过被推荐的企业UU,个人UU,来源id判断单据是否存在
      *
      * @param vendUserUU 推荐的企业UU
-     * @param vendUU 个人UU
-     * @param itemId 来源id
+     * @param vendUU     个人UU
+     * @param itemId     来源id
      */
     List<InquiryRemind> findByVendUserUUAndVendUUAndItemId(Long vendUserUU, Long vendUU, Long itemId);
 
     /**
      * 查出昨晚17点到今早九点的询价信息
+     *
      * @param venduseruu
      * @param venduu
      */
-    @Query(value = "select * from purc$inquiry$remind where ir_date > date_add(now(),interval -16 HOUR) and ir_date < now() and ir_venduseruu =?1 and ir_venduu = ?2 and ir_enuu != ir_venduu",nativeQuery = true)
-    List<InquiryRemind> findInquiryRemind(BigInteger venduseruu,BigInteger venduu);
+    @Query(value = "select * from purc$inquiry$remind where ir_date > date_add(now(),interval -16 HOUR) and ir_date < now() and ir_venduseruu =?1 and ir_venduu = ?2 and ir_enuu != ir_venduu", nativeQuery = true)
+    List<InquiryRemind> findInquiryRemind(BigInteger venduseruu, BigInteger venduu);
 
     /**
      * 查出今早9点到14点的询价信息
+     *
      * @param venduseruu
      * @param venduu
      */
-    @Query(value = "select * from purc$inquiry$remind where ir_date > date_add(now(),interval -5 hour) and ir_date < now() and ir_venduseruu =?1 and ir_venduu = ?2  and ir_enuu != ir_venduu ",nativeQuery = true)
-    List<InquiryRemind> findInquiryRemind1(BigInteger venduseruu,BigInteger venduu);
+    @Query(value = "select * from purc$inquiry$remind where ir_date > date_add(now(),interval -5 hour) and ir_date < now() and ir_venduseruu =?1 and ir_venduu = ?2  and ir_enuu != ir_venduu ", nativeQuery = true)
+    List<InquiryRemind> findInquiryRemind1(BigInteger venduseruu, BigInteger venduu);
 
     /**
      * 查出14点到17点的询价信息
+     *
      * @param venduseruu
      * @param venduu
      */
-    @Query(value = "select * from purc$inquiry$remind where ir_date > date_add(now(),interval -3 hour) and ir_date < now() and ir_venduseruu =?1 and ir_venduu = ?2 and ir_enuu != ir_venduu",nativeQuery = true)
-    List<InquiryRemind> findInquiryRemind2(BigInteger venduseruu,BigInteger venduu);
+    @Query(value = "select * from purc$inquiry$remind where ir_date > date_add(now(),interval -3 hour) and ir_date < now() and ir_venduseruu =?1 and ir_venduu = ?2 and ir_enuu != ir_venduu", nativeQuery = true)
+    List<InquiryRemind> findInquiryRemind2(BigInteger venduseruu, BigInteger venduu);
 
     /**
      * 找到询价推荐表中所有的用户
      */
-    @Query(value = "select ir_venduseruu from purc$inquiry$remind group by ir_venduseruu",nativeQuery = true)
+    @Query(value = "select ir_venduseruu from purc$inquiry$remind group by ir_venduseruu", nativeQuery = true)
     List<BigInteger> findvenduseruu();
 
     /**
      * 找到询价推荐表中的所有公司
      */
-    @Query(value = "select ir_venduu from purc$inquiry$remind group by ir_venduu",nativeQuery = true)
+    @Query(value = "select ir_venduu from purc$inquiry$remind group by ir_venduu", nativeQuery = true)
     List<BigInteger> findvenduu();
 
+    /**
+     * 可配置测试接口
+     * @param startTime
+     * @param endTime
+     * @param useruu
+     * @param enuu
+     * @return
+     */
+    @Query(value = "select * from purc$inquiry$remind where ir_date between ?1 and ?2 and ir_venduseruu =?3 and ir_venduu = ?4",nativeQuery = true)
+    List<InquiryRemind> findTestInfo(Date startTime, Date endTime, Long useruu, Long enuu);
+
 }

+ 98 - 0
src/main/java/com/uas/ps/inquiry/model/Cron.java

@@ -0,0 +1,98 @@
+package com.uas.ps.inquiry.model;
+
+
+import javax.persistence.*;
+import java.util.Date;
+
+/**
+ * created by shicr on 2018/6/8
+ **/
+@Entity
+@Table(name = "cron")
+public class Cron {
+
+    @Id
+    @Column(name = "cr_id")
+    @GeneratedValue(strategy = GenerationType.AUTO)
+    private Long id;
+
+    /**
+     * 任务定时
+     */
+    @Column(name = "cr_cron")
+    private String cron;
+
+    /**
+     * 备注
+     */
+    @Column(name = "cr_remark")
+    private String remark;
+
+    @Column(name = "useruu")
+    private Long useruu;
+
+    @Column(name = "enuu")
+    private Long enuu;
+
+    @Column(name = "startTime")
+    private Date startTime;
+
+    @Column(name = "endTime")
+    private Date endTime;
+
+    public Long getUseruu() {
+        return useruu;
+    }
+
+    public void setUseruu(Long useruu) {
+        this.useruu = useruu;
+    }
+
+    public Long getEnuu() {
+        return enuu;
+    }
+
+    public void setEnuu(Long enuu) {
+        this.enuu = enuu;
+    }
+
+    public Date getStartTime() {
+        return startTime;
+    }
+
+    public void setStartTime(Date startTime) {
+        this.startTime = startTime;
+    }
+
+    public Date getEndTime() {
+        return endTime;
+    }
+
+    public void setEndTime(Date endTime) {
+        this.endTime = endTime;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getCron() {
+        return cron;
+    }
+
+    public void setCron(String cron) {
+        this.cron = cron;
+    }
+
+    public String getRemark() {
+        return remark;
+    }
+
+    public void setRemark(String remark) {
+        this.remark = remark;
+    }
+}

+ 5 - 0
src/main/java/com/uas/ps/inquiry/model/InquiryLog.java

@@ -195,4 +195,9 @@ public class InquiryLog extends BufferedLogable implements Serializable {
         this.date = System.currentTimeMillis();
         this.source = source;
     }
+
+    public InquiryLog(String title, String message){
+        this.title = title;
+        this.message = message;
+    }
 }

+ 13 - 0
src/main/java/com/uas/ps/inquiry/service/InquiryService.java

@@ -8,6 +8,9 @@ import com.uas.ps.inquiry.page.SearchFilter;
 import javassist.NotFoundException;
 import org.springframework.data.domain.Page;
 
+import java.util.Date;
+import java.util.List;
+
 /**
  * 转询价报价后的数据查询
  *
@@ -99,4 +102,14 @@ public interface InquiryService {
      * @return
      */
     PurcInquiryItemInfo findQuotationById(Long id);
+
+    /**
+     * 测试消息
+     * @param startTime
+     * @param endTime
+     * @param useruu
+     * @param enuu
+     * @return
+     */
+    public List<InquiryRemind> testMessage(Date startTime, Date endTime, Long useruu, Long enuu);
 }

+ 18 - 5
src/main/java/com/uas/ps/inquiry/service/impl/InquiryServiceImpl.java

@@ -35,7 +35,6 @@ import javassist.NotFoundException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 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;
@@ -303,12 +302,21 @@ public class InquiryServiceImpl implements InquiryService {
         }
     }
 
+    /**
+     * 可配置的定时任务
+     */
+    public List<InquiryRemind> testMessage(Date startTime, Date endTime, Long useruu, Long enuu){
+        logger.log("公共询价","9点定时统计询价单总数服务开启");
+        List<InquiryRemind> list = inquiryRemindDao.findTestInfo(startTime,endTime,useruu,enuu);
+        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();
@@ -321,6 +329,7 @@ public class InquiryServiceImpl implements InquiryService {
                     }
                 }
             }
+            logger.log("公共询价","9点定时统计询价单总数服务结束");
     }
 
 
@@ -328,7 +337,7 @@ public class InquiryServiceImpl implements InquiryService {
      * 发送消息推送
      *
      */
-    private void sendMessage(final Integer count, final List<InquiryRemind> reminds) {
+    public void sendMessage(final Integer count, final List<InquiryRemind> reminds) {
         ThreadUtils.task(new Runnable() {
             @Override
             public void run() {
@@ -358,7 +367,9 @@ public class InquiryServiceImpl implements InquiryService {
                     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();
                 }
@@ -372,7 +383,7 @@ public class InquiryServiceImpl implements InquiryService {
      */
     @Scheduled(cron = "0 0 14 * * ?" )
     private void sendMessage2(){
-
+        logger.log("公共询价","14点定时统计询价单总数服务开始");
         List<BigInteger> list1 = inquiryRemindDao.findvenduseruu();
         //找到询价推荐表中的所有公司
         List<BigInteger> list2 = inquiryRemindDao.findvenduu();
@@ -385,6 +396,7 @@ public class InquiryServiceImpl implements InquiryService {
                 }
             }
         }
+        logger.log("公共询价","14点定时统计询价单总数服务结束");
     }
 
     /**
@@ -392,7 +404,7 @@ public class InquiryServiceImpl implements InquiryService {
      */
     @Scheduled(cron = "0 0 17 * * ?" )
     private void sendMessage3(){
-
+        logger.log("公共询价","17点定时统计询价单总数服务结束");
         List<BigInteger> list1 = inquiryRemindDao.findvenduseruu();
         //找到询价推荐表中的所有公司
         List<BigInteger> list2 = inquiryRemindDao.findvenduu();
@@ -405,6 +417,7 @@ public class InquiryServiceImpl implements InquiryService {
                 }
             }
         }
+        logger.log("公共询价","17点定时统计询价单总数服务结束");
     }
 
     /**

+ 9 - 0
src/main/java/com/uas/ps/inquiry/support/InquiryBufferedLogger.java

@@ -29,4 +29,13 @@ public class InquiryBufferedLogger extends BufferedLogger<InquiryLog> {
     public void log(String title, String message, String detail, Long userUU, Long enUU, String source) {
         log(new InquiryLog(title, message, detail, userUU, enUU, source));
     }
+
+    /**
+     * 用户操作日志
+     * @param title
+     * @param message
+     */
+    public void log(String title, String message){
+        log(new InquiryLog(title,message));
+    }
 }