111
This commit is contained in:
+24
-6
@@ -179,6 +179,16 @@ public class OnboardingContractTemplateService : DbRepository<ObContractTemplate
|
||||
await sourceStream.CopyToAsync(sourceBytes);
|
||||
var bytes = sourceBytes.ToArray();
|
||||
var variableCodes = DocxTemplateProcessor.ExtractVariableCodes(bytes);
|
||||
|
||||
// 严格比对变量字典库,防止手误和非法占位符
|
||||
var dictList = await Context.Queryable<ObVariableDefinition>().ToListAsync();
|
||||
var dictMap = dictList.ToDictionary(it => it.VariableCode, it => it, StringComparer.OrdinalIgnoreCase);
|
||||
var undefinedCodes = variableCodes.Where(code => !dictMap.ContainsKey(code)).ToList();
|
||||
if (undefinedCodes.Count > 0)
|
||||
{
|
||||
throw Oops.Bah($"DOCX 模板包含未在字典库中注册的占位符:{string.Join(",", undefinedCodes)}。请检查拼写或先在【变量字典】中添加该变量。");
|
||||
}
|
||||
|
||||
var versionId = global::SimpleAdmin.Core.Utils.CommonUtils.GetSingleId();
|
||||
var version = input.Adapt<ObContractTemplateVersion>();
|
||||
version.Id = versionId;
|
||||
@@ -186,7 +196,12 @@ public class OnboardingContractTemplateService : DbRepository<ObContractTemplate
|
||||
version.FileHash = string.IsNullOrWhiteSpace(input.FileHash)
|
||||
? Convert.ToHexString(global::System.Security.Cryptography.SHA256.HashData(bytes))
|
||||
: input.FileHash;
|
||||
var variables = variableCodes.Select((code, index) => BuildDetectedVariable(versionId, code, index)).ToList();
|
||||
|
||||
var variables = variableCodes.Select((code, index) =>
|
||||
{
|
||||
var def = dictMap.TryGetValue(code, out var d) ? d : null;
|
||||
return BuildDetectedVariable(versionId, code, def, index);
|
||||
}).ToList();
|
||||
|
||||
await Context.Ado.BeginTranAsync();
|
||||
try
|
||||
@@ -263,7 +278,7 @@ public class OnboardingContractTemplateService : DbRepository<ObContractTemplate
|
||||
await Context.Updateable(entity).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
private static ObTemplateVariable BuildDetectedVariable(long versionId, string code, int index)
|
||||
private static ObTemplateVariable BuildDetectedVariable(long versionId, string code, ObVariableDefinition def, int index)
|
||||
{
|
||||
var separatorIndex = code.IndexOf('.');
|
||||
var prefix = separatorIndex > 0 ? code[..separatorIndex].ToLowerInvariant() : string.Empty;
|
||||
@@ -277,21 +292,24 @@ public class OnboardingContractTemplateService : DbRepository<ObContractTemplate
|
||||
_ => ContractVariableType.Unresolved
|
||||
};
|
||||
|
||||
var varName = def != null && !string.IsNullOrWhiteSpace(def.VariableName) ? def.VariableName : sourceField;
|
||||
var dataType = def != null && !string.IsNullOrWhiteSpace(def.DataType) ? def.DataType : InferDataType(sourceField);
|
||||
|
||||
return new ObTemplateVariable
|
||||
{
|
||||
Id = global::SimpleAdmin.Core.Utils.CommonUtils.GetSingleId(),
|
||||
TemplateVersionId = versionId,
|
||||
VariableCode = code,
|
||||
VariableName = sourceField,
|
||||
VariableName = varName,
|
||||
VariableType = variableType,
|
||||
DataType = InferDataType(sourceField),
|
||||
DataType = dataType,
|
||||
EmployeeField = variableType is ContractVariableType.Employee or ContractVariableType.Company
|
||||
? sourceField
|
||||
: null,
|
||||
Required = true,
|
||||
Required = def?.Required ?? true,
|
||||
HrEditable = variableType == ContractVariableType.Contract,
|
||||
SortCode = (index + 1) * 10,
|
||||
Remark = variableType == ContractVariableType.Unresolved ? "导入时未识别变量来源,请管理员确认" : null
|
||||
Remark = def?.Remark
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user