123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package series
- 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.SeriesListRequest) (reply *v1.SeriesListReply, err error) {
- reply = &v1.SeriesListReply{}
- // 捕获各个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"))
- }
- }
- }()
- var join bool
- db := database.DB()
- if req.BrandName != "" {
- db = db.Where("brand_name = ?", req.BrandName)
- join = true
- }
- if req.BrandId != "" {
- db = db.Where("t1.brand_id = ?", req.BrandId)
- join = true
- }
- if req.OldSeriesName != ""{
- db = db.Where("old_series_name like ?", "%"+req.OldSeriesName+"%")
- //db = db.Where("old_series_name = ?", req.OldSeriesName)
- }
- if req.SeriesName != "" {
- db = db.Where("series_name = ?", req.SeriesName)
- }
- if req.SeriesId != "" {
- db = db.Where("series_id = ?", req.SeriesId)
- }
- if req.HasImg == -1 {
- db = db.Where("t1.has_img = 0")
- } else if req.HasImg == 1 {
- db = db.Where("t1.has_img = 1")
- }
- if req.Status == -1 {
- db = db.Where("t1.status = 0")
- } else if req.Status == 1 {
- db = db.Where("t1.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.NewSySerieModel().Count(db, join)
- if err != nil && err != gorm.ErrRecordNotFound {
- return reply, errors.SystemError
- }
- if err == gorm.ErrRecordNotFound || count == 0 {
- return reply, nil
- }
- list, err := model.NewSySerieModel().List(db, pagination, join)
- if err != nil && err != gorm.ErrRecordNotFound {
- return reply, errors.SystemError
- }
- if err == gorm.ErrRecordNotFound {
- return reply, nil
- }
- reply.List = make([]*v1.SeriesList, 0, len(list))
- for _, v := range list {
- reply.List = append(reply.List, &v1.SeriesList{
- Id: v.ID,
- BrandName: v.BrandName,
- SeriesId: v.SeriesId,
- Maker: v.Maker,
- SeriesName: v.SeriesName,
- Status: v.Status,
- Image: utils.GetSeriesLogoById(v.SeriesId, v.HasImg),
- BrandId: v.BrandId,
- OldSeriesName:v.OldSeriesName,
- })
- }
- // 计算分页结果
- 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
- }
|