| | |
| | | package controllers |
| | | |
| | | import ( |
| | | "basic.com/dbapi.git" |
| | | "encoding/json" |
| | | "fmt" |
| | | "log" |
| | | "strconv" |
| | | "time" |
| | | "webserver/extend/logger" |
| | | |
| | | "github.com/gin-gonic/gin" |
| | | "github.com/satori/go.uuid" |
| | |
| | | // @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 { |
| | | fmt.Print("Json marshaling failed:%s\n", e) |
| | | |
| | | 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 |
| | | } |
| | | params := string(personbytes) |
| | | fmt.Print("请求url:%s;\n 请求参数params:%s", url, params) |
| | | data, _ := esutil.PutEsDataReq(url, params) |
| | | //c.JSON(200, changeEsRespData(data, "添加人员成功")) |
| | | result = changeEsRespData(data, "添加成功") |
| | | return result |
| | | } |
| | | |
| | |
| | | // @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) |
| | | } |
| | | //fmt.Printf("%s\n", dbTableByte) |
| | | params := "{\"doc\":" + string(dbTableByte) + "}" |
| | | fmt.Print("请求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 = "修改底库人员成功" |
| | | 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 删除底库人员 |
| | |
| | | // @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") + "\"}}" |
| | | fmt.Print("删除请求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 = "删除底库人员成功" |
| | | 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, "删除失败") |
| | | } |
| | | } |
| | | |
| | |
| | | // @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=wait_for" // + 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 + "}}}" |
| | | fmt.Print("删除请求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 = "删除底库人员成功" |
| | | 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, "删除失败") |
| | | } |
| | | } |
| | | |
| | |
| | | // @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) |
| | | } |
| | | orderName := "_id" |
| | | if tableId == "" { |
| | | util.ResponseFormat(c,code.RequestParamError,"参数有误,底库id不能为空") |
| | | return |
| | | } |
| | | orderName := "id" |
| | | if reqBody["orderName"] != nil { |
| | | orderName = reqBody["orderName"].(string) |
| | | } // 列名 |
| | |
| | | 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 contentValue != "" { |
| | | contentParam = ",\"must\":[{\"multi_match\":{\"query\":\"" + contentValue + "\",\"type\":\"best_fields\"," + |
| | | "\"fields\":[\"personName\",\"sex\",\"idCard\",\"phoneNum\"],\"tie_breaker\":0.3}}]" |
| | | } |
| | | |
| | | if orderType == "desc" { |
| | |
| | | } else { |
| | | orderType = "asc" |
| | | } |
| | | |
| | | params := "{\"query\":{\"bool\":{\"must_not\":[" + |
| | | "{\"term\":{\"isDelete\":\"1\"}}],\"must_not\":[" + syncTerm + "]" + contentParam + "}},\"from\":" + strconv.Itoa(from) + ",\"size\":" + strconv.Itoa(size) + ",\"sort\":{\"" + orderName + "\":{\"order\":\"" + orderType + "\"}}}" |
| | | fmt.Print("请求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":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{}{}) |
| | | } |
| | | } |
| | | |
| | | // @Summary 查询底库人员列表 |
| | |
| | | if tableId == "all" || tableId == "" { |
| | | // / 所有人员 |
| | | } else { |
| | | syncTerm = ",{\"term\":{\"tableId\":\"" + tableId + "\"}}" // 底库人员 |
| | | syncTerm = "{\"term\":{\"tableId\":\"" + tableId + "\"}}" // 底库人员 |
| | | } |
| | | if orderType == "desc" { |
| | | orderType = "desc" |
| | |
| | | orderType = "asc" |
| | | } |
| | | |
| | | params := "{\"query\":{\"bool\":{\"filter\":[" + |
| | | "{\"term\":{\"isDelete\":\"0\"}}" + syncTerm + "]" + contentParam + "}},\"from\":" + strconv.Itoa(from) + ",\"size\":" + strconv.Itoa(size) + ",\"sort\":{\"" + orderName + "\":{\"order\":\"" + orderType + "\"}}}" |
| | | fmt.Print("请求url:%s;\n 请求参数params:%s", url, params) |
| | | //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 |