update.go 1.7 KB

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