pasture_sync_service.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "kpt-tmr-group/model"
  6. "kpt-tmr-group/pkg/xerr"
  7. operationPb "kpt-tmr-group/proto/go/backend/operation"
  8. "gorm.io/gorm"
  9. )
  10. const (
  11. FeedCategory = "feed"
  12. FeedCategoryDelete = "feed_delete"
  13. CowCategory = "cow"
  14. CowCategoryDelete = "cow_delete"
  15. )
  16. func (s *StoreEntry) CategorySyncData(ctx context.Context, req *operationPb.CategorySyncRequest) error {
  17. pastureDetail, err := s.GetGroupPastureListById(ctx, int64(req.PastureId))
  18. if err != nil {
  19. return xerr.WithStack(err)
  20. }
  21. switch req.KeyWord {
  22. case FeedCategory:
  23. history := &model.ForageCategory{}
  24. if err = s.DB.Model(new(model.ForageCategory)).
  25. Where("pasture_id = ?", req.PastureId).
  26. Where("data_id = ?", req.Id).
  27. Where("data_source = ? ", operationPb.DataSource_FROM_PASTURE).
  28. First(history).Error; err != nil {
  29. if !errors.Is(err, gorm.ErrRecordNotFound) {
  30. return xerr.WithStack(err)
  31. }
  32. }
  33. if history.IsShow == operationPb.IsShow_OK && history.Id > 0 {
  34. if err = s.DB.Model(new(model.ForageCategory)).Where("id = ?", history.Id).Updates(map[string]interface{}{
  35. "number": req.Number,
  36. "is_show": req.IsShow,
  37. "parent_id": req.ParentId,
  38. "parent_name": req.ParentName,
  39. "name": req.Name,
  40. }).Error; err != nil {
  41. return xerr.WithStack(err)
  42. }
  43. }
  44. newFeedData := model.NewPastureForageCategory(req, pastureDetail)
  45. if err = s.DB.Model(new(model.ForageCategory)).Create(newFeedData).Error; err != nil {
  46. return xerr.WithStack(err)
  47. }
  48. case CowCategory:
  49. history := &model.CattleCategory{}
  50. if err = s.DB.Model(new(model.CattleCategory)).
  51. Where("pasture_id = ?", req.PastureId).
  52. Where("data_id = ?", req.Id).
  53. Where("data_source = ? ", operationPb.DataSource_FROM_PASTURE).
  54. First(history).Error; err != nil {
  55. if !errors.Is(err, gorm.ErrRecordNotFound) {
  56. return xerr.WithStack(err)
  57. }
  58. }
  59. if history.IsShow == operationPb.IsShow_OK && history.Id > 0 {
  60. if err = s.DB.Model(new(model.CattleCategory)).Where("id = ?", history.Id).Updates(map[string]interface{}{
  61. "number": req.Number,
  62. "is_show": req.IsShow,
  63. "parent_id": req.ParentId,
  64. "parent_name": req.ParentName,
  65. "name": req.Name,
  66. }).Error; err != nil {
  67. return xerr.WithStack(err)
  68. }
  69. }
  70. newCattleData := model.NewPastureCattleCategory(req, pastureDetail)
  71. if err = s.DB.Model(new(model.CattleCategory)).Create(newCattleData).Error; err != nil {
  72. return xerr.WithStack(err)
  73. }
  74. }
  75. return nil
  76. }
  77. func (s *StoreEntry) CategoryDeleteData(ctx context.Context, req *operationPb.CategoryDeleteRequest) error {
  78. var modelValue interface{}
  79. switch req.KeyWord {
  80. case FeedCategoryDelete:
  81. modelValue = new(model.ForageCategory)
  82. case CowCategoryDelete:
  83. modelValue = new(model.CattleCategory)
  84. }
  85. if modelValue == nil {
  86. return nil
  87. }
  88. if err := s.DB.Model(modelValue).Where("data_id = ?", req.DataId).
  89. Where("pasture_id = ?", req.PastureId).Where("data_source = ?", operationPb.DataSource_FROM_PASTURE).
  90. Updates(map[string]interface{}{
  91. "is_delete": operationPb.IsShow_OK,
  92. }).Error; err != nil {
  93. return xerr.WithStack(err)
  94. }
  95. return nil
  96. }