viewsAllCodeDeviceDao.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package dao
  2. import "iot_manager_service/app/operation/model"
  3. type ViewsAllCodeDevice struct {
  4. TenantId int `json:"tenantId"`
  5. DeviceType int `json:"deviceType"` //类型 1-14
  6. DeviceTypeName string `json:"deviceTypeName"` //类型中文名
  7. MonthTime string `json:"monthTime,omitempty"`
  8. YearTime string `json:"yearTime,omitempty"`
  9. OffLineCountT int `json:"offLine"` //掉线数
  10. AlarmCountT int `json:"countAlarm"` //报警数
  11. DeviceType1CountT int `json:"deviceType1CountT"`
  12. DeviceType2CountT int `json:"deviceType2CountT"`
  13. DeviceType3CountT int `json:"deviceType3CountT"`
  14. DeviceType4CountT int `json:"deviceType4CountT"`
  15. DeviceType5CountT int `json:"deviceType5CountT"`
  16. DeviceType6CountT int `json:"deviceType6CountT"`
  17. DeviceType7CountT int `json:"deviceType7CountT"`
  18. DeviceType8CountT int `json:"deviceType8CountT"`
  19. DeviceType9CountT int `json:"deviceType9CountT"`
  20. DeviceType10CountT int `json:"deviceType10CountT"`
  21. DeviceType11CountT int `json:"deviceType11CountT"`
  22. DeviceType12CountT int `json:"deviceType12CountT"`
  23. DeviceType13CountT int `json:"deviceType13CountT"`
  24. DeviceType14CountT int `json:"deviceType14CountT"`
  25. DeviceType15CountT int `json:"deviceType15CountT"`
  26. CountT int `json:"countT"` //设备数
  27. }
  28. func (ViewsAllCodeDevice) TableName() string {
  29. return "view_all_code" //此处不是真实表,是使用视图查询的
  30. }
  31. // 总设备数
  32. func (v ViewsAllCodeDevice) GetDeviceCount() ([]ViewsAllCodeDevice, error) {
  33. var list []ViewsAllCodeDevice
  34. err := Db.Debug().Model(&v).Raw("SELECT device_type,device_type_name,count(*) as count_t FROM view_all_code where tenant_id=? GROUP BY device_type,device_type_name", v.TenantId).Scan(&list).Error
  35. return list, err
  36. }
  37. // 按月统计
  38. func (v ViewsAllCodeDevice) GetMonthDeviceCount(req model.RequestDeviceCountFilter) ([]ViewsAllCodeDevice, error) {
  39. var list []ViewsAllCodeDevice
  40. sql := `
  41. SELECT
  42. month_time,
  43. sum(count_t) AS count_t,
  44. SUM(CASE device_type WHEN 1 THEN count_t ELSE 0 END) as device_type1_count_t,
  45. SUM(CASE device_type WHEN 2 THEN count_t ELSE 0 END) as device_type2_count_t,
  46. SUM(CASE device_type WHEN 3 THEN count_t ELSE 0 END) as device_type3_count_t,
  47. SUM(CASE device_type WHEN 4 THEN count_t ELSE 0 END) as device_type4_count_t,
  48. SUM(CASE device_type WHEN 5 THEN count_t ELSE 0 END) as device_type5_count_t,
  49. SUM(CASE device_type WHEN 6 THEN count_t ELSE 0 END) as device_type6_count_t,
  50. SUM(CASE device_type WHEN 7 THEN count_t ELSE 0 END) as device_type7_count_t,
  51. SUM(CASE device_type WHEN 8 THEN count_t ELSE 0 END) as device_type8_count_t,
  52. SUM(CASE device_type WHEN 9 THEN count_t ELSE 0 END) as device_type9_count_t,
  53. SUM(CASE device_type WHEN 10 THEN count_t ELSE 0 END) as device_type10_count_t,
  54. SUM(CASE device_type WHEN 11 THEN count_t ELSE 0 END) as device_type11_count_t,
  55. SUM(CASE device_type WHEN 12 THEN count_t ELSE 0 END) as device_type12_count_t,
  56. SUM(CASE device_type WHEN 13 THEN count_t ELSE 0 END) as device_type13_count_t,
  57. SUM(CASE device_type WHEN 14 THEN count_t ELSE 0 END) as device_type14_count_t,
  58. SUM(CASE device_type WHEN 15 THEN count_t ELSE 0 END) as device_type15_count_t
  59. FROM
  60. (
  61. SELECT
  62. device_type,
  63. count(*) AS count_t,
  64. date_format( create_time, '%Y-%m' ) AS month_time
  65. FROM
  66. view_all_code
  67. where tenant_id=? and create_time >= ? and create_time<=?
  68. GROUP BY
  69. device_type,
  70. month_time
  71. ORDER BY
  72. month_time DESC
  73. ) a
  74. GROUP BY
  75. month_time
  76. `
  77. err := Db.Debug().Model(&v).Raw(sql, v.TenantId, req.StartTime, req.EndTime).Scan(&list).Error
  78. return list, err
  79. }
  80. // 按年统计
  81. func (v ViewsAllCodeDevice) GetYearDeviceCount(req model.RequestDeviceCountFilter) ([]ViewsAllCodeDevice, error) {
  82. var list []ViewsAllCodeDevice
  83. sql := `
  84. SELECT
  85. year_time,
  86. sum(count_t) AS count_t,
  87. SUM(CASE device_type WHEN 1 THEN count_t ELSE 0 END) as device_type1_count_t,
  88. SUM(CASE device_type WHEN 2 THEN count_t ELSE 0 END) as device_type2_count_t,
  89. SUM(CASE device_type WHEN 3 THEN count_t ELSE 0 END) as device_type3_count_t,
  90. SUM(CASE device_type WHEN 4 THEN count_t ELSE 0 END) as device_type4_count_t,
  91. SUM(CASE device_type WHEN 5 THEN count_t ELSE 0 END) as device_type5_count_t,
  92. SUM(CASE device_type WHEN 6 THEN count_t ELSE 0 END) as device_type6_count_t,
  93. SUM(CASE device_type WHEN 7 THEN count_t ELSE 0 END) as device_type7_count_t,
  94. SUM(CASE device_type WHEN 8 THEN count_t ELSE 0 END) as device_type8_count_t,
  95. SUM(CASE device_type WHEN 9 THEN count_t ELSE 0 END) as device_type9_count_t,
  96. SUM(CASE device_type WHEN 10 THEN count_t ELSE 0 END) as device_type10_count_t,
  97. SUM(CASE device_type WHEN 11 THEN count_t ELSE 0 END) as device_type11_count_t,
  98. SUM(CASE device_type WHEN 12 THEN count_t ELSE 0 END) as device_type12_count_t,
  99. SUM(CASE device_type WHEN 13 THEN count_t ELSE 0 END) as device_type13_count_t,
  100. SUM(CASE device_type WHEN 14 THEN count_t ELSE 0 END) as device_type14_count_t,
  101. SUM(CASE device_type WHEN 15 THEN count_t ELSE 0 END) as device_type15_count_t
  102. FROM
  103. (
  104. SELECT
  105. device_type,
  106. count(*) AS count_t,
  107. date_format( create_time, '%Y' ) AS year_time
  108. FROM
  109. view_all_code
  110. where tenant_id=?
  111. GROUP BY
  112. device_type,
  113. year_time
  114. ORDER BY
  115. year_time DESC
  116. ) a
  117. GROUP BY
  118. year_time
  119. `
  120. err := Db.Debug().Model(&v).Raw(sql, v.TenantId).Scan(&list).Error
  121. return list, err
  122. }
  123. func (v ViewsAllCodeDevice) GetDayDeviceCount(req model.RequestDeviceCountFilter) ([]ViewsAllCodeDevice, error) {
  124. var list []ViewsAllCodeDevice
  125. sql := `
  126. SELECT
  127. month_time,
  128. sum(count_t) AS count_t,
  129. SUM(CASE device_type WHEN 1 THEN count_t ELSE 0 END) as device_type1_count_t,
  130. SUM(CASE device_type WHEN 2 THEN count_t ELSE 0 END) as device_type2_count_t,
  131. SUM(CASE device_type WHEN 3 THEN count_t ELSE 0 END) as device_type3_count_t,
  132. SUM(CASE device_type WHEN 4 THEN count_t ELSE 0 END) as device_type4_count_t,
  133. SUM(CASE device_type WHEN 5 THEN count_t ELSE 0 END) as device_type5_count_t,
  134. SUM(CASE device_type WHEN 6 THEN count_t ELSE 0 END) as device_type6_count_t,
  135. SUM(CASE device_type WHEN 7 THEN count_t ELSE 0 END) as device_type7_count_t,
  136. SUM(CASE device_type WHEN 8 THEN count_t ELSE 0 END) as device_type8_count_t,
  137. SUM(CASE device_type WHEN 9 THEN count_t ELSE 0 END) as device_type9_count_t,
  138. SUM(CASE device_type WHEN 10 THEN count_t ELSE 0 END) as device_type10_count_t,
  139. SUM(CASE device_type WHEN 11 THEN count_t ELSE 0 END) as device_type11_count_t,
  140. SUM(CASE device_type WHEN 12 THEN count_t ELSE 0 END) as device_type12_count_t,
  141. SUM(CASE device_type WHEN 13 THEN count_t ELSE 0 END) as device_type13_count_t,
  142. SUM(CASE device_type WHEN 14 THEN count_t ELSE 0 END) as device_type14_count_t,
  143. SUM(CASE device_type WHEN 15 THEN count_t ELSE 0 END) as device_type15_count_t
  144. FROM
  145. (
  146. SELECT
  147. device_type,
  148. count(*) AS count_t,
  149. date_format( create_time, '%Y-%m-%d' ) AS month_time
  150. FROM
  151. view_all_code
  152. where tenant_id=? and create_time >= ? and create_time<=?
  153. GROUP BY
  154. device_type,
  155. month_time
  156. ORDER BY
  157. month_time DESC
  158. ) a
  159. GROUP BY
  160. month_time
  161. `
  162. err := Db.Debug().Model(&v).Raw(sql, v.TenantId, req.StartTime, req.EndTime).Scan(&list).Error
  163. return list, err
  164. }