maker.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package brand
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "adm-vehicle-style/errors"
  7. "adm-vehicle-style/model"
  8. v1 "adm-vehicle-style/pb/v1"
  9. "git.getensh.com/common/gopkgsv2/database"
  10. "git.getensh.com/common/gopkgsv2/logger"
  11. "go.uber.org/zap"
  12. "google.golang.org/grpc/status"
  13. "gorm.io/gorm"
  14. )
  15. func MakerList(ctx context.Context, req *v1.GetMakerRequest) (reply *v1.GetMakerReply, err error) {
  16. reply = &v1.GetMakerReply{}
  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.BrandId == "" {
  30. return reply, errors.ParamsError
  31. }
  32. list, err := model.NewSySerieModel().MakerList(database.DB().Where("brand_id = ?", req.BrandId))
  33. if err != nil && err != gorm.ErrRecordNotFound {
  34. return reply, errors.SystemError
  35. }
  36. if err == gorm.ErrRecordNotFound {
  37. return reply, nil
  38. }
  39. reply.List = make([]*v1.MakerList, 0, len(list))
  40. for _, v := range list {
  41. reply.List = append(reply.List, &v1.MakerList{
  42. BrandId: v.BrandId,
  43. MakerId: v.MakerId,
  44. Maker: v.Maker,
  45. })
  46. }
  47. return reply, nil
  48. }
  49. func UpdateMaker(ctx context.Context, req *v1.UpdateMakerRequest) (reply *v1.EmptyReply, err error) {
  50. reply = &v1.EmptyReply{}
  51. // 捕获各个task中的异常并返回给调用者
  52. defer func() {
  53. if r := recover(); r != nil {
  54. err = fmt.Errorf("%+v", r)
  55. e := &status.Status{}
  56. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  57. logger.Error("err",
  58. zap.String("system_err", err.Error()),
  59. zap.Stack("stacktrace"))
  60. }
  61. }
  62. }()
  63. if req.Maker == "" || req.MakerId == "" {
  64. return reply, errors.ParamsError
  65. }
  66. tx := database.DB().Begin()
  67. defer func() {
  68. if err != nil {
  69. tx.Rollback()
  70. return
  71. }
  72. tx.Commit()
  73. }()
  74. tx = tx.Where("maker_id = ?", req.MakerId)
  75. values := map[string]interface{}{
  76. "maker": req.Maker,
  77. }
  78. err = model.NewSySerieModel().Update(tx, values)
  79. if err != nil {
  80. return reply, err
  81. }
  82. err = model.NewSyStyleModel().Update(tx, values)
  83. if err != nil {
  84. return reply, err
  85. }
  86. return reply, nil
  87. }