StatReportController.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package controllers
  2. import (
  3. "rtzh_elec_temperature/rtelec_app_public_lib/service"
  4. "strings"
  5. )
  6. //数据统计报表相关服务
  7. type StatReportController struct {
  8. BaseController
  9. }
  10. // 多测点对比数据查询 godoc
  11. // @Summary 多测点对比数据查询
  12. // @Description 多测点对比数据查询
  13. // @Tags api
  14. // @Accept x-www-form-urlencoded
  15. // @Produce json
  16. // @Param mplist query string true "对比的测点列表。测点格式为:设备ID.测点属性名(如123.tem1)。多个测点之间使用逗号分隔。"
  17. // @Param startdate query int true "数据查询开始日期"
  18. // @Param enddate query int true "数据查询结束日期"
  19. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  20. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  21. // @Failure 500 status 服务器|后台发生错误
  22. // @router /mutil_mp/comp [get]
  23. func (c *StatReportController) MutilMpCompData() {
  24. obj := new(service.StatReportService)
  25. mplist := c.GetString("mplist")
  26. if mplist == "" {
  27. c.Data["json"] = c.ApiError("对比的测点不能为空")
  28. c.ServeJSON()
  29. return
  30. }
  31. mps := map[string][]string{}
  32. tmpAry := strings.Split(mplist, ",")
  33. for _, v := range tmpAry {
  34. v1 := strings.Split(v, ".")
  35. if len(v1) != 2 {
  36. c.Data["json"] = c.ApiError("对比的测点格式不正确。参考:123.tem1")
  37. c.ServeJSON()
  38. return
  39. }
  40. if t1, h := mps[v1[0]]; h {
  41. t1 = append(t1, v1[1])
  42. mps[v1[0]] = t1
  43. } else {
  44. mps[v1[0]] = []string{v1[1]}
  45. }
  46. }
  47. //logger.Logger.Debug(fmt.Sprintf("对比分析的测点列表:%+v", mps))
  48. obj.UserInfo = c.GetCurrentUserInfo_rt()
  49. startdate := c.GetString("startdate")
  50. enddate := c.GetString("enddate")
  51. r, err := obj.MpComp(startdate, enddate, mps)
  52. if err != nil {
  53. c.Data["json"] = c.ApiError(err.Error())
  54. } else {
  55. c.Data["json"] = c.ApiOK(r)
  56. }
  57. c.ServeJSON()
  58. }
  59. // 多测点对比Echarts Line数据查询 godoc
  60. // @Summary 多测点对比Echarts Line数据查询。按Echarts Line要格式要求返回数据。
  61. // @Description 多测点对比Echarts Line数据查询按Echarts Line要格式要求返回数据。
  62. // @Tags api
  63. // @Accept x-www-form-urlencoded
  64. // @Produce json
  65. // @Param mplist query string true "对比的测点列表。测点格式为:设备ID.测点属性名(如123.tem1)。多个测点之间使用逗号分隔。"
  66. // @Param startdate query int true "数据查询开始日期"
  67. // @Param enddate query int true "数据查询结束日期"
  68. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  69. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  70. // @Failure 500 status 服务器|后台发生错误
  71. // @router /mutil_mp/echarts_data [get]
  72. func (c *StatReportController) MutilMpCompEchartsLineData() {
  73. obj := new(service.StatReportService)
  74. mplist := c.GetString("mplist")
  75. if mplist == "" {
  76. c.Data["json"] = c.ApiError("对比的测点不能为空")
  77. c.ServeJSON()
  78. return
  79. }
  80. mps := map[string][]string{}
  81. tmpAry := strings.Split(mplist, ",")
  82. for _, v := range tmpAry {
  83. v1 := strings.Split(v, ".")
  84. if len(v1) != 2 {
  85. c.Data["json"] = c.ApiError("对比的测点格式不正确。参考:123.tem1")
  86. c.ServeJSON()
  87. return
  88. }
  89. if t1, h := mps[v1[0]]; h {
  90. t1 = append(t1, v1[1])
  91. mps[v1[0]] = t1
  92. } else {
  93. mps[v1[0]] = []string{v1[1]}
  94. }
  95. }
  96. obj.UserInfo = c.GetCurrentUserInfo_rt()
  97. startdate := c.GetString("startdate")
  98. enddate := c.GetString("enddate")
  99. r, err := obj.MpCompEchartsLine(startdate, enddate, mps)
  100. if err != nil {
  101. c.Data["json"] = c.ApiError(err.Error())
  102. } else {
  103. c.Data["json"] = c.ApiOK(r)
  104. }
  105. c.ServeJSON()
  106. }
  107. // 获取报表数据 godoc
  108. // @Summary 获取报表数据
  109. // @Description 获取报表数据
  110. // @Tags api
  111. // @Accept x-www-form-urlencoded
  112. // @Produce json
  113. // @Param deviceid query int true "设备ID"
  114. // @Param reportid query int true "查询条件:报表ID"
  115. // @Param reporttype query int true "查询条件:报表类型。支持以下:1 年报 2 月报 3 周报 4 日报"
  116. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  117. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  118. // @Failure 500 status 服务器|后台发生错误
  119. // @router /data/list [get]
  120. func (c *StatReportController) GetReportData() {
  121. obj := new(service.StatReportService)
  122. deviceid, _ := c.GetInt32("deviceid")
  123. if deviceid == 0 {
  124. c.Data["json"] = c.ApiError("设备ID不能为空")
  125. c.ServeJSON()
  126. return
  127. }
  128. obj.UserInfo = c.GetCurrentUserInfo_rt()
  129. rtid, _ := c.GetInt64("reportid")
  130. typeid := c.GetString("reporttype")
  131. r, err := obj.DataQuery(deviceid, rtid, typeid)
  132. if err != nil {
  133. c.Data["json"] = c.ApiError(err.Error())
  134. } else {
  135. c.Data["json"] = c.ApiOK(r)
  136. }
  137. c.ServeJSON()
  138. }
  139. // 获取年报表列表 godoc
  140. // @Summary 获取年报表列表
  141. // @Description 获取年报表列表。不指定年份时,获取所有的年报表。
  142. // @Tags api
  143. // @Accept x-www-form-urlencoded
  144. // @Produce json
  145. // @Param deviceid query int true "设备ID"
  146. // @Param year query int false "查询条件:年份"
  147. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  148. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  149. // @Failure 500 status 服务器|后台发生错误
  150. // @router /year/list [get]
  151. func (c *StatReportController) GetYearReportList() {
  152. obj := new(service.StatReportService)
  153. deviceid, _ := c.GetInt32("deviceid")
  154. if deviceid == 0 {
  155. c.Data["json"] = c.ApiError("设备ID不能为空")
  156. c.ServeJSON()
  157. return
  158. }
  159. obj.UserInfo = c.GetCurrentUserInfo_rt()
  160. year := c.GetString("year")
  161. r, n, err := obj.QueryYearReportList(deviceid, year)
  162. if err != nil {
  163. c.Data["json"] = c.ApiError(err.Error())
  164. } else {
  165. c.Data["json"] = c.ApiOK(map[string]interface{}{"total": n, "list": r})
  166. }
  167. c.ServeJSON()
  168. }
  169. // 获取月报表列表 godoc
  170. // @Summary 获取月报表列表
  171. // @Description 获取月报表列表。不指定月份时,获取指定年内所有的月报表。
  172. // @Tags api
  173. // @Accept x-www-form-urlencoded
  174. // @Produce json
  175. // @Param deviceid query int true "设备ID"
  176. // @Param year query int true "查询条件:年份"
  177. // @Param month query int false "查询条件:月份"
  178. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  179. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  180. // @Failure 500 status 服务器|后台发生错误
  181. // @router /month/list [get]
  182. func (c *StatReportController) GetMonthReportList() {
  183. obj := new(service.StatReportService)
  184. deviceid, _ := c.GetInt32("deviceid")
  185. if deviceid == 0 {
  186. c.Data["json"] = c.ApiError("设备ID不能为空")
  187. c.ServeJSON()
  188. return
  189. }
  190. obj.UserInfo = c.GetCurrentUserInfo_rt()
  191. year := c.GetString("year")
  192. if year == "" {
  193. c.Data["json"] = c.ApiError("年份不能为空")
  194. c.ServeJSON()
  195. return
  196. }
  197. month := c.GetString("month")
  198. r, n, err := obj.QueryMonthReportList(deviceid, year, month)
  199. if err != nil {
  200. c.Data["json"] = c.ApiError(err.Error())
  201. } else {
  202. c.Data["json"] = c.ApiOK(map[string]interface{}{"total": n, "list": r})
  203. }
  204. c.ServeJSON()
  205. }
  206. // 获取周报表列表 godoc
  207. // @Summary 获取周报表列表
  208. // @Description 获取周报表列表。不指定周时,获取指定年内所有的周报表。
  209. // @Tags api
  210. // @Accept x-www-form-urlencoded
  211. // @Produce json
  212. // @Param deviceid query int true "设备ID"
  213. // @Param year query int true "查询条件:年份"
  214. // @Param week query int false "查询条件:周"
  215. // @Param pageindex query int false "查询条件:当前页码。默认为1"
  216. // @Param pagesize query int false "查询条件:每页记录数。默认为20"
  217. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  218. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  219. // @Failure 500 status 服务器|后台发生错误
  220. // @router /week/list [get]
  221. func (c *StatReportController) GetWeekReportList() {
  222. obj := new(service.StatReportService)
  223. deviceid, _ := c.GetInt32("deviceid")
  224. if deviceid == 0 {
  225. c.Data["json"] = c.ApiError("设备ID不能为空")
  226. c.ServeJSON()
  227. return
  228. }
  229. obj.UserInfo = c.GetCurrentUserInfo_rt()
  230. year := c.GetString("year")
  231. if year == "" {
  232. c.Data["json"] = c.ApiError("年份不能为空")
  233. c.ServeJSON()
  234. return
  235. }
  236. week := c.GetString("week")
  237. pagei, _ := c.GetInt("pageindex", 1)
  238. pagen, _ := c.GetInt("pagesize", 20)
  239. r, n, err := obj.QueryWeekReportList(deviceid, year, week, pagei, pagen)
  240. if err != nil {
  241. c.Data["json"] = c.ApiError(err.Error())
  242. } else {
  243. c.Data["json"] = c.ApiOK(map[string]interface{}{"total": n, "list": r})
  244. }
  245. c.ServeJSON()
  246. }
  247. // 获取日报表列表 godoc
  248. // @Summary 获取日报表列表
  249. // @Description 获取日报表列表。不指定月时,获取指定年内所有的日报表。
  250. // @Tags api
  251. // @Accept x-www-form-urlencoded
  252. // @Produce json
  253. // @Param deviceid query int true "设备ID"
  254. // @Param year query int true "查询条件:年份"
  255. // @Param month query int false "查询条件:月份"
  256. // @Param pageindex query int false "查询条件:当前页码。默认为1"
  257. // @Param pagesize query int false "查询条件:每页记录数。默认为20"
  258. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  259. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  260. // @Failure 500 status 服务器|后台发生错误
  261. // @router /day/list [get]
  262. func (c *StatReportController) GetDayReportList() {
  263. obj := new(service.StatReportService)
  264. deviceid, _ := c.GetInt32("deviceid")
  265. if deviceid == 0 {
  266. c.Data["json"] = c.ApiError("设备ID不能为空")
  267. c.ServeJSON()
  268. return
  269. }
  270. obj.UserInfo = c.GetCurrentUserInfo_rt()
  271. year := c.GetString("year")
  272. if year == "" {
  273. c.Data["json"] = c.ApiError("年份不能为空")
  274. c.ServeJSON()
  275. return
  276. }
  277. month := c.GetString("month")
  278. pagei, _ := c.GetInt("pageindex", 1)
  279. pagen, _ := c.GetInt("pagesize", 20)
  280. r, n, err := obj.QueryDayReportList(deviceid, year, month, pagei, pagen)
  281. if err != nil {
  282. c.Data["json"] = c.ApiError(err.Error())
  283. } else {
  284. c.Data["json"] = c.ApiOK(map[string]interface{}{"total": n, "list": r})
  285. }
  286. c.ServeJSON()
  287. }
  288. // 获取自定义周期内的日报表列表 godoc
  289. // @Summary 获取自定义周期内的日报表列表
  290. // @Description 获取自定义周期内的日报表列表。建议开始和结束日期范围不超过3个月!
  291. // @Tags api
  292. // @Accept x-www-form-urlencoded
  293. // @Produce json
  294. // @Param deviceid query int true "设备ID"
  295. // @Param s1 query string true "查询条件:开始日期"
  296. // @Param s2 query string false "查询条件:结束日期"
  297. // @Param pageindex query int false "查询条件:当前页码。默认为1"
  298. // @Param pagesize query int false "查询条件:每页记录数。默认为20"
  299. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  300. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  301. // @Failure 500 status 服务器|后台发生错误
  302. // @router /custmer/list [get]
  303. func (c *StatReportController) GetAnyDayReportList() {
  304. obj := new(service.StatReportService)
  305. deviceid, _ := c.GetInt32("deviceid")
  306. if deviceid == 0 {
  307. c.Data["json"] = c.ApiError("设备ID不能为空")
  308. c.ServeJSON()
  309. return
  310. }
  311. obj.UserInfo = c.GetCurrentUserInfo_rt()
  312. s1 := c.GetString("s1")
  313. s2 := c.GetString("s2")
  314. if s1 == "" || s2 == "" {
  315. c.Data["json"] = c.ApiError("查询的开始和结束日期不能为空")
  316. c.ServeJSON()
  317. return
  318. }
  319. pagei, _ := c.GetInt("pageindex", 1)
  320. pagen, _ := c.GetInt("pagesize", 20)
  321. r, n, err := obj.QueryDayReportListByAnyDate(deviceid, s1, s2, pagei, pagen)
  322. if err != nil {
  323. c.Data["json"] = c.ApiError(err.Error())
  324. } else {
  325. c.Data["json"] = c.ApiOK(map[string]interface{}{"total": n, "list": r})
  326. }
  327. c.ServeJSON()
  328. }
  329. // 获取最高数据(温度、湿度、电压等)统计信息
  330. // @Summary 获取最高数据(温度、湿度、电压等)统计信息
  331. // @Description 获取最高数据(温度、湿度、电压等)统计信息。包括:tem(温度、时间、设备名称)对象,hum(湿度、时间、设备名称)对象,vol(电压、时间、设备名称)对象.以上信息后台会通过主题/rtelec/runtime/today/maxdata进行发布。前端可订阅该主题。
  332. // @Tags api
  333. // @Accept x-www-form-urlencoded
  334. // @Produce json
  335. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  336. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  337. // @Failure 500 status 服务器|后台发生错误
  338. // @router /maxdata [get]
  339. func (c *StatReportController) GetMaxDataInfo() {
  340. var hisotryObject = new(service.HistoryService)
  341. list, err := hisotryObject.GetMaxDataInfo()
  342. if err != nil {
  343. c.Data["json"] = c.ApiError(err.Error())
  344. } else {
  345. c.Data["json"] = c.ApiOK(list)
  346. }
  347. c.ServeJSON()
  348. }
  349. // 获取设备数量统计信息
  350. // @Summary 获取设备数量统计信息
  351. // @Description 获取设备数量统计信息。包括:总设备数、在线数、离线数、告警数。以上信息每分钟后台会通过主题/rtelec/runtime/device/total进行发布。前端可订阅该主题。
  352. // @Tags api
  353. // @Accept x-www-form-urlencoded
  354. // @Produce json
  355. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  356. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  357. // @Failure 500 status 服务器|后台发生错误
  358. // @router /device/total [get]
  359. func (c *StatReportController) GetTotalDataInfo() {
  360. var obj = new(service.StatReportService)
  361. obj.UserInfo = c.GetCurrentUserInfo_rt()
  362. list, err := obj.DeviceTotalStat()
  363. if err != nil {
  364. c.Data["json"] = c.ApiError(err.Error())
  365. } else {
  366. c.Data["json"] = c.ApiOK(list)
  367. }
  368. c.ServeJSON()
  369. }