From 000e51f17eba47c3502ee8f717f53f1fae3d0bbd Mon Sep 17 00:00:00 2001 From: liuxiaolong <736321739@qq.com> Date: 星期三, 13 十一月 2019 19:11:35 +0800 Subject: [PATCH] new files --- controllers/dbtableperson.go | 337 +++++++++++++++++++++++++------------------------------ 1 files changed, 154 insertions(+), 183 deletions(-) diff --git a/controllers/dbtableperson.go b/controllers/dbtableperson.go index bad1888..0cc064d 100644 --- a/controllers/dbtableperson.go +++ b/controllers/dbtableperson.go @@ -1,10 +1,9 @@ package controllers import ( + "basic.com/dbapi.git" "encoding/json" - "log" "strconv" - "time" "webserver/extend/logger" "github.com/gin-gonic/gin" @@ -25,54 +24,50 @@ // @Produce json // @Tags dbperson 搴曞簱浜哄憳 // @Param obj body models.Dbtablepersons true "搴曞簱浜哄憳鏁版嵁" -// @Success 200 {object} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}" -// @Failure 500 {string} json "{"code":500, msg:"杩斿洖閿欒淇℃伅", success:false}" -// @Failure 400 {object} json code.RequestParamError +// @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) - c.BindJSON(&dbperson) - if dbperson.TableId == "" { + err := c.BindJSON(&dbperson) + if err!=nil || dbperson.TableId == "" { // 搴曞簱id涓嶅瓨鍦� - c.JSON(401, "TableId 涓嶅瓨鍦�") + util.ResponseFormat(c,code.RequestParamError,"鍙傛暟鏈夎") + return } - result := addDbPerson(dbperson) - if result["success"].(bool) { - //code.Success.Message = "娣诲姞搴曞簱浜哄憳鎴愬姛" - util.ResponseFormat(c, code.Success, result["data"]) + var pApi dbapi.DbPersonApi + paramBody := util.Struct2Map(dbperson) + b, data := pApi.AddDbPerson(paramBody) + if b { + util.ResponseFormat(c, code.Success, data) } else { - //code.ServiceInsideError.Message += result["msg"].(string) - util.ResponseFormat(c, code.ServiceInsideError, result["data"]) + util.ResponseFormat(c, code.ServiceInsideError, "") } } func addDbPerson(dbperson *models.Dbtablepersons) (result map[string]interface{}) { - personId := uuid.NewV4().String() // 浠ュ悗鏇夸唬 渚濇嵁鐢熸垚瑙勫垯 + personId := uuid.NewV4().String() dbperson.Id = personId dbperson.PriInsert() - url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport + - "/" + config.EsInfo.EsIndex.Dbtablepersons.IndexName + "/" + config.EsInfo.EsIndex.Dbtablepersons.IndexType + "/" + personId + "?refresh=wait_for" - personbytes, e := json.Marshal(dbperson) - if e != nil { - logger.Debug("Json marshaling failed锛�%s\n", e) - } - params := string(personbytes) - logger.Debug("璇锋眰url:%s;\n 璇锋眰鍙傛暟params:%s", url, params) - data, _ := esutil.PutEsDataReq(url, params) - //if data["_id"] !=""{ - // //閫氱煡姣斿杩涚▼缂撳瓨鏇存柊 - // discovery.AddDbMessage(&protomsg.EsPersonCacheChange{ - // Type: protomsg.EsCacheChanged_T_DbTablePerson, - // PersonId: personId, - // TableId: []string{ dbperson.TableId }, - // Feature: dbperson.FaceFeature, - // Action: protomsg.DbAction_Insert, - // }) - //} - //c.JSON(200, changeEsRespData(data, "娣诲姞浜哄憳鎴愬姛")) - result = changeEsRespData(data, "娣诲姞鎴愬姛") + 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 } @@ -87,49 +82,60 @@ // @Router /data/api-v/dbperson/updateDbPerson [POST] func (dbc DbPersonController) UpdateDbPerson(c *gin.Context) { var dbperson models.Dbtablepersons - c.BindJSON(&dbperson) - personid := dbperson.Id - if personid == "" { + err := c.BindJSON(&dbperson) + if err !=nil || dbperson.Id == "" { util.ResponseFormat(c, code.RequestParamError, nil) return } dbperson.PriUpdate() - url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport + - "/" + config.EsInfo.EsIndex.Dbtablepersons.IndexName + "/" + config.EsInfo.EsIndex.Dbtablepersons.IndexType + "/" + personid + "/_update?refresh=wait_for" - dbTableByte, err := json.Marshal(dbperson) - if err != nil { - log.Fatalf("Json marshaling failed锛�%s", err) - } - //logger.Debugf("%s\n", dbTableByte) - params := "{\"doc\":" + string(dbTableByte) + "}" - logger.Debug("璇锋眰url:%s;\n 璇锋眰鍙傛暟params:%s", url, params) - data := esutil.GetEsDataReq(url, params, false) - //c.JSON(200, changeEsRespData(data,"淇敼鎴愬姛")) - result := changeEsRespData(data, "淇敼鎴愬姛") - if result["success"].(bool) { - //code.Success.Message = "淇敼搴曞簱浜哄憳鎴愬姛" - //if dbperson.Enable == 1 { - // discovery.AddDbMessage(&protomsg.EsPersonCacheChange{ - // Type: protomsg.EsCacheChanged_T_DbTablePerson, - // PersonId: dbperson.Id, - // TableId: []string{ dbperson.TableId }, - // Feature: "", - // Action: protomsg.DbAction_Insert, - // }) - //} else { - // discovery.AddDbMessage(&protomsg.EsPersonCacheChange{ - // Type: protomsg.EsCacheChanged_T_DbTablePerson, - // PersonId: dbperson.Id, - // TableId: []string{ dbperson.TableId }, - // Feature: "", - // Action: protomsg.DbAction_Delete, - // }) - //} - util.ResponseFormat(c, code.Success, result["data"]) + var pApi dbapi.DbPersonApi + paramBody := util.Struct2Map(dbperson) + b, data := pApi.UpdateDbPerson(paramBody) + if b { + util.ResponseFormat(c, code.Success, data) } else { - //code.ServiceInsideError.Message += result["msg"].(string) - util.ResponseFormat(c, code.ServiceInsideError, result["data"]) + 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 鍒犻櫎搴曞簱浜哄憳 @@ -141,83 +147,46 @@ // @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) { - uuid := c.Params.ByName("uuid") - url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport + - "/" + config.EsInfo.EsIndex.Dbtablepersons.IndexName + "/" + config.EsInfo.EsIndex.Dbtablepersons.IndexType + "/" + uuid + "/_update?refresh=wait_for" - params := "{\"doc\":{\"isDelete\":\"1\",\"updateTime\":\"" + time.Now().Format("2006-01-02 15:04:05") + "\"}}" - logger.Debug("鍒犻櫎璇锋眰url:%s;\n 璇锋眰鍙傛暟params:%s", url, params) - - data := esutil.GetEsDataReq(url, params, false) - - //c.JSON(200, changeEsRespData(data,"鍒犻櫎鎴愬姛")) - result := changeEsRespData(data, "鍒犻櫎鎴愬姛") - if result["success"].(bool) { - //code.Success.Message = "鍒犻櫎搴曞簱浜哄憳鎴愬姛" - //閫氱煡姣斿杩涚▼锛屾浜哄凡鍒犻櫎 - //dbperArr, e := esApi.Dbpersoninfosbyid([]string{uuid}, config.EsInfo.EsIndex.Dbtablepersons.IndexName, config.EsInfo.Masterip, config.EsInfo.Httpport) - //if e ==nil && len(dbperArr) > 0{ - // if dbperArr[0].TableId !=""{ - // discovery.AddDbMessage(&protomsg.EsPersonCacheChange{ - // Type: protomsg.EsCacheChanged_T_DbTablePerson, - // PersonId: uuid, - // TableId: []string{ dbperArr[0].TableId }, - // Feature: "", - // Action: protomsg.DbAction_Delete, - // }) - // } - //} - - util.ResponseFormat(c, code.Success, result["data"]) + 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 { - //code.ServiceInsideError.Message += result["msg"].(string) - util.ResponseFormat(c, code.ServiceInsideError, result["data"]) + util.ResponseFormat(c, code.ServiceInsideError, "鍒犻櫎澶辫触") } } -// @Summary 鍒犻櫎搴曞簱浜哄憳 -// @Description 鍒犻櫎搴撲汉鍛� +type multiIds []string + +// @Summary 鎵归噺鍒犻櫎搴曞簱浜哄憳 +// @Description 鎵归噺鍒犻櫎搴撲汉鍛� // @Accept json // @Produce json // @Tags dbperson 搴曞簱浜哄憳 -// @Param uuids body []string true "搴曞簱浜哄憳ids " +// @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) - c.BindJSON(&uuids) - bytes, _ := json.Marshal(uuids) - s := string(bytes) - url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport + - "/" + config.EsInfo.EsIndex.Dbtablepersons.IndexName + "/_update_by_query?refresh" // + config.EsInfo.EsIndex.Dbtablepersons.IndexType + "/" + "/" + s - params := "{\"script\":{\"lang\":\"painless\",\"inline\":\"ctx._source.isDelete = \\\"1\\\";" + - "ctx._source.updateTime = \\\"" + time.Now().Format("2006-01-02 15:04:05") + "\\\"\"},\"query\":{\"terms\":{\"_id\":" + s + "}}}" - logger.Debug("鍒犻櫎璇锋眰url:%s;\n 璇锋眰鍙傛暟params:%s", url, params) - data := esutil.GetEsDataReq(url, params, false) - //c.JSON(200, changeEsRespData(data,"鍒犻櫎鎴愬姛")) - //result := changeEsRespData(data, "鍒犻櫎鎴愬姛") - if data["error"] == nil { - //code.Success.Message = "鍒犻櫎搴曞簱浜哄憳鎴愬姛" - //dbperArr, e := esApi.Dbpersoninfosbyid(uuids, config.EsInfo.EsIndex.Dbtablepersons.IndexName, config.EsInfo.Masterip, config.EsInfo.Httpport) - //if e ==nil && len(dbperArr) > 0{ - // for _,esPer :=range dbperArr { - // if esPer.TableId !=""{ - // discovery.AddDbMessage(&protomsg.EsPersonCacheChange{ - // Type: protomsg.EsCacheChanged_T_DbTablePerson, - // PersonId: esPer.Id, - // TableId: []string{ esPer.TableId }, - // Feature: "", - // Action: protomsg.DbAction_Delete, - // }) - // } - // } - //} + 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 { - //code.ServiceInsideError.Message += result["msg"].(string) - util.ResponseFormat(c, code.ServiceInsideError, data["error"]) + util.ResponseFormat(c, code.ServiceInsideError, "鍒犻櫎澶辫触") } } @@ -226,72 +195,80 @@ // @Accept json // @Produce json // @Tags dbperson 搴曞簱浜哄憳 -// @Param reqMap body map false "{"tableId":"","orderName":"uuid","orderType":"desc","contentValue":"","page":1,"size":8}" +// @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) { - url := "http://" + config.EsInfo.Masterip + ":" + config.EsInfo.Httpport + - "/" + config.EsInfo.EsIndex.Dbtablepersons.IndexName + "/_search" - reqBody := make(map[string]interface{}, 5) - c.BindJSON(&reqBody) - tableId := "" - if reqBody["tableId"] != nil { - tableId = reqBody["tableId"].(string) + //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 } - orderName := "_id" - if reqBody["orderName"] != nil { - orderName = reqBody["orderName"].(string) + + if reqBody.TableId == "" { + util.ResponseFormat(c,code.RequestParamError,"鍙傛暟鏈夎锛屽簳搴搃d涓嶈兘涓虹┖") + return + } + orderName := "id" + if reqBody.OrderName != "" { + orderName = reqBody.OrderName } // 鍒楀悕 orderType := "desc" - if reqBody["orderType"] != nil { - orderType = reqBody["orderType"].(string) + if reqBody.OrderType != "" { + orderType = reqBody.OrderType } // 鍒楃被鍨� contentValue := "" - if reqBody["contentValue"] != nil { - contentValue = reqBody["contentValue"].(string) - } //杈撳叆妗嗗唴瀹� + page := 1 - if reqBody["page"] != nil { - page = int(reqBody["page"].(float64)) + if reqBody.Page >1 { + page = reqBody.Page } // 椤电爜 size := 8 - if reqBody["size"] != nil { - size = int(reqBody["size"].(float64)) + if reqBody.Size >8 { + size = reqBody.Size } // 鏉℃暟 - from := (page - 1) * size - syncTerm := "" - contentParam := "" - if tableId == "all" || tableId == "" { - // / 鎵�鏈変汉鍛� - } else { - syncTerm = "{\"term\":{\"tableId\":\"" + tableId + "\"}}" // 搴曞簱浜哄憳 - } - if contentValue != "" { - contentParam = ",\"must\":[{\"multi_match\":{\"query\":\"" + contentValue + "\",\"type\":\"best_fields\"," + - "\"fields\":[\"personName\",\"sex\",\"idCard\",\"phoneNum\"],\"tie_breaker\":0.3}}]" - } if orderType == "desc" { orderType = "desc" } else { orderType = "asc" } - - 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) - //c.JSON(200, data) - util.ResponseFormat(c, code.Success, data) + 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":"uuid","orderType":"desc","contentValue":"","page":1,"size":8}" +// @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] @@ -352,14 +329,7 @@ data := esutil.GetEsDataReq(url, params, true) featByte := make([]byte, 0, 1024) if len(faceUrl) > 3 { // linux - /*fileName := picUrl // picIp 瀹氫箟鍦� fileController weedfs 鏂囦欢鏈嶅姟鍣� 璁块棶 璺緞 鍓嶇紑 - detect := gorun.GetSimpleFaceDetect(fileName) - if len(detect) != 1 { - util.ResponseFormat(c,code.TooManyFeatureFindError,"鍥剧墖鐗瑰緛鍊艰繃澶�") - return - }else { - featByte = detect[0]["feature"].([]byte) - }*/ // linux + } to := page * size datalist := sourceCompare(data["datalist"].([]interface{}), false, featByte, threshold) @@ -375,3 +345,4 @@ //c.JSON(200, data) util.ResponseFormat(c, code.Success, data) } +*/ -- Gitblit v1.8.0