event_check.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "time"
  7. "go.uber.org/zap"
  8. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  9. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  10. "gitee.com/xuyiping_admin/pkg/xerr"
  11. )
  12. // EventMatingCheckBatchModel 批量配种
  13. type EventMatingCheckBatchModel struct {
  14. CowList []*model.Cow
  15. FrozenSemen *model.FrozenSemen
  16. OperationUser *model.SystemUser
  17. }
  18. // PregnantCheckBatchModel 批量孕检
  19. type PregnantCheckBatchModel struct {
  20. EventPregnancyCheck *model.EventPregnantCheck
  21. Cow *model.Cow
  22. OperationUser *model.SystemUser
  23. PregnantCheckAt int32 // 孕检日期
  24. PregnantCheckResult pasturePb.PregnantCheckResult_Kind // 孕检结果
  25. PregnantCheckMethod pasturePb.PregnantCheckMethod_Kind // 孕检方式
  26. Remarks string
  27. LastMating *model.EventMating
  28. }
  29. // MatingTimes 更新配次
  30. type MatingTimes struct {
  31. Mt int32
  32. CowId int64
  33. EventMatingId int64
  34. }
  35. type AbortionCheckBatchModel struct {
  36. EventAbortion *model.EventAbortion
  37. Cow *model.Cow
  38. OperationUser *model.SystemUser
  39. }
  40. func (s *StoreEntry) EnterCheck(ctx context.Context, req *pasturePb.EventEnterRequest) error {
  41. var count int64
  42. if err := s.DB.Model(new(model.Cow)).Where("ear_number = ?", req.EarNumber).Count(&count).Error; err != nil {
  43. return xerr.WithStack(err)
  44. }
  45. if count > 0 {
  46. return xerr.Custom("该牛只已存在")
  47. }
  48. return nil
  49. }
  50. func (s *StoreEntry) MatingCreateCheck(ctx context.Context, pastureId int64, req *pasturePb.EventMating) (*EventMatingCheckBatchModel, error) {
  51. if len(req.CowIds) <= 0 {
  52. return nil, xerr.Custom("请选择相关牛只")
  53. }
  54. cowIds := make([]int64, 0)
  55. for _, v := range req.CowIds {
  56. cowIds = append(cowIds, int64(v))
  57. }
  58. if len(cowIds) > 20 {
  59. return nil, xerr.Custom("最多只能选择50只牛只")
  60. }
  61. cowList, err := s.GetCowInfoByCowIds(ctx, pastureId, cowIds)
  62. if err != nil {
  63. return nil, xerr.WithStack(err)
  64. }
  65. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  66. if err != nil {
  67. return nil, xerr.WithStack(err)
  68. }
  69. frozenSemen := &model.FrozenSemen{}
  70. if err = s.DB.Where("bull_id = ?", req.FrozenSemenNumber).
  71. First(frozenSemen).Error; err != nil {
  72. return nil, xerr.Custom("未找到冻精信息")
  73. }
  74. if frozenSemen.Quantity < req.FrozenSemenCount {
  75. return nil, xerr.Custom("冻精数量不足")
  76. }
  77. for _, cow := range cowList {
  78. if cow.Sex != pasturePb.Genders_Female {
  79. return nil, xerr.Customf("牛只: %d,不是母牛", cow.Id)
  80. }
  81. if int64(req.MatingAt) < cow.LastMatingAt {
  82. return nil, xerr.Customf("牛只: %d,最近一次配种时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastMatingAt, req.MatingAt)
  83. }
  84. if int64(req.MatingAt) < cow.LastPregnantCheckAt {
  85. return nil, xerr.Customf("牛只: %d,最近一次孕检时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastPregnantCheckAt, req.MatingAt)
  86. }
  87. if int64(req.MatingAt) < cow.LastAbortionAt {
  88. return nil, xerr.Customf("牛只: %d,最近一次流产时间: %d,不能小于本次配种时间: %d", cow.Id, cow.LastAbortionAt, req.MatingAt)
  89. }
  90. if int64(req.MatingAt) < cow.BirthAt {
  91. return nil, xerr.Customf("牛只: %d,出生时间: %d,不能小于本次配种时间: %d", cow.Id, cow.BirthAt, req.MatingAt)
  92. }
  93. if cow.BreedStatus == pasturePb.BreedStatus_Pregnant || cow.BreedStatus == pasturePb.BreedStatus_No_Mating {
  94. return nil, xerr.Customf("牛只: %d,当前状态为: %s,不能进行配种", cow.Id, cow.BreedStatus.String())
  95. }
  96. }
  97. return &EventMatingCheckBatchModel{
  98. CowList: cowList,
  99. FrozenSemen: frozenSemen,
  100. OperationUser: operationUser,
  101. }, nil
  102. }
  103. func (s *StoreEntry) PregnantCheckDataCheck(ctx context.Context, pastureId int64, req *pasturePb.EventPregnantCheckBatch) ([]*PregnantCheckBatchModel, error) {
  104. if len(req.Items) <= 0 {
  105. return nil, xerr.Custom("请选择相关牛只数据")
  106. }
  107. pregnantCheckBatchModelList := make([]*PregnantCheckBatchModel, 0)
  108. cowInfo := &model.Cow{}
  109. var err error
  110. for _, item := range req.Items {
  111. cowInfo, err = s.GetCowInfoByCowId(ctx, pastureId, int64(item.CowId))
  112. if err != nil {
  113. return nil, xerr.WithStack(err)
  114. }
  115. if cowInfo.Sex != pasturePb.Genders_Female {
  116. return nil, xerr.Customf("牛只: %d,不是母牛", cowInfo.Id)
  117. }
  118. operationUser, err := s.GetSystemUserById(ctx, int64(item.OperationId))
  119. if err != nil {
  120. zaplog.Error("PregnantCheckDataCheck", zap.Any("id", item.OperationId), zap.Any("error", err.Error()))
  121. return nil, xerr.Customf("获取操作人员信息失败")
  122. }
  123. // 过滤掉没有配种状态的牛只
  124. if cowInfo.BreedStatus != pasturePb.BreedStatus_Breeding && cowInfo.BreedStatus != pasturePb.BreedStatus_Pregnant {
  125. return nil, xerr.Customf("牛只: %s 未参加配种,不能进行孕检", cowInfo.EarNumber)
  126. }
  127. if cowInfo.LastMatingAt <= 0 {
  128. return nil, xerr.Customf("牛只: %s,最近一次配种数据异常", cowInfo.EarNumber)
  129. }
  130. itemEventPregnantCheck, err := s.FindEventPregnantCheckIsExIstByCowId(ctx, cowInfo)
  131. if err != nil {
  132. return nil, xerr.WithStack(err)
  133. }
  134. if itemEventPregnantCheck.Id <= 0 {
  135. return nil, xerr.Customf("未发现该牛只: %s 孕检数据", cowInfo.EarNumber)
  136. }
  137. lastEventMating, err := s.FindLastEventMatingByCowId(ctx, pastureId, cowInfo.Id)
  138. if err != nil {
  139. return nil, xerr.WithStack(err)
  140. }
  141. if lastEventMating == nil || lastEventMating.Status == pasturePb.IsShow_No {
  142. return nil, xerr.Customf("未发现该牛只: %s 配种数据", cowInfo.EarNumber)
  143. }
  144. pregnantCheckBatchModelList = append(pregnantCheckBatchModelList, &PregnantCheckBatchModel{
  145. Cow: cowInfo,
  146. OperationUser: operationUser,
  147. PregnantCheckAt: item.PregnantCheckAt,
  148. PregnantCheckMethod: item.PregnantCheckMethod,
  149. PregnantCheckResult: item.PregnantCheckResult,
  150. Remarks: item.Remarks,
  151. EventPregnancyCheck: itemEventPregnantCheck,
  152. LastMating: lastEventMating,
  153. })
  154. }
  155. return pregnantCheckBatchModelList, nil
  156. }
  157. func (s *StoreEntry) EstrusCheckDataCheck(ctx context.Context, userModel *model.UserModel, items []*pasturePb.EventNaturalEstrusItems) ([]*model.EventEstrus, []*model.EventMating, error) {
  158. eventEstrusList := make([]*model.EventEstrus, 0)
  159. eventMatingList := make([]*model.EventMating, 0)
  160. unMatingReasonsMap := s.UnMatingReasonsMap()
  161. for _, item := range items {
  162. cowInfo, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(item.CowId))
  163. if err != nil {
  164. return nil, nil, xerr.Custom("牛只信息不存在")
  165. }
  166. if cowInfo.Sex != pasturePb.Genders_Female {
  167. return nil, nil, xerr.Custom("该牛只不是母牛")
  168. }
  169. existsEventEstrus, isExists, err := s.FindEventEstrusByCowId(ctx, userModel.AppPasture.Id, int64(item.CowId))
  170. if err != nil {
  171. return nil, nil, xerr.WithStack(err)
  172. }
  173. // 如果存在,并且是脖环揭发则跳过
  174. if isExists {
  175. if existsEventEstrus.ExposeEstrusType == pasturePb.ExposeEstrusType_Neck_Ring {
  176. zaplog.Info("EventNaturalEstrusBatch", zap.Any("existsEventEstrus", existsEventEstrus), zap.Any("item", item))
  177. continue
  178. }
  179. realityDay := time.Unix(existsEventEstrus.RealityDay, 0).Format(model.LayoutDate2)
  180. planDay := time.Unix(int64(item.EstrusAt), 0).Format(model.LayoutDate2)
  181. if realityDay == planDay {
  182. return nil, nil, xerr.Customf("该牛只:%d,今天已经提交过发情数据", item.CowId)
  183. }
  184. }
  185. operationUser, _ := s.GetSystemUserById(ctx, int64(item.OperationId))
  186. item.OperationName = operationUser.Name
  187. newEventEstrus := model.NewEventEstrus(userModel.AppPasture.Id, cowInfo, pasturePb.ExposeEstrusType_Natural_Estrus,
  188. pasturePb.IsShow_Ok, item.IsMating, item.EstrusAt, operationUser, userModel.SystemUser)
  189. fmt.Println("item.IsMating", item.IsMating, item.UnMatingReasonsKind)
  190. if item.IsMating == pasturePb.IsShow_Ok {
  191. newEventMating := model.NewEventMating(userModel.AppPasture.Id, cowInfo, time.Now().Unix(), pasturePb.ExposeEstrusType_Natural_Estrus)
  192. eventMatingList = append(eventMatingList, newEventMating)
  193. } else {
  194. newEventEstrus.UnMatingReasonsKind = item.UnMatingReasonsKind
  195. newEventEstrus.UnMatingReasonsName = unMatingReasonsMap[item.UnMatingReasonsKind]
  196. }
  197. newEventEstrus.Remarks = item.Remarks
  198. eventEstrusList = append(eventEstrusList, newEventEstrus)
  199. }
  200. return eventEstrusList, eventMatingList, nil
  201. }
  202. func (s *StoreEntry) AbortionEventDataCheck(ctx context.Context, userModel *model.UserModel, items []*pasturePb.EventAbortionItem) ([]*AbortionCheckBatchModel, error) {
  203. abortionCheckBatchModelList := make([]*AbortionCheckBatchModel, 0)
  204. for _, item := range items {
  205. cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(item.CowId))
  206. if err != nil {
  207. return nil, xerr.WithStack(err)
  208. }
  209. if cow.Sex != pasturePb.Genders_Female {
  210. return nil, xerr.Customf("牛只: %s,不是母牛", cow.EarNumber)
  211. }
  212. if cow.IsPregnant != pasturePb.IsShow_Ok {
  213. return nil, xerr.Customf("牛只: %s,不是怀孕状态", cow.EarNumber)
  214. }
  215. if cow.BreedStatus != pasturePb.BreedStatus_Pregnant {
  216. return nil, xerr.Customf("牛只: %s,不是怀孕状态", cow.EarNumber)
  217. }
  218. operationUser, err := s.GetSystemUserById(ctx, int64(item.OperationId))
  219. if err != nil {
  220. return nil, xerr.WithStack(err)
  221. }
  222. item.OperationName = operationUser.Name
  223. item.AbortionReasonsName = s.AbortionReasonsMap()[item.AbortionReasons]
  224. newEventAbortion := model.NewEventAbortion(userModel.AppPasture.Id, cow, item, pasturePb.IsShow_No)
  225. abortionCheckBatchModelList = append(abortionCheckBatchModelList, &AbortionCheckBatchModel{
  226. EventAbortion: newEventAbortion,
  227. Cow: cow,
  228. OperationUser: operationUser,
  229. })
  230. }
  231. return abortionCheckBatchModelList, nil
  232. }