|
@@ -0,0 +1,107 @@
|
|
|
+package dao
|
|
|
+
|
|
|
+import (
|
|
|
+ "github.com/jinzhu/gorm"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+type LightControl struct {
|
|
|
+ ID int `gorm:"primary_key" json:"id"`
|
|
|
+ Name string `gorm:"type:varchar(64)" json:"name"`
|
|
|
+ SN string `gorm:"type:varchar(60)" json:"sn"`
|
|
|
+ ControlType int `gorm:"type:int" json:"controlType"`
|
|
|
+ GroupId int `gorm:"type:int" json:"groupId"`
|
|
|
+ GatewayId int `gorm:"type:varchar(32)" json:"gatewayId"`
|
|
|
+ LampPoleId int `gorm:"type:int" json:"lamp_pole_id"`
|
|
|
+ LampPoleName string `gorm:"type:varchar(64)" json:"lamp_pole_name"`
|
|
|
+ LampPoleSN string `gorm:"type:varchar(64)" json:"lamp_pole_sn"`
|
|
|
+ LampPoleLocation string `gorm:"type:varchar(255)" json:"lamp_pole_location"`
|
|
|
+ LampLat float64 `gorm:"type:double(17,14)" json:"lampLat"`
|
|
|
+ LampLng float64 `gorm:"type:double(17,14)" json:"lampLng"`
|
|
|
+ RatedPower float32 `gorm:"type:double(8,2)" json:"ratedPower"`
|
|
|
+ BrandId int `gorm:"type:int" json:"brandId"`
|
|
|
+ ModelId int `gorm:"type:int" json:"modelId"`
|
|
|
+ InstallTime time.Time `gorm:"type:date" json:"installTime"`
|
|
|
+ TenantId string `gorm:"type:varchar(12)" json:"tenantId"`
|
|
|
+ CreateTime time.Time `gorm:"type:datetime" json:"createTime"`
|
|
|
+ CreateUser string `gorm:"type:varchar(60)" json:"createUser"`
|
|
|
+ UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"`
|
|
|
+ UpdateUser string `gorm:"type:varchar(60)" 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"`
|
|
|
+ ControlNO string `gorm:"type:varchar(10)" json:"controlNo"`
|
|
|
+ ChannelNum int `gorm:"type:int" json:"channelNum"`
|
|
|
+ NetworkNum int `gorm:"type:int" json:"networkNum"`
|
|
|
+ ZigbeeId string `gorm:"type:varchar(10)" json:"zigbeeId"`
|
|
|
+ ZigbeeName string `gorm:"type:varchar(10)" json:"zigbeeName"`
|
|
|
+ ZigbeeSn string `gorm:"type:varchar(10)" json:"zigbeeSn"`
|
|
|
+ IsEnable int `gorm:"type:int" json:"isEnable"`
|
|
|
+ IsOnDemand int `gorm:"type:int" json:"isOnDemand"`
|
|
|
+}
|
|
|
+
|
|
|
+func (LightControl) TableName() string {
|
|
|
+ return "t_dev_light_control"
|
|
|
+}
|
|
|
+
|
|
|
+func (c *LightControl) Delete() error {
|
|
|
+ return GDb.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 LightControl) IsExistedBySN() bool {
|
|
|
+ var count = 0
|
|
|
+ _ = GDb.Model(&c).Where("sn = ? and is_deleted = ?",
|
|
|
+ c.SN, c.IsDeleted).Count(&count).Error
|
|
|
+ return count > 0
|
|
|
+}
|
|
|
+
|
|
|
+func (c LightControl) IsExistedByNameAndCode() bool {
|
|
|
+ var devices []LightControl
|
|
|
+ err := GDb.Model(&c).Where("pole_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 *LightControl) Create() error {
|
|
|
+ return GDb.Model(&c).Save(&c).Error
|
|
|
+}
|
|
|
+
|
|
|
+func (c *LightControl) Update() error {
|
|
|
+ return GDb.Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
|
|
|
+}
|
|
|
+
|
|
|
+func (c *LightControl) GetDevice() error {
|
|
|
+ err := GDb.Model(&c).Where(" id = ? ", c.ID).First(&c).Error
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+func (c LightControl) GetDevices(offset, limit int) ([]LightControl, error) {
|
|
|
+ var devices []LightControl
|
|
|
+
|
|
|
+ db := GDb.Model(&c)
|
|
|
+ if c.SN != "" {
|
|
|
+ db = db.Where("name like ? or sn like ?", "%"+c.SN+"%", "%"+c.Name+"%")
|
|
|
+ }
|
|
|
+ if c.GroupId != -1 {
|
|
|
+ db = db.Where("group_id = ?", c.GroupId)
|
|
|
+ }
|
|
|
+
|
|
|
+ err := db.Offset(offset).Limit(limit).Find(&devices).Error
|
|
|
+ return devices, err
|
|
|
+}
|
|
|
+
|
|
|
+func (c LightControl) GetAllDevices() ([]*LightControl, error) {
|
|
|
+ var devices []*LightControl
|
|
|
+ err := GDb.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
|
|
|
+ return devices, err
|
|
|
+}
|