lifuquan 1 year ago
commit
a860b05fb7

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+target
+download

+ 1 - 0
README.md

@@ -0,0 +1 @@
+# dingtalk_auto

+ 99 - 0
pom.xml

@@ -0,0 +1,99 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.nokia</groupId>
+    <artifactId>dingtalk_auto</artifactId>
+    <version>2.0</version>
+
+    <packaging>jar</packaging>
+
+    <properties>
+        <!-- 跳过测试代码 -->
+        <skipTests>
+            true</skipTests>
+        <!-- 指定java的版本 -->
+        <java.version>1.8</java.version>
+        <!-- 文件拷贝时的编码 -->
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+        <!-- 指定maven-compiler-plugin的配置属性开始 -->
+        <!-- 编译时的编码 -->
+        <maven.compiler.encoding>
+            UTF-8</maven.compiler.encoding>
+        <!-- 指定编译的版本 -->
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
+        <!-- 指定maven-compiler-plugin的配置属性结束 -->
+    </properties>
+
+    <dependencies>
+        <!-- com.nokia.common -->
+        <dependency>
+            <groupId>com.nokia</groupId>
+            <artifactId>common</artifactId>
+            <version>1.0</version>
+        </dependency>
+        <!-- starter-redis -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+        </dependency>
+        <!--starter-web-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <!--lombok-->
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <!-- springboot-test -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <dependencyManagement>
+        <dependencies>
+            <!-- 当不把spring-boot-parent
+            作为parent项目时,需要使用dependencyManagement引入spring-boot-dependencies管理依赖 -->
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-dependencies</artifactId>
+                <version>2.6.14</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <build>
+        <finalName>dingtalk_auto</finalName>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>2.6.14</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+                <configuration>
+                    <classifier>exec</classifier>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 12 - 0
src/main/java/com/nokia/dingtalk_auto/DingtalkAutoApplication.java

@@ -0,0 +1,12 @@
+package com.nokia.dingtalk_auto;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class DingtalkAutoApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(DingtalkAutoApplication.class, args);
+    }
+}

+ 43 - 0
src/main/java/com/nokia/dingtalk_auto/config/DingTalkUtilConfig.java

@@ -0,0 +1,43 @@
+package com.nokia.dingtalk_auto.config;
+
+import javax.annotation.Resource;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.web.client.RestTemplate;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.nokia.common.dingtalk.DingtalkUtil;
+
+/**
+ * 注入钉钉机器人API
+ */
+@Configuration
+public class DingTalkUtilConfig {
+
+    @Resource
+    private RestTemplate restTemplate;
+    @Resource
+    private RedisTemplate<String, Object> redisTemplate;
+    @Resource
+    private ObjectMapper objectMapper;
+
+    @Value("${dingtalk.default.appkey}")
+    private String appKey;
+    @Value("${dingtalk.default.appSecret}")
+    private String appSecret;
+
+    @Primary
+    @Bean("defaultDingtalkUtil")
+    public DingtalkUtil defaultDingtalkUtil() {
+        return new DingtalkUtil()
+                .setAppKey(appKey)
+                .setAppSecret(appSecret)
+                .setObjectMapper(objectMapper)
+                .setRestTemplate(restTemplate)
+                .setRedisTemplate(redisTemplate);
+    }
+}

+ 40 - 0
src/main/java/com/nokia/dingtalk_auto/config/NetBrainDingTalkUtilConfig.java

@@ -0,0 +1,40 @@
+package com.nokia.dingtalk_auto.config;
+
+import javax.annotation.Resource;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.web.client.RestTemplate;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.nokia.common.dingtalk.DingtalkUtil;
+
+/**
+ * 网络大脑机器人
+ */
+@Configuration
+public class NetBrainDingTalkUtilConfig {
+    @Resource
+    private RestTemplate restTemplate;
+    @Resource
+    private RedisTemplate<String, Object> redisTemplate;
+    @Resource
+    private ObjectMapper objectMapper;
+
+    @Value("${netbrain.dingtalk.appkey}")
+    private String appKey;
+    @Value("${netbrain.dingtalk.appSecret}")
+    private String appSecret;
+
+    @Bean("netbrainDingtalkUtil")
+    public DingtalkUtil netbrainDingtalkUtil() {
+        return new DingtalkUtil()
+                .setAppKey(appKey)
+                .setAppSecret(appSecret)
+                .setObjectMapper(objectMapper)
+                .setRestTemplate(restTemplate)
+                .setRedisTemplate(redisTemplate);
+    }
+}

+ 17 - 0
src/main/java/com/nokia/dingtalk_auto/config/RedisTemplateConfig.java

