123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package data_management
- import (
- "adm-management/consts"
- "adm-management/errors"
- "adm-management/model"
- v1 "adm-management/pb/v1"
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgsv2/database"
- "git.getensh.com/common/gopkgsv2/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "gorm.io/gorm"
- )
- func DataList(ctx context.Context, req *v1.DataListRequest) (reply *v1.DataListReply, err error) {
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- reply = &v1.DataListReply{}
- db := database.DB()
- if req.TableName == "" {
- db = db.Where("table_name is NOT null")
- }
- if req.TableName != "" {
- data, err := model.NewDataList().Get(database.DB().Where("table_name = ?", req.TableName))
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, err
- }
- if err == gorm.ErrRecordNotFound {
- return reply, nil
- }
- db = db.Where("id = ?", data.Id)
- }
- if req.Desc == "" {
- db = db.Where("table_name is NOT null")
- }
- if req.Desc != "" {
- db = db.Where("`desc` like ?", "%"+req.Desc+"%")
- }
- if req.Level == "" {
- db = db.Where("level is NOT null")
- }
- if req.Level != "" {
- var s string
- if req.Level == "ods" {
- s = "1"
- }
- if req.Level == "dws" {
- s = "2"
- }
- if req.Level == "ads" {
- s = "3"
- }
- db = db.Where("level = ?", s)
- }
- pageSize := consts.PageSize
- if req.PageSize != 0 {
- pageSize = int(req.PageSize)
- }
- // 构造分页类
- pagination := model.NewPagination(int(req.Page), pageSize, 0).GetLimitOffset()
- count, err := model.NewDataList().Count(db)
- if err != nil && err != gorm.ErrRecordNotFound {
- return reply, errors.SystemError
- }
- if err == gorm.ErrRecordNotFound || count == 0 {
- return reply, nil
- }
- list, err := model.NewDataList().List(db, pagination)
- if err != nil && err != gorm.ErrRecordNotFound {
- return reply, errors.SystemError
- }
- if err == gorm.ErrRecordNotFound {
- pagination.CalPage(int(count))
- reply.CurrentPage = int64(pagination.CurrentPage)
- reply.PerPage = int64(pagination.PerPage)
- reply.Total = int64(pagination.Total)
- reply.FirstPage = int64(pagination.FirstPage)
- reply.LastPage = int64(pagination.LastPage)
- reply.PrevPage = int64(pagination.PrevPage)
- reply.NextPage = int64(pagination.NextPage)
- return reply, nil
- }
- reply.List = make([]*v1.DataList, 0, len(list))
- for _, v := range list {
- reply.List = append(reply.List, &v1.DataList{
- Id: v.Id,
- TableName: v.TableName,
- Desc: v.Desc,
- Level: v.Level,
- SourceCode: v.SourceCode,
- Db: v.Db,
- })
- }
- // 计算分页结果
- pagination.CalPage(int(count))
- reply.CurrentPage = int64(pagination.CurrentPage)
- reply.PerPage = int64(pagination.PerPage)
- reply.Total = int64(pagination.Total)
- reply.FirstPage = int64(pagination.FirstPage)
- reply.LastPage = int64(pagination.LastPage)
- reply.PrevPage = int64(pagination.PrevPage)
- reply.NextPage = int64(pagination.NextPage)
- return reply, nil
- }
|