diff --git a/global/process_test.go b/global/process_test.go index 4c17c099..698a1bce 100644 --- a/global/process_test.go +++ b/global/process_test.go @@ -162,6 +162,39 @@ func TestProcessDeleteWhere(t *testing.T) { capsule.Query().Table("service").Where("id", id).Delete() } +func TestProcessDeleteIn(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 := table.ProcessSave(process) + assert.NotNil(t, response) + assert.True(t, any.Of(response).IsInt()) + + id := any.Of(response).CInt() + args = []interface{}{ + "service", + any.Of(response).Int(), + "id", + } + process = gou.NewProcess("xiang.table.DeleteIn", args...) + response = table.ProcessDeleteIn(process) + + assert.NotNil(t, response) + assert.True(t, any.Of(response).IsInt()) + assert.Equal(t, any.Of(response).CInt(), 1) + + // 清空数据 + capsule.Query().Table("service").Where("id", id).Delete() +} + func TestProcessUpdateWhere(t *testing.T) { args := []interface{}{ "service", @@ -201,6 +234,42 @@ func TestProcessUpdateWhere(t *testing.T) { capsule.Query().Table("service").Where("id", id).Delete() } +func TestProcessUpdateIn(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 := table.ProcessSave(process) + assert.NotNil(t, response) + assert.True(t, any.Of(response).IsInt()) + + id := any.Of(response).CInt() + args = []interface{}{ + "service", + id, + "id", + map[string]interface{}{ + "name": "腾讯黑岩云主机UP", + }, + } + process = gou.NewProcess("xiang.table.UpdateIn", args...) + response = table.ProcessUpdateIn(process) + + assert.NotNil(t, response) + assert.True(t, any.Of(response).IsInt()) + assert.Equal(t, any.Of(response).CInt(), 1) + + // 清空数据 + capsule.Query().Table("service").Where("id", id).Delete() +} + func TestProcessSetting(t *testing.T) { args := []interface{}{"service", ""} process := gou.NewProcess("xiang.table.Setting", args...) diff --git a/table/process.go b/table/process.go index 082f42c2..07aba938 100644 --- a/table/process.go +++ b/table/process.go @@ -16,6 +16,8 @@ func init() { gou.RegisterProcessHandler("xiang.table.Insert", ProcessInsert) gou.RegisterProcessHandler("xiang.table.UpdateWhere", ProcessUpdateWhere) gou.RegisterProcessHandler("xiang.table.DeleteWhere", ProcessDeleteWhere) + gou.RegisterProcessHandler("xiang.table.UpdateIn", ProcessUpdateIn) + gou.RegisterProcessHandler("xiang.table.DeleteIn", ProcessDeleteIn) gou.RegisterProcessHandler("xiang.table.Setting", ProcessSetting) } @@ -105,6 +107,30 @@ func ProcessDeleteWhere(process *gou.Process) interface{} { return gou.NewProcess(api.Process, param).Run() } +// ProcessDeleteIn xiang.table.DeleteIn +// 按条件批量删除数据, 请求成功返回删除行数 +func ProcessDeleteIn(process *gou.Process) interface{} { + + process.ValidateArgNums(3) + name := process.ArgsString(0) + table := Select(name) + api := table.APIs["delete-in"].ValidateLoop("xiang.table.DeleteIn") + if process.NumOfArgsIs(4) && api.IsAllow(process.Args[3]) { + return nil + } + + // 批量删除 + ids := strings.Split(process.ArgsString(1), ",") + primary := process.ArgsString(2, "id") + param := gou.QueryParam{ + Wheres: []gou.QueryWhere{ + {Column: primary, OP: "in", Value: ids}, + }, + } + + return gou.NewProcess(api.Process, param).Run() +} + // ProcessUpdateWhere xiang.table.UpdateWhere // 按条件批量更新数据, 请求成功返回更新行数 func ProcessUpdateWhere(process *gou.Process) interface{} { @@ -125,6 +151,29 @@ func ProcessUpdateWhere(process *gou.Process) interface{} { return gou.NewProcess(api.Process, param, process.Args[2]).Run() } +// ProcessUpdateIn xiang.table.UpdateWhere +// 按条件批量更新数据, 请求成功返回更新行数 +func ProcessUpdateIn(process *gou.Process) interface{} { + + process.ValidateArgNums(4) + name := process.ArgsString(0) + table := Select(name) + api := table.APIs["update-in"].ValidateLoop("xiang.table.UpdateIn") + if process.NumOfArgsIs(5) && api.IsAllow(process.Args[4]) { + return nil + } + + // 批量删除 + ids := strings.Split(process.ArgsString(1), ",") + primary := process.ArgsString(2, "id") + param := gou.QueryParam{ + Wheres: []gou.QueryWhere{ + {Column: primary, OP: "in", Value: ids}, + }, + } + return gou.NewProcess(api.Process, param, process.Args[3]).Run() +} + // ProcessInsert xiang.table.Insert // 插入多条数据记录,请求成功返回插入行数 func ProcessInsert(process *gou.Process) interface{} { diff --git a/xiang/apis/table.http.json b/xiang/apis/table.http.json index 8a8384fa..021df18a 100644 --- a/xiang/apis/table.http.json +++ b/xiang/apis/table.http.json @@ -69,7 +69,7 @@ "path": "/:name/delete/in", "method": "POST", "process": "xiang.table.DeleteIn", - "in": ["$param.name", "$query.ids", ":payload"], + "in": ["$param.name", "$query.ids", "$query.primary"], "out": { "status": 200, "type": "application/json" @@ -89,7 +89,7 @@ "path": "/:name/update/in", "method": "POST", "process": "xiang.table.UpdateIn", - "in": ["$param.name", "$query.ids", ":payload"], + "in": ["$param.name", "$query.ids", "$query.primary", ":payload"], "out": { "status": 200, "type": "application/json"