lampswitch.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package models
  2. import (
  3. "bytes"
  4. "fmt"
  5. "time"
  6. )
  7. //手动控制,开灯/关灯记录表
  8. type LampSwitchRecord struct {
  9. ID uint `gorm:"primary_key"` //主键,自增长
  10. DID string `gorm:"type:varchar(32)" json:"did"` //设备编码
  11. SwitchOn uint8 `gorm:"type:tinyint(1)" json:"switchon"` //类型,1:开灯,0:关灯
  12. Duration uint `gorm:"type:int" json:"duration"` //开灯或关灯时长,单位秒
  13. Brightness uint8 `gorm:"type:smallint" json:"brightness"` //亮度,关灯时亮度为0
  14. TStart time.Time `gorm:"type:datetime;not null" json:"start"` //开始时间
  15. TEnd time.Time `gorm:"type:datetime;not null" json:"end"` //结束时间
  16. CreatedAt time.Time `gorm:"type:datetime;not null"` //结束时间
  17. }
  18. func (LampSwitchRecord) TableName() string {
  19. return "device_lamp_switch_record"
  20. }
  21. func MultiInsertLampSwitchRecord(datas []LampSwitchRecord) error {
  22. if len(datas) == 0 {
  23. return nil
  24. }
  25. var buffer bytes.Buffer
  26. sql := "insert into t_device_lamp_switch_record(d_id,switch_on,duration,brightness,t_start,t_end,created_at) values"
  27. if _, err := buffer.WriteString(sql); err != nil {
  28. return err
  29. }
  30. for i, e := range datas {
  31. if i == len(datas)-1 {
  32. buffer.WriteString(fmt.Sprintf("('%s',%d,%d,%d,'%s','%s','%s');", e.DID, e.SwitchOn, e.Duration, e.Brightness,
  33. e.TStart.Format("2006-01-02 15:04:05"), e.TEnd.Format("2006-01-02 15:04:05"), e.CreatedAt.Format("2006-01-02 15:04:05")))
  34. } else {
  35. buffer.WriteString(fmt.Sprintf("('%s',%d,%d,%d,'%s','%s','%s'),", e.DID, e.SwitchOn, e.Duration, e.Brightness,
  36. e.TStart.Format("2006-01-02 15:04:05"), e.TEnd.Format("2006-01-02 15:04:05"), e.CreatedAt.Format("2006-01-02 15:04:05")))
  37. }
  38. }
  39. return G_db.Exec(buffer.String()).Error
  40. }