sub_device.go 836 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package model
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "sparrow/pkg/models"
  5. )
  6. // SubDevice sub_devices data
  7. type SubDevice struct {
  8. db *gorm.DB
  9. }
  10. // Init sub_device
  11. func (a *SubDevice) Init(db *gorm.DB) *SubDevice {
  12. a.db = db
  13. return a
  14. }
  15. // GetSubDeviceCount
  16. func (a *SubDevice) GetSubDeviceCount(deviceCode string) (count int, err error) {
  17. subDevice := &models.SubDevice{
  18. DeviceCode: deviceCode,
  19. }
  20. err = a.db.Model(subDevice).Where(subDevice).Count(&count).Error
  21. return
  22. }
  23. // GetSubDevices 获取子设备列表
  24. func (a *SubDevice) GetSubDevices(deviceCode string, pi, ps int) (data []*models.SubDevice, total int, err error) {
  25. tx := a.db
  26. if deviceCode != "" {
  27. tx = tx.Where("device_code = ?", deviceCode)
  28. }
  29. err = tx.Limit(ps).Offset((pi - 1) * ps).Find(&data).Error
  30. tx.Model(&models.SubDevice{}).Count(&total)
  31. return
  32. }