liuxiaolong
2019-08-27 dd14647ee77aef511d76ade8e0993f67efc94844
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package controllers
 
import (
    "basic.com/dbapi.git"
    "encoding/json"
    "strconv"
    "webserver/extend/logger"
 
    "github.com/gin-gonic/gin"
    "github.com/satori/go.uuid"
    "webserver/extend/code"
    "webserver/extend/config"
    "webserver/extend/esutil"
    "webserver/extend/util"
    "webserver/models"
)
 
type DbPersonController struct {
}
 
// @Summary 添加底库人员
// @Description 添加底库人员
// @Accept  json
// @Produce json
// @Tags dbperson 底库人员
// @Param obj body models.Dbtablepersons true "底库人员数据"
// @Success 200 {string} json "{"code":200, msg:"", success:true}"
// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
// @Router /data/api-v/dbperson/addDbPerson [PUT]
func (dbc DbPersonController) AddDbPerson(c *gin.Context) {
    dbperson := new(models.Dbtablepersons)
    err := c.BindJSON(&dbperson)
    if err!=nil || dbperson.TableId == "" {
        // 底库id不存在
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    var pApi dbapi.DbPersonApi
    paramBody := util.Struct2Map(dbperson)
    b, data := pApi.AddDbPerson(paramBody)
    if b {
        util.ResponseFormat(c, code.Success, data)
    } else {
        util.ResponseFormat(c, code.ServiceInsideError, "")
    }
}
 
func addDbPerson(dbperson *models.Dbtablepersons) (result map[string]interface{}) {
 
    personId := uuid.NewV4().String()
    dbperson.Id = personId
    dbperson.PriInsert()
 
    var pApi dbapi.DbPersonApi
    paramBody := util.Struct2Map(dbperson)
    b, _ := pApi.AddDbPerson(paramBody)
    result = map[string]interface{}{}
    if b {
        result["code"] = 200
        data := make(map[string]interface{})
        data["uuid"] = personId
        result["data"] = data
        result["success"] = true
        result["msg"] = "添加成功"
    } else {
        result["data"] = nil
        result["success"] = false
        result["msg"] = "服务器异常"
        result["code"] = 500
    }
    return result
}
 
// @Summary 修改底库人员
// @Description 修改底库人员
// @Accept  json
// @Produce json
// @Tags dbperson 底库人员
// @Param person body models.Dbtablepersons true "底库人员数据"
// @Success 200 {string} json "{"code":200, msg:"目录结构数据", success:true}"
// @Failure 500 {string} json "{"code":500,  msg:"返回错误信息", success:false}"
// @Router /data/api-v/dbperson/updateDbPerson [POST]
func (dbc DbPersonController) UpdateDbPerson(c *gin.Context) {
    var dbperson models.Dbtablepersons
    err := c.BindJSON(&dbperson)
    if err !=nil || dbperson.Id == "" {
        util.ResponseFormat(c, code.RequestParamError, nil)
        return
    }
    dbperson.PriUpdate()
    var pApi dbapi.DbPersonApi
    paramBody := util.Struct2Map(dbperson)
    b, data := pApi.UpdateDbPerson(paramBody)
    if b {
        util.ResponseFormat(c, code.Success, data)
    } else {
        util.ResponseFormat(c, code.ServiceInsideError, "")
    }
}
 
func UpdateDbPersonsOfDbTable(id string) (message string) {
    url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
        "/" + config.EsInfo.EsIndex.Dbtablepersons.IndexName + "/_update_by_query?refresh"
    jsonDSL := `
            {
    "script": {
        "lang": "painless",
        "inline": "ctx._source.enable = 0"
    },
    "query": {
        "term": {
            "tableId": "` + id + `"
        }
    }
}
`
    buf, err := esutil.EsReq("POST", url, []byte(jsonDSL))
    if err != nil {
        logger.Debug("http request info is err!")
        message = "修改失败"
    }
    var info interface{}
    json.Unmarshal(buf, &info)
    out, ok := info.(map[string]interface{})
    if !ok {
        logger.Debug("http response interface can not change map[string]interface{}")
        message = "修改失败"
    }
    middle, ok := out["updated"].(float64)
    if !ok {
        logger.Debug("first result change error!")
        message = "修改失败"
    }
    if middle >= 0 {
        logger.Debug("修改成功")
        message = "修改成功,更新状态条数为" + strconv.Itoa(int(middle))
    }
    return message
}
 
// @Summary 删除底库人员
// @Description 删除库人员
// @Accept  x-www-form-urlencoded
// @Produce json
// @Tags dbperson 底库人员
// @Param uuid path string true "底库人员id "
// @Success 200 {string} json "{"code":200, msg:"目录结构数据", success:true}"
// @Failure 500 {string} json "{"code":500,  msg:"返回错误信息", success:false}"
// @Router /data/api-v/dbperson/deleteDbPersonById/{uuid} [POST]
func (dbc DbPersonController) DeleteDbPerson(c *gin.Context) {
    id := c.Params.ByName("uuid")
    if id == "" {
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    var pApi dbapi.DbPersonApi
    b, data := pApi.DeleteDbPerson(id)
    if b {
        util.ResponseFormat(c, code.Success, data)
    } else {
        util.ResponseFormat(c, code.ServiceInsideError, "删除失败")
    }
}
 
type multiIds []string
 
// @Summary 批量删除底库人员
// @Description 批量删除库人员
// @Accept  json
// @Produce json
// @Tags dbperson 底库人员
// @Param uuids body controllers.multiIds true "底库人员ids"
// @Success 200 {string} json "{"code":200, msg:"目录结构数据", success:true}"
// @Failure 500 {string} json "{"code":500,  msg:"返回错误信息", success:false}"
// @Router /data/api-v/dbperson/deleteMoreDbPerson [POST]
func (dbc DbPersonController) DeleteMoreDbPerson(c *gin.Context) {
    uuids := make([]string, 0, 5)
    err := c.BindJSON(&uuids)
    if err !=nil || len(uuids)==0{
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
    var pApi dbapi.DbPersonApi
    paramBody := util.Struct2Map(uuids)
    b, _ := pApi.DeleteMoreDbPerson(paramBody)
    if b {
        util.ResponseFormat(c, code.Success, "删除底库人员成功")
    } else {
        util.ResponseFormat(c, code.ServiceInsideError, "删除失败")
    }
}
 
// @Summary 查询底库人员列表
// @Description 查询库人员列表
// @Accept  json
// @Produce json
// @Tags dbperson 底库人员
// @Param reqMap body controllers.DbtSearch false "{"tableId":"","orderName":"id","orderType":"desc","contentValue":"","page":1,"size":8}"
// @Success 200 {string} json "{"code":200, "msg":"目录结构数据", "success":true,"data":{}}"
// @Failure 500 {string} json "{code:500,  msg:"返回错误信息", success:false,data:{}}"
// @Router /data/api-v/dbperson/queryDbPersonsByTbId [POST]
func (dbc DbPersonController) QueryDbPersonsByTbId(c *gin.Context) {
    //reqBody := make(map[string]interface{}, 5)
    var reqBody DbtSearch
    err := c.BindJSON(&reqBody)
    if err !=nil || reqBody.Page <=0 || reqBody.Size <=0 {
        util.ResponseFormat(c,code.RequestParamError,"参数有误")
        return
    }
 
    if reqBody.TableId == "" {
        util.ResponseFormat(c,code.RequestParamError,"参数有误,底库id不能为空")
        return
    }
    orderName := "id"
    if reqBody.OrderName != "" {
        orderName = reqBody.OrderName
    } // 列名
    orderType := "desc"
    if reqBody.OrderType != "" {
        orderType = reqBody.OrderType
    } // 列类型
    contentValue := ""
 
    page := 1
    if reqBody.Page >1 {
        page = reqBody.Page
    } // 页码
    size := 8
    if reqBody.Size >8 {
        size = reqBody.Size
    } // 条数
 
    if orderType == "desc" {
        orderType = "desc"
    } else {
        orderType = "asc"
    }
    var pApi dbapi.DbPersonApi
    paramBody := map[string]interface{}{
        "tableId": reqBody.TableId,
        "orderName":orderName,
        "orderType":orderType,
        "contentValue":contentValue,
        "page":page,
        "size":size,
    }
    b, data := pApi.QueryDbPersonsByTbId(paramBody)
    if b{
        util.ResponseFormat(c,code.Success,data)
    } else {
        util.ResponseFormat(c,code.ComError,[]interface{}{})
    }
}
 
type DbtSearch struct {
    TableId string `json:"tableId"`
    OrderName string `json:"orderName"`
    OrderType string `json:"orderType"`
    ContentValue string `json:"contentValue"`
    Page int `json:"page"`
    Size int `json:"size"`
}
 
/*
// @Summary 查询底库人员列表
// @Description 查询库人员列表
// @Accept  json
// @Produce json
// @Tags dbperson 底库人员
// @Param reqMap body map false "{"tableId":"","orderName":"id","orderType":"desc","contentValue":"","page":1,"size":8}"
// @Success 200 {string} json "{"code":200, "msg":"目录结构数据", "success":true,"data":{}}"
// @Failure 500 {string} json "{code:500,  msg:"返回错误信息", success:false,data:{}}"
// @Router /data/api-v/dbperson/queryDbPersonsByCampare [POST]
func (dbc DbPersonController) QueryDbPersonsByCampare(c *gin.Context) {
    url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport +
        "/" + config.EsInfo.EsIndex.Dbtablepersons.IndexName + "/_search" // ?refresh=wait_for
    reqBody := make(map[string]interface{}, 5)
    c.BindJSON(&reqBody)
    tableId := ""
    if reqBody["tableId"] != nil {
        tableId = reqBody["tableId"].(string)
    }
    orderName := "_id"
    if reqBody["orderName"] != nil {
        orderName = reqBody["orderName"].(string)
    } // 列名
    orderType := "desc"
    if reqBody["orderType"] != nil {
        orderType = reqBody["orderType"].(string)
    } // 列类型
    faceUrl := ""
    var threshold float32
    if reqBody["faceUrl"] != nil && reqBody["threshold"] != nil {
        faceUrl = reqBody["faceUrl"].(string)
        threshold = float32(reqBody["threshold"].(float64))
    } else {
        util.ResponseFormat(c, code.RequestParamError, nil) // 图片路径有问题
        return
    }
    //输入框内容
    page := 1
    if reqBody["page"] != nil {
        page = int(reqBody["page"].(float64))
    } // 页码
    size := 8
    if reqBody["size"] != nil {
        size = int(reqBody["size"].(float64))
    } // 条数
    from := (page - 1) * size
    syncTerm := ""
    contentParam := ""
    if tableId == "all" || tableId == "" {
        // / 所有人员
    } else {
        syncTerm = "{\"term\":{\"tableId\":\"" + tableId + "\"}}" // 底库人员
    }
    if orderType == "desc" {
        orderType = "desc"
    } else {
        orderType = "asc"
    }
 
    //params := "{\"query\":{\"bool\":{\"filter\":[" +
    //    "{\"term\":{\"isDelete\":\"0\"}}" + syncTerm + "]" + contentParam + "}},\"from\":" + strconv.Itoa(from) + ",\"size\":" + strconv.Itoa(size) + ",\"sort\":{\"" + orderName + "\":{\"order\":\"" + orderType + "\"}}}"
    params := "{\"query\":{\"bool\":{\"must_not\":[" +
        "{\"term\":{\"isDelete\":\"1\"}}],\"filter\":[" + syncTerm + "]" + contentParam + "}},\"from\":" + strconv.Itoa(from) + ",\"size\":" + strconv.Itoa(size) + ",\"sort\":{\"" + orderName + "\":{\"order\":\"" + orderType + "\"}}}"
    logger.Debug("请求url:%s;\n 请求参数params:%s", url, params)
    data := esutil.GetEsDataReq(url, params, true)
    featByte := make([]byte, 0, 1024)
    if len(faceUrl) > 3 { //   linux
 
    }
    to := page * size
    datalist := sourceCompare(data["datalist"].([]interface{}), false, featByte, threshold)
    total := len(datalist)
    if from > total {
        from = total
    }
    if to > total {
        to = total
    }
    data["datalist"] = datalist[from:to]
    data["total"] = len(datalist)
    //c.JSON(200, data)
    util.ResponseFormat(c, code.Success, data)
}
*/