FaultReportServices.cs
4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using AutoMapper;
using Azure.Core;
using Hh.Mes.Common.config;
using Hh.Mes.Common.Exel;
using Hh.Mes.Common.Http;
using Hh.Mes.Common.log;
using Hh.Mes.POJO.Entity;
using Hh.Mes.Service.Repository;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using NPOI.HPSF;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using static Hh.Mes.Service.QiYeWeiXin.QiYiWeiXinGlobalContext;
namespace Hh.Mes.Service
{
public class FaultReportService : RepositorySqlSugar<sys_user>
{
private static string FilePath { get; set; }
private DataTable GetSevenDayFaultData()
{
// 从数据库或API获取近七天故障数据
var dt = new DataTable();
// 假设有以下列
dt.Columns.Add("项目名称", typeof(string));
dt.Columns.Add("故障开始时间", typeof(DateTime));
dt.Columns.Add("故障结束时间", typeof(DateTime));
dt.Columns.Add("状态", typeof(string));
dt.Columns.Add("处理结果", typeof(string));
// 添加示例数据
dt.Rows.Add("IOT设备监控", DateTime.Now.AddHours(-3), DateTime.Now, "已处理", "已解决");
dt.Rows.Add("IOT设备监控", DateTime.Now.AddHours(-8), DateTime.Now.AddHours(-6), "已处理", "已解决");
dt.Rows.Add("服务器集群", DateTime.Now.AddHours(-12), DateTime.Now.AddHours(-9), "已处理", "已修复");
dt.Rows.Add("数据库服务", DateTime.Now.AddDays(-1), DateTime.Now.AddHours(-20), "处理中", "修复中");
dt.Rows.Add("网络设备", DateTime.Now.AddHours(-5), DateTime.Now.AddHours(-4), "已处理", "已解决");
return dt;
}
private DataTable GetMonthFaultData()
{
// 从数据库或API获取本月故障数据
var dt = new DataTable();
dt.Columns.Add("项目名称", typeof(string));
dt.Columns.Add("故障开始时间", typeof(DateTime));
dt.Columns.Add("故障结束时间", typeof(DateTime));
dt.Columns.Add("状态", typeof(string));
dt.Columns.Add("处理结果", typeof(string));
// 添加示例数据
for (int i = 1; i <= 15; i++)
{
string project = i % 3 == 0 ? "IOT设备监控" :
i % 3 == 1 ? "服务器集群" : "网络设备";
string status = i % 5 == 0 ? "处理中" : "已处理";
string result = status == "处理中" ? "修复中" : "已解决";
dt.Rows.Add(project,
DateTime.Now.AddDays(-i).AddHours(-2),
DateTime.Now.AddDays(-i),
status,
result);
}
return dt;
}
/// <summary>
/// 将Excel作为企业微信附件发送
/// </summary>
public async Task SendToWeChatWorkAsync()
{
var tempPath = FilePath;
try
{
// 2. 上传到企业微信获取media_id
string mediaId = UploadFileToWeChatWorkAsync(tempPath);
if (!string.IsNullOrEmpty(mediaId))
{
// 3. 发送文件消息
await SendFileMessage(mediaId);
}
}
finally
{
// 4. 清理临时文件
if (File.Exists(tempPath))
{
File.Delete(tempPath);
}
}
}
public async Task Export(DataTable dt, string fileName)
{
try
{
string fileurl = ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend8;
FilePath = $"{fileurl}/{fileName}.xlsx";
//获取当年周数
int week = ISOWeek.GetWeekOfYear(DateTime.Now);
await NPOIHelper.ExportDTtoExcelAsync(dt, $"IOT项目故障报告", FilePath, fileurl);
}
catch (Exception ex)
{
Log4NetHelper.Instance.Error($"项目故障报告信息导出Excel异常:{ex.Message}");
}
}
private async Task SendFileMessage(string mediaId)
{
// 实现企业微信消息发送逻辑
var content = GetContent(GetAgentId(), "CS230032", WxMsgType.Text, $"IOT设备故障报告:推送近七天【{DateTime.Today.AddDays(-7)}~{DateTime.Now}】和本月故障汇总分钟数和妥善率,附件消息稍后推送请注意查收!");
SendMsg(content);
//间隔10s
await Task.Delay(10000);
var filecontent = GetContent(GetAgentId(), "CS230032", WxMsgType.File, $"{mediaId}");
SendMsg(filecontent);
}
}
}