package brand import ( "context" "encoding/json" "fmt" "adm-vehicle-style/consts" "adm-vehicle-style/errors" "adm-vehicle-style/model" v1 "adm-vehicle-style/pb/v1" "adm-vehicle-style/utils" "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 List(ctx context.Context, req *v1.BrandListRequest) (reply *v1.BrandListReply, err error) { reply = &v1.BrandListReply{} // 捕获各个task中的异常并返回给调用者 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")) } } }() db := database.DB() if req.BrandName != "" { db = db.Where("brand_name = ?", req.BrandName) } if req.BrandId != "" { db = db.Where("brand_id = ?", req.BrandId) } if req.Initial != "" { db = db.Where("initial = ?", req.Initial) } if req.OldBrandName != ""{ db = db.Where("old_brand_name like ?", "%"+req.OldBrandName+"%") //db = db.Where("old_brand_name = ?", req.OldBrandName) } if req.HasImg == -1 { db = db.Where("has_img = 0") } else if req.HasImg == 1 { db = db.Where("has_img = 1") } if req.Status == -1 { db = db.Where("status = 0") } else if req.Status == 1 { db = db.Where("status = 1") } pageSize := consts.PageSize if req.PageSize != 0 { pageSize = int(req.PageSize) } // 构造分页类 pagination := model.NewPagination(int(req.Page), pageSize, 0).GetLimitOffset() count, err := model.NewSyBrand().Count(db) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound || count == 0 { return reply, nil } list, err := model.NewSyBrand().List(db, pagination) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, nil } reply.List = make([]*v1.BrandList, 0, len(list)) for _, v := range list { reply.List = append(reply.List, &v1.BrandList{ Id: v.ID, Initial: v.Initial, BrandName: v.BrandName, BrandId: v.BrandId, Weight: v.Weight, HasImg: v.HasImg, Status: v.Status, Image: utils.GetBrandLogoById(v.BrandId, int32(v.HasImg)), OldBrandName:v.OldBrandName, }) } // 计算分页结果 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, err }