environmentDao.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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:"temp"` //温度
  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. AirName string `gorm:"varchar(100)" json:"airName"` //空气质量指数
  20. IsDay int `gorm:"type:int" json:"isDay"` //是否用于天数据 7点的采集数据用于天数据,0否 1是
  21. TenantId string `gorm:"type:varchar(12)" json:"tenantId"`
  22. PostTime time.Time `gorm:"type:datetime" json:"postTime"` //采集时间
  23. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  24. CreateDate string `gorm:"type:varchar(50)" json:"createDate"` //显示时间
  25. }
  26. type EnvironmentScopeData struct {
  27. Time string `json:"time"`
  28. Val string `json:"val"`
  29. Maxval string `json:"maxval"`
  30. Minval string `json:"minval"`
  31. ModelType int `json:"modelType"`
  32. }
  33. func (EnvironmentData) TableName() string {
  34. return "data_environment"
  35. }
  36. func (c *EnvironmentData) Save() error {
  37. return Db.Debug().Model(&c).Create(&c).Error
  38. }
  39. func (c *EnvironmentData) BatchSave(data []EnvironmentData) error {
  40. return Db.Debug().Model(&c).Create(&data).Error
  41. }
  42. func (c *EnvironmentData) Get() error {
  43. return Db.Debug().Model(&c).Where("device_id = ?", c.DeviceId).Order("post_time desc").First(&c).Error
  44. }
  45. func (c *EnvironmentData) GetByTime(start, end time.Time) ([]EnvironmentData, error) {
  46. var data []EnvironmentData
  47. err := Db.Debug().Model(&c).Where("device_id = ? and is_day = ? and create_time >= ? and create_time <= ?",
  48. c.DeviceId, c.IsDay, start, end).Order("create_time").Find(&data).Error
  49. return data, err
  50. }
  51. func (c *EnvironmentData) GetEnvironmentScope(queryType int, s string) []EnvironmentScopeData {
  52. var environmentScope []EnvironmentScopeData
  53. var field string
  54. switch queryType {
  55. case 1:
  56. field = "air"
  57. case 2:
  58. field = "pm25"
  59. case 3:
  60. field = "pm10"
  61. case 4:
  62. field = "temperature"
  63. case 5:
  64. field = "humidity"
  65. case 6:
  66. field = "wind_speed"
  67. case 7:
  68. field = "hpa"
  69. case 8:
  70. field = "noise"
  71. }
  72. dateStr := "%Y-%m-%d %H:00:00"
  73. postTime := time.Now().Format("2006-01-02")
  74. if s == "history" {
  75. postTime = time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  76. }
  77. if c.IsDay != 1 {
  78. dateStr = "%Y-%m-%d 00:00:00"
  79. postTime = time.Now().Format("2006-01-02")
  80. if s == "history" {
  81. postTime = time.Now().AddDate(0, -1, 0).Format("2006-01-02")
  82. }
  83. }
  84. sql := `
  85. SELECT
  86. DATE_FORMAT( ( post_time ), '` + dateStr + `' ) AS time,
  87. ROUND(min(` + field + `), 2) AS minval,
  88. ROUND(max(` + field + `),2) as maxval,
  89. ROUND(avg(` + field + `),2)as val
  90. FROM
  91. data_environment
  92. WHERE
  93. device_id = ? and
  94. post_time LIKE "` + postTime + `%"
  95. GROUP BY DATE_FORMAT( ( post_time ), '` + dateStr + `' )
  96. `
  97. Db.Debug().Raw(sql, c.DeviceId).Find(&environmentScope)
  98. return environmentScope
  99. }
  100. // EnvironmentDataDay 环境监测器天数据统计
  101. type EnvironmentDataSummary struct {
  102. DeviceId int `gorm:"type:int" json:"deviceId"` //设备名称
  103. //1=空气质量 2=PM2.5 3=pm10 4=温度 5=湿度 6=风速 7=气压 8=噪音 9=光照强度 10=风向 11=降雨量 12=紫外线
  104. DataType int `gorm:"type:int" json:"dataType"`
  105. ValAvg float32 `gorm:"type:float(10, 2)" json:"valAvg"` //天均值
  106. ValMax float32 `gorm:"type:float(10, 2)" json:"valMax"` //天最大
  107. ValMin float32 `gorm:"type:float(10, 2)" json:"valMin"` //天最小
  108. CreateTime time.Time `gorm:"type:datetime;index" json:"createTime"` //新增时间
  109. }
  110. func (EnvironmentDataSummary) TableName() string {
  111. return "data_environment_summary"
  112. }
  113. func (c *EnvironmentDataSummary) DaySummary(start, end time.Time) error {
  114. err := Db.Debug().Model(&c).Exec(`INSERT INTO data_environment_summary
  115. select device_id,
  116. data_type,
  117. round(avg(value), 2) val_avg,
  118. max(if(value='',0,value)) val_max,
  119. min(if(value='',0,value)) val_min,
  120. adddate(current_date, -1) create_time
  121. from (
  122. SELECT device_id, 2 AS data_type, pm25 AS value, create_time
  123. FROM data_environment
  124. UNION ALL
  125. SELECT device_id, 3 AS data_type, pm10 AS value, create_time
  126. FROM data_environment
  127. UNION ALL
  128. SELECT device_id, 4 AS data_type, wind_speed AS value, create_time
  129. FROM data_environment
  130. UNION ALL
  131. SELECT device_id, 5 AS data_type, wind_direction AS value, create_time
  132. FROM data_environment
  133. UNION ALL
  134. SELECT device_id, 6 AS data_type, hpa AS value, create_time
  135. FROM data_environment
  136. UNION ALL
  137. SELECT device_id, 7 AS data_type, rainfall AS value, create_time
  138. FROM data_environment
  139. UNION ALL
  140. SELECT device_id, 8 AS data_type, temperature AS value, create_time
  141. FROM data_environment
  142. UNION ALL
  143. SELECT device_id, 9 AS data_type, humidity AS value, create_time
  144. FROM data_environment
  145. UNION ALL
  146. SELECT device_id, 10 AS data_type, noise AS value, create_time
  147. FROM data_environment
  148. UNION ALL
  149. SELECT device_id, 11 AS data_type, ultraviolet AS value, create_time
  150. FROM data_environment
  151. UNION ALL
  152. SELECT device_id, 12 AS data_type, illuminance AS value, create_time
  153. FROM data_environment
  154. UNION ALL
  155. SELECT device_id, 1 AS data_type, air AS value, create_time
  156. FROM data_environment
  157. ) a
  158. where data_type!=5 and create_time > ? and create_time <= ?
  159. group by device_id, data_type`, start, end).Error
  160. return err
  161. }
  162. func (c *EnvironmentDataSummary) Get() error {
  163. return Db.Debug().Model(&c).Where("device_id = ?", c.DeviceId).Order("create_time desc").First(&c).Error
  164. }