功能:按资源配置平台列表搜索

This commit is contained in:
czl231
2026-08-11 22:22:36 +08:00
parent 8d6ffca1e2
commit 95d0f7dc54
19 changed files with 741 additions and 62 deletions

View File

@@ -20,11 +20,12 @@ const (
// ResourceContract is the expected cross-layer representation of one resource.
type ResourceContract struct {
Domain string
Name string
Path string
PageKind string
Mode ResourceMode
Domain string
Name string
Path string
PageKind string
Mode ResourceMode
SearchFields []common.KeywordSearchField
}
// ResourceDefinition describes a route resource and the fields it may change.
@@ -94,7 +95,10 @@ func ExpectedResources() []ResourceContract {
}
func resourceContract(domain, name string, mode ResourceMode, pageKind string) ResourceContract {
return ResourceContract{Domain: domain, Name: name, Path: resourcePath(domain, name), Mode: mode, PageKind: pageKind}
return ResourceContract{
Domain: domain, Name: name, Path: resourcePath(domain, name), Mode: mode,
PageKind: pageKind, SearchFields: resourceSearchFields(name),
}
}
func resourcePath(domain, name string) string {

View File

@@ -0,0 +1,73 @@
// Package platform 定义平台总后台各资源的可见字段搜索策略。
// 版本v1.0.0
package platform
import (
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
)
type resourceSearchDefinition struct {
name string
model any
fields []common.KeywordSearchField
}
var resourceSearchDefinitions = []resourceSearchDefinition{
searchDefinition("gas_basic", &models.GasBasic{}, text("code"), text("name")),
searchDefinition("gas_account", &models.GasAccount{}, text("username"), text("display_name"), enum("role_code", value("admin", "气站管理员"))),
searchDefinition("delivery_basic", &models.DeliveryBasic{}, text("delivery_code"), text("name")),
searchDefinition("delivery_account", &models.DeliveryAccount{}, text("username"), text("display_name"), enum("role_code", value("admin", "配送点管理员"))),
searchDefinition("staff_account", &models.StaffAccount{}, text("username"), enum("role_code", value("installer", "安装人员"), value("delivery", "配送人员"), value("operations", "运维人员"))),
searchDefinition("staff_credential", &models.StaffCredential{}, text("credential_type")),
searchDefinition("user_account", &models.UserAccount{}, text("username")),
searchDefinition("producer_account", &models.ProducerAccount{}, text("name")),
searchDefinition("product_type", &models.ProductType{}, text("code"), text("name")),
searchDefinition("product_warehouse", &models.ProductWarehouse{}, text("code"), text("name")),
searchDefinition("product_info", &models.ProductInfo{}, text("code"), text("name")),
searchDefinition("product_repair", &models.ProductRepair{}, enum("result", value("pending", "待处理"), value("passed", "通过"), value("failed", "未通过"))),
searchDefinition("ec_product", &models.EcProduct{}, text("product_code"), text("name")),
searchDefinition("ec_product_attribute", &models.EcProductAttribute{}, text("name"), text("value")),
searchDefinition("gasorder_contract", &models.GasorderContract{}, text("contract_no"), text("title")),
searchDefinition("gasorder_basic", &models.GasorderBasic{}, text("request_no"), enum("creator_type", value("user", "用户"), value("staff", "工作人员"), value("delivery", "配送站"), value("gas", "气站"))),
searchDefinition("fin_settlement", &models.FinSettlement{}, text("settlement_no"), text("subject_type")),
searchDefinition("cms_content", &models.CmsContent{}, text("content_type"), text("title"), text("publish_status")),
searchDefinition("cs_ticket", &models.CsTicket{}, text("ticket_no"), text("category"), text("priority")),
searchDefinition("platform_account", &models.PlatformAccount{}, text("username"), text("display_name")),
searchDefinition("platform_role", &models.PlatformRole{}, text("role_code"), text("name"), enum("location_scope", value("standard", "脱敏坐标"), value("precise", "精确坐标"))),
searchDefinition("wallet_basic", &models.WalletBasic{}, text("owner_type")),
searchDefinition("payment_refund", &models.PaymentRefund{}, text("refund_no")),
searchDefinition("wallet_apply_cash", &models.WalletApplyCash{}, text("cash_no")),
}
func init() {
for _, definition := range resourceSearchDefinitions {
common.RegisterKeywordSearchPolicy(definition.model, definition.fields)
}
}
// resourceSearchFields 返回资源公开搜索契约的副本;未配置资源明确不支持搜索。
func resourceSearchFields(name string) []common.KeywordSearchField {
for _, definition := range resourceSearchDefinitions {
if definition.name == name {
return common.ConfiguredKeywordSearchFields(definition.model)
}
}
return nil
}
func searchDefinition(name string, model any, fields ...common.KeywordSearchField) resourceSearchDefinition {
return resourceSearchDefinition{name: name, model: model, fields: fields}
}
func text(key string) common.KeywordSearchField {
return common.KeywordSearchField{Key: key, Kind: common.KeywordSearchText}
}
func enum(key string, values ...common.KeywordSearchValue) common.KeywordSearchField {
return common.KeywordSearchField{Key: key, Kind: common.KeywordSearchEnum, Values: values}
}
func value(code, label string) common.KeywordSearchValue {
return common.KeywordSearchValue{Value: code, Label: label}
}

View File

@@ -0,0 +1,51 @@
package platform
import "testing"
func TestResourceSearchContractMatchesConfirmedScope(t *testing.T) {
tests := []struct {
resource string
keys []string
}{
{resource: "staff_account", keys: []string{"username", "role_code"}},
{resource: "user_address", keys: nil},
{resource: "platform_account", keys: []string{"username", "display_name"}},
{resource: "product_repair", keys: []string{"result"}},
}
contracts := ExpectedResources()
for _, test := range tests {
contract := findResourceContract(contracts, test.resource)
if contract == nil {
t.Fatalf("资源契约不存在:%s", test.resource)
}
if len(contract.SearchFields) != len(test.keys) {
t.Fatalf("%s 搜索字段数量=%d期望=%d", test.resource, len(contract.SearchFields), len(test.keys))
}
for index, key := range test.keys {
if contract.SearchFields[index].Key != key {
t.Fatalf("%s 第 %d 个搜索字段=%s期望=%s", test.resource, index, contract.SearchFields[index].Key, key)
}
}
}
}
func TestStaffRoleSearchUsesChineseLabels(t *testing.T) {
contract := findResourceContract(ExpectedResources(), "staff_account")
role := contract.SearchFields[1]
if role.Kind != "enum" || len(role.Values) != 3 {
t.Fatalf("工作人员角色搜索契约不完整:%+v", role)
}
if role.Values[1].Value != "delivery" || role.Values[1].Label != "配送人员" {
t.Fatalf("工作人员角色中英文映射错误:%+v", role.Values[1])
}
}
func findResourceContract(contracts []ResourceContract, name string) *ResourceContract {
for index := range contracts {
if contracts[index].Name == name {
return &contracts[index]
}
}
return nil
}