123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- 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
- }
|