123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package field_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 Field(ctx context.Context, req *v1.FieldRequest) (reply *v1.FieldReply, 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.FieldReply{}
- db := database.DB()
- if req.Field != "" {
- db = db.Where("`field` like ?", "%"+req.Field+"%")
- }
- 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.NewFieldList().Count(db)
- if err != nil && err != gorm.ErrRecordNotFound {
- return reply, errors.SystemError
- }
- if err == gorm.ErrRecordNotFound || count == 0 {
- return reply, nil
- }
- list, err := model.NewFieldList().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.Field, 0, len(list))
- for _, v := range list {
- reply.List = append(reply.List, &v1.Field{
- Field: v.Field,
- Desc: v.Desc,
- Type: "string",
- })
- }
- // 计算分页结果
- 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
- }
|