package controllers
|
|
import (
|
"basic.com/pubsub/protomsg.git"
|
"basic.com/valib/bhomeclient.git"
|
"encoding/json"
|
"github.com/satori/go.uuid"
|
"strings"
|
"vamicro/scene-service/models"
|
"vamicro/scene-service/service"
|
)
|
|
type CameraGroupController struct {
|
}
|
|
// @Summary 查询所有摄像机分组
|
// @Description 查询所有摄像机分组
|
// @Produce json
|
// @Tags 摄像机分组
|
// @Param groupName query string 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/camera/group/findAll [get]
|
func (cgc *CameraGroupController) FindAll(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
var result = make([]CameraGroupVo, 0)
|
var cg models.CameraGroup
|
arr := cg.FindAll()
|
if arr != nil {
|
for _, g := range arr {
|
gn := CameraGroupVo{
|
Id: g.Id,
|
GroupName: g.GroupName,
|
}
|
gn.CameraIds = strings.Split(g.CameraIds, ",")
|
|
result = append(result, gn)
|
}
|
}
|
return &bhomeclient.Reply{Success: true, Data: result}
|
}
|
|
type CameraGroupVo struct {
|
Id string `json:"id" binding:"required"`
|
GroupName string `json:"groupName" binding:"required"`
|
CameraIds []string `json:"cameraIds"`
|
}
|
|
// @Summary 新增摄像机分组
|
// @Description 新增摄像机分组
|
// @Produce json
|
// @Tags 摄像机分组
|
// @Param reqBody body controllers.CameraGroupVo 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/camera/group/save [post]
|
func (cgc *CameraGroupController) Save(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
var reqBody CameraGroupVo
|
err := c.BindJSON(&reqBody)
|
if err != nil {
|
return &bhomeclient.Reply{Msg: "参数有误"}
|
}
|
|
if reqBody.Id == "" {
|
tmp := models.CameraGroup{
|
Id: uuid.NewV4().String(),
|
GroupName: reqBody.GroupName,
|
CameraIds: strings.Join(reqBody.CameraIds, ","),
|
}
|
if b, err := tmp.Insert(); b {
|
return &bhomeclient.Reply{Success: true, Data: tmp}
|
} else {
|
return &bhomeclient.Reply{Msg: err.Error()}
|
}
|
} else {
|
tmp := models.CameraGroup{
|
Id: reqBody.Id,
|
GroupName: reqBody.GroupName,
|
CameraIds: strings.Join(reqBody.CameraIds, ","),
|
}
|
if b, err := tmp.Update(); b {
|
return &bhomeclient.Reply{Success: true, Data: tmp}
|
} else {
|
return &bhomeclient.Reply{Msg: err.Error()}
|
}
|
}
|
}
|
|
// @Summary 删除摄像机分组
|
// @Description 删除摄像机分组
|
// @Produce json
|
// @Tags 摄像机分组
|
// @Param reqBody body controllers.CameraGroupVo 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/camera/group/del [delete]
|
func (cgc *CameraGroupController) Delete(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
groupId := c.Query("groupId")
|
if groupId == "" {
|
return &bhomeclient.Reply{Msg: "参数有误"}
|
}
|
|
var cg models.CameraGroup
|
if cg.Delete(groupId) {
|
cgc.addDbChangeMsg(h.Bk, protomsg.TableChanged_T_CameraPolygonRelation, "", protomsg.DbAction_Delete, "")
|
return &bhomeclient.Reply{Success: true, Msg: "删除成功"}
|
} else {
|
return &bhomeclient.Reply{Msg: "删除失败"}
|
}
|
}
|
|
func (cgc *CameraGroupController) addDbChangeMsg(bk bhomeclient.Broker, tChanged protomsg.TableChanged, id string, action protomsg.DbAction, info string) {
|
dbMsg := protomsg.DbChangeMessage{
|
Table: tChanged,
|
Id: id,
|
Action: action,
|
Info: info,
|
}
|
pb, _ := json.Marshal(dbMsg)
|
bk.Publish(service.ProcName, pb)
|
}
|