diff --git a/app/models/manu.json b/app/models/manu.json index a04069ad..354a8986 100644 --- a/app/models/manu.json +++ b/app/models/manu.json @@ -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": "企业服务" } ] } diff --git a/app/models/service.json b/app/models/service.json index 9b76a95c..589548fb 100644 --- a/app/models/service.json +++ b/app/models/service.json @@ -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": ["按月订阅"] + } + ] } diff --git a/table/init_test.go b/table/init_test.go new file mode 100644 index 00000000..6470466b --- /dev/null +++ b/table/init_test.go @@ -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) +} diff --git a/table/process.go b/table/process.go index efab09b6..8032b835 100644 --- a/table/process.go +++ b/table/process.go @@ -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 } diff --git a/table/process_test.go b/table/process_test.go index 7d659730..a089971e 100644 --- a/table/process_test.go +++ b/table/process_test.go @@ -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")) } diff --git a/table/table_test.go b/table/table_test.go index eb67cded..a17065fc 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -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"]