item.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package maintainance
  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 ItemList(ctx context.Context, req *v1.ItemListRequest) (reply *v1.ItemListReply, err error) {
  16. reply = &v1.ItemListReply{}
  17. defer func() {
  18. if r := recover(); r != nil {
  19. err = fmt.Errorf("%+v", r)
  20. e := &status.Status{}
  21. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  22. logger.Error("err",
  23. zap.String("system_err", err.Error()),
  24. zap.Stack("stacktrace"))
  25. }
  26. }
  27. }()
  28. if req.ItemType < 1 || req.ItemType > 3 {
  29. return reply, errors.ParamsError
  30. }
  31. list, err := model.NewMaintainDetailModel().MaintainDetailList(database.DB().Where("t1.style_id = ?", req.StyleId))
  32. if err != nil && err != gorm.ErrRecordNotFound {
  33. return nil, errors.SystemError
  34. }
  35. itemList, err := model.NewMaintainItemModel().
  36. List(database.DB().Where("item_type = ?", req.ItemType), "id, item_id, item_name")
  37. if err != nil {
  38. return nil, errors.SystemError
  39. }
  40. exist := func(itemId int64, list []model.MaintainDetailItem) bool {
  41. if len(list) == 0 {
  42. return false
  43. }
  44. for _, v := range list {
  45. if itemId == v.ItemId {
  46. return true
  47. }
  48. }
  49. return false
  50. }
  51. reply.List = make([]*v1.ItemListReply_Item, 0, len(itemList))
  52. for _, v := range itemList {
  53. if !exist(v.ItemId, list) {
  54. reply.List = append(reply.List, &v1.ItemListReply_Item{
  55. ItemId: v.ItemId,
  56. ItemName: v.ItemName,
  57. })
  58. }
  59. }
  60. return reply, nil
  61. }
  62. func TemplateItemList(ctx context.Context, req *v1.TemplateItemListRequest) (reply *v1.ItemListReply, err error) {
  63. reply = &v1.ItemListReply{}
  64. defer func() {
  65. if r := recover(); r != nil {
  66. err = fmt.Errorf("%+v", r)
  67. e := &status.Status{}
  68. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  69. logger.Error("err",
  70. zap.String("system_err", err.Error()),
  71. zap.Stack("stacktrace"))
  72. }
  73. }
  74. }()
  75. if req.ItemType < 1 || req.ItemType > 3 {
  76. return reply, errors.ParamsError
  77. }
  78. list, err := model.NewMaintainDetailTemplateModel().ListByItem(database.DB().Where("t1.tid = ?", req.Tid))
  79. if err != nil && err != gorm.ErrRecordNotFound {
  80. return nil, errors.SystemError
  81. }
  82. itemList, err := model.NewMaintainItemModel().
  83. List(database.DB().Where("item_type = ?", req.ItemType), "id, item_id, item_name")
  84. if err != nil {
  85. return nil, errors.SystemError
  86. }
  87. exist := func(itemId int64, list []model.MaintenanceDetailTemplateItem) bool {
  88. if len(list) == 0 {
  89. return false
  90. }
  91. for _, v := range list {
  92. if itemId == v.ItemId {
  93. return true
  94. }
  95. }
  96. return false
  97. }
  98. reply.List = make([]*v1.ItemListReply_Item, 0, len(itemList))
  99. for _, v := range itemList {
  100. if !exist(v.ItemId, list) {
  101. reply.List = append(reply.List, &v1.ItemListReply_Item{
  102. ItemId: v.ItemId,
  103. ItemName: v.ItemName,
  104. })
  105. }
  106. }
  107. return reply, nil
  108. }