123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package app
- import (
- "errors"
- "github.com/sirupsen/logrus"
- "gorm.io/gorm"
- "lcfns/global"
- "lcfns/model/app"
- "lcfns/model/app/request"
- "lcfns/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, State: 1}
- 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, total int64, err error) {
- //SELECT g.id,g.name AS gateway_name,g.osn,
- // c.`name` AS camera_name, RIGHT(c.sn,9) AS sn,stream_id,stream_url,
- // l.device_id AS ls_id,l.`status` AS ls_status
- //FROM gateway g LEFT JOIN camera c ON g.id = c.gid LEFT JOIN loudspeaker l ON g.id = l.gid ;
- db := global.Db.Select("g.id,g.uid,g.gateway_name,g.isn,g.osn,g.state AS gateway_state,g.description," +
- "c.name AS camera_name, RIGHT(c.sn,9) AS camera_sn,c.stream_id,c.stream_url,c.state AS camera_state," +
- //"i.device_id AS ipcast_id," +
- "i.state AS ipcast_state").
- Table("gateway g LEFT JOIN camera c ON g.id = c.gid LEFT JOIN ipcast i ON g.id = i.gid").
- Where("g.is_deleted = 0")
- //条件查询设备,网关名,网关osn,摄像头sn,ip音柱devid,
- if req.Uid > 1 {
- db.Where("g.uid = ?", req.Uid)
- }
- if req.Osn != "" {
- db.Where("g.osn = ?", req.Osn)
- }
- if req.IpcastId != 0 {
- db.Where("i.ipcast_id = ?", req.IpcastId)
- }
- if req.CameraSn != "" {
- db.Where("c.sn LIKE ?", "%"+req.CameraSn)
- }
- if req.Name != "" {
- db.Where("gateway_name LIKE ?", "%"+req.Name+"%")
- }
- if req.Page != 0 || req.PageSize != 0 {
- db.Offset(req.PageSize * (req.Page - 1)).Limit(req.PageSize)
- }
- err = db.Debug().Find(&list).Error
- global.Db.Table("gateway").Where("is_deleted = 0").Count(&total)
- 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
- }
- func (gs *GatewayService) UpdateState(gw *app.Gateway) error {
- return global.Db.Model(&app.Gateway{}).Where("isn = ? AND is_deleted = 0", gw.Isn).Update("state", gw.State).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)
- }
- }
- //删除ip音柱
- if devs.Iid != 0 {
- err = global.Db.Model(&app.Ipcast{}).Where("id = ?", devs.Iid).Update("is_deleted", 1).Error
- if err != nil {
- logrus.Error("删除ip音柱失败", 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 devs.Iid != 0 {
- if err2 := global.Db.Model(&app.Ipcast{}).Where("id = ?", devs.Iid).Update("gid", 0).Error; err2 != nil {
- logrus.WithFields(logrus.Fields{"网关": devs.Osn, "ip音柱": devs.Iid}).Error("解绑失败", err)
- err = errors.Join(err2)
- }
- }
- if err == nil {
- logrus.WithFields(logrus.Fields{"网关": devs.Osn, "摄像头": devs.Csn, "ip音柱": devs.Iid}).Info("解绑成功")
- }
- return err
- }
- func (gs *GatewayService) DevIds(id uint) (devs app.Devices) {
- //查询网关及关联设备的id
- global.Db.Select("g.id AS gid,g.osn,c.id AS cid,RIGHT(c.sn,9) AS csn,i.id AS iid").
- Table("gateway g LEFT JOIN camera c ON g.id = c.gid LEFT JOIN ipcast i ON g.id = i.gid").
- Where("g.id = ?", id).
- Find(&devs)
- return
- }
|