event_check.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package backend
  2. import (
  3. "context"
  4. "kpt-pasture/model"
  5. "time"
  6. "go.uber.org/zap"
  7. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  8. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  9. "gitee.com/xuyiping_admin/pkg/xerr"
  10. )
  11. // EventMatingCheckBatchModel 批量配种
  12. type EventMatingCheckBatchModel struct {
  13. CowList []*model.Cow
  14. FrozenSemen *model.FrozenSemen
  15. OperationUser *model.SystemUser
  16. }
  17. // PregnantCheckBatchModel 批量孕检
  18. type PregnantCheckBatchModel struct {
  19. EventPregnancyCheck *model.EventPregnantCheck
  20. Cow *model.Cow
  21. OperationUser *model.SystemUser
  22. PregnantCheckAt int32 // 孕检日期
  23. PregnantCheckResult pasturePb.PregnantCheckResult_Kind // 孕检结果
  24. PregnantCheckMethod pasturePb.PregnantCheckMethod_Kind // 孕检方式
  25. Remarks string
  26. LastMating *model.EventMating
  27. }
  28. // MatingTimes 更新配次
  29. type MatingTimes struct {
  30. Mt int32
  31. CowId int64
  32. EventMatingId int64
  33. }
  34. type AbortionCheckBatchModel struct {
  35. EventAbortion *model.EventAbortion
  36. Cow *model.Cow
  37. OperationUser *model.SystemUser
  38. IsLact pasturePb.IsShow_Kind
  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) > 50 {
  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. if len(req.Items) > 50 {
  108. return nil, xerr.Custom("一次性最多限制提交50牛数据")
  109. }
  110. pregnantCheckBatchModelList := make([]*PregnantCheckBatchModel, 0)
  111. cowInfo := &model.Cow{}
  112. var err error
  113. for _, item := range req.Items {
  114. cowInfo, err = s.GetCowInfoByEarNumber(ctx, pastureId, item.EarNumber)
  115. if err != nil {
  116. return nil, xerr.WithStack(err)
  117. }
  118. if cowInfo.Sex != pasturePb.Genders_Female {
  119. return nil, xerr.Customf("牛只: %d,不是母牛", cowInfo.Id)
  120. }
  121. operationUser, err := s.GetSystemUserById(ctx, int64(item.OperationId))
  122. if err != nil {
  123. zaplog.Error("PregnantCheckDataCheck", zap.Any("id", item.OperationId), zap.Any("error", err.Error()))
  124. return nil, xerr.Customf("获取操作人员信息失败")
  125. }
  126. // 过滤掉没有配种状态的牛只
  127. if cowInfo.BreedStatus != pasturePb.BreedStatus_Breeding && cowInfo.BreedStatus != pasturePb.BreedStatus_Pregnant {
  128. return nil, xerr.Customf("牛只: %s 未参加配种,不能进行孕检", cowInfo.EarNumber)
  129. }
  130. if cowInfo.LastMatingAt <= 0 {
  131. return nil, xerr.Customf("牛只: %s,最近一次配种数据异常", cowInfo.EarNumber)
  132. }
  133. itemEventPregnantCheck, err := s.FindEventPregnantCheckIsExIstByCowId(ctx, cowInfo)
  134. if err != nil {
  135. return nil, xerr.WithStack(err)
  136. }
  137. if itemEventPregnantCheck.Id <= 0 {
  138. return nil, xerr.Customf("未发现该牛只: %s 孕检数据", cowInfo.EarNumber)
  139. }
  140. lastEventMating, err := s.FindLastEventMatingByCowId(ctx, pastureId, cowInfo.Id)
  141. if err != nil {
  142. return nil, xerr.WithStack(err)
  143. }
  144. if lastEventMating == nil || lastEventMating.Status == pasturePb.IsShow_No {
  145. return nil, xerr.Customf("未发现该牛只: %s 配种数据", cowInfo.EarNumber)
  146. }
  147. pregnantCheckBatchModelList = append(pregnantCheckBatchModelList, &PregnantCheckBatchModel{
  148. Cow: cowInfo,
  149. OperationUser: operationUser,
  150. PregnantCheckAt: item.PregnantCheckAt,
  151. PregnantCheckMethod: item.PregnantCheckMethod,
  152. PregnantCheckResult: item.PregnantCheckResult,
  153. Remarks: item.Remarks,
  154. EventPregnancyCheck: itemEventPregnantCheck,
  155. LastMating: lastEventMating,
  156. })
  157. }
  158. return pregnantCheckBatchModelList, nil
  159. }
  160. func (s *StoreEntry) EstrusCheckDataCheck(ctx context.Context, userModel *model.UserModel, items []*pasturePb.EventNaturalEstrusItems) ([]*model.EventEstrus, []*model.EventMating, error) {
  161. eventEstrusList := make([]*model.EventEstrus, 0)
  162. eventMatingList := make([]*model.EventMating, 0)
  163. unMatingReasonsMap := s.UnMatingReasonsMap()
  164. for _, item := range items {
  165. cowInfo, err := s.GetCowInfoByEarNumber(ctx, userModel.AppPasture.Id, item.EarNumber)
  166. if err != nil {
  167. return nil, nil, xerr.Custom("牛只信息不存在")
  168. }
  169. if cowInfo.Sex != pasturePb.Genders_Female {
  170. return nil, nil, xerr.Custom("该牛只不是母牛")
  171. }
  172. if item.EstrusAt <= 0 {
  173. return nil, nil, xerr.Custom("发情时间不能为空")
  174. }
  175. if int64(item.EstrusAt) <= cowInfo.BirthAt {
  176. return nil, nil, xerr.Custom("发情时间不能小于出生时间")
  177. }
  178. if int64(item.EstrusAt) <= cowInfo.LastCalvingAt {
  179. return nil, nil, xerr.Custom("发情时间不能小于产犊时间")
  180. }
  181. existsEventEstrus, isExists, err := s.FindEventEstrusByCowId(ctx, userModel.AppPasture.Id, int64(item.CowId))
  182. if err != nil {
  183. return nil, nil, xerr.WithStack(err)
  184. }
  185. // 如果存在,并且是脖环揭发则跳过
  186. if isExists {
  187. if existsEventEstrus.ExposeEstrusType == pasturePb.ExposeEstrusType_Neck_Ring {
  188. zaplog.Info("EventNaturalEstrusBatch", zap.Any("existsEventEstrus", existsEventEstrus), zap.Any("item", item))
  189. continue
  190. }
  191. realityDay := time.Unix(existsEventEstrus.RealityDay, 0).Format(model.LayoutDate2)
  192. planDay := time.Unix(int64(item.EstrusAt), 0).Format(model.LayoutDate2)
  193. if realityDay == planDay {
  194. return nil, nil, xerr.Customf("该牛只:%d,今天已经提交过发情数据", item.CowId)
  195. }
  196. }
  197. operationUser, _ := s.GetSystemUserById(ctx, int64(item.OperationId))
  198. item.OperationName = operationUser.Name
  199. newEventEstrus := model.NewEventEstrus(userModel.AppPasture.Id, cowInfo, pasturePb.ExposeEstrusType_Natural_Estrus,
  200. pasturePb.IsShow_Ok, item.IsMating, item.EstrusAt, operationUser, userModel.SystemUser)
  201. if item.IsMating == pasturePb.IsShow_Ok {
  202. newEventMating := model.NewEventMating(userModel.AppPasture.Id, cowInfo, time.Now().Unix(), pasturePb.ExposeEstrusType_Natural_Estrus)
  203. eventMatingList = append(eventMatingList, newEventMating)
  204. } else {
  205. newEventEstrus.UnMatingReasonsKind = item.UnMatingReasonsKind
  206. newEventEstrus.UnMatingReasonsName = unMatingReasonsMap[item.UnMatingReasonsKind]
  207. }
  208. newEventEstrus.Remarks = item.Remarks
  209. eventEstrusList = append(eventEstrusList, newEventEstrus)
  210. }
  211. return eventEstrusList, eventMatingList, nil
  212. }
  213. func (s *StoreEntry) AbortionEventDataCheck(ctx context.Context, userModel *model.UserModel, items []*pasturePb.EventAbortionItem) ([]*AbortionCheckBatchModel, error) {
  214. if len(items) <= 0 {
  215. return nil, xerr.Custom("请选择相关数据")
  216. }
  217. if len(items) > 50 {
  218. return nil, xerr.Custom("一次性最多限制提交50牛数据")
  219. }
  220. abortionCheckBatchModelList := make([]*AbortionCheckBatchModel, 0)
  221. for _, item := range items {
  222. cow, err := s.GetCowInfoByEarNumber(ctx, userModel.AppPasture.Id, item.EarNumber)
  223. if err != nil {
  224. return nil, xerr.WithStack(err)
  225. }
  226. if cow.Sex != pasturePb.Genders_Female {
  227. return nil, xerr.Customf("牛只: %s,不是母牛", cow.EarNumber)
  228. }
  229. if cow.IsPregnant != pasturePb.IsShow_Ok {
  230. return nil, xerr.Customf("牛只: %s,不是怀孕状态", cow.EarNumber)
  231. }
  232. if cow.BreedStatus != pasturePb.BreedStatus_Pregnant {
  233. return nil, xerr.Customf("牛只: %s,不是怀孕状态", cow.EarNumber)
  234. }
  235. operationUser, err := s.GetSystemUserById(ctx, int64(item.OperationId))
  236. if err != nil {
  237. return nil, xerr.WithStack(err)
  238. }
  239. item.OperationName = operationUser.Name
  240. item.AbortionReasonsName = s.AbortionReasonsMap()[item.AbortionReasons]
  241. newEventAbortion := model.NewEventAbortion(userModel.AppPasture.Id, cow, item, pasturePb.IsShow_No)
  242. abortionCheckBatchModelList = append(abortionCheckBatchModelList, &AbortionCheckBatchModel{
  243. EventAbortion: newEventAbortion,
  244. Cow: cow,
  245. OperationUser: operationUser,
  246. IsLact: item.IsLact,
  247. })
  248. }
  249. return abortionCheckBatchModelList, nil
  250. }