event_check.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. type EventCheckModel struct {
  9. CowList []*model.Cow
  10. CurrentUser *model.SystemUser
  11. OperationUser *model.SystemUser
  12. }
  13. func (s *StoreEntry) MatingCreateCheck(ctx context.Context, req *pasturePb.EventMating) (*EventCheckModel, error) {
  14. if len(req.CowIds) <= 0 {
  15. return nil, xerr.Custom("请选择相关牛只")
  16. }
  17. cowList, err := s.ParseCowIds(ctx, req.CowIds)
  18. if err != nil {
  19. return nil, xerr.WithStack(err)
  20. }
  21. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  22. if err != nil {
  23. return nil, xerr.WithStack(err)
  24. }
  25. currentUser, err := s.GetCurrentSystemUser(ctx)
  26. if err != nil {
  27. return nil, xerr.Customf("获取当前用户失败: %s", err.Error())
  28. }
  29. for _, cow := range cowList {
  30. if cow.Sex != pasturePb.Genders_Female {
  31. return nil, xerr.Customf("牛只: %d,不是母牛", cow.Id)
  32. }
  33. if int64(req.MatingAt) < cow.LastMatingAt {
  34. return nil, xerr.Customf("牛只: %d,最近一次配种时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastMatingAt, req.MatingAt)
  35. }
  36. if int64(req.MatingAt) < cow.LastPregnantCheckAt {
  37. return nil, xerr.Customf("牛只: %d,最近一次孕检时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastPregnantCheckAt, req.MatingAt)
  38. }
  39. if int64(req.MatingAt) < cow.LastAbortionAt {
  40. return nil, xerr.Customf("牛只: %d,最近一次流产时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastAbortionAt, req.MatingAt)
  41. }
  42. if int64(req.MatingAt) < cow.BirthAt {
  43. return nil, xerr.Customf("牛只: %d,出生时间: %d,不能小于本次配种时间: %d", cow.Id, cow.BirthAt, req.MatingAt)
  44. }
  45. if cow.BreedStatus == pasturePb.BreedStatus_Pregnant || cow.BreedStatus == pasturePb.BreedStatus_No_Mating {
  46. return nil, xerr.Customf("牛只: %d,当前状态为: %s,不能进行配种", cow.Id, cow.BreedStatus.String())
  47. }
  48. }
  49. return &EventCheckModel{
  50. CowList: cowList,
  51. CurrentUser: currentUser,
  52. OperationUser: operationUser,
  53. }, nil
  54. }
  55. func (s *StoreEntry) PregnantCheckCreateCheck(ctx context.Context, req *pasturePb.EventPregnantCheck) (*EventCheckModel, error) {
  56. if len(req.CowId) <= 0 {
  57. return nil, xerr.Custom("请选择相关牛只")
  58. }
  59. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  60. if err != nil {
  61. return nil, xerr.WithStack(err)
  62. }
  63. currentUser, err := s.GetCurrentSystemUser(ctx)
  64. if err != nil {
  65. return nil, xerr.Customf("获取当前用户失败: %s", err.Error())
  66. }
  67. cowList, err := s.ParseCowIds(ctx, req.CowId)
  68. if err != nil {
  69. return nil, xerr.WithStack(err)
  70. }
  71. for _, cow := range cowList {
  72. // 过滤掉没有配种状态的牛只
  73. if cow.BreedStatus != pasturePb.BreedStatus_Breeding {
  74. return nil, xerr.Customf("牛只: %d,当前状态为: %s,不能进行孕检", cow.Id, cow.BreedStatus.String())
  75. }
  76. if int64(req.PregnantCheckAt) < cow.LastMatingAt {
  77. return nil, xerr.Customf("牛只: %d,最近一次配种时间: %d,不能小于本次孕检时间: %d", cow.Id, req.PregnantCheckAt)
  78. }
  79. if int64(req.PregnantCheckAt) < cow.LastCalvingAt {
  80. return nil, xerr.Customf("牛只: %d,最近一次产犊时间: %d,不能小于本次孕检时间: %d", cow.Id, cow.LastCalvingAt, req.PregnantCheckAt)
  81. }
  82. }
  83. return &EventCheckModel{
  84. CowList: cowList,
  85. CurrentUser: currentUser,
  86. OperationUser: operationUser,
  87. }, nil
  88. }