lifuquan 2 سال پیش
والد
کامیت
72dd939c7c

+ 3 - 0
doc/开发文档/任务特点分析.md

@@ -0,0 +1,3 @@
+# 任务特点分析
+
+<https://oapi.dingtalk.com/robot/send?access_token=b2f1424d6119affaacab614b184f043fcd2c73db2651bb86eff29992d66820bf>

BIN
doc/开发文档/样本数据/输入/数据表头说明.xlsx


+ 1 - 1
doc/开发文档/部署环境接口测试.md

@@ -1,6 +1,6 @@
 # 接口测试
 
-nohup java -jar tsl_data-1.1-exec.jar >output.log 2>&1 &
+nohup java -jar tsl_data-1.2-exec.jar >output.log 2>&1 &
 
 nohup java -jar dingtalk_auto-1.1-exec.jar >output.log 2>&1 &
 

+ 1 - 1
tsl_data/pom.xml

@@ -13,7 +13,7 @@
 
     <groupId>com.nokia</groupId>
     <artifactId>tsl_data</artifactId>
-    <version>1.1</version>
+    <version>1.2</version>
 
     <packaging>jar</packaging>
 

+ 14 - 0
tsl_data/src/main/java/com/nokia/common/codec/MD5Util.java

@@ -0,0 +1,14 @@
+package com.nokia.common.codec;
+
+import org.springframework.util.DigestUtils;
+
+public class MD5Util {
+
+    public static String encrypt(String src) {
+        return DigestUtils.md5DigestAsHex(src.getBytes());
+    }
+
+    public static String encode(String src) {
+        return encrypt(src);
+    }
+}

+ 225 - 0
tsl_data/src/main/java/com/nokia/common/message/pushmessage/PushMessageUtil.java

