|
@@ -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);
|
|
|
+ }
|
|
|
+}
|