123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package app
- import (
- "errors"
- "github.com/sirupsen/logrus"
- "gorm.io/gorm"
- "lc-fangdaosha/global"
- "lc-fangdaosha/model/app"
- "lc-fangdaosha/model/app/request"
- "lc-fangdaosha/model/app/response"
- )
- var ExistError = errors.New("已存在")
- type GatewayService struct {
- }
- func (gs *GatewayService) CreateIfNotExist(isn string) (*app.Gateway, error) {
- var gate *app.Gateway
- err := global.Db.Select("id, isn").Model(&app.Gateway{}).Where("isn = ? AND is_deleted = 0", isn).Scan(&gate).Debug().Error
- if err != nil {
- return nil, err
- }
- if gate != nil {
- return gate, ExistError
- }
- var gateway = &app.Gateway{Isn: isn}
- err = global.Db.Create(gateway).Debug().Error
- if err != nil {
- return nil, err
- }
- logrus.Infof("网关[%s]注册成功", isn)
- return gateway, nil
- }
- // List 该用户下所有设备数据
- func (gs *GatewayService) List(req request.GatewayRequest) (list []response.Gateways, err error) {
- db := global.Db.Select("g.id,g.gateway_name,g.osn,g.description,c.name AS camera_name, RIGHT(c.sn,9) AS camera_sn,c.stream_id,c.stream_url").
- Table("gateway g LEFT JOIN camera c ON g.id = c.gid").
- Where("g.is_deleted = 0")
- //条件查询设备,网关名,网关osn,摄像头sn
- if req.Uid > 0 {
- db.Where("g.uid = ?", req.Uid)
- }
- if req.Osn != "" {
- db.Where("g.osn = ?", req.Osn)
- }
- if req.CameraSn != "" {
- db.Where("c.sn LIKE ?", "%"+req.CameraSn)
- }
- if req.Name != "" {
- db.Where("gateway_name LIKE ?", "%"+req.Name+"%")
- }
- db.Offset(req.PageSize * (req.Page - 1)).Limit(req.PageSize)
- err = db.Debug().Find(&list).Error
- return
- }
- // AdminSet 用于后台给网关配置osn或分配用户,只有超级管理员才有权限
- func (gs *GatewayService) AdminSet(gw *app.Gateway) error {
- var m = make(map[string]any)
- if gw.Uid != 0 {
- m["uid"] = gw.Uid
- }
- if gw.Osn != "" {
- m["osn"] = gw.Osn
- }
- return global.Db.Model(&app.Gateway{}).Where("id = ? AND is_deleted = 0", gw.ID).Updates(m).Debug().Error
- }
- // Update 由于普通用户配置网关名和描述信息
- func (gs *GatewayService) Update(gw *app.Gateway) error {
- var m = make(map[string]any)
- if gw.GatewayName != "" {
- m["gateway_name"] = gw.GatewayName
- }
- if gw.Description != "" {
- m["description"] = gw.Description
- }
- return global.Db.Model(&app.Gateway{}).Where("id = ? AND is_deleted = 0", gw.ID).Updates(m).Debug().Error
- }
- // DeleteGateways 删除网关及关联设备
- func (gs *GatewayService) DeleteGateways(gw app.Gateway) (err error) {
- devs := gs.DevIds(gw.ID)
- err = global.Db.Transaction(func(tx *gorm.DB) error {
- //删除网关
- if err := tx.Debug().Model(&app.Gateway{}).Where("id = ?", gw.ID).Update("is_deleted", 1).Error; err != nil {
- return err
- }
- if err = tx.Debug().Delete(&gw).Error; err != nil {
- return err
- }
- return nil
- })
- //删除摄像头
- cs := CameraService{}
- if devs.Cid != 0 {
- var c app.Camera
- c.ID = devs.Cid
- err = cs.DeleteCamera(c)
- if err != nil {
- logrus.Error("删除摄像头失败", err)
- }
- }
- return err
- }
- // UnbindGateways 解绑网关下面的设备
- func (gs *GatewayService) UnbindGateways(gw app.Gateway) error {
- devs := gs.DevIds(gw.ID)
- var err error
- if devs.Cid != 0 {
- if err1 := global.Db.Model(&app.Camera{}).Where("id = ?", devs.Cid).Update("gid", 0).Error; err1 != nil {
- logrus.WithFields(logrus.Fields{"网关": devs.Osn, "摄像头": devs.Csn}).Error("解绑失败", err)
- err = errors.Join(err1)
- }
- }
- if err == nil {
- logrus.WithFields(logrus.Fields{"网关": devs.Osn, "摄像头": devs.Csn}).Info("解绑成功")
- }
- return err
- }
- // DevIds 查询网关及关联设备的id
- func (gs *GatewayService) DevIds(id uint) (devs app.Devices) {
- global.Db.Select("g.id AS gid,g.osn,c.id AS cid,RIGHT(c.sn,9) AS csn").
- Table("gateway g LEFT JOIN camera c ON g.id = c.gid").
- Where("g.id = ?", id).
- Find(&devs)
- return
- }
|