LabelTemplateScopeSchemaHelper.cs
5.26 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
using SqlSugar;
namespace FoodLabeling.Application.Helpers;
/// <summary>
/// 探测标签模板 Company/Region scope 列与关联表是否已迁移(<c>fl_label_template_scope.sql</c>)。
/// 未迁移时 ORM 实体不含 <c>AppliedPartnerType</c>/<c>AppliedRegionType</c>,读写走 raw SQL 或关联表推断。
/// </summary>
public static class LabelTemplateScopeSchemaHelper
{
private static LabelTemplateScopeSchemaStatus? _cached;
public sealed class LabelTemplateScopeSchemaStatus
{
public bool HasAppliedPartnerTypeColumn { get; init; }
public bool HasAppliedRegionTypeColumn { get; init; }
public bool HasPartnerScopeTable { get; init; }
public bool HasRegionScopeTable { get; init; }
/// <summary>partner/region 关联表均已创建(用于多选明细)。</summary>
public bool HasPartnerRegionScopeTables => HasPartnerScopeTable && HasRegionScopeTable;
/// <summary>列 + 关联表均已迁移。</summary>
public bool IsFullSchema =>
HasAppliedPartnerTypeColumn
&& HasAppliedRegionTypeColumn
&& HasPartnerRegionScopeTables;
}
public static async Task<LabelTemplateScopeSchemaStatus> GetStatusAsync(ISqlSugarClient db)
{
if (_cached is not null)
{
return _cached;
}
try
{
var partnerColCount = await db.Ado.GetIntAsync(
"""
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'fl_label_template'
AND COLUMN_NAME = 'AppliedPartnerType'
""");
var regionColCount = await db.Ado.GetIntAsync(
"""
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'fl_label_template'
AND COLUMN_NAME = 'AppliedRegionType'
""");
var partnerTableCount = await db.Ado.GetIntAsync(
"""
SELECT COUNT(*)
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'fl_label_template_partner'
""");
var regionTableCount = await db.Ado.GetIntAsync(
"""
SELECT COUNT(*)
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'fl_label_template_region'
""");
_cached = new LabelTemplateScopeSchemaStatus
{
HasAppliedPartnerTypeColumn = partnerColCount > 0,
HasAppliedRegionTypeColumn = regionColCount > 0,
HasPartnerScopeTable = partnerTableCount > 0,
HasRegionScopeTable = regionTableCount > 0
};
}
catch
{
_cached = new LabelTemplateScopeSchemaStatus();
}
return _cached;
}
/// <summary>单元测试或切换库后调用。</summary>
public static void ResetCacheForTests() => _cached = null;
/// <summary>
/// 是否已创建 <c>fl_label_template_partner</c> 与 <c>fl_label_template_region</c>。
/// </summary>
public static async Task<bool> HasPartnerRegionScopeTablesAsync(ISqlSugarClient db)
{
var status = await GetStatusAsync(db);
return status.HasPartnerRegionScopeTables;
}
/// <summary>
/// 已迁移 scope 列时,写入 <c>AppliedPartnerType</c> / <c>AppliedRegionType</c>(未迁移则 no-op)。
/// </summary>
public static async Task SetAppliedScopeTypesAsync(
ISqlSugarClient db,
string templateId,
string appliedPartnerType,
string appliedRegionType)
{
if (string.IsNullOrWhiteSpace(templateId))
{
return;
}
var status = await GetStatusAsync(db);
if (!status.HasAppliedPartnerTypeColumn && !status.HasAppliedRegionTypeColumn)
{
return;
}
if (status.HasAppliedPartnerTypeColumn && status.HasAppliedRegionTypeColumn)
{
await db.Ado.ExecuteCommandAsync(
"""
UPDATE fl_label_template
SET AppliedPartnerType = @partnerType,
AppliedRegionType = @regionType
WHERE Id = @id
""",
new
{
partnerType = appliedPartnerType.Trim(),
regionType = appliedRegionType.Trim(),
id = templateId.Trim()
});
return;
}
if (status.HasAppliedPartnerTypeColumn)
{
await db.Ado.ExecuteCommandAsync(
"UPDATE fl_label_template SET AppliedPartnerType = @type WHERE Id = @id",
new { type = appliedPartnerType.Trim(), id = templateId.Trim() });
}
if (status.HasAppliedRegionTypeColumn)
{
await db.Ado.ExecuteCommandAsync(
"UPDATE fl_label_template SET AppliedRegionType = @type WHERE Id = @id",
new { type = appliedRegionType.Trim(), id = templateId.Trim() });
}
}
}