浏览代码

还差钉钉发送的定时任务

lifuquan 2 年之前
父节点
当前提交
5e8fa62223

+ 94 - 6
dingtalk_auto/src/main/java/com/nokia/common/dingtalk/DingTalkUtil.java

@@ -52,7 +52,7 @@ public class DingTalkUtil {
     /*
      * 上传文件,返回mediaid
      */
-    public String upload(String filePath) throws DingTalkApiException {
+    public String upload(String filePath, String type) throws DingTalkApiException {
         String url = "https://oapi.dingtalk.com/media/upload?access_token={0}";
         HttpHeaders httpHeaders = new HttpHeaders();
         httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
@@ -60,7 +60,7 @@ public class DingTalkUtil {
         MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
         FileSystemResource fileSystemResource = new FileSystemResource(filePath);
         multiValueMap.add("media", fileSystemResource);
-        multiValueMap.add("type", "image");
+        multiValueMap.add("type", type);
 
         HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(multiValueMap, httpHeaders);
 
@@ -84,15 +84,20 @@ public class DingTalkUtil {
         }
     }
 
-    /*
-     * 使用钉钉机器人发送消息
+    /**
+     * 使用钉钉机器人发送图片消息
+     * 
+     * @param msgParam
+     * @throws DingTalkApiException
      */
-    public void sendMsgWithRobot(Object msgParam) throws DingTalkApiException {
+    public void sendImageMsgWithRobot(String mediaId) throws DingTalkApiException {
         String url = "https://api.dingtalk.com/v1.0/robot/groupMessages/send";
 
         HttpHeaders httpHeaders = new HttpHeaders();
         httpHeaders.setContentType(MediaType.APPLICATION_JSON);
         httpHeaders.set("x-acs-dingtalk-access-token", getApiToken());
+        Map<String, Object> msgParam = new HashMap<>();
+        msgParam.put("photoURL", mediaId);
 
         Map<String, Object> request = new HashMap<>();
         try {
@@ -114,7 +119,90 @@ public class DingTalkUtil {
                 }
             }
         } catch (JsonProcessingException e) {
-            throw new DingTalkApiException("入参格式错误--" + msgParam.toString());
+            throw new DingTalkApiException("JSON转化失败--" + msgParam);
+        }
+    }
+
+    /**
+     * 使用钉钉机器人发送File消息
+     * 
+     * @param mediaId
+     * @param fileName
+     * @throws DingTalkApiException
+     */
+    public void sendFileMsgWithRobot(String mediaId, String fileName) throws DingTalkApiException {
+        String url = "https://api.dingtalk.com/v1.0/robot/groupMessages/send";
+
+        HttpHeaders httpHeaders = new HttpHeaders();
+        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
+        httpHeaders.set("x-acs-dingtalk-access-token", getApiToken());
+
+        Map<String, Object> msgParam = new HashMap<>();
+        msgParam.put("mediaId", mediaId);
+        msgParam.put("fileName", fileName);
+        msgParam.put("fileType", fileName.substring(fileName.lastIndexOf('.') + 1));
+
+        Map<String, Object> request = new HashMap<>();
+        try {
+            request.put("msgParam", objectMapper.writeValueAsString(msgParam));
+            request.put("msgKey", "sampleFile");
+            request.put("openConversationId", conversationId);
+            request.put("robotCode", appKey);
+
+            HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(request, httpHeaders);
+
+            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
+
+            if (responseEntity.getStatusCode().is2xxSuccessful()) {
+                JsonNode node = objectMapper.readTree(responseEntity.getBody());
+                if (node.get("processQueryKey") != null) {
+                    return;
+                } else {
+                    throw new DingTalkApiException("api返回格式有误--" + responseEntity.getBody());
+                }
+            }
+        } catch (JsonProcessingException e) {
+            throw new DingTalkApiException("入参格式错误--" + msgParam);
+        }
+    }
+
+    /**
+     * 使用钉钉机器人发送文字消息
+     * 
+     * @param msg
+     * @throws DingTalkApiException
+     */
+    public void sendTextMsgWithRobot(String msg) throws DingTalkApiException {
+        String url = "https://api.dingtalk.com/v1.0/robot/groupMessages/send";
+
+        HttpHeaders httpHeaders = new HttpHeaders();
+        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
+        httpHeaders.set("x-acs-dingtalk-access-token", getApiToken());
+
+        Map<String, Object> msgParam = new HashMap<>();
+        msgParam.put("content", msg);
+
+        Map<String, Object> request = new HashMap<>();
+        try {
+            request.put("msgParam", objectMapper.writeValueAsString(msgParam));
+            request.put("msgKey", "sampleText");
+            request.put("openConversationId", conversationId);
+            request.put("robotCode", appKey);
+
+            HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(request, httpHeaders);
+
+            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
+
+            if (responseEntity.getStatusCode().is2xxSuccessful()) {
+                JsonNode node = objectMapper.readTree(responseEntity.getBody());
+                if (node.get("processQueryKey") != null) {
+                    return;
+                } else {
+                    throw new DingTalkApiException("api返回格式有误--" + responseEntity.getBody());
+                }
+            }
+        } catch (JsonProcessingException e) {
+            throw new DingTalkApiException("入参格式错误--" + msgParam);
         }
     }
 

+ 1 - 1
dingtalk_auto/src/main/java/com/nokia/common/dingtalk/exception/DingTalkApiException.java

@@ -1,6 +1,6 @@
 package com.nokia.common.dingtalk.exception;
 
-public class DingTalkApiException extends Throwable {
+public class DingTalkApiException extends Exception {
 
     public DingTalkApiException(String msg) {
         super(msg);

+ 30 - 16
dingtalk_auto/src/main/java/com/nokia/dingtalk_auto/service/TslTaskService.java

@@ -5,10 +5,7 @@ import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
-
 import javax.annotation.Resource;
 
 import org.springframework.beans.factory.annotation.Value;
@@ -68,32 +65,49 @@ public class TslTaskService {
     @Resource
     private ObjectMapper objectMapper;
 
+    private DingTalkUtil dingTalkUtil;
+    private boolean isDingTalkUtilInited = false;
+
     // 完成指定日期的发送任务
-    public void runTask(String day) {
+    public void runTask(String day) throws DingTalkApiException, Exception {
         // 1. 下载文件
         download(day);
-        // 2. 发送文件
+        // 2. 生成要发送的内容
+        String message = getFilesToSend(day);
+        // 3. 发送文件
         sendDingTalkMsg(day);
+        // 4. 发送消息
+        if (!isDingTalkUtilInited) {
+            initDingTalkUtil();
+        }
+        dingTalkUtil.sendTextMsgWithRobot(message);
     }
 
-    public void sendDingTalkMsg(String day) {
-        DingTalkUtil dingTalkUtil = new DingTalkUtil(appKey, appSecret, openConversationId, restTemplate,
-                redisTemplate, objectMapper);
+    public void sendDingTalkMsg(String day) throws DingTalkApiException {
+        if (!isDingTalkUtilInited) {
+            initDingTalkUtil();
+        }
         String localPath = localDir + "/" + day;
         File[] files = new File(localPath).listFiles();
         for (File file : files) {
-            try {
-                String mediaId = dingTalkUtil.upload(file.getAbsolutePath());
-                Map<String, Object> map = new HashMap<>();
-                map.put("photoURL", mediaId);
-                dingTalkUtil.sendMsgWithRobot(map);
-            } catch (DingTalkApiException e) {
-                // TODO Auto-generated catch block
-                e.printStackTrace();
+            if (file.getName().toLowerCase().endsWith(".png")) {
+                String mediaId = dingTalkUtil.upload(file.getAbsolutePath(), "image");
+                dingTalkUtil.sendImageMsgWithRobot(mediaId);
+            } else if (file.getName().toLowerCase().endsWith(".xlsx")) {
+                String mediaId = dingTalkUtil.upload(file.getAbsolutePath(), "file");
+                dingTalkUtil.sendFileMsgWithRobot(mediaId, file.getName());
             }
         }
     }
 
+    private void initDingTalkUtil() {
+        if (!isDingTalkUtilInited) {
+            dingTalkUtil = new DingTalkUtil(appKey, appSecret, openConversationId, restTemplate,
+                    redisTemplate, objectMapper);
+            isDingTalkUtilInited = true;
+        }
+    }
+
     /**
      * 截图,组织一段话
      * 

+ 0 - 25
dingtalk_auto/src/test/java/com/nokia/common/jsch/SftpUtilTest.java

@@ -1,25 +0,0 @@
-package com.nokia.common.jsch;
-
-import java.io.IOException;
-import java.util.List;
-
-import org.junit.jupiter.api.Test;
-
-import com.jcraft.jsch.JSchException;
-import com.jcraft.jsch.SftpException;
-
-public class SftpUtilTest {
-    @Test
-    void testGet() throws IOException, JSchException, SftpException {
-        SftpUtil sftpUtil = new SftpUtil()
-                .setHost("133.96.94.105")
-                .setPort(22)
-                .setUser("do")
-                .setPassword("Richr00t");
-        // sftpUtil.get("/data/report_auto/test/1_客服投诉清单各地市投诉率情况.png", "D:\\aaa\\1_客服投诉清单各地市投诉率情况.png");
-        sftpUtil.connect();
-        List<String> ls = sftpUtil.ls("/data/report_auto/test");
-        sftpUtil.disconnect();
-        System.out.println(ls.toString());
-    }
-}

+ 5 - 10
dingtalk_auto/src/test/java/com/nokia/dingtalk_auto/service/task/TslTaskServiceTest.java

@@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
+import com.nokia.common.dingtalk.exception.DingTalkApiException;
 import com.nokia.dingtalk_auto.service.TslTaskService;
 
 @SpringBootTest
@@ -12,16 +13,10 @@ public class TslTaskServiceTest {
     private TslTaskService tslTaskService;
 
     @Test
-    void test() throws Exception {
+    void test() throws Exception, DingTalkApiException {
         // tslTaskService.download("20230426");
-        // 2023年3月截至26日移动网投诉情况统计:管理端-移网质量类:投诉率:石家庄、承德、雄安、张家口、沧州、全省、衡水未达到目标值,
-        // 石家庄、承德、雄安排名靠后;重复投诉率:张家口、秦皇岛、保定增长较快。
-        // 客户端-移网网络体验:投诉问题解决满意率:全省、张家口、雄安较低,与达标值差距较大;
-        // 投诉问题解决率:全省、张家口、雄安较低,与达标值差距较大;投诉问题响应率:全省、雄安、张家口较低,与达标值差距较大。
-        // 投诉处理时长、超时工单概况:超时工单:石家庄、雄安、张家口分公司超时工单占比较高,石家庄、唐山、邯郸超时工单数量较多。
-        // 平均处理时长:本月相对较长的地市为雄安、石家庄、张家口;与Aspose.Cells.Cell
-        // [ H2; ValueType : IsString; Value : 2月 ]比Aspose.Cells.Cell [ G3; ValueType :
-        // IsString; Value : 雄安 ]时长增幅较大。
-        System.out.println(tslTaskService.getFilesToSend("20230426"));
+        // System.out.println(tslTaskService.getFilesToSend("20230426"));
+        // tslTaskService.sendDingTalkMsg("20230426");
+        tslTaskService.runTask("20230425");
     }
 }

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

@@ -1,5 +1,7 @@
 # 接口测试
 
+nohup java -jar tsl_data-1.0-exec.jar >output.log 2>&1 &
+
 ## 数据手动入库
 
 ```http

+ 4 - 4
tsl_data/src/main/java/com/nokia/tsl_data/dao/TslDao.java

@@ -80,7 +80,7 @@ public interface TslDao {
      * @param monthId
      * @return
      */
-    List<Map<String, Object>> selectCityTslForMonth(String monthId);
+    List<Map<String, Object>> selectCityTslForMonth(String day);
 
     /**
      * 投诉清单全省计数
@@ -88,7 +88,7 @@ public interface TslDao {
      * @param monthId
      * @return
      */
-    List<Map<String, Object>> selectAllTslForMonth(String monthId);
+    List<Map<String, Object>> selectAllTslForMonth(String day);
 
     /**
      * 投诉清单地市总数
@@ -96,7 +96,7 @@ public interface TslDao {
      * @param monthId
      * @return
      */
-    List<Map<String, Object>> selectCityAllForMonth(String monthId);
+    List<Map<String, Object>> selectCityAllForMonth(String day);
 
     /**
      * 投诉清单按月计算总数
@@ -104,7 +104,7 @@ public interface TslDao {
      * @param monthId
      * @return
      */
-    int selectAllForMonth(String monthId);
+    int selectAllForMonth(String day);
 
     /**
      * 查询用户数

+ 7 - 5
tsl_data/src/main/java/com/nokia/tsl_data/service/TslDataService.java

@@ -325,7 +325,7 @@ public class TslDataService {
      * @param day
      * @return
      */
-    public Map<String, List<Object>> getSeet1Data(String day) {
+    public Map<String, List<Object>> getSheet1Data(String day) {
         // 获取当前月
         Calendar calendar = Calendar.getInstance(Locale.CHINA);
         try {
@@ -338,11 +338,13 @@ public class TslDataService {
         // monthId
         String monthId = new SimpleDateFormat("yyyyMM").format(calendar.getTime());
 
-        List<Map<String, Object>> cityTslforMonth = tslDao.selectCityTslForMonth(monthId);
-        List<Map<String, Object>> allTslforMonth = tslDao.selectAllTslForMonth(monthId);
-        List<Map<String, Object>> cityAllforMonth = tslDao.selectCityAllForMonth(monthId);
-        int total = tslDao.selectAllForMonth(monthId);
+        List<Map<String, Object>> cityTslforMonth = tslDao.selectCityTslForMonth(day);
+        List<Map<String, Object>> allTslforMonth = tslDao.selectAllTslForMonth(day);
+        List<Map<String, Object>> cityAllforMonth = tslDao.selectCityAllForMonth(day);
+        int total = tslDao.selectAllForMonth(day);
+        // 当月用户数
         List<Map<String, Object>> userCount = getUserCount(monthId);
+        // 目标投诉率
         List<Map<String, Object>> targetTsRatio = tslDao.selectTargetTsRatioForMonth(monthId);
 
         Map<String, List<Object>> result = new HashMap<>();

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

@@ -669,7 +669,7 @@ public class TslReportService {
         cell.setCellStyle(cellStyle1);
 
         // 获取数据
-        Map<String, List<Object>> seet1Data = tslDataService.getSeet1Data(day);
+        Map<String, List<Object>> seet1Data = tslDataService.getSheet1Data(day);
 
         int rowNum = 2;
         int cellNum = 0;

+ 10 - 7
tsl_data/src/main/resources/mapper/TslDao.xml

@@ -64,19 +64,22 @@
         sum(repeat_num) /sum(total_num)::numeric as repeat_ratio from t5 union select * from t5 </select>
 
     <select id="selectCityTslForMonth" resultType="Map"> select compl_area_local, day_id, count(1)
-        as num from report_auto.he_d_mobile_comp hdmc where month_id = #{monthId} group by
-        compl_area_local, day_id order by compl_area_local,day_id </select>
+        as num from report_auto.he_d_mobile_comp hdmc where month_id = substring(#{day} from 1 for
+        6) and day_id &lt;= substring(#{day} from 7 for 2) group by compl_area_local, day_id order
+        by compl_area_local,day_id </select>
 
     <select id="selectAllTslForMonth" resultType="Map"> select day_id, count(1) as num from
-        report_auto.he_d_mobile_comp hdmc where month_id = #{monthId} group by day_id order by
-        day_id </select>
+        report_auto.he_d_mobile_comp hdmc where month_id = substring(#{day} from 1 for 6) and day_id
+        &lt;= substring(#{day} from 7 for 2) group by day_id order by day_id </select>
 
     <select id="selectCityAllForMonth" resultType="Map">select compl_area_local, count(1) as num
-        from report_auto.he_d_mobile_comp hdmc where month_id = #{monthId} group by compl_area_local
-        order by compl_area_local </select>
+        from report_auto.he_d_mobile_comp hdmc where month_id = substring(#{day} from 1 for 6) and
+        day_id &lt;= substring(#{day} from 7 for 2) group by compl_area_local order by
+        compl_area_local </select>
 
     <select id="selectAllForMonth" resultType="int">select count(1) as num from
-        report_auto.he_d_mobile_comp hdmc where month_id =#{monthId}</select>
+        report_auto.he_d_mobile_comp hdmc where month_id = substring(#{day} from 1 for 6) and day_id
+        &lt;= substring(#{day} from 7 for 2)</select>
 
     <select id="selectUserCountForMonth" resultType="Map">select city_name, user_count from
         report_auto.user_count where month_id = #{monthId}</select>

+ 0 - 10
tsl_data/src/test/java/com/nokia/tsl_data/TslDataApplicationTest.java

@@ -5,7 +5,6 @@ import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
-import com.nokia.tsl_data.dao.TslDao;
 import com.nokia.tsl_data.service.TslTaskService;
 import com.nokia.tsl_data.service.exception.TslTaskException;
 
@@ -19,13 +18,4 @@ public class TslDataApplicationTest {
     void test() throws IOException, TslTaskException {
         taskService.reportGenerateTask("20230425");
     }
-
-    @Autowired
-    private TslDao tslDao;
-
-    @Test
-    void test1() {
-        System.out.println(tslDao.selectCompCountForDay("20230420"));
-        System.out.println(tslDao.selectQualityCountForDay("2023-04-20"));
-    }
 }