bigScreen.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package dao
  2. import (
  3. "iot_manager_service/util/logger"
  4. )
  5. type Big interface {
  6. ListDevices() ([]Device, int64, error)
  7. CountFault() (int64, error)
  8. }
  9. func getLinkNum(sn string) int64 {
  10. var i int64
  11. //SELECT total FROM device_wisdom_gateway a LEFT JOIN device_gateway_relation b ON a.id = b.id
  12. //WHERE a.lamp_pole_sn='ZHDG4306268O408R' ORDER BY total DESC LIMIT 1;
  13. Db.Select("total").Table("device_wisdom_gateway a").
  14. Joins("LEFT JOIN device_gateway_relation b ON a.id = b.id").
  15. Where("a.lamp_pole_sn = ?", sn).
  16. Order("total DESC").Limit(1).Find(&i)
  17. return i
  18. }
  19. func GetEnergy(start, end string) []Energy {
  20. var rsp = make([]Energy, 0, 12)
  21. //select SUBSTR(date FROM 1 FOR 7) AS date1,SUM(difference) AS difference
  22. //FROM `t_device_energy_day_data` t WHERE date BETWEEN "2023-04-01" AND "2023-06-01" GROUP BY date1;
  23. rows, err := Db.Select("SUBSTR(date FROM 1 FOR 7) AS dates,SUM(difference) AS difference").
  24. Table("`t_device_energy_day_data`").Where("date BETWEEN ? AND ?", start, end).Group("dates").Rows()
  25. defer rows.Close()
  26. if err != nil {
  27. logger.Logger.Errorf("GetEnergy error: %v", err)
  28. }
  29. for rows.Next() {
  30. var e Energy
  31. rows.Scan(&e.Date, &e.Difference)
  32. rsp = append(rsp, e)
  33. }
  34. return rsp
  35. }
  36. // GetLightRate
  37. // SELECT DATE_FORMAT(NOW(),'%Y-%m') AS month, AVG(rate) AS rate FROM `t_lamp_lighting_rate_day_data` WHERE SUBSTR(date FROM 1 FOR 7) = DATE_FORMAT(NOW(),'%Y-%m') AND tenant = '000000';
  38. // SELECT QUARTER(NOW()) AS quarter, AVG(rate) AS rate FROM `t_lamp_lighting_rate_day_data` WHERE QUARTER(date) = QUARTER(NOW()) AND tenant = '000000';
  39. // SELECT YEAR(NOW()) AS year, AVG(rate) AS rate FROM `t_lamp_lighting_rate_day_data` WHERE YEAR(date) = YEAR(NOW()) AND tenant = '000000';
  40. func GetLightRate(tenant string) []interface{} {
  41. var rsp = make([]interface{}, 0, 4)
  42. var lrm LightRateM
  43. var lrq LightRateQ
  44. var lry LightRateY
  45. Db.Select("DATE_FORMAT(NOW(),'%Y-%m') AS month,ROUND(AVG(rate),2) AS rate").Table("`t_lamp_lighting_rate_day_data`").
  46. Where("SUBSTR(date FROM 1 FOR 7) = DATE_FORMAT(NOW(),'%Y-%m') And tenant = ?", tenant).Find(&lrm)
  47. Db.Select("QUARTER(NOW()) AS quarter, ROUND(AVG(rate),2) AS rate").Table("`t_lamp_lighting_rate_day_data`").
  48. Where("quarter(date) = quarter(NOW()) AND tenant = ?", tenant).Find(&lrq)
  49. Db.Select("YEAR(NOW()) AS year, ROUND(AVG(rate),2) AS rate").Table("`t_lamp_lighting_rate_day_data`").
  50. Where("YEAR(date) = YEAR(NOW()) AND tenant = ?", tenant).Find(&lry)
  51. //lr[0].Month = ""
  52. rsp = append(rsp, []interface{}{})
  53. rsp = append(rsp, lrm)
  54. rsp = append(rsp, lrq)
  55. rsp = append(rsp, lry)
  56. return rsp
  57. }
  58. // GetAlarmInfo 1待处理2忽略3已处理4其他
  59. // #待处理
  60. // SELECT COUNT(id) FROM `warn_platform_alarm` WHERE arm_handle = 1;
  61. // SELECT arm_content,arm_device_type_name,dict_value AS level_name,arm_time
  62. // FROM `warn_platform_alarm` w JOIN `dict` d
  63. // ON w.arm_level = d.dict_key
  64. // WHERE w.arm_handle = 1 AND d.`code` = 'arm_level_type'
  65. // LIMIT 10;
  66. // #本月已处理
  67. // SELECT COUNT(id) FROM `warn_platform_alarm` WHERE SUBSTR(arm_end_time FROM 1 FOR 7)=SUBSTR(NOW() FROM 1 FOR 7) AND arm_handle = 3;
  68. // #本年已处理
  69. // SELECT COUNT(id) FROM `warn_platform_alarm` WHERE YEAR(arm_end_time)=YEAR(NOW()) AND arm_handle = 3;
  70. func GetAlarmInfo() AlarmList {
  71. var al AlarmList
  72. Db.Select("COUNT(id)").Table("`warn_platform_alarm`").
  73. Where("arm_handle = 1").Find(&al.Backlog)
  74. Db.Select("COUNT(id)").Table("`warn_platform_alarm`").
  75. Where("SUBSTR(arm_end_time FROM 1 FOR 7)=SUBSTR(NOW() FROM 1 FOR 7) AND arm_handle = 3").Find(&al.LastMonthAlarm)
  76. Db.Select("COUNT(id)").Table("`warn_platform_alarm`").
  77. Where("YEAR(arm_end_time)=YEAR(NOW()) AND arm_handle = 3").Find(&al.LastYearAlarm)
  78. Db.Select("arm_content,arm_device_type_name,dict_value AS arm_level_name,arm_time AS create_time").
  79. Table("`warn_platform_alarm` AS w JOIN `dict` AS d ON w.arm_level = d.dict_key").
  80. Where("w.arm_handle = 1 AND d.`code` = 'arm_level_type'").Order("create_time DESC").Limit(7).Find(&al.List)
  81. return al
  82. }
  83. type OverView struct {
  84. Id int64 `gorm:"primary_key" json:"id"`
  85. TenantId string `json:"tenantId"`
  86. PadScreen string `json:"padScreen"`
  87. Site string `json:"site"`
  88. SiteValue int64 `json:"siteValue"`
  89. }
  90. func (o *OverView) TableName() string {
  91. return "big_screen_overview"
  92. }
  93. type OverViewInfo struct {
  94. Id int64 `gorm:"primary_key" json:"id"`
  95. SiteValue int64 `json:"siteValue"`
  96. }
  97. func (o *OverView) Query(tenantId, padScreen string) []OverViewInfo {
  98. var rsp []OverViewInfo
  99. Db.Select("id, site_value").Table(o.TableName()).Where("tenant_id = ? AND pad_screen = ?", tenantId, padScreen).Find(&rsp)
  100. return rsp
  101. }
  102. func (o *OverView) Submit(data OverView) {
  103. Db.Model(&o).Select("pad_screen", "site_value").Where("id=?", data.Id).Updates(&data)
  104. }
  105. func GetEnvData(id string) Environment {
  106. var env Environment
  107. //"lampPoleLocation"
  108. Db.Select("air, create_date, temperature, humidity, pm25, pm10, noise, hpa, wind_direction, wind_speed").
  109. Table("data_environment").Where("device_id=?", id).
  110. Order("create_date DESC").Limit(1).Find(&env)
  111. return env
  112. }
  113. // GetLEDProgramList
  114. // SELECT m.id,m.name,m.duration,d.dict_value, end_time
  115. // FROM `media_publish_libraries` m JOIN `dict` d
  116. // ON dict_key = m.resolution
  117. // WHERE m.tenant_id = '000000' and m.sys_type=0 and m.is_deleted = 0 AND d.code = 'resolution_type'
  118. // ORDER BY id desc LIMIT 10
  119. func GetLEDProgramList(tenantId string) []InfoBoardProgram {
  120. var list []InfoBoardProgram
  121. Db.Select("m.id,m.name,m.duration,d.dict_value AS resolution_name, end_time ,(LENGTH(info_id) - LENGTH(REPLACE(info_id, ',', '')) + 1) AS count").
  122. Table("`media_publish_libraries` m JOIN `dict` d ON dict_key = m.resolution").
  123. Where("d.code='resolution_type' AND m.sys_type=0 AND m.is_deleted=0 AND m.tenant_id=?", tenantId).
  124. Order("id DESC").Limit(10).Find(&list)
  125. return list
  126. }
  127. func GetProgramUrl(id string) Program {
  128. var rsp Program
  129. //SELECT name FROM `media_publish_libraries` WHERE id = 45
  130. t1 := Db.Select("name").Table("media_publish_libraries").Where("id=(?)", id)
  131. //SELECT id FROM `media_program` WHERE name =?
  132. t2 := Db.Select("id").Table("media_program").Where("name=(?)", t1)
  133. //SELECT library_id FROM media_program_relation WHERE program_id = ?
  134. t3 := Db.Select("library_id").Table("media_program_relation").Where("program_id=(?)", t2)
  135. //SELECT material_address FROM `media_library` WHERE id =?
  136. Db.Select("material_address AS send_url").Table("media_library").Where("id =(?)", t3).Find(&rsp)
  137. return rsp
  138. }
  139. func GetBridgeSensors(tenantId string) []Sensor {
  140. var rsp []Sensor
  141. //SELECT id,name FROM `device_bridge_sensor` WHERE tenant_id = '000000' AND is_deleted = 0
  142. Db.Select("id,name").Table("device_bridge_sensor").Where("tenant_id = ? AND is_deleted = 0", tenantId).Find(&rsp)
  143. return rsp
  144. }