event_check.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/xerr"
  7. )
  8. // EventCheckBatchModel 批量事件
  9. type EventCheckBatchModel struct {
  10. CowList []*model.Cow
  11. CurrentUser *model.SystemUser
  12. OperationUser *model.SystemUser
  13. }
  14. // PregnantCheckBatchModel 批量孕检
  15. type PregnantCheckBatchModel struct {
  16. EventPregnancyCheck *model.EventPregnantCheck
  17. Cow *model.Cow
  18. OperationUser *model.SystemUser
  19. PregnantCheckAt int32 // 孕检日期
  20. PregnantCheckResult pasturePb.PregnantCheckResult_Kind // 孕检结果
  21. PregnantCheckMethod pasturePb.PregnantCheckMethod_Kind // 孕检方式
  22. Remarks string
  23. }
  24. // MatingTimes 更新配次
  25. type MatingTimes struct {
  26. Mt int32
  27. CowId int64
  28. EventMatingId int64
  29. }
  30. func (s *StoreEntry) MatingCreateCheck(ctx context.Context, req *pasturePb.EventMating) (*EventCheckBatchModel, error) {
  31. if len(req.CowIds) <= 0 {
  32. return nil, xerr.Custom("请选择相关牛只")
  33. }
  34. cowList, err := s.ParseCowIds(ctx, req.CowIds)
  35. if err != nil {
  36. return nil, xerr.WithStack(err)
  37. }
  38. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  39. if err != nil {
  40. return nil, xerr.WithStack(err)
  41. }
  42. currentUser, err := s.GetCurrentSystemUser(ctx)
  43. if err != nil {
  44. return nil, xerr.Customf("获取当前用户失败: %s", err.Error())
  45. }
  46. for _, cow := range cowList {
  47. if cow.Sex != pasturePb.Genders_Female {
  48. return nil, xerr.Customf("牛只: %d,不是母牛", cow.Id)
  49. }
  50. if int64(req.MatingAt) < cow.LastMatingAt {
  51. return nil, xerr.Customf("牛只: %d,最近一次配种时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastMatingAt, req.MatingAt)
  52. }
  53. if int64(req.MatingAt) < cow.LastPregnantCheckAt {
  54. return nil, xerr.Customf("牛只: %d,最近一次孕检时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastPregnantCheckAt, req.MatingAt)
  55. }
  56. if int64(req.MatingAt) < cow.LastAbortionAt {
  57. return nil, xerr.Customf("牛只: %d,最近一次流产时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastAbortionAt, req.MatingAt)
  58. }
  59. if int64(req.MatingAt) < cow.BirthAt {
  60. return nil, xerr.Customf("牛只: %d,出生时间: %d,不能小于本次配种时间: %d", cow.Id, cow.BirthAt, req.MatingAt)
  61. }
  62. if cow.BreedStatus == pasturePb.BreedStatus_Pregnant || cow.BreedStatus == pasturePb.BreedStatus_No_Mating {
  63. return nil, xerr.Customf("牛只: %d,当前状态为: %s,不能进行配种", cow.Id, cow.BreedStatus.String())
  64. }
  65. }
  66. return &EventCheckBatchModel{
  67. CowList: cowList,
  68. CurrentUser: currentUser,
  69. OperationUser: operationUser,
  70. }, nil
  71. }
  72. func (s *StoreEntry) PregnantCheckDataCheck(ctx context.Context, req *pasturePb.EventPregnantCheckBatch) ([]*PregnantCheckBatchModel, error) {
  73. if len(req.Item) <= 0 {
  74. return nil, xerr.Custom("请选择相关牛只")
  75. }
  76. pregnantCheckBatchModelList := make([]*PregnantCheckBatchModel, 0)
  77. for _, pregnantCheckData := range req.Item {
  78. operationUser, err := s.GetSystemUserById(ctx, int64(pregnantCheckData.OperationId))
  79. if err != nil {
  80. return nil, xerr.WithStack(err)
  81. }
  82. cow, err := s.GetCowInfoByCowId(ctx, int64(pregnantCheckData.CowId))
  83. if err != nil {
  84. return nil, xerr.WithStack(err)
  85. }
  86. // 过滤掉没有配种状态的牛只
  87. if cow.BreedStatus != pasturePb.BreedStatus_Breeding {
  88. return nil, xerr.Customf("牛只: %d,当前状态为: %s,不能进行孕检", cow.Id, cow.BreedStatus.String())
  89. }
  90. pregnantCheckBatchModelList = append(pregnantCheckBatchModelList, &PregnantCheckBatchModel{
  91. Cow: cow,
  92. OperationUser: operationUser,
  93. PregnantCheckAt: pregnantCheckData.PregnantCheckAt,
  94. PregnantCheckMethod: pregnantCheckData.PregnantCheckMethod,
  95. PregnantCheckResult: pregnantCheckData.PregnantCheckResult,
  96. Remarks: pregnantCheckData.Remarks,
  97. })
  98. }
  99. return pregnantCheckBatchModelList, nil
  100. }
  101. func (s *StoreEntry) EventEstrusCheck(ctx context.Context, req *pasturePb.EventEstrus) (*model.EventEstrus, error) {
  102. if req.CowId <= 0 {
  103. return nil, xerr.Custom("请选择相关牛只")
  104. }
  105. cow, err := s.GetCowInfoByCowId(ctx, int64(req.CowId))
  106. if err != nil {
  107. return nil, xerr.WithStack(err)
  108. }
  109. currentUser, err := s.GetCurrentSystemUser(ctx)
  110. if err != nil {
  111. return nil, xerr.Custom("获取当前登录用户失败")
  112. }
  113. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  114. if err != nil {
  115. return nil, xerr.WithStack(err)
  116. }
  117. return model.NewEventEstrus(cow, currentUser, operationUser, req), nil
  118. }