// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package jt import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "strings" "xingjia-management-gateway/apis" "xingjia-management-gateway/consts" "xingjia-management-gateway/errors" dbmodel "xingjia-management-gateway/model" ) func checkJtContentListParam(req *apis.JtContentListRequest) error { switch { case consts.JtContentTable[req.ContentType] == "": return status.Error(10003, "未知内容类型") } if req.Page == 0 { req.Page = 1 } if req.PageSize == 0 { req.PageSize = 10 } return nil } // func JtContentList(ctx context.Context, req *apis.JtContentListRequest) (reply *apis.JtContentListReply, err error) { reply = &apis.JtContentListReply{} // 捕获各个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")) } } }() if err = checkJtContentListParam(req); err != nil { return nil, err } p := dbmodel.NewJtContent(consts.JtContentTable[req.ContentType]) reply.Page = req.Page reply.Total, err = p.Count(database.DB(), nil, nil) if err != nil { return nil, errors.DataBaseError } if reply.Total == 0 { return reply, nil } list, err := p.List(database.DB(), nil, nil, int(req.Page), int(req.PageSize)) if err != nil { return nil, errors.DataBaseError } reply.List = make([]*apis.JtContentItem, len(list)) for i, v := range list { item := &apis.JtContentItem{ Id: v.ID, CreatedAt: v.UpdatedAt.Unix(), Title: v.Title, Content: v.Content, FirstPics: []string{}, PublishStatus: v.PublishStatus, } array := strings.Split(v.FirstPics, ";") if len(array) > 0 && array[0] != "" { item.FirstPics = array } else { item.FirstPics = make([]string, 0) } reply.List[i] = item } return reply, nil }