dataPlan.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package metadata
  2. import (
  3. "errors"
  4. "go-nc/configs/global"
  5. "go-nc/model"
  6. "go-nc/model/common"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // 查询数据套餐列表
  10. func GetDataPlanList(c *gin.Context) {
  11. // 查询数据套餐列表
  12. type Param struct {
  13. model.Metadata_package
  14. common.Pagination
  15. }
  16. var param Param
  17. param.Pagination = common.NewPagination()
  18. if err := c.ShouldBindJSON(&param); err != nil {
  19. c.Error(errors.New("缺少参数:" + err.Error()))
  20. return
  21. }
  22. // 先查询总记录数
  23. var total int64
  24. if err := global.App.DB.Model(&model.Metadata_package{}).Count(&total).Error; err != nil {
  25. c.Error(err)
  26. return
  27. }
  28. // 使用分页查询
  29. var dbList []model.Metadata_package
  30. if err := global.App.DB.Limit(param.PageSize).
  31. Offset((param.Current - 1) * param.PageSize).
  32. Find(&dbList).Error; err != nil {
  33. c.Error(err)
  34. return
  35. }
  36. // 序列化
  37. records := make([]interface{}, len(dbList))
  38. for i, user := range dbList {
  39. records[i] = user
  40. }
  41. // 返回数据
  42. data := common.BuildPagination(records, param.Current, param.PageSize, total)
  43. c.Set("res_data", data)
  44. }