package task_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 TaskList(ctx context.Context, req *v1.TaskListRequest) (reply *v1.TaskListReply, 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.TaskListReply{} db := database.DB() if req.TaskName != "" { db = db.Where("task_name = ?", req.TaskName) } 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.NewTaskList().Count(db) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound || count == 0 { return reply, nil } list, err := model.NewTaskList().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 } if err != nil { if err == gorm.ErrRecordNotFound { return reply, nil } return reply, errors.SystemError } for _, v := range list { reply.List = append(reply.List, &v1.TaskList{ TaskId: v.TaskId, TaskName: v.TaskName, Desc: v.Desc, TargetTable: v.TargetTable, }) } //reply.NameList = make([]string, 0, len(list)) //for _, v := range list { // reply.NameList = append(reply.NameList, v.TaskName) //} // 计算分页结果 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 }