完善基础数据
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Domain.Entities.SYS.Gongyingshangs;
|
||||
using F9WebAiTest.Domain.Enums.SYS.Gongyingshangs;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.Data;
|
||||
|
||||
/// <summary>
|
||||
/// 往来单位(供应商/客户)数据导入:来源于 F9 系统 biz_base_gongyingshang 表导出数据。
|
||||
/// 幂等:按原始 Id 判重,已存在则跳过;名称 + 业务类型重复的也跳过。
|
||||
/// </summary>
|
||||
public static class GongyingshangSeedData
|
||||
{
|
||||
private const string Resource = "F9WebAiTest.Infrastructure.Data.SeedData.biz_base_gongyingshang.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 existing = fsql.Select<Gongyingshang>().DisableGlobalFilter("软删除").ToList();
|
||||
var existIds = existing.Select(x => x.Id).ToHashSet();
|
||||
var existKeys = existing.Select(x => $"{x.Type}|{x.Name}|{x.BusinessGroup}").ToHashSet();
|
||||
|
||||
var inserts = new List<Gongyingshang>();
|
||||
var duplicate = 0;
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (item.Id <= 0 || existIds.Contains(item.Id)) continue;
|
||||
if (string.IsNullOrWhiteSpace(item.Name)) continue;
|
||||
|
||||
var key = $"{(Gys_Type_Enum)item.BusinessType}|{item.Name.Trim()}|{(Gys_Group_Enum)item.BusinessGroup}";
|
||||
if (existKeys.Contains(key) || inserts.Any(x => $"{x.Type}|{x.Name}|{x.BusinessGroup}" == key))
|
||||
{
|
||||
duplicate++;
|
||||
continue;
|
||||
}
|
||||
|
||||
inserts.Add(new Gongyingshang(item.Id, (Gys_Type_Enum)item.BusinessType, item.Name.Trim(),
|
||||
item.Phone ?? string.Empty, item.Lianxiren ?? string.Empty, item.Address ?? string.Empty,
|
||||
item.Pinyin ?? string.Empty, (Gys_Group_Enum)item.BusinessGroup, (Gys_SaleType_Enum)item.SaleType)
|
||||
{
|
||||
CreatedAt = ParseLegacyDate(item.CreatedAt) ?? DateTime.Now,
|
||||
CreatedBy = item.CreatedBy ?? string.Empty,
|
||||
UpdatedAt = ParseLegacyDate(item.UpdatedAt)
|
||||
});
|
||||
}
|
||||
|
||||
if (inserts.Count > 0) fsql.Insert(inserts).ExecuteAffrows();
|
||||
Console.WriteLine($"[GongyingshangSeed] 往来单位:来源 {items.Count} 条,新增 {inserts.Count} 条,重复 {duplicate} 条");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[GongyingshangSeed] 往来单位数据导入跳过:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static List<GongyingshangSeedItem> Load()
|
||||
{
|
||||
var assembly = typeof(GongyingshangSeedData).Assembly;
|
||||
using var stream = assembly.GetManifestResourceStream(Resource);
|
||||
if (stream == null)
|
||||
{
|
||||
Console.WriteLine($"[GongyingshangSeed] 未找到内嵌资源:{Resource}");
|
||||
return new List<GongyingshangSeedItem>();
|
||||
}
|
||||
return JsonSerializer.Deserialize<List<GongyingshangSeedItem>>(stream, JsonOptions) ?? new List<GongyingshangSeedItem>();
|
||||
}
|
||||
|
||||
/// <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 GongyingshangSeedItem
|
||||
{
|
||||
[JsonPropertyName("id")] public long Id { get; set; }
|
||||
[JsonPropertyName("created_by")] public string CreatedBy { get; set; }
|
||||
[JsonPropertyName("created_at")] public string CreatedAt { get; set; }
|
||||
[JsonPropertyName("updated_at")] public string UpdatedAt { get; set; }
|
||||
[JsonPropertyName("businesstype")] public int BusinessType { get; set; }
|
||||
[JsonPropertyName("name")] public string Name { get; set; }
|
||||
[JsonPropertyName("phone")] public string Phone { get; set; }
|
||||
[JsonPropertyName("lianxiren")] public string Lianxiren { get; set; }
|
||||
[JsonPropertyName("address")] public string Address { get; set; }
|
||||
[JsonPropertyName("pinyin")] public string Pinyin { get; set; }
|
||||
[JsonPropertyName("businessgroup")] public int BusinessGroup { get; set; }
|
||||
|
||||
/// <summary>F9 导出字段名为 Sale_Type</summary>
|
||||
[JsonPropertyName("Sale_Type")] public int SaleType { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user