Jelajahi Sumber

【U审批】【自定义消息推送(定时任务形式)】

wuyx 5 tahun lalu
induk
melakukan
8644dddddd

+ 75 - 0
uas-office-qywx/src/main/java/com/usoftchina/uas/office/qywx/entity/UApprovalMsg.java

@@ -0,0 +1,75 @@
+package com.usoftchina.uas.office.qywx.entity;
+
+
+/**
+ * @author wuyx
+ * @date 2020/2/16
+ */
+public class UApprovalMsg {
+    private Integer id_;
+    private String type_;
+    private String title_;
+    private String msg_;
+    private String ems_;
+    private String url_;
+
+    public Integer getId_() {
+        return id_;
+    }
+
+    public void setId_(Integer id_) {
+        this.id_ = id_;
+    }
+
+    public String getType_() {
+        return type_;
+    }
+
+    public void setType_(String type_) {
+        this.type_ = type_;
+    }
+
+    public String getTitle_() {
+        return title_;
+    }
+
+    public void setTitle_(String title_) {
+        this.title_ = title_;
+    }
+
+    public String getMsg_() {
+        return msg_;
+    }
+
+    public void setMsg_(String msg_) {
+        this.msg_ = msg_;
+    }
+
+    public String getEms_() {
+        return ems_;
+    }
+
+    public void setEms_(String ems_) {
+        this.ems_ = ems_;
+    }
+
+    public String getUrl_() {
+        return url_;
+    }
+
+    public void setUrl_(String url_) {
+        this.url_ = url_;
+    }
+
+    @Override
+    public String toString() {
+        return "UApprovalMsg{" +
+                "id_='" + id_ + '\'' +
+                ", type_='" + type_ + '\'' +
+                ", title_='" + title_ + '\'' +
+                ", msg_='" + msg_ + '\'' +
+                ", ems_='" + ems_ + '\'' +
+                ", url_='" + url_ + '\'' +
+                '}';
+    }
+}

+ 25 - 0
uas-office-qywx/src/main/java/com/usoftchina/uas/office/qywx/service/UasUApprovalMsgService.java

@@ -0,0 +1,25 @@
+package com.usoftchina.uas.office.qywx.service;
+
+import com.usoftchina.uas.office.qywx.entity.UApprovalMsg;
+import com.usoftchina.uas.office.service.AbstractService;
+import org.springframework.stereotype.Service;
+
+import java.util.*;
+
+/**
+ * @author wuyx
+ * @date 2020/5/11
+ */
+@Service
+public class UasUApprovalMsgService extends AbstractService {
+    public List<UApprovalMsg> getAllMsg() {
+        return queryForBeanList("select * from UAPPROVALMSG where EMS_ IS NOT NULL AND TYPE_ IS NOT NULL", UApprovalMsg.class);
+    }
+    public void delMsg(){
+        jdbcTemplate.execute("delete UAPPROVALMSG where ID_ IN (SELECT ID_ FROM UAPPROVALMSG_HIS) ");
+    }
+    public void recodMsg(Integer id_,String status,String reason){
+        jdbcTemplate.execute("INSERT INTO UAPPROVALMSG_HIS (ID_, TYPE_, TITLE_, MSG_, EMS_, URL_,PUSHDATE_,PUSHSTATUS_,FAILREASON_) " +
+                " SELECT TT.*,SYSDATE,'"+status+"','"+reason+"' FROM UAPPROVALMSG TT WHERE TT.ID_ = "+id_);
+    }
+}

+ 77 - 0
uas-office-qywx/src/main/java/com/usoftchina/uas/office/qywx/task/QywxPushMsgTask.java

@@ -0,0 +1,77 @@
+package com.usoftchina.uas.office.qywx.task;
+
+import com.usoftchina.qywx.sdk.MessageSdk;
+import com.usoftchina.qywx.sdk.dto.SendMessageReq;
+import com.usoftchina.uas.office.entity.DataCenter;
+import com.usoftchina.uas.office.jdbc.DataSourceHolder;
+import com.usoftchina.uas.office.qywx.entity.UApprovalMsg;
+import com.usoftchina.uas.office.qywx.service.UasUApprovalMsgService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+
+/**
+ * @author wuyx
+ * @date 2020/5/11
+ */
+@Component
+public class QywxPushMsgTask {
+
+    @Autowired
+    private MessageSdk messageSdk;
+
+    @Autowired
+    private UasUApprovalMsgService uasUApprovalMsgService;
+
+    private final Logger logger = LoggerFactory.getLogger(QywxPushMsgTask.class);
+
+    /**
+     *U审批自定义消息推送
+     */
+    @Scheduled(cron="0 0/2 * * * ?")
+    public void pushUapprovalMsg() {
+        if (!messageSdk.isAgentEnabled("Uas")) {
+            return;
+        }
+        DataCenter dataCenter = DataCenter.INSTANCE;
+        if (null != dataCenter.getUsername() && null != dataCenter.getPassword() && null != dataCenter.getUrl()) {
+            try {
+                DataSourceHolder.set(dataCenter);
+                try {
+                    List<UApprovalMsg> uMsgList = uasUApprovalMsgService.getAllMsg();
+                    for(UApprovalMsg uMsg : uMsgList){
+                        SendMessageReq sendMsg = new SendMessageReq();
+                        if(uMsg.getType_().equals("MARKDOWN")){
+                            sendMsg.markdown(uMsg.getMsg_());
+                        }else if(uMsg.getType_().equals("TEXT")){
+                            sendMsg.text(uMsg.getMsg_());
+                        }else if(uMsg.getType_().equals("TEXTCARD")){
+                            sendMsg.textCard(uMsg.getTitle_(),uMsg.getMsg_(),uMsg.getUrl_(),"查看详情");
+                        }else {
+                            continue;
+                        }
+                        sendMsg.toUser(uMsg.getEms_().split(","));
+                        try {
+                            messageSdk.send("Uas", sendMsg);
+                            uasUApprovalMsgService.recodMsg(uMsg.getId_(),"SUCESS","");
+                            logger.info("send uas custom message sucess {}", uMsg);
+                        }catch (Exception e){
+                            uasUApprovalMsgService.recodMsg(uMsg.getId_(),"FAIL",(e.getMessage().length() >= 1000 ? e.getMessage().substring(0,1000) : e.getMessage()));
+                            throw e;
+                        }
+                    }
+                }catch (Exception e){
+                    logger.error("send uas custom message error", e);
+                }
+            } finally {
+                uasUApprovalMsgService.delMsg();
+                DataSourceHolder.clear();
+            }
+        }
+    }
+}