123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- 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
- }
|