12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package dao
- import (
- "github.com/jinzhu/gorm"
- "time"
- )
- type Gateway struct {
- ID int `gorm:"primary_key" json:"id"`
- GatewayName string `gorm:"type:varchar(64)" json:"gatewayName"`
- GatewaySN string `gorm:"type:varchar(60)" json:"gatewaySn"`
- LampPoleId int `gorm:"type:int" json:"lampPoleId"`
- LampPoleSn string `gorm:"type:varchar(64)" json:"lampPoleSn"`
- LampPoleName string `gorm:"type:varchar(64)" json:"lampPoleName"`
- LampPoleLocation string `gorm:"type:varchar(255)" json:"lampPoleLocation"`
- LampLat float64 `gorm:"type:double(17,14)" json:"lampLat"`
- LampLng float64 `gorm:"type:double(17,14)" json:"lampLng"`
- BrandId int `gorm:"type:int" json:"brandId"`
- ModelId int `gorm:"type:int" json:"modelId"`
- GatewayInstallTime time.Time `gorm:"type:date" json:"gatewayInstallTime"`
- RatedPower float32 `gorm:"type:double(8,2);default:0.00" json:"ratedPower"`
- IpAddress string `gorm:"type:varchar(50)" json:"ipAddress"`
- DiNum int `gorm:"type:int;default 0" json:"diNum"`
- DoNum int `gorm:"type:int;default 0" json:"doNum"`
- Num485 int `gorm:"type:int;default 0" json:"num485"`
- NetworkNum int `gorm:"type:int;default 0" json:"networkNum"`
- TenantId int `gorm:"type:int" json:"tenantId"`
- CreateTime time.Time `gorm:"type:datetime" json:"createTime"`
- CreateUser int64 `gorm:"type:bigint" json:"createUser"`
- UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"`
- UpdateUser int64 `gorm:"type:bigint" json:"updateUser"`
- IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"`
- Status int `gorm:"type:int" json:"status"`
- Tag string `gorm:"type:varchar(255)" json:"tag"`
- }
- func (Gateway) TableName() string {
- return "t_dev_wisdom_gateway"
- }
- func (c *Gateway) Delete() error {
- return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
- "update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
- }
- func (c Gateway) IsExistedBySN() bool {
- var count = 0
- _ = Db.Debug().Model(&c).Where(" gateway_sn = ? and is_deleted = ?",
- c.GatewaySN, c.IsDeleted).Count(&count).Error
- return count > 0
- }
- func (c Gateway) IsExistedByNameAndCode() bool {
- var devices []Gateway
- err := Db.Debug().Model(&c).Where("gateway_sn = ? and is_deleted = ?",
- c.TenantId, c.IsDeleted).Find(&devices).Error
- if gorm.IsRecordNotFoundError(err) {
- return false
- }
- for _, d := range devices {
- if d.ID != c.ID {
- return true
- }
- }
- return false
- }
- func (c *Gateway) Create() error {
- return Db.Debug().Model(&c).Save(&c).Error
- }
- func (c *Gateway) Update() error {
- return Db.Debug().Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
- }
- func (c *Gateway) GetDevice() error {
- err := Db.Debug().Model(&c).Where(" id = ? ", c.ID).First(&c).Error
- return err
- }
- func (c Gateway) GetDevices(offset, limit int) ([]Gateway, error) {
- var devices []Gateway
- db := Db.Debug().Model(&c)
- if c.GatewaySN != "" {
- db = db.Where("gateway_name like ? or gateway_sn like ?", "%"+c.GatewaySN+"%", "%"+c.GatewaySN+"%")
- }
- err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&devices).Error
- return devices, err
- }
- func (c Gateway) GetAllDevices() ([]*Gateway, error) {
- var devices []*Gateway
- err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
- return devices, err
- }
|