123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package api_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 KeyList(ctx context.Context, req *v1.KeyListRequest) (reply *v1.KeyListReply, 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.KeyListReply{}
- db := database.DB()
- if req.Key != "" {
- db = db.Where("`key` like ?", "%"+req.Key+"%")
- }
- if req.Desc != "" {
- db = db.Where("`desc` like ?", "%"+req.Desc+"%")
- }
- pageSize := consts.PageSize
- if req.PageSize != 0 {
- pageSize = int(req.PageSize)
- }
- // 构造分页类
- pagination := model.NewPagination(int(req.Page), pageSize, 0).GetLimitOffset()
- count, err := model.NewKeyList().Count(db)
- if err != nil && err != gorm.ErrRecordNotFound {
- return reply, errors.SystemError
- }
- if err == gorm.ErrRecordNotFound || count == 0 {
- return reply, nil
- }
- list, err := model.NewKeyList().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.KeyList, 0, len(list))
- for _, v := range list {
- reply.List = append(reply.List, &v1.KeyList{
- Key: v.Key,
- Desc: v.Desc,
- CreatedAt: v.CreatedAt,
- })
- }
- // 计算分页结果
- 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
- }
|