123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package brand
- import (
- "context"
- "encoding/json"
- "fmt"
- "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 MakerList(ctx context.Context, req *v1.GetMakerRequest) (reply *v1.GetMakerReply, err error) {
- reply = &v1.GetMakerReply{}
- // 捕获各个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 req.BrandId == "" {
- return reply, errors.ParamsError
- }
- list, err := model.NewSySerieModel().MakerList(database.DB().Where("brand_id = ?", req.BrandId))
- if err != nil && err != gorm.ErrRecordNotFound {
- return reply, errors.SystemError
- }
- if err == gorm.ErrRecordNotFound {
- return reply, nil
- }
- reply.List = make([]*v1.MakerList, 0, len(list))
- for _, v := range list {
- reply.List = append(reply.List, &v1.MakerList{
- BrandId: v.BrandId,
- MakerId: v.MakerId,
- Maker: v.Maker,
- })
- }
- return reply, nil
- }
- func UpdateMaker(ctx context.Context, req *v1.UpdateMakerRequest) (reply *v1.EmptyReply, err error) {
- reply = &v1.EmptyReply{}
- // 捕获各个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 req.Maker == "" || req.MakerId == "" {
- return reply, errors.ParamsError
- }
- tx := database.DB().Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- return
- }
- tx.Commit()
- }()
- tx = tx.Where("maker_id = ?", req.MakerId)
- values := map[string]interface{}{
- "maker": req.Maker,
- }
- err = model.NewSySerieModel().Update(tx, values)
- if err != nil {
- return reply, err
- }
- err = model.NewSyStyleModel().Update(tx, values)
- if err != nil {
- return reply, err
- }
- return reply, nil
- }
|