dataList.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package data_management
  2. import (
  3. "adm-management/consts"
  4. "adm-management/errors"
  5. "adm-management/model"
  6. v1 "adm-management/pb/v1"
  7. "context"
  8. "encoding/json"
  9. "fmt"
  10. "git.getensh.com/common/gopkgsv2/database"
  11. "git.getensh.com/common/gopkgsv2/logger"
  12. "go.uber.org/zap"
  13. "google.golang.org/grpc/status"
  14. "gorm.io/gorm"
  15. )
  16. func DataList(ctx context.Context, req *v1.DataListRequest) (reply *v1.DataListReply, err error) {
  17. defer func() {
  18. if r := recover(); r != nil {
  19. err = fmt.Errorf("%+v", r)
  20. e := &status.Status{}
  21. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  22. logger.Error("err",
  23. zap.String("system_err", err.Error()),
  24. zap.Stack("stacktrace"))
  25. }
  26. }
  27. }()
  28. reply = &v1.DataListReply{}
  29. db := database.DB()
  30. if req.TableName == "" {
  31. db = db.Where("table_name is NOT null")
  32. }
  33. if req.TableName != "" {
  34. data, err := model.NewDataList().Get(database.DB().Where("table_name = ?", req.TableName))
  35. if err != nil && err != gorm.ErrRecordNotFound {
  36. return nil, err
  37. }
  38. if err == gorm.ErrRecordNotFound {
  39. return reply, nil
  40. }
  41. db = db.Where("id = ?", data.Id)
  42. }
  43. if req.Desc == "" {
  44. db = db.Where("table_name is NOT null")
  45. }
  46. if req.Desc != "" {
  47. db = db.Where("`desc` like ?", "%"+req.Desc+"%")
  48. }
  49. if req.Level == "" {
  50. db = db.Where("level is NOT null")
  51. }
  52. if req.Level != "" {
  53. var s string
  54. if req.Level == "ods" {
  55. s = "1"
  56. }
  57. if req.Level == "dws" {
  58. s = "2"
  59. }
  60. if req.Level == "ads" {
  61. s = "3"
  62. }
  63. db = db.Where("level = ?", s)
  64. }
  65. pageSize := consts.PageSize
  66. if req.PageSize != 0 {
  67. pageSize = int(req.PageSize)
  68. }
  69. // 构造分页类
  70. pagination := model.NewPagination(int(req.Page), pageSize, 0).GetLimitOffset()
  71. count, err := model.NewDataList().Count(db)
  72. if err != nil && err != gorm.ErrRecordNotFound {
  73. return reply, errors.SystemError
  74. }
  75. if err == gorm.ErrRecordNotFound || count == 0 {
  76. return reply, nil
  77. }
  78. list, err := model.NewDataList().List(db, pagination)
  79. if err != nil && err != gorm.ErrRecordNotFound {
  80. return reply, errors.SystemError
  81. }
  82. if err == gorm.ErrRecordNotFound {
  83. pagination.CalPage(int(count))
  84. reply.CurrentPage = int64(pagination.CurrentPage)
  85. reply.PerPage = int64(pagination.PerPage)
  86. reply.Total = int64(pagination.Total)
  87. reply.FirstPage = int64(pagination.FirstPage)
  88. reply.LastPage = int64(pagination.LastPage)
  89. reply.PrevPage = int64(pagination.PrevPage)
  90. reply.NextPage = int64(pagination.NextPage)
  91. return reply, nil
  92. }
  93. reply.List = make([]*v1.DataList, 0, len(list))
  94. for _, v := range list {
  95. reply.List = append(reply.List, &v1.DataList{
  96. Id: v.Id,
  97. TableName: v.TableName,
  98. Desc: v.Desc,
  99. Level: v.Level,
  100. SourceCode: v.SourceCode,
  101. Db: v.Db,
  102. })
  103. }
  104. // 计算分页结果
  105. pagination.CalPage(int(count))
  106. reply.CurrentPage = int64(pagination.CurrentPage)
  107. reply.PerPage = int64(pagination.PerPage)
  108. reply.Total = int64(pagination.Total)
  109. reply.FirstPage = int64(pagination.FirstPage)
  110. reply.LastPage = int64(pagination.LastPage)
  111. reply.PrevPage = int64(pagination.PrevPage)
  112. reply.NextPage = int64(pagination.NextPage)
  113. return reply, nil
  114. }