using System.Collections.Generic; using System.Text.Json; using WmsLite.Api.Common; using WmsLite.Api.Models; using WmsLite.Api.Repositories.Interfaces; using WmsLite.Api.Services.Interfaces; namespace WmsLite.Api.Services; public class TemplateFieldValidationService : ITemplateFieldValidationService { private readonly ISysTemplateRepository _sysTemplateRepository; public TemplateFieldValidationService(ISysTemplateRepository sysTemplateRepository) { _sysTemplateRepository = sysTemplateRepository; } public async Task ValidateAttrsAsync( string tenantId, string ownerCode, string? bizType, Dictionary? attrs) { if (attrs is null || attrs.Count == 0) { return await _sysTemplateRepository.GetByBizTypeAsync(tenantId, ownerCode, bizType); } var template = await RequireTemplateAsync(tenantId, ownerCode, bizType); var fields = ParseFields(template); foreach (var key in attrs.Keys) { if (!fields.TryGetValue(key, out var field)) { throw new BusinessException($"Attrs field '{key}' is not defined in template '{bizType}'."); } if (!MatchesType(attrs[key], field.Type)) { throw new BusinessException($"Attrs field '{key}' does not match template type '{field.Type}'."); } } foreach (var field in fields.Values) { if (field.Required && (!attrs.TryGetValue(field.Field, out var value) || IsEmpty(value))) { throw new BusinessException($"Attrs field '{field.Field}' is required by template '{bizType}'."); } } return template; } public async Task ValidateSearchParamAsync( string tenantId, string ownerCode, string? bizType, Dictionary? searchParam) { if (searchParam is null || searchParam.Count == 0) { return await _sysTemplateRepository.GetByBizTypeAsync(tenantId, ownerCode, bizType); } var template = await RequireTemplateAsync(tenantId, ownerCode, bizType); var fields = ParseFields(template); foreach (var entry in searchParam) { if (!fields.TryGetValue(entry.Key, out var field)) { throw new BusinessException($"SearchParam field '{entry.Key}' is not defined in template '{bizType}'."); } if (!field.AllowQuery) { throw new BusinessException($"SearchParam field '{entry.Key}' is not queryable."); } if (!MatchesType(entry.Value, field.Type)) { throw new BusinessException($"SearchParam field '{entry.Key}' does not match template type '{field.Type}'."); } } return template; } private async Task RequireTemplateAsync(string tenantId, string ownerCode, string? bizType) { if (string.IsNullOrWhiteSpace(bizType)) { throw new BusinessException("BizType is required when attrs or SearchParam is provided."); } var template = await _sysTemplateRepository.GetByBizTypeAsync(tenantId, ownerCode, bizType); if (template is null || string.IsNullOrWhiteSpace(template.ConfigJson)) { throw new BusinessException($"Template '{bizType}' is not configured."); } return template; } private static Dictionary ParseFields(SysTemplate template) { var fields = new Dictionary(StringComparer.OrdinalIgnoreCase); if (string.IsNullOrWhiteSpace(template.ConfigJson)) { return fields; } var parsed = TemplateConfigJsonValidator.ValidateAndParse(template.ConfigJson); foreach (var f in parsed) { fields[f.Field] = new TemplateFieldDefinition( f.Field, f.Type, f.Required, f.AllowQuery); } return fields; } internal static bool MatchesType(object? value, string type) { if (value is null) { return true; } var normalized = type.Trim().ToLowerInvariant(); if (value is JsonElement json) { return normalized switch { "number" or "decimal" => json.ValueKind == JsonValueKind.Number || IsDecimalString(json), "int" => json.ValueKind == JsonValueKind.Number ? json.TryGetInt32(out _) : int.TryParse(json.ToString(), out _), "bool" => json.ValueKind is JsonValueKind.True or JsonValueKind.False || bool.TryParse(json.ToString(), out _), "date" or "datetime" => DateTime.TryParse(json.ToString(), out _), _ => json.ValueKind is JsonValueKind.String or JsonValueKind.Number or JsonValueKind.True or JsonValueKind.False }; } return normalized switch { "number" or "decimal" => value is byte or short or int or long or float or double or decimal || IsDecimalString(value.ToString()), "int" => value switch { byte or short or int or long => true, _ => int.TryParse(value.ToString(), out _) }, "bool" => value is bool || bool.TryParse(value.ToString(), out _), "date" or "datetime" => value is DateTime || DateTime.TryParse(value.ToString(), out _), _ => true }; } private static bool IsDecimalString(string? s) { return !string.IsNullOrWhiteSpace(s) && decimal.TryParse(s, out _); } private static bool IsDecimalString(JsonElement json) { if (json.ValueKind == JsonValueKind.Number) { return true; } return json.ValueKind == JsonValueKind.String && decimal.TryParse(json.GetString(), out _); } private static bool IsEmpty(object? value) { return value is null || string.IsNullOrWhiteSpace(value.ToString()); } private sealed record TemplateFieldDefinition(string Field, string Type, bool Required, bool AllowQuery); }