package style import ( "context" "encoding/json" "fmt" "strings" "gorm.io/gorm" "time" "adm-vehicle-style/errors" "adm-vehicle-style/model" v1 "adm-vehicle-style/pb/v1" jsoniter "github.com/json-iterator/go" "git.getensh.com/common/gopkgsv2/database" "git.getensh.com/common/gopkgsv2/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) func Update(ctx context.Context, req *v1.UpdateStyleRequest) (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.StyleName == "" { return reply, errors.ParamsError } values := map[string]interface{}{ "style_name": req.StyleName, "is_on": req.Status, "updated_at": time.Now().Unix(), } if err = model.NewSyStyleModel().Update(database.DB().Where("id = ?", req.Id), values); err != nil { return reply, errors.SystemError } return reply, nil } func updateAds14(db *gorm.DB,styleId string ,c2Id int ,attributeRule string) error { ads14 := &model.Ads14{} ads14.StyleId = styleId ads14.C2Id = c2Id err := ads14.Query(db,map[string]interface{}{"style_id":ads14.StyleId,"c2_id":ads14.C2Id}) ads14.AttributeRule = attributeRule if err != nil{ if err == gorm.ErrRecordNotFound{ err = ads14.Insert(db) if err != nil && !strings.Contains(err.Error(), "Duplicate") { return errors.SystemError } }else{ return errors.SystemError } }else{ err = ads14.Update(db) if err != nil && err != gorm.ErrRecordNotFound { return errors.SystemError } } return nil } func UpdateStyleInfo(ctx context.Context, req *v1.UpdateStyleInfoRequest) (reply *v1.EmptyReply, err error){ reply = &v1.EmptyReply{} if req.Id <= 0 || req.Data == "" { return reply, errors.ParamsError } db := database.DB().Begin() err = UpdateStyleInfoImpl(db,req) if err != nil{ db.Rollback() }else{ db.Commit() } return reply,err } func UpdateStyleInfoImpl(db *gorm.DB, req *v1.UpdateStyleInfoRequest) (err error) { // 捕获各个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")) } } }() values := make(map[string]interface{}) _ = jsoniter.UnmarshalFromString(req.Data, &values) values["updated_at"] = time.Now().Unix() res, err := model.NewSyStyleModel().Get(db.Where("id = ?", req.Id)) if err != nil { return errors.SystemError } if err = model.NewSyStyleModel().Update(db.Where("id = ?", req.Id), values); err != nil { return errors.SystemError } var front ,back string if value ,ok := values["front_wheel_specification"];ok{ front = value.(string) } if value,ok := values["back_wheel_specification"];ok{ back = value.(string) } if front != "" && front != res.FrontWheelSpecification{ err = updateAds14(db,res.StyleId,1001,front) if err != nil{ return err } } if back != "" && back != res.BackWheelSpecification{ return updateAds14(db,res.StyleId,1002,back) } return nil }