LocationBatchExcelHelper.cs
11.9 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System.Globalization;
using ClosedXML.Excel;
using FoodLabeling.Application.Contracts.Dtos.Location;
namespace FoodLabeling.Application.Helpers;
/// <summary>
/// Location 批量导入/导出 Excel(列名与 Web「Location Manager」表头对齐,兼容中英文表头别名)
/// </summary>
public static class LocationBatchExcelHelper
{
/// <summary>导出表头顺序(与模板一致)</summary>
public static readonly string[] ExportHeaders =
{
"Company", "Region", "Location ID", "Location Name", "Street", "City", "State", "Country",
"Zip Code", "Phone", "Email", "Latitude", "Longitude", "Active"
};
/// <summary>
/// 将门店列表写入 xlsx 内存流。
/// </summary>
public static MemoryStream BuildExportWorkbook(IReadOnlyList<LocationGetListOutputDto> rows)
{
var ms = new MemoryStream();
using var wb = new XLWorkbook();
var ws = wb.AddWorksheet("Locations");
for (var i = 0; i < ExportHeaders.Length; i++)
{
ws.Cell(1, i + 1).Value = ExportHeaders[i];
ws.Cell(1, i + 1).Style.Font.Bold = true;
}
var r = 2;
foreach (var x in rows)
{
ws.Cell(r, 1).Value = x.Partner ?? string.Empty;
ws.Cell(r, 2).Value = x.GroupName ?? string.Empty;
ws.Cell(r, 3).Value = x.LocationCode ?? string.Empty;
ws.Cell(r, 4).Value = x.LocationName ?? string.Empty;
ws.Cell(r, 5).Value = x.Street ?? string.Empty;
ws.Cell(r, 6).Value = x.City ?? string.Empty;
ws.Cell(r, 7).Value = x.StateCode ?? string.Empty;
ws.Cell(r, 8).Value = x.Country ?? string.Empty;
ws.Cell(r, 9).Value = x.ZipCode ?? string.Empty;
ws.Cell(r, 10).Value = x.Phone ?? string.Empty;
ws.Cell(r, 11).Value = x.Email ?? string.Empty;
ws.Cell(r, 12).Value = x.Latitude?.ToString(CultureInfo.InvariantCulture) ?? string.Empty;
ws.Cell(r, 13).Value = x.Longitude?.ToString(CultureInfo.InvariantCulture) ?? string.Empty;
ws.Cell(r, 14).Value = x.State ? "TRUE" : "FALSE";
r++;
}
ws.Columns().AdjustToContents();
wb.SaveAs(ms);
ms.Position = 0;
return ms;
}
/// <summary>
/// 从上传的 Excel 解析为创建入参列表(行号从 2 起为数据行)。
/// </summary>
public static List<(int RowNumber, LocationCreateInputVo Input)> ParseImportWorkbook(Stream stream,
int maxRows,
out List<LocationBatchImportErrorDto> parseErrors)
{
parseErrors = new List<LocationBatchImportErrorDto>();
var result = new List<(int, LocationCreateInputVo)>();
using var wb = new XLWorkbook(stream);
var ws = wb.Worksheets.FirstOrDefault();
if (ws is null)
{
parseErrors.Add(new LocationBatchImportErrorDto
{
RowNumber = 0,
Message = "Excel 中无工作表"
});
return result;
}
var headerRow = ws.Row(1);
if (!headerRow.CellsUsed().Any())
{
parseErrors.Add(new LocationBatchImportErrorDto { RowNumber = 1, Message = "表头为空" });
return result;
}
var colMap = BuildHeaderColumnMap(headerRow);
if (!colMap.ContainsKey("locationcode"))
{
parseErrors.Add(new LocationBatchImportErrorDto
{
RowNumber = 1,
Message = "未找到「Location ID」列(或同义表头),请使用官方模板"
});
return result;
}
var lastRow = ws.LastRowUsed()?.RowNumber() ?? 1;
var dataRowCount = 0;
for (var rowNum = 2; rowNum <= lastRow; rowNum++)
{
if (dataRowCount >= maxRows)
{
parseErrors.Add(new LocationBatchImportErrorDto
{
RowNumber = rowNum,
Message = $"已超过单次导入上限 {maxRows} 行,后续行已忽略"
});
break;
}
var locCode = GetCellByField(colMap, ws, rowNum, "locationcode");
if (string.IsNullOrWhiteSpace(locCode) && IsRowEmpty(colMap, ws, rowNum))
{
continue;
}
dataRowCount++;
var errPrefix = $"第 {rowNum} 行";
try
{
var input = BuildCreateInputFromRow(colMap, ws, rowNum, out var rowErrs);
if (rowErrs.Count > 0)
{
foreach (var e in rowErrs)
{
parseErrors.Add(new LocationBatchImportErrorDto
{
RowNumber = rowNum,
LocationCode = locCode,
Message = $"{errPrefix}:{e}"
});
}
continue;
}
result.Add((rowNum, input!));
}
catch (Exception ex)
{
parseErrors.Add(new LocationBatchImportErrorDto
{
RowNumber = rowNum,
LocationCode = locCode,
Message = $"{errPrefix}:{ex.Message}"
});
}
}
return result;
}
private static Dictionary<string, int> BuildHeaderColumnMap(IXLRow headerRow)
{
var map = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
foreach (var cell in headerRow.CellsUsed())
{
var key = NormalizeHeaderKey(cell.GetString());
if (string.IsNullOrEmpty(key))
{
continue;
}
var field = MapHeaderToField(key);
if (field is null)
{
continue;
}
if (!map.ContainsKey(field))
{
map[field] = cell.Address.ColumnNumber;
}
}
return map;
}
private static string? MapHeaderToField(string normalizedHeader)
{
return normalizedHeader switch
{
"company" or "partner" or "合作伙伴" or "公司" => "partner",
"region" or "groupname" or "group" or "区域" or "组织" => "groupname",
"locationid" or "locationcode" or "门店编码" or "门店id" => "locationcode",
"locationname" or "门店名称" or "name" => "locationname",
"street" or "地址" => "street",
"city" or "城市" => "city",
"state" or "statecode" or "省州" => "statecode",
"country" or "国家" => "country",
"zipcode" or "zip" or "邮编" or "邮政编码" => "zipcode",
"phone" or "电话" or "手机" => "phone",
"email" or "邮箱" => "email",
"latitude" or "lat" or "纬度" => "latitude",
"longitude" or "lng" or "lon" or "经度" => "longitude",
"active" or "启用" or "状态" or "isactive" => "active",
_ => null
};
}
private static string NormalizeHeaderKey(string raw)
{
var s = raw.Trim();
if (s.Length > 0 && s[0] == '\uFEFF')
{
s = s.TrimStart('\uFEFF');
}
s = s.Trim();
return string.Concat(s.Where(c => !char.IsWhiteSpace(c))).ToLowerInvariant();
}
private static bool IsRowEmpty(Dictionary<string, int> colMap, IXLWorksheet ws, int rowNum)
{
foreach (var col in colMap.Values)
{
var t = ws.Cell(rowNum, col).GetString().Trim();
if (!string.IsNullOrEmpty(t))
{
return false;
}
}
return true;
}
private static string GetCellByField(Dictionary<string, int> colMap, IXLWorksheet ws, int row, string field)
{
if (!colMap.TryGetValue(field, out var col))
{
return string.Empty;
}
return ws.Cell(row, col).GetString().Trim();
}
private static LocationCreateInputVo? BuildCreateInputFromRow(Dictionary<string, int> colMap, IXLWorksheet ws,
int rowNum, out List<string> errors)
{
errors = new List<string>();
var partner = GetCellByField(colMap, ws, rowNum, "partner");
var groupName = GetCellByField(colMap, ws, rowNum, "groupname");
var locationCode = GetCellByField(colMap, ws, rowNum, "locationcode");
var locationName = GetCellByField(colMap, ws, rowNum, "locationname");
var street = GetCellByField(colMap, ws, rowNum, "street");
var city = GetCellByField(colMap, ws, rowNum, "city");
var stateCode = GetCellByField(colMap, ws, rowNum, "statecode");
var country = GetCellByField(colMap, ws, rowNum, "country");
var zipCode = GetCellByField(colMap, ws, rowNum, "zipcode");
var phone = GetCellByField(colMap, ws, rowNum, "phone");
var email = GetCellByField(colMap, ws, rowNum, "email");
var latStr = GetCellByField(colMap, ws, rowNum, "latitude");
var lngStr = GetCellByField(colMap, ws, rowNum, "longitude");
var activeStr = GetCellByField(colMap, ws, rowNum, "active");
if (string.IsNullOrWhiteSpace(locationCode))
{
errors.Add("Location ID 不能为空");
}
if (string.IsNullOrWhiteSpace(locationName))
{
errors.Add("Location Name 不能为空");
}
decimal? lat = null;
if (!string.IsNullOrWhiteSpace(latStr))
{
if (!decimal.TryParse(latStr, NumberStyles.Any, CultureInfo.InvariantCulture, out var latVal))
{
errors.Add("Latitude 格式不正确");
}
else
{
lat = latVal;
}
}
decimal? lng = null;
if (!string.IsNullOrWhiteSpace(lngStr))
{
if (!decimal.TryParse(lngStr, NumberStyles.Any, CultureInfo.InvariantCulture, out var lngVal))
{
errors.Add("Longitude 格式不正确");
}
else
{
lng = lngVal;
}
}
if (errors.Count > 0)
{
return null;
}
var state = ParseBool(activeStr, defaultValue: true);
return new LocationCreateInputVo
{
Partner = NullIfEmpty(partner),
GroupName = NullIfEmpty(groupName),
LocationCode = locationCode,
LocationName = locationName,
Street = NullIfEmpty(street),
City = NullIfEmpty(city),
StateCode = NullIfEmpty(stateCode),
Country = NullIfEmpty(country),
ZipCode = NullIfEmpty(zipCode),
Phone = NullIfEmpty(phone),
Email = NullIfEmpty(email),
Latitude = lat,
Longitude = lng,
State = state
};
}
private static string? NullIfEmpty(string s)
{
var t = s.Trim();
return string.IsNullOrEmpty(t) ? null : t;
}
private static bool ParseBool(string? raw, bool defaultValue)
{
if (string.IsNullOrWhiteSpace(raw))
{
return defaultValue;
}
var s = raw.Trim();
if (bool.TryParse(s, out var b))
{
return b;
}
if (int.TryParse(s, out var n))
{
return n != 0;
}
if (string.Equals(s, "是", StringComparison.Ordinal) ||
string.Equals(s, "Y", StringComparison.OrdinalIgnoreCase) ||
string.Equals(s, "Yes", StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (string.Equals(s, "否", StringComparison.Ordinal) ||
string.Equals(s, "N", StringComparison.OrdinalIgnoreCase) ||
string.Equals(s, "No", StringComparison.OrdinalIgnoreCase))
{
return false;
}
return defaultValue;
}
}