update.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package style
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "gorm.io/gorm"
  8. "time"
  9. "adm-vehicle-style/errors"
  10. "adm-vehicle-style/model"
  11. v1 "adm-vehicle-style/pb/v1"
  12. jsoniter "github.com/json-iterator/go"
  13. "git.getensh.com/common/gopkgsv2/database"
  14. "git.getensh.com/common/gopkgsv2/logger"
  15. "go.uber.org/zap"
  16. "google.golang.org/grpc/status"
  17. )
  18. func Update(ctx context.Context, req *v1.UpdateStyleRequest) (reply *v1.EmptyReply, err error) {
  19. reply = &v1.EmptyReply{}
  20. // 捕获各个task中的异常并返回给调用者
  21. defer func() {
  22. if r := recover(); r != nil {
  23. err = fmt.Errorf("%+v", r)
  24. e := &status.Status{}
  25. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  26. logger.Error("err",
  27. zap.String("system_err", err.Error()),
  28. zap.Stack("stacktrace"))
  29. }
  30. }
  31. }()
  32. if req.Id <= 0 || req.StyleName == "" {
  33. return reply, errors.ParamsError
  34. }
  35. values := map[string]interface{}{
  36. "style_name": req.StyleName,
  37. "is_on": req.Status,
  38. "updated_at": time.Now().Unix(),
  39. }
  40. if err = model.NewSyStyleModel().Update(database.DB().Where("id = ?", req.Id), values); err != nil {
  41. return reply, errors.SystemError
  42. }
  43. return reply, nil
  44. }
  45. func updateAds14(db *gorm.DB,styleId string ,c2Id int ,attributeRule string) error {
  46. ads14 := &model.Ads14{}
  47. ads14.StyleId = styleId
  48. ads14.C2Id = c2Id
  49. err := ads14.Query(db,map[string]interface{}{"style_id":ads14.StyleId,"c2_id":ads14.C2Id})
  50. ads14.AttributeRule = attributeRule
  51. if err != nil{
  52. if err == gorm.ErrRecordNotFound{
  53. err = ads14.Insert(db)
  54. if err != nil && !strings.Contains(err.Error(), "Duplicate") {
  55. return errors.SystemError
  56. }
  57. }else{
  58. return errors.SystemError
  59. }
  60. }else{
  61. err = ads14.Update(db)
  62. if err != nil && err != gorm.ErrRecordNotFound {
  63. return errors.SystemError
  64. }
  65. }
  66. return nil
  67. }
  68. func UpdateStyleInfo(ctx context.Context, req *v1.UpdateStyleInfoRequest) (reply *v1.EmptyReply, err error){
  69. reply = &v1.EmptyReply{}
  70. if req.Id <= 0 || req.Data == "" {
  71. return reply, errors.ParamsError
  72. }
  73. db := database.DB().Begin()
  74. err = UpdateStyleInfoImpl(db,req)
  75. if err != nil{
  76. db.Rollback()
  77. }else{
  78. db.Commit()
  79. }
  80. return reply,err
  81. }
  82. func UpdateStyleInfoImpl(db *gorm.DB, req *v1.UpdateStyleInfoRequest) (err error) {
  83. // 捕获各个task中的异常并返回给调用者
  84. defer func() {
  85. if r := recover(); r != nil {
  86. err = fmt.Errorf("%+v", r)
  87. e := &status.Status{}
  88. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  89. logger.Error("err",
  90. zap.String("system_err", err.Error()),
  91. zap.Stack("stacktrace"))
  92. }
  93. }
  94. }()
  95. values := make(map[string]interface{})
  96. _ = jsoniter.UnmarshalFromString(req.Data, &values)
  97. values["updated_at"] = time.Now().Unix()
  98. res, err := model.NewSyStyleModel().Get(db.Where("id = ?", req.Id))
  99. if err != nil {
  100. return errors.SystemError
  101. }
  102. if err = model.NewSyStyleModel().Update(db.Where("id = ?", req.Id), values); err != nil {
  103. return errors.SystemError
  104. }
  105. var front ,back string
  106. if value ,ok := values["front_wheel_specification"];ok{
  107. front = value.(string)
  108. }
  109. if value,ok := values["back_wheel_specification"];ok{
  110. back = value.(string)
  111. }
  112. if front != "" && front != res.FrontWheelSpecification{
  113. err = updateAds14(db,res.StyleId,1001,front)
  114. if err != nil{
  115. return err
  116. }
  117. }
  118. if back != "" && back != res.BackWheelSpecification{
  119. return updateAds14(db,res.StyleId,1002,back)
  120. }
  121. return nil
  122. }