@@ -0,0 +1,225 @@
+package com.nokia.common.message.pushmessage;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.springframework.lang.Nullable;
+import org.springframework.web.client.RestTemplate;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.nokia.common.codec.MD5Util;
+
+import lombok.Builder;
+
+/**
+ * 使用RestTemplate调用能力商店钉钉消息推送服务API接口
+ */
+@Builder
+public class PushMessageUtil {
+    // api-url,不增加转发时应该时能力商店的url
+    private String url;
+    // 能力商店的appid
+    private String appId;
+    // 能力商店正式环境密钥
+    private String appSecret;
+    // 系统编号
+    private String systemId;
+    // 模块编号
+    private String moduleId;
+    // 业务编码
+    private String busiCode;
+    // 钉钉群机器人accessToken
+    private String accessToken;
+    private RestTemplate restTemplate;
+    @Builder.Default
+    private DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
+
+    /**
+     * 发送文本消息
+     * 
+     * @param message
+     */
+    public void sendTextMessage(String message) {
+        sendTextMessage(message, null, null);
+    }
+
+    /**
+     * 发送文本消息
+     * 
+     * @param message
+     * @param atList
+     */
+    public void sendTextMessage(String message, @Nullable List<Integer> atList) {
+        sendTextMessage(message, atList, null);
+    }
+
+    /**
+     * 发送文本消息
+     * 
+     * @param message
+     * @param atAll
+     */
+    public void sendTextMessage(String message, @Nullable Boolean atAll) {
+        sendTextMessage(message, null, atAll);
+    }
+
+    /**
+     * 发送文本消息
+     * 
+     * @param messge
+     * @param atList
+     * @param atAll
+     */
+    public void sendTextMessage(String messge, @Nullable List<Integer> atList, @Nullable Boolean atAll) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("MSG_TYPE", "text");
+        Map<String, Object> data = new HashMap<>();
+        map.put("DATA", data);
+        Map<String, Object> text = new HashMap<>();
+        data.put("TEXT", text);
+        text.put("CONTENT", messge);
+        if (atList != null) {
+            data.put("AT_LIST", atList);
+        }
+        if (atAll != null) {
+            data.put("AT_ALL", atAll);
+        }
+        sendMessage(map);
+    }
+
+    /**
+     * 发送markdown消息
+     * 
+     * @param messge
+     */
+    public void sendMarkdownMessage(String messge) {
+        sendMarkdownMessage(messge, null, null, null);
+    }
+
+    /**
+     * 发送markdown消息
+     * 
+     * @param messge
+     * @param title
+     */
+    public void sendMarkdownMessage(String messge, String title) {
+        sendMarkdownMessage(messge, title, null, null);
+    }
+
+    /**
+     * 发送markdown消息
+     * 
+     * @param messge
+     * @param atList
+     */
+    public void sendMarkdownMessage(String messge, List<Integer> atList) {
+        sendMarkdownMessage(messge, null, atList, null);
+    }
+
+    /**
+     * 发送markdown消息
+     * 
+     * @param messge
+     * @param atAll
+     */
+    public void sendMarkdownMessage(String messge, @Nullable Boolean atAll) {
+        sendMarkdownMessage(messge, null, null, atAll);
+    }
+
+    /**
+     * 发送markdown消息
+     * 
+     * @param messge
+     * @param title
+     * @param atList
+     * @param atAll
+     */
+    public void sendMarkdownMessage(String messge, @Nullable String title, @Nullable List<Integer> atList,
+            @Nullable Boolean atAll) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("MSG_TYPE", "markdown");
+        Map<String, Object> data = new HashMap<>();
+        map.put("DATA", data);
+        Map<String, Object> markdown = new HashMap<>();
+        data.put("MARK_DOWN", markdown);
+        title = title == null || "".equals(title) ? "markdown消息" : title;
+        markdown.put("TITLE", title);
+        markdown.put("TEXT", messge);
+        if (atList != null) {
+            data.put("AT_LIST", atList);
+        }
+        if (atAll != null) {
+            data.put("AT_ALL", atAll);
+        }
+        sendMessage(map);
+    }
+
+    private void sendMessage(Map<String, Object> reqMap) {
+        JsonNode respon = restTemplate.postForObject(url, getRequest(reqMap), JsonNode.class);
+        if (respon == null) {
+            throw new RuntimeException("调用结果为空");
+        }
+        JsonNode node = respon.get("UNI_BSS_HEAD");
+        if ("00000".equals(node.get("RESP_CODE").textValue())) {
+            node = respon.get("UNI_BSS_BODY").get("PUSH_MESSAGE_RSP");
+            if (node != null && "0000".equals(node.get("STATUS").textValue())) {
+                node = node.get("RSP");
+                if (node != null && "0000".equals(node.get("RSP_CODE").textValue())) {
+                    // 只有到这里才说明调用成功了
+                    return;
+                }
+            }
+        }
+        throw new RuntimeException(respon.toString());
+    }
+
+    private Map<String, Object> getRequest(Map<String, Object> reqMap) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("UNI_BSS_HEAD", getUniBssHead());
+        map.put("UNI_BSS_BODY", getUniBssBody(reqMap));
+        return map;
+    }
+
+    private Map<String, Object> getUniBssBody(Map<String, Object> reqMap) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("PUSH_MESSAGE_REQ", reqMap);
+        reqMap.put("ACCESS_TOKEN", accessToken);
+        reqMap.put("BUSI_CODE", busiCode);
+        reqMap.put("MODULE_ID", moduleId);
+        reqMap.put("SYSTEM_ID", systemId);
+        return map;
+    }
+
+    private Map<String, Object> getUniBssHead() {
+        Map<String, Object> map = new HashMap<>();
+        Date date = new Date();
+        String timeStamp = getTimeStamp(date);
+        String transId = getTransId(date);
+        map.put("APP_ID", appId);
+        map.put("TIMESTAMP", timeStamp);
+        map.put("TRANS_ID", transId);
+        map.put("TOKEN", getToken(timeStamp, transId));
+        return map;
+    }
+
+    private String getToken(String timeStamp, String transId) {
+        String beforeEncode = "APP_ID" + appId + "TIMESTAMP" + timeStamp + "TRANS_ID" + transId + appSecret;
+        return MD5Util.encode(beforeEncode);
+    }
+
+    private String getTransId(Date date) {
+        return (new SimpleDateFormat("yyyyMMddHHmmssSSS" + getRandomNumber())).format(date);
+    }
+
+    private String getTimeStamp(Date date) {
+        return dateFormat1.format(date);
+    }
+
+    private int getRandomNumber() {
+        return (int) ((Math.random() * 9D + 1D) * 100000D);
+    }
+}

+ 41 - 0
tsl_data/src/main/java/com/nokia/tsl_data/config/PushMessageUtilConfig.java

