123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package controller
- import (
- "cold-logistics/app/admin/model"
- "cold-logistics/app/admin/service"
- "cold-logistics/app/admin/service/dto"
- "cold-logistics/common/actions"
- cdto "cold-logistics/common/dto"
- "cold-logistics/common/nats/nats_server"
- "github.com/gin-gonic/gin"
- "github.com/gin-gonic/gin/binding"
- "gogs.baozhida.cn/zoie/OAuth-core/api"
- )
- type DeviceController struct {
- api.Api
- }
- // GetSensorList 获取传感器列表
- // @Summary 获取传感器列表
- // @Description 获取传感器列表
- // @Tags 设备
- // @Success 200 {object} response.Response{data=response.Page{list=[]nats_server.Device_R}} "{"code": 200, "data": [...]}"
- // @Router /api/device/sensor-list [get]
- // @Security Bearer
- func (e DeviceController) GetSensorList(c *gin.Context) {
- s := service.Device{}
- err := e.MakeContext(c).
- MakeOrm().
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- //数据权限检查
- p := actions.GetPermissionFromContext(c)
- // 获取传感器信息
- deviceSensorList, count, _ := s.GetSensorList(p)
- e.PageOK(deviceSensorList, int(count), 0, 0, "查询成功")
- }
- // GetData 获取温湿度信息
- // @Summary 获取温湿度信息
- // @Description 获取温湿度信息
- // @Tags 设备
- // @Param t_sn query string false "设备sn"
- // @Param t_id query string false "传感器id"
- // @Param startTime query string false "开始时间"
- // @Param endTime query string false "结束时间"
- // @Param pageSize query int false "页条数"
- // @Param page query int false "页码"
- // @Success 200 {object} response.Response{data=response.Page{list=[]nats_server.Device_R}} "{"code": 200, "data": [...]}"
- // @Router /api/device/sensor-list [get]
- // @Security Bearer
- func (e DeviceController) GetData(c *gin.Context) {
- s := service.Device{}
- req := dto.DeviceGetDataReq{}
- err := e.MakeContext(c).
- MakeOrm().
- Bind(&req, binding.Query).
- MakeService(&s.Service).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- //数据权限检查
- p := actions.GetPermissionFromContext(c)
- // 获取传感器信息
- dataList, count, _ := s.GetData(req, p)
- e.PageOK(dataList, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
- }
- // GetDevicePage 获取未绑定的设备列表
- // @Summary 获取未绑定的设备列表
- // @Description 获取未绑定的设备列表
- // @Tags 设备
- // @Param name query string false "设备名称"
- // @Param pageSize query int false "页条数"
- // @Param page query int false "页码"
- // @Success 200 {object} response.Response{data=response.Page{list=[]nats_server.Device_R}} "{"code": 200, "data": [...]}"
- // @Router /api/device [get]
- // @Security Bearer
- func (e DeviceController) GetDevicePage(c *gin.Context) {
- coolerBoxSvc := service.CoolerBox{}
- warehouseSvc := service.Warehouse{}
- carSvc := service.Car{}
- req := dto.DeviceGetPageReq{}
- err := e.MakeContext(c).
- MakeOrm().
- MakeService(&coolerBoxSvc.Service).
- MakeService(&warehouseSvc.Service).
- MakeService(&carSvc.Service).
- Bind(&req, binding.Query).
- Errors
- if err != nil {
- e.Logger.Error(err)
- e.Error(500, err, err.Error())
- return
- }
- list := make([]nats_server.Device_R, 0)
- p := actions.GetPermissionFromContext(c)
- var coolerBoxCount int64
- coolerBoxList := make([]model.CoolerBox, 0)
- err = coolerBoxSvc.GetPage(&dto.CoolerBoxGetPageReq{Pagination: cdto.Pagination{PageSize: 9999}}, &coolerBoxList, &coolerBoxCount, p)
- var warehouseCount int64
- warehouseList := make([]model.Warehouse, 0)
- err = warehouseSvc.GetPage(&dto.WarehouseGetPageReq{Pagination: cdto.Pagination{PageSize: 9999}}, &warehouseList, &warehouseCount, p)
- var carCount int64
- carList := make([]model.Car, 0)
- err = carSvc.GetPage(&dto.CarGetPageReq{Pagination: cdto.Pagination{PageSize: 9999}}, &carList, &carCount, p)
- var count int64
- company, err := model.GetCompanyById(p.DeptId)
- if err != nil {
- e.Error(500, err, err.Error())
- return
- }
- list, count, err = nats_server.Cold_ReadDevice_List(req.Name, company.ColdKey, req.GetPageIndex(), 9999)
- if err != nil {
- e.Error(500, err, err.Error())
- return
- }
- var bindMap = make(map[string]struct{})
- for _, v := range coolerBoxList {
- bindMap[v.Sn] = struct{}{}
- }
- for _, v := range warehouseList {
- bindMap[v.Sn] = struct{}{}
- }
- for _, v := range carList {
- bindMap[v.Sn] = struct{}{}
- }
- list2 := make([]nats_server.Device_R, 0)
- for i := 0; i < len(list); i++ {
- if _, ok := bindMap[list[i].T_sn]; ok {
- count--
- continue
- }
- list2 = append(list2, list[i])
- }
- // 计算总页数
- totalPages := (len(list2) + req.GetPageSize() - 1) / req.GetPageSize()
- // 如果n超出范围,返回错误
- if req.GetPageIndex() > totalPages {
- req.Page = totalPages
- }
- // 计算当前页的数据
- start := (req.GetPageIndex() - 1) * req.GetPageSize()
- end := start + req.GetPageSize()
- if end > len(list2) {
- end = len(list2)
- }
- currentPage := list2[start:end]
- e.PageOK(currentPage, len(list2), req.GetPageIndex(), req.GetPageSize(), "查询成功")
- }
|