| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- package busmodels
- import (
- "device-manage/app/admin/models"
- orm "device-manage/common/global"
- "errors"
- "strconv"
- )
- type BusAppDeviceBind struct {
- BindId int `json:"bindId" gorm:"primary_key;AUTO_INCREMENT"`
- AppId int `json:"appId"`
- DeviceId int `json:"deviceId"`
- UpdateBy string `json:"updateBy" gorm:"size:128;"`
- CreateBy string `json:"createBy" gorm:"size:128;"`
- BaseModel
- }
- type AppDeviceBindedInfo struct {
- BusAppDeviceBind
- DeviceSn string `json:"deviceSn" gorm:"size:128;"`
- GroupName string `json:"groupName" gorm:"size:128;"`
- }
- type QueryBindedDeviceParams struct {
- DeviceSn string `json:"deviceSn" gorm:"size:128;"`
- AppId int `json:"appId"`
- PageSize int `json:"pageSize"`
- PageIndex int `json:"pageIndex"`
- Gids []int `json:"gids"`
- }
- type AppDeviceData struct {
- AppId int `json:"appId"`
- DevIds []int `json:"deviceId"`
- }
- func (BusAppDeviceBind) TableName() string {
- return "bus_app_device_bind"
- }
- func (e *BusAppDeviceBind) GetBindedPage(pageSize int, pageIndex int, deviceSn string, gids []int) ([]AppDeviceBindedInfo, int, error) {
- var doc []AppDeviceBindedInfo
- var devices []BusDevice
- tableBind := orm.Eloquent.Table(e.TableName())
- tableDevice := orm.Eloquent.Table("bus_device")
- tableUser := orm.Eloquent.Table("sys_user")
- //根据设备序列号得到所有的设备id
- if deviceSn != "" {
- tableDevice = tableDevice.Where("device_sn = ?", deviceSn)
- }
- //根据群组过滤目标设备
- if len(gids) > 0 {
- tableDevice = tableDevice.Where("group_id in (?)", gids)
- }
- if err := tableDevice.Find(&devices).Error; err != nil {
- return nil, 0, err
- }
- devids := make([]int, len(devices))
- for _, devid := range devices {
- devids = append(devids, devid.DeviceId)
- }
- //根据设备序列号查出来的id 过滤当前应用绑定的设备
- if e.AppId != 0 {
- tableBind = tableBind.Where("app_id = ?", e.AppId)
- }
- var count int64
- if err := tableBind.Where("device_id in (?)", devids).Order("bind_id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
- return nil, 0, err
- }
- for k, v := range doc {
- var group BusGroup
- var device BusDevice
- tableGroup := orm.Eloquent.Table("bus_group")
- tableDevice = orm.Eloquent.Table("bus_device")
- user := models.SysUser{}
- id, _ := strconv.Atoi(v.CreateBy)
- if err := tableUser.Where("user_id = ?", id).Find(&user).Error; err == nil {
- doc[k].CreateBy = user.Username
- }
- if err := tableDevice.Where("device_id = ?", v.DeviceId).Find(&device).Error; err == nil {
- doc[k].DeviceSn = device.DeviceSn
- if err := tableGroup.Where("group_id = ?", device.GroupId).Find(&group).Error; err == nil {
- doc[k].GroupName = group.GroupName
- }
- }
- }
- return doc, int(count), nil
- }
- func (e *BusAppDeviceBind) GetUnBindedPage(pageSize int, pageIndex int, deviceSn string, gids []int) ([]DeviceInfo, int, error) {
- var bindedDevices []BusAppDeviceBind
- var doc []DeviceInfo
- tableBind := orm.Eloquent.Table(e.TableName())
- tableDevice := orm.Eloquent.Table("bus_device")
- tableUser := orm.Eloquent.Table("sys_user")
- if deviceSn != "" {
- tableDevice = tableDevice.Where("device_sn like ?", "%"+deviceSn+"%")
- }
- if e.AppId != 0 {
- tableBind = tableBind.Where("app_id = ?", e.AppId)
- }
- if err := tableBind.Find(&bindedDevices).Error; err != nil {
- return nil, 0, err
- }
- //只查找在当前用户拥有的群组下的设备.
- if len(gids) > 0 {
- tableDevice = tableDevice.Where("group_id in (?)", gids)
- }
- var count int64
- if len(bindedDevices) > 0 {
- devids := make([]int, len(bindedDevices))
- for _, devid := range bindedDevices {
- devids = append(devids, devid.DeviceId)
- }
- if err := tableDevice.Where("device_id not in (?)", devids).Order("device_id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Order("created_at ASC").Offset(-1).Limit(-1).Count(&count).Error; err != nil {
- return nil, 0, err
- }
- } else {
- if err := tableDevice.Order("device_id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
- return nil, 0, err
- }
- }
- for k, v := range doc {
- var group BusGroup
- tableGroup := orm.Eloquent.Table("bus_group")
- user := models.SysUser{}
- id, _ := strconv.Atoi(v.CreateBy)
- if err := tableUser.Where("user_id = ?", id).Find(&user).Error; err == nil {
- doc[k].CreateBy = user.Username
- }
- if err := tableGroup.Where("group_id = ?", v.GroupId).Find(&group).Error; err == nil {
- doc[k].GroupName = group.GroupName
- }
- }
- return doc, int(count), nil
- }
- func (e *BusAppDeviceBind) Insert(bindData AppDeviceData) (id []int, err error) {
- //添加数据
- var count int64
- id = make([]int, len(bindData.DevIds))
- for _, devid := range bindData.DevIds {
- e.BindId = 0
- e.DeviceId = devid
- e.AppId = bindData.AppId
- orm.Eloquent.Table(e.TableName()).Where("app_id = ?", e.AppId).Where("device_id = ?", e.DeviceId).Count(&count)
- if count > 0 {
- err = errors.New("device 已存在!")
- id = append(id, devid)
- } else {
- if err = orm.Eloquent.Table(e.TableName()).Create(&e).Error; err != nil {
- id = append(id, devid)
- }
- }
- }
- return
- }
- func (e *BusAppDeviceBind) Delete(id int) (success bool, err error) {
- if err = orm.Eloquent.Table(e.TableName()).Where("bind_id = ?", id).Delete(&BusAppDeviceBind{}).Error; err != nil {
- success = false
- return
- }
- success = true
- return
- }
- func (e *BusAppDeviceBind) BatchDelete(id []int) (Result bool, err error) {
- if err = orm.Eloquent.Table(e.TableName()).Where("bind_id in (?)", id).Delete(&BusAppDeviceBind{}).Error; err != nil {
- return
- }
- Result = true
- return
- }
|