style_item.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package maintainance
  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. "gorm.io/gorm"
  15. )
  16. func StyleItem(ctx context.Context, req *v1.MaintainManualRequest) (reply *v1.StyleItemReply, err error) {
  17. reply = &v1.StyleItemReply{}
  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. list, err := model.NewStyleItemModel().List(database.DB().Where("style_id = ?", req.StyleId))
  30. if err != nil && err != gorm.ErrRecordNotFound {
  31. return nil, errors.SystemError
  32. }
  33. if err == gorm.ErrRecordNotFound {
  34. return reply, nil
  35. }
  36. reply.List = make([]*v1.StyleItem, 0, len(list))
  37. for _, v := range list {
  38. reply.List = append(reply.List, &v1.StyleItem{
  39. Id: v.ID,
  40. C2Name: v.C2Name,
  41. C2Id: v.C2Id,
  42. Useage: v.Useage,
  43. Detail: v.Detail,
  44. AttributeRule: v.AttributeRule,
  45. StyleId: req.StyleId,
  46. })
  47. }
  48. return reply, nil
  49. }
  50. func UpdateStyleItem(ctx context.Context, req *v1.UpdateStyleItemRequest) (reply *v1.EmptyReply, err error) {
  51. reply = &v1.EmptyReply{}
  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 len(req.List) == 0 {
  64. return reply, errors.ParamsError
  65. }
  66. var (
  67. insert []model.GdStyleItem
  68. tx = database.DB().Begin()
  69. )
  70. defer func() {
  71. if err != nil {
  72. tx.Rollback()
  73. return
  74. }
  75. tx.Commit()
  76. }()
  77. for _, v := range req.List {
  78. if v.Id == 0 {
  79. insert = append(insert, model.GdStyleItem{
  80. StyleId: v.StyleId,
  81. C2Id: v.C2Id,
  82. Useage: v.Useage,
  83. Detail: v.Detail,
  84. AttributeRule: v.AttributeRule,
  85. CreatedAt: time.Now().Unix(),
  86. UpdatedAt: time.Now().Unix(),
  87. })
  88. continue
  89. }
  90. values := map[string]interface{}{
  91. "c2_id": v.C2Id,
  92. "useage": v.Useage,
  93. "detail": v.Detail,
  94. "attribute_rule": v.AttributeRule,
  95. "updated_at": time.Now().Unix(),
  96. }
  97. if err := model.NewStyleItemModel().Update(tx.Where("id = ?", v.Id), values); err != nil {
  98. return reply, errors.SystemError
  99. }
  100. }
  101. if len(insert) != 0 {
  102. if err := model.NewStyleItemModel().Insert(tx, insert); err != nil {
  103. return reply, errors.SystemError
  104. }
  105. }
  106. return reply, nil
  107. }
  108. func DeleteStyleItem(ctx context.Context, req *v1.DeleteStyleItemRequest) (reply *v1.EmptyReply, err error) {
  109. reply = &v1.EmptyReply{}
  110. defer func() {
  111. if r := recover(); r != nil {
  112. err = fmt.Errorf("%+v", r)
  113. e := &status.Status{}
  114. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  115. logger.Error("err",
  116. zap.String("system_err", err.Error()),
  117. zap.Stack("stacktrace"))
  118. }
  119. }
  120. }()
  121. if req.Id == 0 {
  122. return reply, errors.ParamsError
  123. }
  124. if err := model.NewStyleItemModel().Delete(database.DB().Where("id = ?", req.Id)); err != nil {
  125. return reply, errors.SystemError
  126. }
  127. return reply, nil
  128. }