zhangzengfei
2023-10-24 c5654846d3b8b002284dee57aa50e95d67649f0e
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package controllers
 
import (
    "basic.com/valib/bhomeclient.git"
    "basic.com/valib/bhomedbapi.git"
    "basic.com/valib/logger.git"
    "github.com/satori/go.uuid"
    "sort"
    "vamicro/system-service/models"
)
 
type DictionaryController struct {
}
 
type DicTypeVo struct {
    Type string                  `json:"type"`
    Dics []DicWithChildren         `json:"dics"`
}
 
type DicWithChildren struct {
    models.Dictionary
    Children     []DicWithChildren     `json:"children"`
}
 
type DicList []DicWithChildren
func (dl DicList) Len()int {
    return len(dl)
}
func (dl DicList) Swap(i,j int) {
    dl[i],dl[j] = dl[j],dl[i]
}
func (dl DicList) Less(i,j int) bool {
    return dl[i].Sort < dl[j].Sort
}
 
func recursiveChildren(parentId string, allList *[]models.Dictionary) []DicWithChildren {
    var children = make([]DicWithChildren, 0)
    for _,d := range *allList {
        if d.ParentId == parentId {
            dwc := DicWithChildren{}
            dwc.Dictionary = d
            dwc.Children = []DicWithChildren{}
            children = append(children, dwc)
        }
    }
    var dArr DicList = children
    sort.Sort(dArr)
    return dArr
}
 
// @Summary 根据类型查找字典
// @Description  根据类型查找字典
// @Produce json
// @Tags 字典
// @Param type query string false "字典类型"
// @Success 200 {string} json "{"code":200, success:true, msg:"请求处理成功", data:"成功信息"}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:"错误信息内容"}"
// @Router /data/api-v/dictionary/findByType [get]
func (controller DictionaryController) FindByType(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    var model models.Dictionary
    dicType := c.Query("type")
    logger.Debug("dicType:", dicType)
    allDics, err := model.FindAll()
    if err != nil {
        return &bhomeclient.Reply{ Msg: "查询失败"}
    }
 
    resMap := make(map[string]DicList, 0)
 
    for _, dic := range allDics {
        if dic.ParentId == "0" {
            dwc := DicWithChildren{}
            dwc.Dictionary = dic
            dwc.Children = recursiveChildren(dwc.Id, &allDics)
            resMap[dic.Type] = append(resMap[dic.Type], dwc)
        }
    }
    //时间规则
    var tApi bhomedbapi.TimePgnApi
    tb,timeRules := tApi.FindAllTimeRules()
    timeList := make(DicList, 0)
    if tb && timeRules != nil {
        for idx,t := range timeRules {
            dwc := DicWithChildren{}
            dwc.Dictionary = models.Dictionary{
                Value: t.Id,
                Name: t.Name,
                Sort: idx+1,
            }
            timeList = append(timeList, dwc)
        }
    }
    resMap["time_rule"] = timeList
    //人员底库
    personTableList := make(DicList, 0)
    var tableApi bhomedbapi.DbTableApi
    personTables, dtErr := tableApi.FindAllDbTablesByType("0", "person")
    if dtErr == nil && personTables != nil {
        for idx,t := range personTables {
            dwc := DicWithChildren{}
            dwc.Dictionary = models.Dictionary{
                Value: t.Id,
                Name: t.TableName,
                Sort: idx +1,
            }
            personTableList = append(personTableList, dwc)
        }
    }
    resMap["compareBase"] = personTableList
    // 车辆底库
    carTableList := make(DicList, 0)
    carTables, _ := tableApi.FindAllDbTablesByType("0", "car")
    if carTables != nil {
        for idx,t := range carTables {
            dwc := DicWithChildren{}
            dwc.Dictionary = models.Dictionary{
                Value: t.Id,
                Name: t.TableName,
                Sort: idx +1,
            }
            carTableList = append(carTableList, dwc)
        }
    }
    resMap["compareCarBase"] = carTableList
    for k,dList :=range resMap {
        v := dList
        sort.Sort(v)
        resMap[k] = v
    }
 
    return &bhomeclient.Reply{ Success: true, Data: resMap}
}
 
// @Summary 根据父ID查找字典
// @Description  根据父ID查找字典
// @Produce json
// @Tags 字典
// @Param parentId query string false "parentId"
// @Success 200 {string} json "{"code":200, success:true, msg:"请求处理成功", data:"成功信息"}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:"错误信息内容"}"
// @Router /data/api-v/dictionary/findByParentId [get]
func (controller DictionaryController) FindByParentId(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    parentId := c.Query("parentId")
    if parentId == "" {
        return &bhomeclient.Reply{ Msg: "参数有误"}
    }
    var model models.Dictionary
    if flag, err := model.SelectById(parentId); !flag && err == nil {
 
        if model.Value == models.EVENTRULETOPIC_DBTABLE || model.Value == models.EVENTRULETOPIC_TASK || model.Value == models.EVENTRULETOPIC_ALARMLEVEL {
            empty := models.Dictionary{
                Value: "",
                Name:  "空",
            }
            return &bhomeclient.Reply{ Success: true, Data: []models.Dictionary{empty} }
        } else {
            dics, err := model.FindByParentId(parentId)
            if err != nil {
                return &bhomeclient.Reply{ Msg: "查询失败"}
            } else {
                return &bhomeclient.Reply{ Success:true, Data: dics }
            }
        }
    } else {
        return &bhomeclient.Reply{ Msg: "查询失败"}
    }
}
 
 
// @Summary 根据type查找字典列表
// @Description  根据type查找字典列表
// @Produce json
// @Tags 字典
// @Param type query string false "type"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/dictionary/listByType [get]
func (controller DictionaryController) ListByType(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    typ := c.Query("type")
    if typ == "" {
        return &bhomeclient.Reply{ Msg: "参数有误"}
    }
    var model models.Dictionary
    dics, err := model.FindByType(typ)
    if err == nil {
        return &bhomeclient.Reply{ Success:true, Data: dics }
    } else {
        return &bhomeclient.Reply{ Msg: "查询失败"}
    }
}
 
// @Summary 保存字典
// @Description  保存字典
// @Accept json
// @Produce json
// @Tags 字典
// @Param dictionary body models.Dictionary 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/dictionary/save [post]
func (controller DictionaryController) Save(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    var model models.Dictionary
    if err := c.BindJSON(&model); err != nil {
        return &bhomeclient.Reply{ Msg: "参数有误"}
    }
    var flag bool
    if model.Id == "" {
        model.Id = uuid.NewV4().String()
        flag, _ = model.Insert()
    } else {
        flag, _ = model.Update()
    }
 
    if !flag {
        return &bhomeclient.Reply{ Msg: "保存失败"}
    } else {
        return &bhomeclient.Reply{ Success:true, Msg: "保存成功"}
    }
}