调通 Search 方法

This commit is contained in:
Max 2021-09-28 15:27:34 +08:00
parent 6d576a1f9e
commit 4c92fa3015
6 changed files with 161 additions and 32 deletions

View file

@ -101,13 +101,25 @@
"relations": {},
"option": { "timestamps": true, "soft_deletes": true },
"values": [
{ "name": "北京云道天成科技有限公司", "short_name": "云道天成" },
{ "name": "象传高新(北京)数字科技有限公司", "short_name": "象传高新" },
{ "name": "象传智数(北京)软件科技有限公司", "short_name": "象传智数" },
{ "name": "象传智慧(北京)软件科技有限公司", "short_name": "象传智慧" },
{ "name": "象传数据服务引擎", "short_name": "服务引擎" },
{ "name": "象传数据应用开发平台", "short_name": "应用开发平台" },
{ "name": "象传数据解决方案", "short_name": "解决方案" },
{ "name": "象传数据企业服务", "short_name": "企业服务" }
{ "id": 1, "name": "北京云道天成科技有限公司", "short_name": "云道天成" },
{
"id": 2,
"name": "象传高新(北京)数字科技有限公司",
"short_name": "象传高新"
},
{
"id": 3,
"name": "象传智数(北京)软件科技有限公司",
"short_name": "象传智数"
},
{
"id": 4,
"name": "象传智慧(北京)软件科技有限公司",
"short_name": "象传智慧"
},
{ "id": 5, "name": "象传数据服务引擎", "short_name": "服务引擎" },
{ "id": 6, "name": "象传数据应用开发平台", "short_name": "应用开发平台" },
{ "id": 7, "name": "象传数据解决方案", "short_name": "解决方案" },
{ "id": 8, "name": "象传数据企业服务", "short_name": "企业服务" }
]
}

View file

@ -22,15 +22,15 @@
"index": true
},
{
"name": "kind",
"type": "string",
"name": "kind_id",
"type": "bigInteger",
"length": 200,
"comment": "服务类型",
"index": true
},
{
"name": "manu",
"type": "string",
"name": "manu_id",
"type": "bigInteger",
"length": 200,
"comment": "所属厂商",
"index": true
@ -75,10 +75,10 @@
},
{
"name": "price_options",
"type": "string",
"type": "json",
"length": 500,
"default": "[\"按月订阅\"]",
"comment": "JSON|订阅方式(多选)",
"nullable": true,
"comment": "订阅方式(多选)",
"index": true
},
{
@ -107,16 +107,48 @@
"relations": {
"manu": {
"type": "hasOne",
"key": "manu",
"model": "Manu",
"foreign": "id"
"model": "manu",
"key": "id",
"foreign": "manu_id",
"query": { "select": ["id", "name", "short_name", "status"] }
},
"kind": {
"type": "hasOne",
"key": "kind",
"model": "Kind",
"foreign": "id"
"model": "kind",
"key": "id",
"foreign": "kind_id",
"query": { "select": ["id", "name"] }
}
},
"option": { "timestamps": true, "soft_deletes": true }
"option": { "timestamps": true, "soft_deletes": true },
"values": [
{
"name": "腾讯云主机CVM",
"short_name": "云主机",
"kind_id": 3,
"manu_id": 1,
"price_options": ["按月订阅"]
},
{
"name": "腾讯云磁盘",
"short_name": "云磁盘",
"kind_id": 6,
"manu_id": 1,
"price_options": ["按月订阅"]
},
{
"name": "阿里主机",
"short_name": "云主机",
"kind_id": 3,
"manu_id": 2,
"price_options": ["按月订阅"]
},
{
"name": "UCloud云主机",
"short_name": "云主机",
"kind_id": 3,
"manu_id": 3,
"price_options": ["按月订阅"]
}
]
}

28
table/init_test.go Normal file
View file

@ -0,0 +1,28 @@
package table
import (
"os"
"path"
"testing"
"github.com/yaoapp/gou"
"github.com/yaoapp/xiang/global"
)
func TestMain(m *testing.M) {
// 加载模型等
global.Load(global.Conf)
// 加载表格(临时)
root := "fs://" + path.Join(global.Conf.Source, "/app/tables/service.json")
Load(root, "service").Reload()
// Run test suites
exitVal := m.Run()
// we can do clean up code here
gou.KillPlugins()
os.Exit(exitVal)
}

View file

@ -1,13 +1,64 @@
package table
import "github.com/yaoapp/gou"
import (
"strings"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/any"
"github.com/yaoapp/kun/exception"
)
func init() {
// 注册处理器
gou.RegisterProcessHandler("xiang.table.Search", processSearch)
}
// processSearch 用户检索
func processSearch(process *gou.Process) interface{} {
process.ValidateArgNums(4)
name := any.Of(process.Args[0]).String()
page := any.Of(process.Args[2]).CInt()
pagesize := any.Of(process.Args[3]).CInt()
return nil
param := gou.QueryParam{}
ok := false
if process.Args[1] != nil {
param, ok = process.Args[1].(gou.QueryParam)
if !ok {
exception.New("参数不是QueryParam", 400).Throw()
}
}
table := Select(name)
api := table.APIs["search"]
param = mergeQueryParam(param, api, 0)
if strings.ToLower(api.Process) == "xiang.table.search" {
exception.New("循环引用 xiang.table.search", 400).Throw()
}
return gou.NewProcess(api.Process, param, page, pagesize).Run()
}
func mergeQueryParam(param gou.QueryParam, api API, i int) gou.QueryParam {
if len(api.Default) > i && api.Default[i] != nil {
defaults, ok := api.Default[i].(gou.QueryParam)
if !ok {
exception.New("参数默认值数据结构错误", 400).Throw()
}
if defaults.Withs != nil {
param.Withs = defaults.Withs
}
if defaults.Wheres != nil {
if param.Wheres == nil {
param.Wheres = []gou.QueryWhere{}
}
param.Wheres = append(param.Wheres, defaults.Wheres...)
}
if defaults.Orders != nil {
param.Orders = append(param.Orders, defaults.Orders...)
}
}
return param
}

View file

@ -3,7 +3,9 @@ package table
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/any"
)
func TestProcessSearch(t *testing.T) {
@ -18,6 +20,16 @@ func TestProcessSearch(t *testing.T) {
2,
}
process := gou.NewProcess("xiang.table.Search", args...)
processSearch(process)
response := processSearch(process)
assert.NotNil(t, response)
res := any.Of(response).Map()
assert.True(t, res.Has("data"))
assert.True(t, res.Has("next"))
assert.True(t, res.Has("page"))
assert.True(t, res.Has("pagecnt"))
assert.True(t, res.Has("pagesize"))
assert.True(t, res.Has("prev"))
assert.True(t, res.Has("total"))
assert.Equal(t, 1, res.Get("page"))
assert.Equal(t, 2, res.Get("pagesize"))
}

View file

@ -1,21 +1,15 @@
package table
import (
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/xiang/global"
)
func TestLoad(t *testing.T) {
global.Load(global.Conf)
root := "fs://" + path.Join(global.Conf.Source, "/app/tables/service.json")
Load(root, "service").Reload()
table, has := Tables["service"]
assert.Equal(t, table.Table, "service")
assert.Equal(t, table.Source, root)
assert.True(t, has)
_, has = table.Columns["id"]