@@ -0,0 +1,17 @@
+package com.nokia.dingtalk_auto.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+
+@Configuration
+public class RedisTemplateConfig {
+    
+    @Bean
+    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
+        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
+        redisTemplate.setConnectionFactory(redisConnectionFactory);
+        return redisTemplate;
+    }
+}

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

@@ -0,0 +1,14 @@
+package com.nokia.dingtalk_auto.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();
+    }
+}

+ 24 - 0
src/main/java/com/nokia/dingtalk_auto/config/TaskSchedulerConfig.java

@@ -0,0 +1,24 @@
+package com.nokia.dingtalk_auto.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
+
+@Configuration
+@EnableScheduling
+public class TaskSchedulerConfig {
+
+    private int poolSize = 3;
+
+    @Bean
+    public ThreadPoolTaskScheduler taskScheduler() {
+        // 使用最常用的ThreadPoolTaskScheduler
+        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
+        // 线程数
+        taskScheduler.setPoolSize(poolSize);
+        taskScheduler.setRemoveOnCancelPolicy(true);
+        taskScheduler.setThreadNamePrefix("taskSchedulerThreadPool-");
+        return taskScheduler;
+    }
+}

+ 23 - 0
src/main/java/com/nokia/dingtalk_auto/controller/ZeroTrafficTaskController.java

@@ -0,0 +1,23 @@
+package com.nokia.dingtalk_auto.controller;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RestController;
+
+import com.nokia.common.http.R;
+import com.nokia.dingtalk_auto.service.ZeroTrafficTaskService;
+
+@RestController
+public class ZeroTrafficTaskController {
+
+    @Autowired
+    private ZeroTrafficTaskService zeroTrafficTaskService;
+
+    public R recallMessage(String messageId) {
+        zeroTrafficTaskService.recallMessage(messageId);
+        return R.ok();
+    }
+
+    public R sendMessageManually(String month) {
+        return R.ok();
+    }
+}

+ 28 - 0
src/main/java/com/nokia/dingtalk_auto/service/MessageService.java

@@ -0,0 +1,28 @@
+package com.nokia.dingtalk_auto.service;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import com.nokia.common.dingtalk.DingtalkUtil;
+
+@Service
+public class MessageService {
+
+    @Value("${netbrain.dingtalk.openConversationId}")
+    private String openConversationId;
+
+    @Autowired
+    @Qualifier("defaultDingtalkUtil")
+    private DingtalkUtil dingtalkUtil;
+
+    public void message(String message) {
+        dingtalkUtil.SendText(openConversationId, message);
+    }
+
+    public void error(String errorMessage) {
+        dingtalkUtil.sendMarkdown(openConversationId,
+                String.format("<font color=#FF0000>%s</font>", errorMessage), "ERROR");
+    }
+}

+ 162 - 0
src/main/java/com/nokia/dingtalk_auto/service/ZeroTrafficTaskService.java

