environmentDao.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package dao
  2. import "time"
  3. // EnvironmentData 环境监测器数据
  4. type EnvironmentData struct {
  5. ID int `gorm:"primary_key" json:"id"` //编号
  6. DeviceId int `gorm:"type:int" json:"deviceId"` //设备名称
  7. Pm25 float32 `gorm:"type:float(10, 2)" json:"pm25"` //PM2.5
  8. Pm10 float32 `gorm:"type:float(10, 2)" json:"pm10"` //PM10
  9. WindSpeed float32 `gorm:"type:float(10, 2)" json:"windSpeed"` //风速
  10. WindDirection string `gorm:"type:varchar(10)" json:"windDirection"` //风向
  11. Hpa float32 `gorm:"type:float(10, 2)" json:"hpa"` //气压
  12. Rainfall float32 `gorm:"type:float(10, 2)" json:"rainfall"` //降雨量
  13. Temperature float32 `gorm:"type:float(10, 2)" json:"temperature"` //温度
  14. Humidity float32 `gorm:"type:float(10, 2)" json:"humidity"` //湿度
  15. Noise float32 `gorm:"type:float(10, 2)" json:"noise"` //噪音
  16. Ultraviolet float32 `gorm:"type:float(10, 2)" json:"ultraviolet"` //紫外线
  17. Illuminance float32 `gorm:"type:float(10, 2)" json:"illuminance"` //光照强度
  18. Air float32 `gorm:"type:float(10, 2)" json:"air"` //空气质量指数
  19. IsDay int `gorm:"type:int" json:"isDay"` //是否用于天数据 7点的采集数据用于天数据,0否 1是
  20. PostTime time.Time `gorm:"type:datetime" json:"postTime"` //采集时间
  21. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  22. }
  23. func (EnvironmentData) TableName() string {
  24. return "data_environment"
  25. }
  26. func (c *EnvironmentData) Save() error {
  27. return Db.Debug().Model(&c).Create(&c).Error
  28. }
  29. func (c *EnvironmentData) BatchSave(data []EnvironmentData) error {
  30. return Db.Debug().Model(&c).Create(&data).Error
  31. }
  32. func (c *EnvironmentData) Get() error {
  33. return Db.Debug().Model(&c).Where("device_id = ?", c.DeviceId).Order("create_time desc").First(&c).Error
  34. }
  35. func (c *EnvironmentData) GetByTime(start, end time.Time) ([]EnvironmentData, error) {
  36. var data []EnvironmentData
  37. err := Db.Debug().Model(&c).Where("device_id = ? and is_day = ? and create_time >= ? and create_time <= ?",
  38. c.DeviceId, c.IsDay, start, end).Order("create_time").Find(&data).Error
  39. return data, err
  40. }
  41. // EnvironmentDataDay 环境监测器天数据统计
  42. type EnvironmentDataSummary struct {
  43. DeviceId int `gorm:"type:int" json:"deviceId"` //设备名称
  44. //1=空气质量 2=PM2.5 3=pm10 4=温度 5=湿度 6=风速 7=气压 8=噪音 9=光照强度 10=风向 11=降雨量 12=紫外线
  45. DataType int `gorm:"type:int" json:"dataType"`
  46. ValAvg float32 `gorm:"type:float(10, 2)" json:"valAvg"` //天均值
  47. ValMax float32 `gorm:"type:float(10, 2)" json:"valMax"` //天最大
  48. ValMin float32 `gorm:"type:float(10, 2)" json:"valMin"` //天最小
  49. CreateTime time.Time `gorm:"type:datetime;index" json:"createTime"` //新增时间
  50. }
  51. func (EnvironmentDataSummary) TableName() string {
  52. return "data_environment_summary"
  53. }
  54. func (c *EnvironmentDataSummary) DaySummary(start, end time.Time) error {
  55. err := Db.Debug().Model(&c).Exec(`INSERT INTO data_environment_summary
  56. select device_id,
  57. data_type,
  58. round(avg(value), 2) val_avg,
  59. max(value) val_max,
  60. min(value) val_min,
  61. adddate(current_date, -1) create_time
  62. from (
  63. SELECT device_id, 2 AS data_type, pm25 AS value, create_time
  64. FROM data_environment
  65. UNION ALL
  66. SELECT device_id, 3 AS data_type, pm10 AS value, create_time
  67. FROM data_environment
  68. UNION ALL
  69. SELECT device_id, 4 AS data_type, wind_speed AS value, create_time
  70. FROM data_environment
  71. UNION ALL
  72. SELECT device_id, 5 AS data_type, wind_direction AS value, create_time
  73. FROM data_environment
  74. UNION ALL
  75. SELECT device_id, 6 AS data_type, hpa AS value, create_time
  76. FROM data_environment
  77. UNION ALL
  78. SELECT device_id, 7 AS data_type, rainfall AS value, create_time
  79. FROM data_environment
  80. UNION ALL
  81. SELECT device_id, 8 AS data_type, temperature AS value, create_time
  82. FROM data_environment
  83. UNION ALL
  84. SELECT device_id, 9 AS data_type, humidity AS value, create_time
  85. FROM data_environment
  86. UNION ALL
  87. SELECT device_id, 10 AS data_type, noise AS value, create_time
  88. FROM data_environment
  89. UNION ALL
  90. SELECT device_id, 11 AS data_type, ultraviolet AS value, create_time
  91. FROM data_environment
  92. UNION ALL
  93. SELECT device_id, 12 AS data_type, illuminance AS value, create_time
  94. FROM data_environment
  95. UNION ALL
  96. SELECT device_id, 1 AS data_type, air AS value, create_time
  97. FROM data_environment
  98. ) a
  99. where create_time > ? and create_time <= ?
  100. group by device_id, data_type`, start, end).Error
  101. return err
  102. }
  103. func (c *EnvironmentDataSummary) Get() error {
  104. return Db.Debug().Model(&c).Where("device_id = ?", c.DeviceId).Order("create_time desc").First(&c).Error
  105. }