package brand import ( "context" "encoding/json" "fmt" "strings" "time" "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 Update(ctx context.Context, req *v1.UpdateSYBrandRequest) (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.Id <= 0 || req.BrandName == "" || req.Initial == "" || req.Weight < 0 { return reply, errors.ParamsError } res, err := model.NewSyBrand().Get(database.DB().Where("id = ?", req.Id)) if err != nil && err != gorm.ErrRecordNotFound { return reply, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, errors.BrandNotExistError } tx := database.DB().Begin() defer func() { if err != nil { tx.Rollback() return } tx.Commit() }() values := map[string]interface{}{ "initial": strings.ToUpper(req.Initial), "brand_name": req.BrandName, "weight": req.Weight, "status": req.Status, "update_time": time.Now().Format("200601"), "old_brand_name":req.OldBrandName, } if req.HasImg == 1 { values["has_img"] = 1 } if err = model.NewSyBrand().Update(tx.Where("id = ?", req.Id), values); err != nil { return reply, errors.SystemError } if err = model.NewSySerieModel().Update(tx.Where("brand_id = ?", res.BrandId), map[string]interface{}{ "status": req.Status, }); err != nil { return reply, errors.SystemError } if err = model.NewSyStyleModel().Update(tx.Where("brand_id = ?", res.BrandId), map[string]interface{}{ "brand_name": req.BrandName, "is_on": req.Status, }); err != nil { return reply, errors.SystemError } return reply, nil }