event_check.go 12 KB

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