liuxiaolong
2020-06-28 a75018a48fee3d925a7f6fe6c8337fcf4af59667
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package controllers
 
import (
    "basic.com/dbapi.git"
    "basic.com/pubsub/protomsg.git"
    "bytes"
    "encoding/json"
    "github.com/gin-gonic/gin"
    "github.com/gogo/protobuf/jsonpb"
    "webserver/extend/code"
    "webserver/extend/util"
)
 
type RuleTemplateController struct {
 
}
 
var jsonpbMarshaler = &jsonpb.Marshaler{
    EnumsAsInts: true,
    EmitDefaults: true,
    OrigName: true,
}
 
type RuleTemplateVo struct {
    Id               string                 `json:"id"` //场景模板id
    Name             string                 `json:"name"`                                           //模板名称
    Desc             string                 `json:"desc"`                                           //场景描述
    Txt              string                 `json:"txt"`                                             //场景策略规则组合文字
    Rules             string                 `json:"rules"`                                   //具体的小规则集合
}
 
// @Summary 保存场景模板
// @Description 保存场景模板
// @Accept json
// @Produce json
// @Tags 场景模板
// @Param reqBody body controllers.RuleTemplateVo true "模板保存参数"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/template/rule/save [post]
func (rtc RuleTemplateController) Save(c *gin.Context) {
    var reqBody map[string]interface{}
    c.BindJSON(&reqBody)
    var api dbapi.RuleTemplateApi
    if api.Save(reqBody) {
        util.ResponseFormat(c, code.Success, "保存成功")
    } else {
        util.ResponseFormat(c, code.ComError, "保存失败")
    }
}
 
// @Summary 编辑
// @Description 编辑
// @Accept json
// @Produce json
// @Tags 场景模板
// @Param id path string true "模板id"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/template/rule/{id} [get]
func (rtc RuleTemplateController) Show(c *gin.Context) {
    id := c.Param("id")
    if id == "" {
        util.ResponseFormat(c,code.RequestParamError, "参数有误")
        return
    }
    var api dbapi.RuleTemplateApi
    b, d := api.Show(id)
    if b {
        util.ResponseFormat(c,code.Success, d)
    } else {
        util.ResponseFormat(c,code.ComError, "")
    }
}
 
// @Summary 根据id删除场景模板
// @Description 根据id删除场景模板
// @Accept json
// @Produce json
// @Tags 场景模板
// @Param id path string true "模板id"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/template/rule/{id} [delete]
func (rtc RuleTemplateController) Delete(c *gin.Context) {
    id := c.Param("id")
    if id == "" {
        util.ResponseFormat(c,code.RequestParamError, "参数有误")
        return
    }
    var api dbapi.RuleTemplateApi
    if api.Delete(id) {
        util.ResponseFormat(c,code.Success, "删除成功")
    } else {
        util.ResponseFormat(c,code.ComError, "")
    }
}
 
// @Summary 查找所有场景模板
// @Description 查找所有场景模板
// @Accept json
// @Produce json
// @Tags 场景模板
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/template/rule [get]
func (rtc RuleTemplateController) FindAll(c *gin.Context) {
    var api dbapi.RuleTemplateApi
    b, templates := api.FindAll()
    if b {
        var result protomsg.RuleTemplateList
        for _,r := range templates {
            result.List = append(result.List, &r)
        }
 
        var _buffer bytes.Buffer
        err := jsonpbMarshaler.Marshal(&_buffer, &result)
        if err == nil {
            jsonB := _buffer.Bytes()
            var m map[string]interface{}
            json.Unmarshal(jsonB, &m)
            util.ResponseFormat(c,code.Success, m)
            return
        }
    } else {
        util.ResponseFormat(c, code.ComError, "")
    }
}