environmentDao.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. ID int `gorm:"primary_key" json:"id"` //编号
  44. DeviceId int `gorm:"type:int" json:"deviceId"` //设备名称
  45. //1=空气质量 2=PM2.5 3=pm10 4=温度 5=湿度 6=风速 7=气压 8=噪音 9=光照强度 10=风向 11=降雨量 12=紫外线
  46. DataType int `gorm:"type:int" json:"dataType"`
  47. ValAvg float32 `gorm:"type:float(10, 2)" json:"valAvg"` //天均值
  48. ValMax float32 `gorm:"type:float(10, 2)" json:"valMax"` //天最大
  49. ValMin float32 `gorm:"type:float(10, 2)" json:"valMin"` //天最小
  50. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  51. }
  52. func (EnvironmentDataSummary) TableName() string {
  53. return "data_environment_summary"
  54. }
  55. func (c *EnvironmentDataSummary) DaySummary(start, end time.Time) error {
  56. err := Db.Debug().Model(&c).Exec(`INSERT INTO data_environment_summary
  57. select device_id,
  58. data_type,
  59. round(avg(value), 2) as avg_val,
  60. max(value) as max_val,
  61. min(value) as min_val,
  62. adddate(current_date, -1) create_time
  63. from (
  64. SELECT device_id, 2 AS data_type, pm25 AS value, create_time
  65. FROM data_environment
  66. UNION ALL
  67. SELECT device_id, 3 AS data_type, pm10 AS value, create_time
  68. FROM data_environment
  69. UNION ALL
  70. SELECT device_id, 4 AS data_type, wind_speed AS value, create_time
  71. FROM data_environment
  72. UNION ALL
  73. SELECT device_id, 5 AS data_type, wind_direction AS value, create_time
  74. FROM data_environment
  75. UNION ALL
  76. SELECT device_id, 6 AS data_type, hpa AS value, create_time
  77. FROM data_environment
  78. UNION ALL
  79. SELECT device_id, 7 AS data_type, rainfall AS value, create_time
  80. FROM data_environment
  81. UNION ALL
  82. SELECT device_id, 8 AS data_type, temperature AS value, create_time
  83. FROM data_environment
  84. UNION ALL
  85. SELECT device_id, 9 AS data_type, humidity AS value, create_time
  86. FROM data_environment
  87. UNION ALL
  88. SELECT device_id, 10 AS data_type, noise AS value, create_time
  89. FROM data_environment
  90. UNION ALL
  91. SELECT device_id, 11 AS data_type, ultraviolet AS value, create_time
  92. FROM data_environment
  93. UNION ALL
  94. SELECT device_id, 12 AS data_type, illuminance AS value, create_time
  95. FROM data_environment
  96. UNION ALL
  97. SELECT device_id, 1 AS data_type, air AS value, create_time
  98. FROM data_environment
  99. ) a
  100. where create_time > ? and create_time <= ?
  101. group by device_id, data_type`, start, end).Error
  102. return err
  103. }
  104. func (c *EnvironmentDataSummary) Get() error {
  105. return Db.Debug().Model(&c).Where("device_id = ?", c.DeviceId).Order("create_time desc").First(&c).Error
  106. }