123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package camera
- import (
- "context"
- "github.com/jaryhe/gopkgs/database"
- "github.com/jaryhe/gopkgs/logger"
- "go.uber.org/zap"
- "smart-site-management/errors"
- "smart-site-management/model"
- "smart-site-management/pb/v1"
- )
- func DeviceBindCamera(ctx context.Context, req *v1.DeviceBindCameraRequest) (reply *v1.DeviceBindCameraReply, err error) {
- reply = &v1.DeviceBindCameraReply{}
- if req.DeviceId < 1 || req.ChannelId < 1 {
- return nil, errors.ParamsError
- }
- p := model.TDevice{}
- // 判断是否已绑定摄像头
- where := map[string]interface{}{
- "device_code !=":model.DeviceTypeVedio,
- "vss_channel_id":req.ChannelId,
- }
- count, err := p.Count(database.DB(), where)
- if err != nil {
- logger.Error("DeviceBindCamera",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- if count > 0 {
- return nil, errors.DeviceExistError
- }
- pc := model.Vsschanneltbl{}
- // 判断摄像头是否存在
- where = map[string]interface{}{
- "ID":req.ChannelId,
- }
- count, err = pc.Count(database.DB(), where, nil)
- if err != nil {
- logger.Error("DeviceBindCamera",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- if count == 0 {
- return nil, errors.NoRecordError
- }
- // 判断设备是否存在或可用
- where = map[string]interface{}{
- "id":req.DeviceId,
- "verify_status":1,
- }
- err = p.Find(database.DB(), where)
- if err != nil {
- logger.Error("DeviceBindCamera",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- if p.Id == 0 {
- return nil, errors.NoRecordError
- }
- if p.DeviceCode == model.DeviceTypeVedio {
- return nil, errors.CameraBindError
- }
- values := map[string]interface{}{
- "vss_channel_id":req.ChannelId,
- }
- err = p.Update(database.DB(), where, values)
- if err != nil {
- logger.Error("DeviceBindCamera",
- zap.String("err", err.Error()))
- return nil, errors.DataBaseError
- }
- return reply, nil
- }
|