package controllers
|
|
import (
|
"basic.com/dbapi.git"
|
"basic.com/fileServer/WeedFSClient.git"
|
"basic.com/pubsub/protomsg.git"
|
"encoding/base64"
|
"encoding/json"
|
"io/ioutil"
|
"sort"
|
"strconv"
|
"time"
|
"basic.com/valib/logger.git"
|
"webserver/service"
|
|
"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, "")
|
}
|
}
|
|
// @Summary 底库人员以图搜图
|
// @Description 底库人员以图搜图
|
// @Accept json
|
// @Produce json
|
// @Tags dbperson 底库人员
|
// @Param condition body models.EsSearch true "底库以图搜图参数"
|
// @Success 200 {string} json "{"code":200, msg:"", success:true}"
|
// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
|
// @Router /data/api-v/dbperson/queryDbPersonsByCompare [POST]
|
func (dbc DbPersonController) QueryDbPersonsByCompare(c *gin.Context) {
|
var searchBody models.EsSearch
|
err := c.BindJSON(&searchBody)
|
if err !=nil || searchBody.PicUrl == "" || len(searchBody.DataBases) == 0{
|
util.ResponseFormat(c, code.RequestParamError, "参数有误")
|
return
|
}
|
var faceB []byte
|
if face,ok := faceExtractedMap[searchBody.PicUrl];!ok{
|
util.ResponseFormat(c, code.RequestParamError, "请重新上传图片")
|
return
|
} else {
|
faceB = face.FaceBytes
|
}
|
var sysSetApi dbapi.SysSetApi
|
analyServerId := ""
|
flag, sysconf := sysSetApi.GetServerInfo()
|
if flag {
|
analyServerId = sysconf.ServerId
|
} else {
|
util.ResponseFormat(c, code.ComError, "analyServerId为空,配置有误")
|
return
|
}
|
arg := protomsg.CompareArgs{
|
FaceFeature: faceB,
|
CompareThreshold: 0.6,
|
}
|
arg.TableIds = searchBody.DataBases
|
arg.AnalyServerId = analyServerId
|
compareService := service.NewFaceCompareService(arg)
|
var totalData service.CompareList
|
|
dbPersonTargets := compareService.CompareDbPersons()
|
if dbPersonTargets !=nil {
|
totalData = append(totalData,*dbPersonTargets...)
|
}
|
|
service.SetCompResultByNum(&service.CompareOnce{
|
CompareNum: compareService.CompareNum,
|
CompareData: &totalData,
|
})
|
|
m := make(map[string]interface{},3)
|
if totalData != nil && totalData.Len() > 0{
|
sort.Sort(totalData)
|
total := totalData.Len()
|
|
m["compareNum"] = compareService.CompareNum
|
m["total"] = total
|
var sCompResult protomsg.SdkCompareResult
|
if total <= searchBody.Size {
|
sCompResult.CompareResult = totalData
|
} else {
|
sCompResult.CompareResult = totalData[0:searchBody.Size]
|
}
|
resultList := FillDbPersonDataToCompareResult(&sCompResult)
|
m["totalList"] = resultList
|
|
} else {
|
m["total"] = 0
|
m["compareNum"] = compareService.CompareNum
|
m["totalList"] = []CompareResult{}
|
}
|
util.ResponseFormat(c,code.Success,m)
|
}
|
|
//填充向前端返回的数据
|
func FillDbPersonDataToCompareResult(compResult *protomsg.SdkCompareResult) []DbPersonVo {
|
|
var resultList = make([]DbPersonVo, len(compResult.CompareResult))
|
dbPersonM := make(map[string]ScoreIndex, 0)
|
personIds := make([]string,0)
|
|
for idx,v :=range compResult.CompareResult{
|
dbPersonM[v.Id] = ScoreIndex{
|
Index: idx,
|
CompareScore: v.CompareScore,
|
}
|
personIds = append(personIds,v.Id)
|
}
|
logger.Debug("comp len(personIds):", len(personIds))
|
|
var dbpersons []protomsg.Dbperson
|
if len(personIds) >0 {
|
var dbpApi dbapi.DbPersonApi
|
dbpersons, _ = dbpApi.Dbpersoninfosbyid(personIds)
|
}
|
|
if len(dbpersons) >0 {
|
var dtApi dbapi.DbTableApi
|
for _,p :=range dbpersons {
|
var dbP = DbPersonVo {
|
PersonId: p.Id,
|
IdCard: p.IdCard,
|
CompareScore: util.ParseScore(dbPersonM[p.Id].CompareScore),
|
MonitorLevel: p.MonitorLevel,
|
PersonName: p.PersonName,
|
PersonPicUrl: p.PersonPicUrl,
|
PhoneNum: p.PhoneNum,
|
Sex: p.Sex,
|
TableId: p.TableId,
|
Enable: p.Enable,
|
}
|
dbTableInfos, _ := dtApi.DbtablesById([]string{ p.TableId })
|
if dbTableInfos !=nil{
|
dbP.BwType = dbTableInfos[0].BwType
|
dbP.TableName = dbTableInfos[0].TableName
|
}
|
resultList[dbPersonM[p.Id].Index] = dbP
|
}
|
}
|
|
return resultList
|
}
|
|
// @Summary 更新底库人脸照片
|
// @Description 更新底库人脸照片
|
// @Accept json
|
// @Produce json
|
// @Tags 底库人员
|
// @Param id formData string true "人员id"
|
// @Param file formData file true "人脸图片"
|
// @Success 200 {string} json "{"code":200, msg:"", success:true}"
|
// @Failure 500 {string} json "{"code":500, msg:"", success:false}"
|
// @Router /data/api-v/dbperson/updateFace [POST]
|
func (dbc DbPersonController) UpdateFace(c *gin.Context) {
|
file, header, err := c.Request.FormFile("file")
|
id := c.Request.FormValue("id")
|
if err != nil || id == "" {
|
util.ResponseFormat(c,code.RequestParamError,"参数有误")
|
return
|
}
|
//文件的名称
|
filename := header.Filename
|
defer file.Close()
|
// weedfs 上传
|
fileBytes, err := ioutil.ReadAll(file)
|
if err !=nil {
|
util.ResponseFormat(c,code.ComError,"图片读取失败")
|
return
|
}
|
|
//将上传的图片交人脸检测和人脸提取,获得特征
|
var faceBase64=""
|
faceArr, err, pI := service.GetFaceFeaFromSdk(fileBytes, time.Second*5)
|
if faceArr ==nil {
|
util.ResponseFormat(c,code.ComError,"未到提取人脸")
|
return
|
}
|
var rcFace *protomsg.Rect
|
if err ==nil && len(faceArr) >0 {
|
if len(faceArr) >1 {
|
util.ResponseFormat(c,code.ComError,"人脸大于一张,请换一张人脸图片")
|
return
|
}
|
for _,r := range faceArr {
|
//拿到人脸的坐标
|
rcFace = r.Pos.RcFace
|
|
faceBase64 = base64.StdEncoding.EncodeToString(r.Feats)//获取提取到的第一张人脸特征
|
break
|
}
|
}
|
var weedfsUri = "http://"+config.WeedFs.Ip+":"+strconv.Itoa(config.WeedFs.UploadPort)+"/submit"
|
//根据人脸坐标扣出人脸小图
|
t1 := time.Now()
|
cutFaceImgData := util.SubImg(*pI, int(rcFace.Left), int(rcFace.Top), int(rcFace.Right), int(rcFace.Bottom))
|
logger.Debug("SubImg用时:", time.Since(t1))
|
t1 = time.Now()
|
weedFilePath, e := WeedFSClient.UploadFile(weedfsUri, filename, cutFaceImgData)
|
logger.Debug("上传到weedfs用时:", time.Since(t1))
|
t1 = time.Now()
|
if e != nil {
|
util.ResponseFormat(c,code.ComError,"人脸上传失败")
|
return
|
}
|
m := map[string]interface{} {
|
"faceFeature": faceBase64,
|
"personPicUrl": weedFilePath,
|
}
|
util.ResponseFormat(c,code.Success, m)
|
|
//var dbpApi dbapi.DbPersonApi
|
//b,d := dbpApi.UpdateFace(id,faceBase64,weedFilePath)
|
//if b {
|
// util.ResponseFormat(c,code.UpdateSuccess,d)
|
//} else {
|
// util.ResponseFormat(c,code.UpdateFail,"更新人脸失败")
|
//}
|
}
|
|
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 DelMultiPerson []string
|
|
// @Summary 删除底库人员
|
// @Description 删除库人员
|
// @Accept json
|
// @Produce json
|
// @Tags dbperson 底库人员
|
// @Param uuids body controllers.DelMultiPerson 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) {
|
var uuids DelMultiPerson
|
err := c.BindJSON(&uuids)
|
if err !=nil || len(uuids)==0{
|
util.ResponseFormat(c,code.RequestParamError,"参数有误")
|
return
|
}
|
logger.Debug("DeleteMoreDbPerson len(uuids):",len(uuids))
|
var pApi dbapi.DbPersonApi
|
m := map[string]interface{}{
|
"ids": uuids,
|
}
|
b, _ := pApi.DeleteMoreDbPerson(m)
|
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 := reqBody.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"`
|
}
|