111 lines
4.9 KiB
C#
111 lines
4.9 KiB
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using FreeSql;
|
|
using F9WebAiTest.Domain.Entities.SYS.YuanliaoZhuangkous;
|
|
using F9WebAiTest.Domain.Enums.SYS.YuanliaoZhuangkous;
|
|
|
|
namespace F9WebAiTest.Infrastructure.Data;
|
|
|
|
/// <summary>
|
|
/// 原料庄口数据导入:来源于 F9 系统 biz_base_yuanliaozhuangkou 表导出数据。
|
|
/// 幂等:按原始 Id 判重,已存在则跳过。
|
|
/// </summary>
|
|
public static class YuanliaoZhuangkouSeedData
|
|
{
|
|
private const string Resource = "F9WebAiTest.Infrastructure.Data.SeedData.biz_base_yuanliaozhuangkou.json";
|
|
|
|
private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true };
|
|
|
|
public static void Seed(IFreeSql fsql)
|
|
{
|
|
try
|
|
{
|
|
var items = Load();
|
|
if (items.Count == 0) return;
|
|
|
|
var existIds = fsql.Select<YuanliaoZhuangkou>()
|
|
.DisableGlobalFilter("软删除")
|
|
.ToList()
|
|
.Select(x => x.Id)
|
|
.ToHashSet();
|
|
|
|
var inserts = new List<YuanliaoZhuangkou>();
|
|
foreach (var item in items)
|
|
{
|
|
if (item.Id <= 0 || existIds.Contains(item.Id)) continue;
|
|
if (string.IsNullOrWhiteSpace(item.Name)) continue;
|
|
|
|
inserts.Add(new YuanliaoZhuangkou(item.Id, item.Year, item.Name.Trim(), item.Code ?? string.Empty,
|
|
item.Jibie ?? string.Empty, (BendiWaidi_Enum)item.Bendiwaidi, item.Chandi ?? string.Empty,
|
|
item.Canpinzhong ?? string.Empty, item.Wzmlid, item.Zhandian ?? string.Empty, item.Pici)
|
|
{
|
|
CreatedAt = ParseLegacyDate(item.CreatedAt) ?? DateTime.Now,
|
|
CreatedBy = item.CreatedBy ?? string.Empty,
|
|
UpdatedAt = ParseLegacyDate(item.UpdatedAt),
|
|
UpdateBy = item.UpdateBy ?? string.Empty
|
|
});
|
|
}
|
|
|
|
if (inserts.Count > 0) fsql.Insert(inserts).ExecuteAffrows();
|
|
Console.WriteLine($"[YuanliaoZhuangkouSeed] 原料庄口:来源 {items.Count} 条,新增 {inserts.Count} 条,跳过已存在 {items.Count - inserts.Count} 条");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[YuanliaoZhuangkouSeed] 原料庄口数据导入跳过:{ex.Message}");
|
|
}
|
|
}
|
|
|
|
private static List<YuanliaoZhuangkouSeedItem> Load()
|
|
{
|
|
var assembly = typeof(YuanliaoZhuangkouSeedData).Assembly;
|
|
using var stream = assembly.GetManifestResourceStream(Resource);
|
|
if (stream == null)
|
|
{
|
|
Console.WriteLine($"[YuanliaoZhuangkouSeed] 未找到内嵌资源:{Resource}");
|
|
return new List<YuanliaoZhuangkouSeedItem>();
|
|
}
|
|
return JsonSerializer.Deserialize<List<YuanliaoZhuangkouSeedItem>>(stream, JsonOptions) ?? new List<YuanliaoZhuangkouSeedItem>();
|
|
}
|
|
|
|
/// <summary>解析 F9 导出的日期(形如 d/M/yyyy H:mm:ss.fff),1970 占位值视为未更新返回 null</summary>
|
|
private static readonly string[] LegacyDateFormats =
|
|
{
|
|
"d/M/yyyy H:mm:ss.fff", "d/M/yyyy H:mm:ss", "d/M/yyyy H:mm",
|
|
"M/d/yyyy H:mm:ss.fff", "M/d/yyyy H:mm:ss", "M/d/yyyy H:mm",
|
|
"yyyy-MM-dd HH:mm:ss.fff", "yyyy-MM-dd HH:mm:ss"
|
|
};
|
|
|
|
private static DateTime? ParseLegacyDate(string value)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value)) return null;
|
|
|
|
if (DateTime.TryParseExact(value, LegacyDateFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out var exact))
|
|
return exact.Year <= 1900 ? null : exact;
|
|
|
|
if (DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var any))
|
|
return any.Year <= 1900 ? null : any;
|
|
|
|
return null;
|
|
}
|
|
|
|
private sealed class YuanliaoZhuangkouSeedItem
|
|
{
|
|
[JsonPropertyName("id")] public long Id { get; set; }
|
|
[JsonPropertyName("created_by")] public string CreatedBy { get; set; }
|
|
[JsonPropertyName("created_at")] public string CreatedAt { get; set; }
|
|
[JsonPropertyName("update_by")] public string UpdateBy { get; set; }
|
|
[JsonPropertyName("updated_at")] public string UpdatedAt { get; set; }
|
|
[JsonPropertyName("year")] public int Year { get; set; }
|
|
[JsonPropertyName("name")] public string Name { get; set; }
|
|
[JsonPropertyName("code")] public string Code { get; set; }
|
|
[JsonPropertyName("jibie")] public string Jibie { get; set; }
|
|
[JsonPropertyName("bendiwaidi")] public int Bendiwaidi { get; set; }
|
|
[JsonPropertyName("chandi")] public string Chandi { get; set; }
|
|
[JsonPropertyName("canpinzhong")] public string Canpinzhong { get; set; }
|
|
[JsonPropertyName("wzmlid")] public long Wzmlid { get; set; }
|
|
[JsonPropertyName("zhandian")] public string Zhandian { get; set; }
|
|
[JsonPropertyName("pici")] public int Pici { get; set; }
|
|
}
|
|
}
|