device.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package controller
  2. import (
  3. "cold-delivery/app/admin/model"
  4. "cold-delivery/app/admin/service"
  5. "cold-delivery/app/admin/service/dto"
  6. "cold-delivery/common/actions"
  7. "cold-delivery/common/nats/nats_server"
  8. "github.com/gin-gonic/gin"
  9. "github.com/gin-gonic/gin/binding"
  10. "gogs.baozhida.cn/zoie/OAuth-core/api"
  11. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  12. )
  13. type DeviceController struct {
  14. api.Api
  15. }
  16. // GetDevicePage 获取没有被保温箱和传感器绑定的设备列表
  17. // @Summary 获取没有被保温箱和传感器绑定的设备列表
  18. // @Description 获取没有被保温箱和传感器绑定的设备列表
  19. // @Tags 设备
  20. // @Param name query string false "设备名称"
  21. // @Param pageSize query int false "页条数"
  22. // @Param page query int false "页码"
  23. // @Success 200 {object} response.Response{data=response.Page{list=[]nats_server.Device_R}} "{"code": 200, "data": [...]}"
  24. // @Router /api/device [get]
  25. // @Security Bearer
  26. func (e DeviceController) GetDevicePage(c *gin.Context) {
  27. coolerBoxSvc := service.CoolerBox{}
  28. iceLockerSvc := service.IceLocker{}
  29. req := dto.DeviceGetPageReq{}
  30. err := e.MakeContext(c).
  31. MakeOrm().
  32. MakeService(&coolerBoxSvc.Service).
  33. MakeService(&iceLockerSvc.Service).
  34. Bind(&req, binding.Query).
  35. Errors
  36. if err != nil {
  37. e.Logger.Error(err)
  38. e.Error(500, err, err.Error())
  39. return
  40. }
  41. list := make([]nats_server.Device_R, 0)
  42. p := actions.GetPermissionFromContext(c)
  43. var coolerBoxCount int64
  44. coolerBoxList := make([]model.CoolerBox, 0)
  45. var iceLockerCount int64
  46. iceLockerList := make([]model.IceLocker, 0)
  47. coolerBoxReq := dto.CoolerBoxGetPageReq{}
  48. coolerBoxReq.PageSize = 9999
  49. err = coolerBoxSvc.GetPage(&coolerBoxReq, &coolerBoxList, &coolerBoxCount, p)
  50. iceLockerReq := dto.IceLockerGetPageReq{}
  51. iceLockerReq.PageSize = 9999
  52. err = iceLockerSvc.GetPage(&iceLockerReq, &iceLockerList, &iceLockerCount, p)
  53. var count int64
  54. company, err := model.GetCompanyById(p.DeptId)
  55. if err != nil {
  56. e.Error(500, err, err.Error())
  57. return
  58. }
  59. list, count, err = nats_server.Cold_ReadDevice_List(req.Name, company.ColdKey, req.GetPageIndex(), 9999)
  60. if err != nil {
  61. e.Error(500, err, err.Error())
  62. return
  63. }
  64. var bindMap = make(map[string]struct{})
  65. for _, box := range coolerBoxList {
  66. bindMap[box.Sn] = struct{}{}
  67. }
  68. for _, iceLocker := range iceLockerList {
  69. bindMap[iceLocker.Sn] = struct{}{}
  70. }
  71. list2 := make([]nats_server.Device_R, 0)
  72. for i := 0; i < len(list); i++ {
  73. if _, ok := bindMap[list[i].T_sn]; ok {
  74. count--
  75. continue
  76. }
  77. list2 = append(list2, list[i])
  78. }
  79. // Fixme 测试完成之后删除
  80. //for i := 0; i < 20; i++ {
  81. // list2 = append(list2, nats_server.Device_R{
  82. // T_devName: fmt.Sprintf("冷冻柜%03d", i),
  83. // T_sn: fmt.Sprintf("2024114956064%03d", i),
  84. // })
  85. // count++
  86. //}
  87. //if len(list2) == 0 {
  88. // e.PageOK(list2, len(list2), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  89. //}
  90. // 计算总页数
  91. totalPages := (len(list2) + req.GetPageSize() - 1) / req.GetPageSize()
  92. // 如果n超出范围,返回错误
  93. if req.GetPageIndex() > totalPages {
  94. req.Page = totalPages
  95. }
  96. // 计算当前页的数据
  97. start := (req.GetPageIndex() - 1) * req.GetPageSize()
  98. end := start + req.GetPageSize()
  99. if end > len(list2) {
  100. end = len(list2)
  101. }
  102. currentPage := list2[start:end]
  103. e.PageOK(currentPage, len(list2), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  104. }
  105. // GetDeviceSensorPage 获取传感器列表
  106. // @Summary 获取传感器列表
  107. // @Description 获取传感器列表
  108. // @Tags 设备
  109. // @Param sn query string true "sn"
  110. // @Success 200 {object} response.Response{data=response.Page{list=[]nats_server.DeviceSensor_R}} "{"code": 200, "data": [...]}"
  111. // @Router /api/device-sensor [get]
  112. // @Security Bearer
  113. func (e DeviceController) GetDeviceSensorPage(c *gin.Context) {
  114. s := service.Device{}
  115. req := dto.DeviceSensorGetPageReq{}
  116. err := e.MakeContext(c).
  117. MakeOrm().
  118. Bind(&req, binding.Query).
  119. MakeService(&s.Service).
  120. Errors
  121. if err != nil {
  122. e.Logger.Error(err)
  123. e.Error(500, err, err.Error())
  124. return
  125. }
  126. //数据权限检查
  127. p := actions.GetPermissionFromContext(c)
  128. list, count, err := s.GetDeviceSensorList(&req, p)
  129. if err != nil {
  130. e.Error(500, err, err.Error())
  131. return
  132. }
  133. e.PageOK(list, int(count), 0, 0, "查询成功")
  134. }
  135. // GetDeviceSensorDataPage 获取传感器数据列表
  136. // @Summary 获取传感器数据列表
  137. // @Description 获取传感器数据列表
  138. // @Tags 设备
  139. // @Param sn query string true "sn"
  140. // @Param t_ids query string true "传感器id列表"
  141. // @Param startTime query string true "开始时间"
  142. // @Param endTime query string true "结束时间"
  143. // @Success 200 {object} response.Response{data=response.Page{list=[]nats_server.DeviceSensor_R}} "{"code": 200, "data": [...]}"
  144. // @Router /api/device-sensor [get]
  145. // @Security Bearer
  146. func (e DeviceController) GetDeviceSensorDataPage(c *gin.Context) {
  147. s := service.Device{}
  148. req := dto.DeviceSensorGetDataReq{}
  149. err := e.MakeContext(c).
  150. MakeOrm().
  151. Bind(&req, binding.Query).
  152. MakeService(&s.Service).
  153. Errors
  154. if err != nil {
  155. e.Logger.Error(err)
  156. e.Error(500, err, err.Error())
  157. return
  158. }
  159. //数据权限检查
  160. p := actions.GetPermissionFromContext(c)
  161. list, count, err := s.GetData(&req, p)
  162. if err != nil {
  163. e.Error(500, err, err.Error())
  164. return
  165. }
  166. e.PageOK(list, int(count), 0, 0, "查询成功")
  167. }