test_service.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package backend
  2. import (
  3. "context"
  4. "kpt-pasture/model"
  5. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  6. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  7. "go.uber.org/zap"
  8. "gitee.com/xuyiping_admin/pkg/xerr"
  9. )
  10. func (s *StoreEntry) CowNeckRingNumberBound(ctx context.Context, pagination *pasturePb.PaginationModel) error {
  11. userModel, err := s.GetUserModel(ctx)
  12. if err != nil {
  13. return xerr.WithStack(err)
  14. }
  15. lastNeckRing := &model.NeckRing{}
  16. if err = s.DB.Model(new(model.NeckRing)).
  17. Order("cow_id desc").
  18. First(lastNeckRing).Error; err != nil {
  19. return xerr.WithStack(err)
  20. }
  21. cowList := make([]*model.Cow, 0)
  22. if err = s.DB.Model(new(model.Cow)).
  23. Where("id > ?", lastNeckRing.CowId).
  24. Limit(int(pagination.PageSize)).
  25. Offset(int(pagination.PageOffset)).
  26. Find(&cowList).Error; err != nil {
  27. return xerr.WithStack(err)
  28. }
  29. newNeckRingList := make([]*model.NeckRing, 0)
  30. newNeckRingBindLogList := make([]*model.NeckRingBindLog, 0)
  31. for _, v := range cowList {
  32. newNeckRing := model.NewNeckRing(userModel.AppPasture.Id, v.NeckRingNumber, v.Id, userModel.SystemUser)
  33. newNeckRingList = append(newNeckRingList, newNeckRing)
  34. newNeckRingBindLog := model.NewNeckRingBindLog(userModel.AppPasture.Id, v.NeckRingNumber, v.Id, userModel.SystemUser)
  35. newNeckRingBindLogList = append(newNeckRingBindLogList, newNeckRingBindLog)
  36. }
  37. if err = s.DB.Model(new(model.NeckRing)).
  38. Create(newNeckRingList).Error; err != nil {
  39. zaplog.Error("CowNeckRingNumberBound-NewNeckRing", zap.Any("error", err))
  40. }
  41. if err = s.DB.Model(new(model.NeckRingBindLog)).
  42. Create(newNeckRingBindLogList).Error; err != nil {
  43. zaplog.Error("CowNeckRingNumberBound-NeckRingBindLog", zap.Any("error", err))
  44. }
  45. return nil
  46. }
  47. func (s *StoreEntry) CowNeckRingNumberBound2(ctx context.Context, pagination *pasturePb.PaginationModel) error {
  48. userModel, err := s.GetUserModel(ctx)
  49. if err != nil {
  50. return xerr.WithStack(err)
  51. }
  52. lastNeckRing := &model.NeckRing{}
  53. if err = s.DB.Model(new(model.NeckRing)).
  54. Order("cow_id desc").
  55. First(lastNeckRing).Error; err != nil {
  56. return xerr.WithStack(err)
  57. }
  58. cowList := make([]*model.Cow, 0)
  59. if err = s.DB.Model(new(model.Cow)).
  60. Where("neck_ring_number != ?", "").
  61. Limit(int(pagination.PageSize)).
  62. Offset(int(pagination.PageOffset)).
  63. Find(&cowList).Error; err != nil {
  64. return xerr.WithStack(err)
  65. }
  66. newNeckRingList := make([]*model.NeckRing, 0)
  67. newNeckRingBindLogList := make([]*model.NeckRingBindLog, 0)
  68. for _, v := range cowList {
  69. newNeckRing := model.NewNeckRing(userModel.AppPasture.Id, v.NeckRingNumber, v.Id, userModel.SystemUser)
  70. newNeckRingList = append(newNeckRingList, newNeckRing)
  71. newNeckRingBindLog := model.NewNeckRingBindLog(userModel.AppPasture.Id, v.NeckRingNumber, v.Id, userModel.SystemUser)
  72. newNeckRingBindLogList = append(newNeckRingBindLogList, newNeckRingBindLog)
  73. }
  74. if err = s.DB.Model(new(model.NeckRing)).
  75. Create(newNeckRingList).Error; err != nil {
  76. zaplog.Error("CowNeckRingNumberBound-NewNeckRing", zap.Any("error", err))
  77. }
  78. if err = s.DB.Model(new(model.NeckRingBindLog)).
  79. Create(newNeckRingBindLogList).Error; err != nil {
  80. zaplog.Error("CowNeckRingNumberBound-NeckRingBindLog", zap.Any("error", err))
  81. }
  82. return nil
  83. }
  84. func (s *StoreEntry) UpdateCowPen(ctx context.Context, pagination *pasturePb.PaginationModel) error {
  85. cowList := make([]*model.Cow, 0)
  86. if err := s.DB.Model(new(model.Cow)).
  87. Where("pen_name = ?", "").
  88. Limit(int(pagination.PageSize)).
  89. Offset(int(pagination.PageOffset)).
  90. Find(&cowList).Error; err != nil {
  91. return xerr.WithStack(err)
  92. }
  93. penMap := s.PenMap(ctx, 1)
  94. for _, v := range cowList {
  95. pen := penMap[v.PenId]
  96. if pen == nil {
  97. continue
  98. }
  99. if err := s.DB.Model(new(model.Cow)).
  100. Where("id = ?", v.Id).
  101. Update("pen_name", pen.Name).Error; err != nil {
  102. zaplog.Error("UpdateCowPen", zap.Any("error", err))
  103. }
  104. }
  105. return nil
  106. }