123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package style
- import (
- "context"
- "encoding/json"
- "fmt"
- "adm-vehicle-style/consts"
- "adm-vehicle-style/errors"
- "adm-vehicle-style/model"
- v1 "adm-vehicle-style/pb/v1"
- "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.StyleListRequest) (reply *v1.StyleListReply, err error) {
- reply = &v1.StyleListReply{}
- // 捕获各个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.SeriesName != "" {
- db = db.Where("series_name = ?", req.SeriesName)
- }
- if req.Maker != "" {
- db = db.Where("maker = ?", req.Maker)
- }
- if req.ModelYear != 0 {
- db = db.Where("model_year = ?", req.ModelYear)
- }
- if req.StyleName != "" {
- db = db.Where("style_name like ?", "%"+req.StyleName+"%")
- }
- if req.BrandId != "" {
- db = db.Where("brand_id = ?", req.BrandId)
- }
- if req.SeriesId != "" {
- db = db.Where("series_id = ?", req.SeriesId)
- }
- if req.StyleId != "" {
- db = db.Where("t1.style_id = ?", req.StyleId)
- }
- switch req.Status {
- case -1:
- db = db.Where("is_on = 0")
- case 1:
- db = db.Where("is_on = 1")
- }
- var join bool
- switch req.HasManual {
- case -1:
- db = db.Where("t2.id is null")
- join = true
- case 1:
- db = db.Where("t2.id <> 0")
- join = true
- }
- pageSize := consts.PageSize
- if req.PageSize != 0 {
- pageSize = int(req.PageSize)
- }
- // 构造分页类
- pagination := model.NewPagination(int(req.Page), pageSize, 0).GetLimitOffset()
- count, err := model.NewSyStyleModel().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.NewSyStyleModel().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.StyleList, 0, len(list))
- for _, v := range list {
- reply.List = append(reply.List, &v1.StyleList{
- Id: v.ID,
- StyleId: v.StyleId,
- BrandName: v.BrandName,
- Maker: v.Maker,
- SeriesName: v.SeriesName,
- StyleName: v.StyleName,
- Status: v.IsOn,
- MaintainId: v.MaintainId,
- SeriesId: v.SeriesId,
- BrandId: v.BrandId,
- })
- }
- // 计算分页结果
- 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
- }
|