ReportPage.razor
8.72 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
@page "/DataAnalysis/Report"
@using AntDesign.TableModels
@using DataAcquisition.Common.Enums
@using DataAcquisition.DataAccess
@using DataAcquisition.Models
@using DataAcquisition.ViewModels
@using LinqKit
@using Microsoft.EntityFrameworkCore
@using NPOI.XSSF.UserModel
@using System.Linq.Expressions
@inject IJSRuntime JS
@inject IDbContextFactory<DataContext> dbContextFactory;
@inject IMessageService _message
<Flex Justify="space-between" Align="center">
<Flex Justify="flex-start" Align="center" Gap="small">
<Select DataSource="@_selectList"
@bind-Value="@equipmentCode"
ValueProperty="c=>c.Value"
LabelProperty="c=>c.Key"
Style="width:320px">
</Select>
<DatePicker TValue="DateTime" Picker="@DatePickerType.Month" @bind-Value="inputDate" DisabledDate="date => date <= DateTime.Now.AddMonths(-3)|| date >DateTime.Now" Style="width:100px" ShowToday AllowClear="false" />
<Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Search" @onclick="SearchEvent">搜索</Button>
<Button Type="@ButtonType.Default" Icon="@IconType.Outline.Redo" @onclick="ResetEvent">重置</Button>
</Flex>
<Flex Justify="flex-end" Align="center" Gap="small">
<Button @onclick="ExportToExcel" Icon="@IconType.Outline.Download">导出Excel</Button>
</Flex>
</Flex>
<Table @ref="table"
TItem="ReportDto"
DataSource="@dataItems"
@bind-PageSize="_pageSize"
@bind-SelectedRows="selectedRows"
OnChange="OnChange"
Size="TableSize.Small"
RowKey="x=>x.Id">
<Selection Key="@(context.Id.ToString())" />
<PropertyColumn Property="c=>c.Id" Hidden />
<PropertyColumn Title="设备编号" Property="c=>c.EquipmentCode" />
<PropertyColumn Title="日期" Property="c=>c.Date" />
@* <PropertyColumn Title="用气量(L)" Property="c=>c.GasConsumption" /> *@
<PropertyColumn Title="焊丝消耗(KG)" Property="c=>c.WireConsumption" />
<PropertyColumn Title="产量(件)" Property="c=>c.ProductionCapacity" />
<PropertyColumn Title="焊接时间(H)" Property="c=>c.ArcingTime" />
<PropertyColumn Title="待机时间(H)" Property="c=>c.FreeTime" />
<PropertyColumn Title="燃弧率(%)" Property="c=>c.ArcingRate" />
</Table>
@code {
string equipmentCode = EquipmentConst.Fanuc_1.ToString();
DateTime inputDate = DateTime.Now;
IEnumerable<ReportDto> selectedRows = null!;
int _pageSize = 35;
string _currentSelectEquipmentCode = string.Empty;
Dictionary<string, string> _selectList = null!;
IEnumerable<ReportDto> dataItems = null!;
ITable table = null!;
public void SearchEvent()
{
LoadData();
}
public void ResetEvent()
{
equipmentCode = EquipmentConst.Fanuc_1.ToString();
inputDate = DateTime.Now;
}
protected override void OnInitialized()
{
_selectList = new Dictionary<string, string>
{
{ "Fanuc", EquipmentConst.Fanuc_1.ToString() },
};
base.OnInitialized();
}
public void OnChange(QueryModel<ReportDto> queryModel)
{
LoadData();
}
private void LoadData()
{
try
{
_currentSelectEquipmentCode = equipmentCode;
using var context = dbContextFactory.CreateDbContext();
var (startTime, endTime) = GetRangePicker(inputDate);
var dates = Enumerable.Range(0, (endTime - startTime).Days + 1).Select(x => startTime.AddDays(x)).ToList();
dataItems = dates.Select(dateTime =>
{
var runTicks = context.EquipmentPropertyRecords.Where(p => p.EquipmentCode == equipmentCode && p.EquipmentPropertyCode == RobotProps.BootFlag.ToString() && p.CreateTime.Date == dateTime.Date).Sum(s => s.UpdateTime.Ticks - s.CreateTime.Ticks);
var weldTicks = context.EquipmentPropertyRecords.Where(p => p.EquipmentCode == equipmentCode && p.EquipmentPropertyCode == RobotProps.WeldFlag.ToString() && p.CreateTime.Date == dateTime.Date).Sum(s => s.UpdateTime.Ticks - s.CreateTime.Ticks);
var arcingRate = 0d;
if (runTicks > 0)
{
arcingRate = Math.Round(weldTicks * 100d / runTicks, 2);
}
var data = new ReportDto
{
Date = dateTime.ToLongDateString(),
EquipmentCode = equipmentCode,
WireConsumption = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == equipmentCode && x.EquipmentPropertyCode == RobotProps.Weld_Speed.ToString() && x.CreateTime.Date == dateTime.Date).Select(x => x.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
ProductionCapacity = context.WorkpieceProductions.Where(x => x.EquipmentCode == equipmentCode && x.CreateTime.Date == dateTime.Date).Count(),
ArcingTime = Math.Round(TimeSpan.FromTicks(weldTicks).TotalHours, 2),
FreeTime = Math.Round(TimeSpan.FromTicks(runTicks - weldTicks).TotalHours, 2),
ArcingRate = arcingRate
};
return data;
}).ToList();
}
catch (Exception ex)
{
_message.Error($"加载数据异常:{ex.Message}");
}
}
private Expression<Func<WorkpieceProduction, bool>> QueryExpression()
{
var filter = PredicateBuilder.New<WorkpieceProduction>(true);
if (!string.IsNullOrWhiteSpace(equipmentCode))
{
filter = filter.And(x => x.EquipmentCode.Contains(equipmentCode));
}
return filter;
}
private async Task ExportToExcel()
{
try
{
if (dataItems == null || !dataItems.Any())
{
await _message.Warning($"设备{equipmentCode}数据为空!");
return;
}
var stream = ExportToExcel(dataItems);
using var streamRef = new DotNetStreamReference(stream);
var equipmentName = _selectList.Where(x => x.Value == _currentSelectEquipmentCode).Select(x => x.Key).First();
await JS.InvokeVoidAsync("downloadFileFromStream", $"{equipmentName}_{inputDate.ToString("Y")}.xlsx", streamRef);
}
catch (Exception ex)
{
await _message.Error(ex.Message);
}
}
private (DateTime, DateTime) GetRangePicker(DateTime dateTime)
{
var startTime = dateTime.AddDays(1 - DateTime.Now.Day).Date;
var endTime = dateTime.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1);
return (startTime, endTime);
}
private MemoryStream ExportToExcel(IEnumerable<ReportDto> data)
{
var workbook = new XSSFWorkbook();
var sheetName = _selectList.Where(x => x.Value == equipmentCode).Select(x => x.Key).First();
var sheet = workbook.CreateSheet(sheetName);
// 创建表头
var headerRow = sheet.CreateRow(0);
// 填充表头内容
headerRow.CreateCell(0).SetCellValue("日期");
headerRow.CreateCell(1).SetCellValue("焊丝消耗(KG)");
headerRow.CreateCell(2).SetCellValue("产量(件)");
headerRow.CreateCell(3).SetCellValue("焊接时间(H)");
headerRow.CreateCell(4).SetCellValue("待机时间(H)");
headerRow.CreateCell(5).SetCellValue("燃弧率(%)");
// 填充数据行
int rowIndex = 1;
foreach (var item in data)
{
var row = sheet.CreateRow(rowIndex++);
row.CreateCell(0).SetCellValue(item.Date);
row.CreateCell(1).SetCellValue(item.WireConsumption);
row.CreateCell(2).SetCellValue(item.ProductionCapacity);
row.CreateCell(3).SetCellValue(item.ArcingTime);
row.CreateCell(4).SetCellValue(item.FreeTime);
row.CreateCell(5).SetCellValue(item.ArcingRate);
}
//合计
var lastRow = sheet.CreateRow(rowIndex++);
lastRow.CreateCell(0).SetCellValue("合计");
lastRow.CreateCell(1).SetCellValue($"总焊丝消耗:{Math.Round(data.Sum(x => x.WireConsumption), 2)}");
lastRow.CreateCell(2).SetCellValue($"总产量:{data.Sum(x => x.ProductionCapacity)}");
lastRow.CreateCell(3).SetCellValue($"总焊接时间:{Math.Round(data.Sum(x => x.ArcingTime), 2)}");
lastRow.CreateCell(4).SetCellValue($"总待机时间:{Math.Round(data.Sum(x => x.FreeTime), 2)}");
lastRow.CreateCell(5).SetCellValue($"平均燃弧率:{Math.Round(data.Sum(x => x.ArcingRate) / data.Count(), 2)}");
var stream = new MemoryStream();
workbook.Write(stream, true);
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
}