package app import ( "errors" "fmt" "github.com/sirupsen/logrus" "gorm.io/gorm" "io/ioutil" "lc-fangdaosha/global" "lc-fangdaosha/isapi" "lc-fangdaosha/model/app" appReq "lc-fangdaosha/model/app/request" "lc-fangdaosha/model/common/request" "net/http" "net/url" ) type CameraService struct { } // CreateCamera 创建摄像机记录 // Author [piexlmax](https://github.com/piexlmax) func (cameraService *CameraService) CreateCamera(camera *app.Camera) (err error) { err = global.Db.Create(camera).Error return err } // 判断摄像机是否已注册 func (cameraService *CameraService) CreateIfNotExist(info isapi.DeviceInfo, gwId uint) (err error) { var camera app.Camera err = global.Db.Select("id,sn").Model(&app.Camera{}).Where("sn = ? AND is_deleted = 0", info.SerialNumber).Scan(&camera).Debug().Error if err != nil { return err } fmt.Println(camera) if camera.SN != "" { //已注册至数据库,绑定至网关 global.Db.Model(&app.Camera{}).Where("id = ?", camera.ID).Update("gid", gwId) return errors.New("摄像头已注册") } //注册 var ca = &app.Camera{SN: info.SerialNumber, Gid: gwId, MacAddress: info.MacAddress} err = global.Db.Create(ca).Debug().Error if err != nil { return err } logrus.Infof("摄像头注册成功:%s", info.SerialNumber) return nil } // DeleteCamera 删除摄像机记录 // Author [piexlmax](https://github.com/piexlmax) func (cameraService *CameraService) DeleteCamera(camera app.Camera) (err error) { err = global.Db.Transaction(func(tx *gorm.DB) error { if err := tx.Debug().Model(&app.Camera{}).Where("id = ?", camera.ID).Update("is_deleted", 1).Error; err != nil { return err } if err = tx.Debug().Delete(&camera).Error; err != nil { return err } return nil }) return err } // DeleteCameraByIds 批量删除摄像机记录 // Author [piexlmax](https://github.com/piexlmax) func (cameraService *CameraService) DeleteCameraByIds(ids request.IdsReq) (err error) { err = global.Db.Transaction(func(tx *gorm.DB) error { if err := tx.Model(&app.Camera{}).Where("id in ?", ids.Ids).Update("is_deleted", 1).Error; err != nil { return err } if err := tx.Where("id in ?", ids.Ids).Delete(&app.Camera{}).Error; err != nil { return err } return nil }) return err } // UpdateCamera 更新摄像机记录 // Author [piexlmax](https://github.com/piexlmax) func (cameraService *CameraService) UpdateCamera(camera app.Camera) (err error) { fmt.Println("camera", camera) err = global.Db.Debug().Omit("id, created_at, deleted_at, gid, name, sn,mac_address, deleted_by").Save(&camera).Error return err } // GetCamera 根据id获取摄像机记录 // Author [piexlmax](https://github.com/piexlmax) func (cameraService *CameraService) GetCamera(id uint) (camera app.Camera, err error) { err = global.Db.Where("id = ? AND is_deleted = 0", id).First(&camera).Error return } // GetCameraByGid 根据网关id获取摄像机 func (cameraService *CameraService) GetCameraByGid(id string) (cameras []app.Camera, err error) { err = global.Db.Where("gid = ? AND is_deleted = 0", id).Find(&cameras).Debug().Error return } // GetCameraByDeviceId 根据设备id获取摄像机 func (cameraService *CameraService) GetCameraByDeviceId(devId string) (camera app.Camera, err error) { err = global.Db.Where("sn = ? AND is_deleted = 0", devId).Find(&camera).Debug().Error return } // 获取摄像机的注册id func (cameraService *CameraService) SetCameraRegisted(devId string) (err error) { return global.Db.Table("camera").Where("sn = ? AND is_deleted = 0", devId).Update("is_registed", 1).Debug().Error } // GetCameraInfoList 分页获取摄像机记录 // Author [piexlmax](https://github.com/piexlmax) func (cameraService *CameraService) GetCameraInfoList(info appReq.CameraSearch) (list []app.Camera, total int64, err error) { limit := info.PageSize offset := info.PageSize * (info.Page - 1) // 创建db db := global.Db.Model(&app.Camera{}) var cameras []app.Camera // 如果有条件搜索 下方会自动创建搜索语句 if info.StartCreatedAt != nil && info.EndCreatedAt != nil { db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt) } err = db.Count(&total).Error if err != nil { return } if limit != 0 { db = db.Limit(limit).Offset(offset) } err = db.Find(&cameras).Error return cameras, total, err } // GetRewindText 请求回话加密数据 func (cameraService *CameraService) GetRewindText(stearmId string, date string) (string, error) { baseurl := global.Config.Foreign.SecurityRewindUrl api := baseurl + "/list/%v/%v" url2 := fmt.Sprintf(api, stearmId, url.QueryEscape(date)) return getRewindApi(url2) } // getRewindApi 公用请求接口 func getRewindApi(url string) (string, error) { method := "GET" client := &http.Client{} req, err := http.NewRequest(method, url, nil) if err != nil { return "", nil } res, err := client.Do(req) if err != nil { return "", nil } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { return "", nil } return string(body), err }