Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit cad3f61

Browse files
committed
[APP] ExportOrder
1 parent 0d267d5 commit cad3f61

File tree

10 files changed

+690
-438
lines changed

10 files changed

+690
-438
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
namespace TSystems.LoveOTC.AdminHub;
2+
3+
using System.Collections.Immutable;
4+
using System.Text;
5+
using DocumentFormat.OpenXml;
6+
using DocumentFormat.OpenXml.Packaging;
7+
using DocumentFormat.OpenXml.Spreadsheet;
8+
using Entities;
9+
using Microsoft.AspNetCore.SignalR;
10+
using Microsoft.EntityFrameworkCore;
11+
12+
internal partial class AdminHub {
13+
private static DateTime lastExport = DateTime.MinValue;
14+
15+
private static readonly IImmutableList<string> headers = new[] {
16+
"Order Id",
17+
"Order Time",
18+
"Recipient Name",
19+
"E-Mail",
20+
"Phone",
21+
"Address",
22+
"Product Name",
23+
"Types",
24+
"Quantity"
25+
}.ToImmutableArray();
26+
27+
/**
28+
* <remarks>
29+
* @author Aloento
30+
* @since 1.2.0
31+
* @version 0.1.0
32+
* </remarks>
33+
*/
34+
public async IAsyncEnumerable<byte[]> ExportOrder() {
35+
if (DateTime.Now - lastExport < TimeSpan.FromMinutes(3))
36+
throw new HubException("The time interval between two exports shall not be less than 3 minutes.");
37+
38+
lastExport = DateTime.Now;
39+
40+
using var stream = new MemoryStream();
41+
using var document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, true);
42+
43+
var workbookPart = document.AddWorkbookPart();
44+
workbookPart.Workbook = new();
45+
46+
var worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
47+
var sheetData = new SheetData();
48+
worksheetPart.Worksheet = new(sheetData);
49+
50+
var sheets = workbookPart.Workbook.AppendChild(new Sheets());
51+
var sheet = new Sheet {
52+
Id = workbookPart.GetIdOfPart(worksheetPart),
53+
SheetId = 1,
54+
Name = "ProcessingOrder"
55+
};
56+
sheets.Append(sheet);
57+
58+
var headerRow = new Row();
59+
_ = sheetData.AppendChild(headerRow);
60+
headerRow.Append(headers.Select(x => new Cell {
61+
DataType = CellValues.String,
62+
CellValue = new(x)
63+
}));
64+
65+
var userIds = await this.Db.Orders
66+
.Where(x => x.Status == OrderStatus.Processing)
67+
.Select(x => x.UserId)
68+
.Distinct()
69+
.ToArrayAsync();
70+
71+
foreach (var userId in userIds) {
72+
var records = this.Db.OrderCombos
73+
.Where(x => x.Order.UserId == userId)
74+
.Where(x => x.Order.Status == OrderStatus.Processing)
75+
.Include(x => x.Order)
76+
.ThenInclude(o => o.User)
77+
.Include(x => x.Combo)
78+
.ThenInclude(c => c.Product)
79+
.Include(x => x.Combo)
80+
.ThenInclude(c => c.Types)
81+
.ThenInclude(t => t.Variant)
82+
.AsAsyncEnumerable();
83+
84+
var first = true;
85+
await foreach (var record in records) {
86+
var order = record.Order;
87+
var user = order.User;
88+
var combo = record.Combo;
89+
90+
var data = new List<string>(9) {
91+
record.OrderId.ToString(),
92+
order.CreateAt.ToString("yyyy-MM-dd HH:mm:ss"),
93+
user.Name
94+
};
95+
96+
if (first) {
97+
data.AddRange([
98+
user.EMail,
99+
user.Phone,
100+
user.Address
101+
]);
102+
first = false;
103+
} else
104+
data.AddRange([
105+
"-", "-", "-"
106+
]);
107+
108+
data.Add(combo.Product.Name);
109+
110+
var types = combo.Types.Aggregate(
111+
new StringBuilder(),
112+
(prev, curr) => {
113+
_ = prev.Append(curr.Variant.Name);
114+
_ = prev.Append(" : ");
115+
_ = prev.Append(curr.Name);
116+
_ = prev.Append(" ; ");
117+
return prev;
118+
})
119+
.ToString();
120+
121+
data.AddRange([
122+
types,
123+
record.Quantity.ToString()
124+
]);
125+
126+
var row = new Row();
127+
_ = sheetData.AppendChild(row);
128+
row.Append(data.Select(x => new Cell {
129+
DataType = CellValues.String,
130+
CellValue = new(x)
131+
}));
132+
}
133+
134+
var emptyRow = new Row();
135+
_ = sheetData.AppendChild(emptyRow);
136+
}
137+
138+
workbookPart.Workbook.Save();
139+
document.Save();
140+
stream.Position = 0;
141+
142+
var buffer = new byte[30 * 1024];
143+
int bytesRead;
144+
145+
while ((bytesRead = await stream.ReadAsync(buffer)) > 0)
146+
yield return buffer[..bytesRead];
147+
}
148+
}

TSystems.LoveOTC/AdminHub/Product/Post.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public async Task<uint> ProductPostVariant(uint prodId, string name) {
133133

134134
var temp = await this.Db.Variants.AddAsync(new() {
135135
ProductId = prodId,
136-
Name = name,
136+
Name = name
137137
});
138138

139139
await this.Db.SaveChangesAsync();
@@ -184,11 +184,11 @@ public async Task<uint> ProductPostType(uint variantId, string name) {
184184
*/
185185
public async Task<uint> ProductPostCombo(uint prodId, Dictionary<string, string> combo, ushort stock) {
186186
var variTypesDb = (await this.Db.Products
187-
.Include(x => x.Variants)
188-
.ThenInclude(x => x.Types)
189-
.Where(x => x.ProductId == prodId)
190-
.SelectMany(x => x.Variants)
191-
.ToDictionaryAsync(k => k.Name, v => v.Types.ToImmutableArray()))
187+
.Include(x => x.Variants)
188+
.ThenInclude(x => x.Types)
189+
.Where(x => x.ProductId == prodId)
190+
.SelectMany(x => x.Variants)
191+
.ToDictionaryAsync(k => k.Name, v => v.Types.ToImmutableArray()))
192192
.ToImmutableSortedDictionary();
193193

194194
var reqCombo = combo.ToImmutableSortedDictionary();

TSystems.LoveOTC/TSystems.LoveOTC.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
</PropertyGroup>
2626

2727
<ItemGroup>
28+
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.1" />
2829
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
2930
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.1" />
3031
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.1" />

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@lexical/utils": "0.10.0",
3636
"@microsoft/signalr": "^8.0.0",
3737
"@microsoft/signalr-protocol-msgpack": "^8.0.0",
38-
"ahooks": "^3.7.9",
38+
"ahooks": "^3.7.10",
3939
"dayjs": "^1.11.10",
4040
"dexie": "^3.2.4",
4141
"dexie-react-hooks": "^1.1.7",
@@ -52,9 +52,9 @@
5252
},
5353
"devDependencies": {
5454
"@types/lodash-es": "^4.17.12",
55-
"@types/react": "^18.2.48",
55+
"@types/react": "^18.2.52",
5656
"@types/react-dom": "^18.2.18",
57-
"@vitejs/plugin-react-swc": "^3.5.0",
57+
"@vitejs/plugin-react-swc": "^3.6.0",
5858
"typescript": "^5.3.3",
5959
"vite": "^5.0.12"
6060
}

0 commit comments

Comments
 (0)