@@ -0,0 +1,162 @@
+package com.nokia.dingtalk_auto.service;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import com.nokia.common.dingtalk.DingtalkUtil;
+import com.nokia.common.ssh.SftpUtil;
+
+/**
+ * 发送零流量低流量明细
+ */
+@Service
+public class ZeroTrafficTaskService {
+
+    @Value("${netbrain.zeroTraffic.remoteHost}")
+    private String remoteHost;
+
+    @Value("${netbrain.zeroTraffic.remotePort}")
+    private int remotePort;
+
+    @Value("${netbrain.zeroTraffic.remoteUser}")
+    private String remoteUser;
+
+    @Value("${netbrain.zeroTraffic.remotePassword}")
+    private String remotePassword;
+
+    @Value("${netbrain.zeroTraffic.remoteDir}")
+    private String remoteDir;
+
+    @Value("${netbrain.zeroTraffic.localDir}")
+    private String localDir;
+
+    @Value("${netbrain.dingtalk.openConversationId}")
+    private String openConversationId;
+
+    @Autowired
+    @Qualifier("netbrainDingtalkUtil")
+    private DingtalkUtil dingtalkUtil;
+
+    @Autowired
+    private MessageService messageService;
+
+    public void runTask(String month) {
+        try {
+            download(month);
+        } catch (Exception e) {
+            String msg = e.getMessage();
+            if (msg.endsWith("No such file")) {
+                // 文件不存在
+                messageService.error("123456");
+            }
+        }
+    }
+
+    /**
+     * 发送文件
+     * 
+     * @param month 2023-06 样式的表示月份的字符串
+     */
+    public void sendFiles(String month) {
+        String localPath = localDir + month;
+        File[] files = new File(localPath).listFiles();
+        List<String> messageIds = new ArrayList<>();
+        // 根据发送文件的后缀发送文件,保证发送顺序
+        for (File file : files) {
+            if (file.getName().toLowerCase().endsWith("1.png")) {
+                String mediaId = dingtalkUtil.upload(file.getAbsolutePath(), "image");
+                String messageId = dingtalkUtil.sendImage(openConversationId, mediaId);
+                messageIds.add(messageId);
+            }
+        }
+        for (File file : files) {
+            if (file.getName().toLowerCase().endsWith(month + ".xlsx")) {
+                String mediaId = dingtalkUtil.upload(file.getAbsolutePath(), "file");
+                String messageId = dingtalkUtil.SendFile(openConversationId, mediaId, file.getName());
+                messageIds.add(messageId);
+            }
+        }
+        messageService.message(String.format("零流量低流量 %s 账期 完成发送 ==消息id: %s", month, messageIds));
+    }
+
+    /**
+     * 下载文件,根据给出的月份字符串拼接远端路径,把文件下载到本地。
+     * 
+     * @param month 2023-06 样式的表示月份的字符串
+     */
+    public void download(String month) {
+        // 创建SftpUtil
+        SftpUtil sftpUtil = new SftpUtil()
+                .setHost(remoteHost)
+                .setPort(remotePort)
+                .setUser(remoteUser)
+                .setPassword(remotePassword);
+
+        String remotePath = remoteDir + month;
+        String localPath = localDir + month;
+
+        File localFile = new File(localPath);
+        if (!localFile.exists()) {
+            localFile.mkdirs();
+        }
+
+        try {
+            // 连接到sftp服务器
+            sftpUtil.connect();
+            List<String> files = sftpUtil.ls(remotePath);
+            for (String fileName : files) {
+                sftpUtil.get(remotePath + "/" + fileName, localPath + "/" + fileName);
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e.getMessage());
+        } finally {
+            sftpUtil.disconnect();
+        }
+    }
+
+    /**
+     * 手动下载文件
+     * 
+     * @param month 2023-06 样式的表示月份的字符串
+     */
+    public void downloadManually(String month) {
+        SftpUtil sftpUtil = new SftpUtil()
+                .setHost(remoteHost)
+                .setPort(remotePort)
+                .setUser(remoteUser)
+                .setPassword(remotePassword);
+        String remotePath = remoteDir + month;
+        String localPath = localDir + month;
+
+        File localFile = new File(localPath);
+
+        if (!localFile.exists()) {
+            localFile.mkdirs();
+        }
+
+        try {
+
+            sftpUtil.connect();
+            List<String> files = sftpUtil.ls(remotePath);
+            for (String fileName : files) {
+                sftpUtil.get(remotePath + "/" + fileName, localPath + "/" + fileName);
+            }
+        } catch (Exception e) {
+            // 下载失败
+            e.printStackTrace();
+        } finally {
+            // 从sftp服务器断开连接
+            sftpUtil.disconnect();
+        }
+    }
+
+    public void recallMessage(String messageId) {
+        dingtalkUtil.recallMessage(openConversationId, messageId);
+    }
+}

+ 27 - 0
src/main/resources/application.yml

@@ -0,0 +1,27 @@
+server:
+  port: 11111
+
+spring:
+  redis:
+    host: 127.0.0.1
+    port: 6379
+
+dingtalk:
+  default:
+    appkey: dingothmdq6opv6hjrm5
+    appSecret: SeoyAwUnzFIFY4j4CX089HJ0i-pj1BIzByB3AZcnbCQaq94lZvazFpfEGGQwPznc
+  message:
+    openConversationId: cidcWmmFwduUTDB3G0vPNOldQ==
+
+netbrain:
+  dingtalk:
+    appkey: dingothmdq6opv6hjrm5
+    appSecret: SeoyAwUnzFIFY4j4CX089HJ0i-pj1BIzByB3AZcnbCQaq94lZvazFpfEGGQwPznc
+    openConversationId: cidcWmmFwduUTDB3G0vPNOldQ==
+  zeroTraffic:
+    remoteHost: 133.96.94.246
+    remotePort: 22
+    remoteUser: nokia
+    remotePassword: Nokia*123
+    remoteDir: /data/wangluodanao/program/timingFile/
+    localDir: ./download/netbrain/zerotraffic/

+ 24 - 0
src/test/java/com/nokia/dingtalk_auto/DingtalkAutoApplicationTest.java

@@ -0,0 +1,24 @@
+package com.nokia.dingtalk_auto;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import com.nokia.dingtalk_auto.service.ZeroTrafficTaskService;
+
+@SpringBootTest
+public class DingtalkAutoApplicationTest {
+
+    @Autowired
+    private ZeroTrafficTaskService zeroTrafficSendService;
+
+    @Test
+    void test() {
+        zeroTrafficSendService.download("2023-07");
+    }
+
+    @Test
+    void test1() {
+        zeroTrafficSendService.recallMessage("Y3yL5KMxxasU4YGNRJ3riz2jsDZsWl43G2AK8QfOyP4=");
+    }
+}