调通 Search 方法
This commit is contained in:
parent
6d576a1f9e
commit
4c92fa3015
6 changed files with 161 additions and 32 deletions
|
|
@ -101,13 +101,25 @@
|
||||||
"relations": {},
|
"relations": {},
|
||||||
"option": { "timestamps": true, "soft_deletes": true },
|
"option": { "timestamps": true, "soft_deletes": true },
|
||||||
"values": [
|
"values": [
|
||||||
{ "name": "北京云道天成科技有限公司", "short_name": "云道天成" },
|
{ "id": 1, "name": "北京云道天成科技有限公司", "short_name": "云道天成" },
|
||||||
{ "name": "象传高新(北京)数字科技有限公司", "short_name": "象传高新" },
|
{
|
||||||
{ "name": "象传智数(北京)软件科技有限公司", "short_name": "象传智数" },
|
"id": 2,
|
||||||
{ "name": "象传智慧(北京)软件科技有限公司", "short_name": "象传智慧" },
|
"name": "象传高新(北京)数字科技有限公司",
|
||||||
{ "name": "象传数据服务引擎", "short_name": "服务引擎" },
|
"short_name": "象传高新"
|
||||||
{ "name": "象传数据应用开发平台", "short_name": "应用开发平台" },
|
},
|
||||||
{ "name": "象传数据解决方案", "short_name": "解决方案" },
|
{
|
||||||
{ "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": "企业服务" }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,15 +22,15 @@
|
||||||
"index": true
|
"index": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "kind",
|
"name": "kind_id",
|
||||||
"type": "string",
|
"type": "bigInteger",
|
||||||
"length": 200,
|
"length": 200,
|
||||||
"comment": "服务类型",
|
"comment": "服务类型",
|
||||||
"index": true
|
"index": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "manu",
|
"name": "manu_id",
|
||||||
"type": "string",
|
"type": "bigInteger",
|
||||||
"length": 200,
|
"length": 200,
|
||||||
"comment": "所属厂商",
|
"comment": "所属厂商",
|
||||||
"index": true
|
"index": true
|
||||||
|
|
@ -75,10 +75,10 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "price_options",
|
"name": "price_options",
|
||||||
"type": "string",
|
"type": "json",
|
||||||
"length": 500,
|
"length": 500,
|
||||||
"default": "[\"按月订阅\"]",
|
"nullable": true,
|
||||||
"comment": "JSON|订阅方式(多选)",
|
"comment": "订阅方式(多选)",
|
||||||
"index": true
|
"index": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -107,16 +107,48 @@
|
||||||
"relations": {
|
"relations": {
|
||||||
"manu": {
|
"manu": {
|
||||||
"type": "hasOne",
|
"type": "hasOne",
|
||||||
"key": "manu",
|
"model": "manu",
|
||||||
"model": "Manu",
|
"key": "id",
|
||||||
"foreign": "id"
|
"foreign": "manu_id",
|
||||||
|
"query": { "select": ["id", "name", "short_name", "status"] }
|
||||||
},
|
},
|
||||||
"kind": {
|
"kind": {
|
||||||
"type": "hasOne",
|
"type": "hasOne",
|
||||||
"key": "kind",
|
"model": "kind",
|
||||||
"model": "Kind",
|
"key": "id",
|
||||||
"foreign": "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
28
table/init_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
|
@ -1,13 +1,64 @@
|
||||||
package table
|
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() {
|
func init() {
|
||||||
// 注册处理器
|
// 注册处理器
|
||||||
gou.RegisterProcessHandler("xiang.table.Search", processSearch)
|
gou.RegisterProcessHandler("xiang.table.Search", processSearch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// processSearch 用户检索
|
||||||
func processSearch(process *gou.Process) interface{} {
|
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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,9 @@ package table
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/yaoapp/gou"
|
"github.com/yaoapp/gou"
|
||||||
|
"github.com/yaoapp/kun/any"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestProcessSearch(t *testing.T) {
|
func TestProcessSearch(t *testing.T) {
|
||||||
|
|
@ -18,6 +20,16 @@ func TestProcessSearch(t *testing.T) {
|
||||||
2,
|
2,
|
||||||
}
|
}
|
||||||
process := gou.NewProcess("xiang.table.Search", args...)
|
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"))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,15 @@
|
||||||
package table
|
package table
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"path"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/yaoapp/xiang/global"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoad(t *testing.T) {
|
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"]
|
table, has := Tables["service"]
|
||||||
assert.Equal(t, table.Table, "service")
|
assert.Equal(t, table.Table, "service")
|
||||||
assert.Equal(t, table.Source, root)
|
|
||||||
assert.True(t, has)
|
assert.True(t, has)
|
||||||
|
|
||||||
_, has = table.Columns["id"]
|
_, has = table.Columns["id"]
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue