utilController.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/device/model"
  5. "iot_manager_service/app/device/service"
  6. "iot_manager_service/app/middleware"
  7. "iot_manager_service/util"
  8. "net/http"
  9. "strconv"
  10. )
  11. // 工具服务
  12. var Util = new(utilCtl)
  13. type utilCtl struct{}
  14. // GetModelList 获取厂家/品牌/型号列表
  15. // 查询类型 type:1-厂家,2-品牌,3-型号
  16. //deviceType 设备类型
  17. //0-摄像头
  18. //1 灯控
  19. //2 信息屏
  20. //3 配电箱
  21. //4 网关
  22. //5 环境监测
  23. //6 ZigBee
  24. //7 一键告警终端
  25. //8 一键告警服务端
  26. //11 桥梁传感器
  27. //12 ip音柱
  28. //13 井盖
  29. //14 垃圾桶
  30. func (c *utilCtl) GetModelList(ctx *gin.Context) {
  31. t := ctx.Query("type")
  32. d := ctx.Query("deviceType")
  33. id := ctx.Query("id")
  34. pid := ctx.Query("pid")
  35. modeType, err := strconv.Atoi(t)
  36. if err != nil || modeType < 1 || modeType > 3 {
  37. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse("type invalid", nil))
  38. return
  39. }
  40. modeType++ //前端传入的1和2,实际是2和3
  41. deviceType, err := strconv.Atoi(d)
  42. if err != nil {
  43. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse("deviceType invalid", nil))
  44. return
  45. }
  46. parentId := -1
  47. if id != "" {
  48. parentId, _ = strconv.Atoi(id)
  49. } else if pid != "" {
  50. parentId, _ = strconv.Atoi(pid)
  51. }
  52. value, _ := ctx.Get(middleware.Authorization)
  53. claims := value.(*middleware.Claims)
  54. var result []model.VendorDetail
  55. if modeType == model.TypeVendor {
  56. vendors, err := service.UtilService.GetDeviceVendor(claims.TenantId, deviceType)
  57. if err != nil {
  58. ctx.JSON(http.StatusOK, util.FailResponse(err.Error(), nil))
  59. return
  60. }
  61. for _, vendor := range vendors {
  62. result = append(result, model.VendorDetail{
  63. Id: vendor.ID,
  64. ParentId: vendor.ParentId,
  65. Vendor: vendor.VendorValue,
  66. })
  67. }
  68. } else if modeType == model.TypeBrand {
  69. vendors, err := service.UtilService.GetDeviceBrand(claims.TenantId, deviceType)
  70. if err != nil {
  71. ctx.JSON(http.StatusOK, util.FailResponse(err.Error(), nil))
  72. return
  73. }
  74. for _, vendor := range vendors {
  75. result = append(result, model.VendorDetail{
  76. Id: vendor.ID,
  77. ParentId: vendor.ParentId,
  78. Brand: vendor.VendorValue,
  79. })
  80. }
  81. } else if modeType == model.TypeModel {
  82. vendors, err := service.UtilService.GetDeviceModel(claims.TenantId, deviceType, parentId)
  83. if err != nil {
  84. ctx.JSON(http.StatusOK, util.FailResponse(err.Error(), nil))
  85. return
  86. }
  87. for _, vendor := range vendors {
  88. result = append(result, model.VendorDetail{
  89. Id: vendor.ID,
  90. ParentId: vendor.ParentId,
  91. Model: vendor.VendorValue,
  92. })
  93. }
  94. }
  95. ctx.JSON(http.StatusOK, util.SuccessResponse("", result))
  96. }
  97. func (c *utilCtl) GetTenantCode(ctx *gin.Context) {
  98. ctx.JSON(http.StatusOK, util.SuccessResponse("", nil))
  99. }