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

Commit

Permalink
[APP] ExportOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloento committed Feb 4, 2024
1 parent 0d267d5 commit cad3f61
Show file tree
Hide file tree
Showing 10 changed files with 690 additions and 438 deletions.
148 changes: 148 additions & 0 deletions TSystems.LoveOTC/AdminHub/Order/Export.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
namespace TSystems.LoveOTC.AdminHub;

using System.Collections.Immutable;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using Entities;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;

internal partial class AdminHub {
private static DateTime lastExport = DateTime.MinValue;

private static readonly IImmutableList<string> headers = new[] {
"Order Id",
"Order Time",
"Recipient Name",
"E-Mail",
"Phone",
"Address",
"Product Name",
"Types",
"Quantity"
}.ToImmutableArray();

/**
* <remarks>
* @author Aloento
* @since 1.2.0
* @version 0.1.0
* </remarks>
*/
public async IAsyncEnumerable<byte[]> ExportOrder() {
if (DateTime.Now - lastExport < TimeSpan.FromMinutes(3))
throw new HubException("The time interval between two exports shall not be less than 3 minutes.");

lastExport = DateTime.Now;

using var stream = new MemoryStream();
using var document = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, true);

var workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new();

var worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
var sheetData = new SheetData();
worksheetPart.Worksheet = new(sheetData);

var sheets = workbookPart.Workbook.AppendChild(new Sheets());
var sheet = new Sheet {
Id = workbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "ProcessingOrder"
};
sheets.Append(sheet);

var headerRow = new Row();
_ = sheetData.AppendChild(headerRow);
headerRow.Append(headers.Select(x => new Cell {
DataType = CellValues.String,
CellValue = new(x)
}));

var userIds = await this.Db.Orders
.Where(x => x.Status == OrderStatus.Processing)
.Select(x => x.UserId)
.Distinct()
.ToArrayAsync();

foreach (var userId in userIds) {
var records = this.Db.OrderCombos
.Where(x => x.Order.UserId == userId)
.Where(x => x.Order.Status == OrderStatus.Processing)
.Include(x => x.Order)
.ThenInclude(o => o.User)
.Include(x => x.Combo)
.ThenInclude(c => c.Product)
.Include(x => x.Combo)
.ThenInclude(c => c.Types)
.ThenInclude(t => t.Variant)
.AsAsyncEnumerable();

var first = true;
await foreach (var record in records) {
var order = record.Order;
var user = order.User;
var combo = record.Combo;

var data = new List<string>(9) {
record.OrderId.ToString(),
order.CreateAt.ToString("yyyy-MM-dd HH:mm:ss"),
user.Name
};

if (first) {
data.AddRange([
user.EMail,
user.Phone,
user.Address
]);
first = false;
} else
data.AddRange([
"-", "-", "-"
]);

data.Add(combo.Product.Name);

var types = combo.Types.Aggregate(
new StringBuilder(),
(prev, curr) => {
_ = prev.Append(curr.Variant.Name);
_ = prev.Append(" : ");
_ = prev.Append(curr.Name);
_ = prev.Append(" ; ");
return prev;
})
.ToString();

data.AddRange([
types,
record.Quantity.ToString()
]);

var row = new Row();
_ = sheetData.AppendChild(row);
row.Append(data.Select(x => new Cell {
DataType = CellValues.String,
CellValue = new(x)
}));
}

var emptyRow = new Row();
_ = sheetData.AppendChild(emptyRow);
}

workbookPart.Workbook.Save();
document.Save();
stream.Position = 0;

var buffer = new byte[30 * 1024];
int bytesRead;

while ((bytesRead = await stream.ReadAsync(buffer)) > 0)
yield return buffer[..bytesRead];
}
}
12 changes: 6 additions & 6 deletions TSystems.LoveOTC/AdminHub/Product/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public async Task<uint> ProductPostVariant(uint prodId, string name) {

var temp = await this.Db.Variants.AddAsync(new() {
ProductId = prodId,
Name = name,
Name = name
});

await this.Db.SaveChangesAsync();
Expand Down Expand Up @@ -184,11 +184,11 @@ public async Task<uint> ProductPostType(uint variantId, string name) {
*/
public async Task<uint> ProductPostCombo(uint prodId, Dictionary<string, string> combo, ushort stock) {
var variTypesDb = (await this.Db.Products
.Include(x => x.Variants)
.ThenInclude(x => x.Types)
.Where(x => x.ProductId == prodId)
.SelectMany(x => x.Variants)
.ToDictionaryAsync(k => k.Name, v => v.Types.ToImmutableArray()))
.Include(x => x.Variants)
.ThenInclude(x => x.Types)
.Where(x => x.ProductId == prodId)
.SelectMany(x => x.Variants)
.ToDictionaryAsync(k => k.Name, v => v.Types.ToImmutableArray()))
.ToImmutableSortedDictionary();

var reqCombo = combo.ToImmutableSortedDictionary();
Expand Down
1 change: 1 addition & 0 deletions TSystems.LoveOTC/TSystems.LoveOTC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.1" />
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.1" />
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@lexical/utils": "0.10.0",
"@microsoft/signalr": "^8.0.0",
"@microsoft/signalr-protocol-msgpack": "^8.0.0",
"ahooks": "^3.7.9",
"ahooks": "^3.7.10",
"dayjs": "^1.11.10",
"dexie": "^3.2.4",
"dexie-react-hooks": "^1.1.7",
Expand All @@ -52,9 +52,9 @@
},
"devDependencies": {
"@types/lodash-es": "^4.17.12",
"@types/react": "^18.2.48",
"@types/react": "^18.2.52",
"@types/react-dom": "^18.2.18",
"@vitejs/plugin-react-swc": "^3.5.0",
"@vitejs/plugin-react-swc": "^3.6.0",
"typescript": "^5.3.3",
"vite": "^5.0.12"
}
Expand Down
Loading

0 comments on commit cad3f61

Please sign in to comment.