diff --git a/share/const.go b/share/const.go index e662b95b..f1902e1d 100644 --- a/share/const.go +++ b/share/const.go @@ -1,7 +1,7 @@ package share // VERSION 版本号 -const VERSION = "0.9.11" +const VERSION = "0.9.12" // DOMAIN 许可域(废弃) const DOMAIN = "*.iqka.com" diff --git a/table/process.go b/table/process.go index 07aba938..a4b9ffa2 100644 --- a/table/process.go +++ b/table/process.go @@ -16,6 +16,7 @@ func init() { gou.RegisterProcessHandler("xiang.table.Insert", ProcessInsert) gou.RegisterProcessHandler("xiang.table.UpdateWhere", ProcessUpdateWhere) gou.RegisterProcessHandler("xiang.table.DeleteWhere", ProcessDeleteWhere) + gou.RegisterProcessHandler("xiang.table.QuickSave", ProcessQuickSave) gou.RegisterProcessHandler("xiang.table.UpdateIn", ProcessUpdateIn) gou.RegisterProcessHandler("xiang.table.DeleteIn", ProcessDeleteIn) gou.RegisterProcessHandler("xiang.table.Setting", ProcessSetting) @@ -234,3 +235,31 @@ func ProcessSetting(process *gou.Process) interface{} { return gou.NewProcess(api.Process, fields).Run() } + +// ProcessQuickSave xiang.table.QuickSave +// 保存多条记录。如数据记录中包含主键字段则更新,不包含主键字段则创建记录;返回创建或更新的记录主键值 +func ProcessQuickSave(process *gou.Process) interface{} { + process.ValidateArgNums(2) + name := process.ArgsString(0) + table := Select(name) + api := table.APIs["quicksave"].ValidateLoop("xiang.table.quicksave") + if process.NumOfArgsIs(3) && api.IsAllow(process.Args[2]) { + return nil + } + + args := []interface{}{} + payload := process.ArgsMap(1) + ids := []int{} + if payload.Has("delete") { + if v, ok := payload["delete"].([]int); ok { + ids = v + } + } + args = append(args, ids) + args = append(args, payload.Get("data")) + if payload.Has("query") { + args = append(args, payload.Get("query")) + } + + return gou.NewProcess(api.Process, args...).Run() +} diff --git a/table/process_test.go b/table/process_test.go index 89f4bdaf..1658306d 100644 --- a/table/process_test.go +++ b/table/process_test.go @@ -312,3 +312,49 @@ func TestTableProcessSettingListEdit(t *testing.T) { assert.True(t, res.Has("edit.layout")) assert.True(t, res.Has("edit.primary")) } + +func TestTableProcessQuickSave(t *testing.T) { + args := []interface{}{ + "service", + map[string]interface{}{ + "name": "腾讯黑岩云主机", + "short_name": "高性能云主机", + "kind_id": 3, + "manu_id": 1, + "price_options": []string{"按月订阅"}, + }, + } + process := gou.NewProcess("xiang.table.Save", args...) + response := ProcessSave(process) + assert.NotNil(t, response) + assert.True(t, any.Of(response).IsInt()) + + id := any.Of(response).CInt() + args = []interface{}{ + "service", + id, + } + + process = gou.NewProcess("xiang.table.QuickSave", + "service", + map[string]interface{}{ + "delete": []int{id}, + "data": []map[string]interface{}{ + { + "name": "腾讯黑岩云主机2", + "short_name": "高性能云主机2", + "kind_id": 3, + "manu_id": 1, + "price_options": []string{"按月订阅"}, + }, + }, + "query": map[string]interface{}{"manu_id": 1}, + }) + res := process.Run() + newID, ok := res.([]int) + assert.True(t, ok) + assert.NotEqual(t, id, newID[0]) + + // 清空数据 + capsule.Query().Table("service").WhereIn("id", []int{id, newID[0]}).Delete() +} diff --git a/table/table.go b/table/table.go index dab6699e..b924dcf7 100644 --- a/table/table.go +++ b/table/table.go @@ -140,6 +140,7 @@ func getDefaultAPIs(bind Bind) map[string]share.API { "delete-where": apiDefaultWhere(model, bind.Withs, "delete-where", "DeleteWhere"), "update-in": apiDefault(model, "update-in", "UpdateWhere"), "update-where": apiDefaultWhere(model, bind.Withs, "update-where", "UpdateWhere"), + "quicksave": apiDefault(model, "quicksave", "EachSaveAfterDelete"), } return apis diff --git a/xiang/apis/table.http.json b/xiang/apis/table.http.json index 388ad305..2fde952f 100644 --- a/xiang/apis/table.http.json +++ b/xiang/apis/table.http.json @@ -35,6 +35,16 @@ "type": "application/json" } }, + { + "path": "/:name/quicksave", + "method": "POST", + "process": "xiang.table.QuickSave", + "in": ["$param.name", ":payload"], + "out": { + "status": 200, + "type": "application/json" + } + }, { "path": "/:name/delete/:id", "method": "POST",