update.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package brand
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "adm-vehicle-style/errors"
  9. "adm-vehicle-style/model"
  10. v1 "adm-vehicle-style/pb/v1"
  11. "git.getensh.com/common/gopkgsv2/database"
  12. "git.getensh.com/common/gopkgsv2/logger"
  13. "go.uber.org/zap"
  14. "google.golang.org/grpc/status"
  15. "gorm.io/gorm"
  16. )
  17. func Update(ctx context.Context, req *v1.UpdateSYBrandRequest) (reply *v1.EmptyReply, err error) {
  18. reply = &v1.EmptyReply{}
  19. // 捕获各个task中的异常并返回给调用者
  20. defer func() {
  21. if r := recover(); r != nil {
  22. err = fmt.Errorf("%+v", r)
  23. e := &status.Status{}
  24. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  25. logger.Error("err",
  26. zap.String("system_err", err.Error()),
  27. zap.Stack("stacktrace"))
  28. }
  29. }
  30. }()
  31. if req.Id <= 0 || req.BrandName == "" || req.Initial == "" || req.Weight < 0 {
  32. return reply, errors.ParamsError
  33. }
  34. res, err := model.NewSyBrand().Get(database.DB().Where("id = ?", req.Id))
  35. if err != nil && err != gorm.ErrRecordNotFound {
  36. return reply, errors.SystemError
  37. }
  38. if err == gorm.ErrRecordNotFound {
  39. return reply, errors.BrandNotExistError
  40. }
  41. tx := database.DB().Begin()
  42. defer func() {
  43. if err != nil {
  44. tx.Rollback()
  45. return
  46. }
  47. tx.Commit()
  48. }()
  49. values := map[string]interface{}{
  50. "initial": strings.ToUpper(req.Initial),
  51. "brand_name": req.BrandName,
  52. "weight": req.Weight,
  53. "status": req.Status,
  54. "update_time": time.Now().Format("200601"),
  55. "old_brand_name":req.OldBrandName,
  56. }
  57. if req.HasImg == 1 {
  58. values["has_img"] = 1
  59. }
  60. if err = model.NewSyBrand().Update(tx.Where("id = ?", req.Id), values); err != nil {
  61. return reply, errors.SystemError
  62. }
  63. if err = model.NewSySerieModel().Update(tx.Where("brand_id = ?", res.BrandId), map[string]interface{}{
  64. "status": req.Status,
  65. }); err != nil {
  66. return reply, errors.SystemError
  67. }
  68. if err = model.NewSyStyleModel().Update(tx.Where("brand_id = ?", res.BrandId), map[string]interface{}{
  69. "brand_name": req.BrandName,
  70. "is_on": req.Status,
  71. }); err != nil {
  72. return reply, errors.SystemError
  73. }
  74. return reply, nil
  75. }