simCardManageControllers.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * SIM Card Manage API List
  3. * /simCardManage/info - GET 查询SIM卡信息
  4. * /simCardManage/monthly - GET 查询SIM卡月数据
  5. */
  6. package controllers
  7. import (
  8. "fwh-toolbox-server/services"
  9. "regexp"
  10. "strconv"
  11. "github.com/gofiber/fiber/v2"
  12. "go.uber.org/zap"
  13. )
  14. var yearMonthRegexp = regexp.MustCompile(`^\d{4}-(0[1-9]|1[0-2])$`)
  15. // 查询SIM卡信息
  16. // /simCardManage/info
  17. func GetSimCardBatchInfo(c *fiber.Ctx) error {
  18. search := c.Query("search", "")
  19. pageStr := c.Query("page", "1")
  20. page, err := strconv.ParseUint(pageStr, 10, 64)
  21. if err != nil {
  22. zap.L().Warn("Invalid request parameter", zap.String("err", err.Error()), zap.String("page", pageStr))
  23. return Fail(c, "Invalid request parameter (page)")
  24. }
  25. sizeStr := c.Query("size", "20")
  26. size, err := strconv.ParseUint(sizeStr, 10, 64)
  27. if err != nil {
  28. zap.L().Warn("Invalid request parameter", zap.String("err", err.Error()), zap.String("size", sizeStr))
  29. return Fail(c, "Invalid request parameter (size)")
  30. }
  31. status := c.Query("status", "")
  32. var statusPtr *string
  33. if status != "" {
  34. statusPtr = &status
  35. }
  36. date, err := services.GetSimCardBatchInfo(uint(page), uint(size), search, statusPtr)
  37. if err != nil {
  38. return Error(c, "Get SIM card info failed.")
  39. }
  40. return Success(c, date)
  41. }
  42. // 查询SIM卡月数据
  43. // /simCardManage/monthly
  44. func GetSimCardMonthlyData(c *fiber.Ctx) error {
  45. id := c.Query("id", "")
  46. month := c.Query("month", "")
  47. if id == "" {
  48. zap.L().Warn("id parameter is empty")
  49. return Fail(c, "id parameter is empty")
  50. }
  51. if !yearMonthRegexp.MatchString(month) {
  52. zap.L().Warn("month parameter is in the wrong format", zap.String("month", month))
  53. return Fail(c, "month parameter is in the wrong format")
  54. }
  55. date, err := services.GetSimCardMonthlyData(month, id)
  56. if err != nil {
  57. return Error(c, "Get SIM card monthly data failed.")
  58. }
  59. return Success(c, date)
  60. }