lampstrategy.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package models
  2. import "time"
  3. //路灯控制器,数据来自web
  4. type Lampstrategy struct {
  5. ID string `gorm:"type:varchar(32);primary_key"` //策略编码
  6. Name string `gorm:"type:varchar(64)"` //策略名称
  7. CtlType int `gorm:"type:int"` //单灯控制器类型,nb-iot:1,485:2,zigbee:3
  8. TimeInfo string `gorm:"type:text"` //LampTime数组 时间及亮度内容数组,最多4个元素,最少1个元素
  9. Sunset int `gorm:"type:int"` //是否启用日出日落时间,1启用,0禁用
  10. Start time.Time `gorm:"type:datetime"` //时间格式,类似2020-07-20 06:06:06
  11. End time.Time `gorm:"type:datetime"` //时间格式,类似2020-09-20 06:06:06
  12. Year int `gorm:"type:int"` //是否全年,1全年,0非全年
  13. Auto int `gorm:"type:int"` //1,自动延续,0非自动延续
  14. Tenant string `gorm:"type:varchar(8)"` //租户ID
  15. Longitude float64 `gorm:"type:decimal(10,7)"` //东正西负。双精度浮点数,整数最多3字符,小数最多6字符
  16. Latitude float64 `gorm:"type:decimal(10,7)"` //北正南负,双精度浮点数,整数最多2字符,小数最多6字符
  17. LcModel
  18. }
  19. func (Lampstrategy) TableName() string {
  20. //从视图中查询
  21. return "t_lamp_strategy"
  22. }
  23. func (o Lampstrategy) IsExistedByCode() (bool, error) {
  24. var count int = 0
  25. err := G_db.Model(&o).Where(" id = ? ", o.ID).Count(&count).Error
  26. return count > 0, err
  27. }
  28. //设备创建和更新
  29. func (o Lampstrategy) SaveFromWeb() error {
  30. has, err := o.IsExistedByCode()
  31. if err != nil {
  32. return err
  33. }
  34. if has {
  35. return G_db.Model(&o).Updates(map[string]interface{}{
  36. "name": o.Name, "ctl_type": o.CtlType, "time_info": o.TimeInfo, "sunset": o.Sunset, "start": o.Start,
  37. "end": o.End, "year": o.Year, "auto": o.Auto, "tenant": o.Tenant,
  38. "longitude": o.Longitude, "latitude": o.Latitude}).Error
  39. } else {
  40. //插入
  41. return G_db.Create(&o).Error
  42. }
  43. }
  44. type LampStrategy struct {
  45. Ontime string `json:"ontime"` //开始时间,24小时制,格式{小时}:{分钟}:{秒}类似13:06:00,为13点6分
  46. Offtime string `json:"offtime"` //结束时间,24小时制,格式同ontime
  47. Brightness int `json:"brightness"` //亮度,1到100
  48. }