tenant.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/system/dao"
  5. "iot_manager_service/app/system/model"
  6. "iot_manager_service/app/system/service"
  7. "iot_manager_service/util"
  8. "math"
  9. "net/http"
  10. "strconv"
  11. )
  12. var Tenant = new(tenant)
  13. type tenant struct{}
  14. func (c *tenant) GetDetail(ctx *gin.Context) {
  15. id := ctx.Query("id")
  16. iId, err := strconv.ParseInt(id, 10, 64)
  17. if err != nil {
  18. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  19. return
  20. }
  21. tenant, err := service.TenantService.Get(iId)
  22. if err != nil {
  23. ctx.JSON(http.StatusOK, err)
  24. return
  25. }
  26. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, tenant))
  27. }
  28. func (c *tenant) List(ctx *gin.Context) {
  29. current, _ := strconv.Atoi(ctx.Query("current"))
  30. size, _ := strconv.Atoi(ctx.Query("size"))
  31. tenantId := ctx.Query("tenantId")
  32. tenantName := ctx.Query("tenantName")
  33. linkman := ctx.Query("linkman")
  34. if current == 0 {
  35. current = 1
  36. }
  37. if size <= 0 || size > 100 {
  38. size = 10
  39. }
  40. tenants, err := service.TenantService.List(tenantId, tenantName, linkman, current, size)
  41. if err != nil {
  42. ctx.JSON(http.StatusOK, err)
  43. return
  44. }
  45. pages := math.Ceil(float64(len(tenants)) / float64(size))
  46. rsp := model.RsqTenantList{
  47. Current: current,
  48. Size: size,
  49. Total: len(tenants),
  50. Pages: int(pages),
  51. Records: tenants,
  52. }
  53. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
  54. }
  55. func (c *tenant) Submit(ctx *gin.Context) {
  56. var req dao.Tenant
  57. if err := ctx.ShouldBindJSON(&req); err != nil {
  58. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  59. return
  60. }
  61. err := service.TenantService.Submit(req)
  62. ctx.JSON(http.StatusOK, err)
  63. }
  64. func (c *tenant) Remove(ctx *gin.Context) {
  65. var req *model.ReqTenantRemove
  66. if err := ctx.ShouldBindJSON(&req); err != nil {
  67. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  68. return
  69. }
  70. err := service.TenantService.Remove(req.IDs)
  71. ctx.JSON(http.StatusOK, err)
  72. }
  73. func (c *tenant) GetList(ctx *gin.Context) {
  74. tenants, err := service.TenantService.GetList()
  75. if err != nil {
  76. ctx.JSON(http.StatusOK, util.FailResponse(err.Error(), nil))
  77. }
  78. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, tenants))
  79. }