device.go 4.9 KB

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