@@ -0,0 +1,41 @@
+package com.nokia.tsl_data.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+
+import com.nokia.common.message.pushmessage.PushMessageUtil;
+
+@Configuration
+public class PushMessageUtilConfig {
+
+    // api-url,不增加转发时应该时能力商店的url
+    private String url = "http://10.244.18.105:8000/api/chinaUnicom/microservice/notice/pushMessage/v1";
+    // 能力商店的appid
+    private String appId = "ENWaB7YdUD";
+    // 能力商店正式环境密钥
+    private String appSecret = "oz4OgKBaMNwi4LWfLPbhrPbbuCS8T0Rb";
+    // 系统编号
+    private String systemId = "10000078";
+    // 模块编号
+    private String moduleId = "20000156";
+    // 业务编码
+    private String busiCode = "30000111";
+    // 钉钉群机器人accessToken
+    // https://oapi.dingtalk.com/robot/send?access_token=b2f1424d6119affaacab614b184f043fcd2c73db2651bb86eff29992d66820bf
+    private String accessToken = "b2f1424d6119affaacab614b184f043fcd2c73db2651bb86eff29992d66820bf";
+
+    @Bean
+    public PushMessageUtil pushMessageUtil(RestTemplate restTemplate) {
+        return PushMessageUtil.builder()
+                .url(url)
+                .appId(appId)
+                .appSecret(appSecret)
+                .systemId(systemId)
+                .moduleId(moduleId)
+                .busiCode(busiCode)
+                .accessToken(accessToken)
+                .restTemplate(restTemplate)
+                .build();
+    }
+}

+ 14 - 0
tsl_data/src/main/java/com/nokia/tsl_data/config/RestTemplateConfig.java

@@ -0,0 +1,14 @@
+package com.nokia.tsl_data.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+
+@Configuration
+public class RestTemplateConfig {
+
+    @Bean
+    public RestTemplate restTemplate() {
+        return new RestTemplate();
+    }
+}

+ 33 - 1
tsl_data/src/main/java/com/nokia/tsl_data/service/TslTaskService.java

@@ -21,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional;
 
 import com.nokia.common.excel.entity.CellRect;
 import com.nokia.common.excel.poi.PoiUtil;
+import com.nokia.common.message.pushmessage.PushMessageUtil;
 import com.nokia.tsl_data.dao.TslDao;
 
 import lombok.extern.slf4j.Slf4j;
@@ -35,16 +36,19 @@ public class TslTaskService {
     private final TslReportService tslReportService;
     private final TslWaraHouseService tslWaraHouseService;
     private final TslDao tslDao;
+    private final PushMessageUtil pushMessageUtil;
 
     @Value("${tslTask.outputPath}")
     private String outputPath;
 
     private String outputFileName = "投诉清单各地市投诉率";
 
-    public TslTaskService(TslReportService tslReportService, TslWaraHouseService tslWaraHouseService, TslDao tslDao) {
+    public TslTaskService(TslReportService tslReportService, TslWaraHouseService tslWaraHouseService, TslDao tslDao,
+            PushMessageUtil pushMessageUtil) {
         this.tslReportService = tslReportService;
         this.tslWaraHouseService = tslWaraHouseService;
         this.tslDao = tslDao;
+        this.pushMessageUtil = pushMessageUtil;
     }
 
     /**
@@ -62,18 +66,46 @@ public class TslTaskService {
                         .format(new Date(System.currentTimeMillis() - 1000L * 3600 * 24));
 
                 try {
+                    // 检查数据源
+                    checkSrcExsit(day);
+                    // 数据入库
                     dataWaraHouseTask(day);
+                    // 生成xlsx
                     reportGenerateTask(day);
+                    // 截图
                     screenShotTask(day);
                 } catch (Exception e) {
                     log.error("定时任务出错--{}", e.getMessage());
                     e.printStackTrace();
+                    pushMessageUtil.sendTextMessage("定时任务出错--" + e.getMessage());
                 }
             }
 
         };
     }
 
+    /**
+     * 检查源文件是否存在,不存在发送提醒,等待1小时后再次检查
+     * 
+     * @param day
+     */
+    public void checkSrcExsit(String day) {
+        boolean flag = false;
+        do {
+            String checkResult = tslWaraHouseService.check(day);
+            if (checkResult == null || !checkResult.endsWith("数据源正常。")) {
+                pushMessageUtil.sendTextMessage(checkResult);
+                // 等待1小时后再次检查
+                try {
+                    Thread.sleep(1000L * 3600L);
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                    throw new RuntimeException(e.getMessage());
+                }
+            }
+        } while (flag);
+    }
+
     /**
      * 报表生成任务
      *