event_check.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "kpt-pasture/model"
  6. "kpt-pasture/util"
  7. "time"
  8. "gorm.io/gorm"
  9. "go.uber.org/zap"
  10. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  11. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  12. "gitee.com/xuyiping_admin/pkg/xerr"
  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. LastMating *model.EventMating
  24. }
  25. // MatingTimes 更新配次
  26. type MatingTimes struct {
  27. Mt int32
  28. CowId int64
  29. EventMatingId int64
  30. }
  31. type AbortionCheckBatchModel struct {
  32. EventAbortion *model.EventAbortion
  33. Cow *model.Cow
  34. OperationUser *model.SystemUser
  35. IsLact pasturePb.IsShow_Kind
  36. }
  37. func (s *StoreEntry) EnterCheck(ctx context.Context, pastureId int64, req *pasturePb.EventEnterRequest) error {
  38. var count int64
  39. if err := s.DB.Model(new(model.Cow)).
  40. Where("ear_number = ?", req.EarNumber).
  41. Where("pasture_id = ?", pastureId).
  42. Count(&count).Error; err != nil {
  43. return xerr.WithStack(err)
  44. }
  45. if count > 0 {
  46. return xerr.Custom("该牛只已存在")
  47. }
  48. if req.BirthAt > req.EnterAt {
  49. return xerr.Custom("出生时间不能大于入场时间")
  50. }
  51. return nil
  52. }
  53. func (s *StoreEntry) MatingCreateCheck(ctx context.Context, pastureId int64, req *pasturePb.EventMatingBatch) ([]*model.EventMatingCheckBatchModel, error) {
  54. if len(req.Items) <= 0 {
  55. return nil, xerr.Custom("请选择相关牛只")
  56. }
  57. if len(req.Items) > 50 {
  58. return nil, xerr.Custom("最多只能选择50只牛只")
  59. }
  60. eventMatingCheckBatchModelList := make([]*model.EventMatingCheckBatchModel, 0)
  61. for _, v := range req.Items {
  62. cowInfo, err := s.GetCowInfoByEarNumber(ctx, pastureId, v.EarNumber)
  63. if err != nil {
  64. return nil, xerr.WithStack(err)
  65. }
  66. if cowInfo.GetEventDayAge(int64(v.MatingAt)) < 0 {
  67. return nil, xerr.Customf("牛号:%s,配种时间不能早于牛只出生时间", cowInfo.EarNumber)
  68. }
  69. operationUser, err := s.GetSystemUserById(ctx, int64(v.OperationId))
  70. if err != nil {
  71. return nil, xerr.WithStack(err)
  72. }
  73. frozenSemen := &model.FrozenSemen{}
  74. if err = s.DB.Where("bull_id = ?", v.FrozenSemenNumber).
  75. First(frozenSemen).Error; err != nil {
  76. return nil, xerr.Custom("未找到冻精信息")
  77. }
  78. if frozenSemen.Quantity < v.FrozenSemenCount {
  79. return nil, xerr.Custom("冻精数量不足")
  80. }
  81. if cowInfo.Sex != pasturePb.Genders_Female {
  82. return nil, xerr.Customf("牛只: %d,不是母牛", cowInfo.EarNumber)
  83. }
  84. if int64(v.MatingAt) < cowInfo.LastMatingAt {
  85. return nil, xerr.Customf("牛只: %s,最近一次配种时间: %s,不能小于本次配种时间: %d",
  86. cowInfo.EarNumber,
  87. time.Unix(cowInfo.LastMatingAt, 0).Format(model.LayoutDate2),
  88. time.Unix(int64(v.MatingAt), 0).Format(model.LayoutDate2),
  89. )
  90. }
  91. if int64(v.MatingAt) < cowInfo.LastPregnantCheckAt {
  92. return nil, xerr.Customf("牛只: %s,最近一次孕检时间: %s,不能小于本次配种时间: %s",
  93. cowInfo.EarNumber,
  94. time.Unix(cowInfo.LastPregnantCheckAt, 0).Format(model.LayoutDate2),
  95. time.Unix(int64(v.MatingAt), 0).Format(model.LayoutDate2),
  96. )
  97. }
  98. if int64(v.MatingAt) < cowInfo.LastAbortionAt {
  99. return nil, xerr.Customf("牛只: %s,最近一次流产时间: %d,不能小于本次配种时间: %d",
  100. cowInfo.EarNumber,
  101. cowInfo.LastAbortionAt,
  102. v.MatingAt,
  103. )
  104. }
  105. if int64(v.MatingAt) < cowInfo.BirthAt {
  106. return nil, xerr.Customf("牛只: %s,出生时间: %d,不能小于本次配种时间: %d",
  107. cowInfo.EarNumber,
  108. time.Unix(cowInfo.BirthAt, 0).Format(model.LayoutDate2),
  109. time.Unix(int64(v.MatingAt), 0).Format(model.LayoutDate2),
  110. )
  111. }
  112. if cowInfo.BreedStatus == pasturePb.BreedStatus_Pregnant || cowInfo.BreedStatus == pasturePb.BreedStatus_No_Mating {
  113. return nil, xerr.Customf("牛只: %s,当前状态为: %s,不能进行配种",
  114. cowInfo.EarNumber,
  115. cowInfo.BreedStatus.String(),
  116. )
  117. }
  118. eventMatingCheckBatchModelList = append(eventMatingCheckBatchModelList, &model.EventMatingCheckBatchModel{
  119. Cow: cowInfo,
  120. FrozenSemen: frozenSemen,
  121. OperationUser: operationUser,
  122. MatingAt: int64(v.MatingAt),
  123. FrozenSemenCount: v.FrozenSemenCount,
  124. Remarks: v.Remarks,
  125. ExposeEstrusType: v.ExposeEstrusType,
  126. })
  127. }
  128. return eventMatingCheckBatchModelList, nil
  129. }
  130. func (s *StoreEntry) PregnantCheckDataCheck(ctx context.Context, pastureId int64, req *pasturePb.EventPregnantCheckBatch) ([]*PregnantCheckBatchModel, error) {
  131. if len(req.Items) <= 0 {
  132. return nil, xerr.Custom("请选择相关牛只数据")
  133. }
  134. if len(req.Items) > 50 {
  135. return nil, xerr.Custom("一次性最多限制提交50牛数据")
  136. }
  137. pregnantCheckBatchModelList := make([]*PregnantCheckBatchModel, 0)
  138. cowInfo := &model.Cow{}
  139. var err error
  140. for _, item := range req.Items {
  141. cowInfo, err = s.GetCowInfoByEarNumber(ctx, pastureId, item.EarNumber)
  142. if err != nil {
  143. return nil, xerr.WithStack(err)
  144. }
  145. if cowInfo.Sex != pasturePb.Genders_Female {
  146. return nil, xerr.Customf("牛只: %d,不是母牛", cowInfo.Id)
  147. }
  148. if cowInfo.GetEventDayAge(int64(item.PregnantCheckAt)) < 0 {
  149. return nil, xerr.Customf("牛号: %s,孕检时间不能早于牛只出生时间", cowInfo.EarNumber)
  150. }
  151. operationUser, err := s.GetSystemUserById(ctx, int64(item.OperationId))
  152. if err != nil {
  153. zaplog.Error("PregnantCheckDataCheck", zap.Any("id", item.OperationId), zap.Any("error", err.Error()))
  154. return nil, xerr.Customf("获取操作人员信息失败")
  155. }
  156. // 过滤掉没有配种状态的牛只
  157. if cowInfo.BreedStatus != pasturePb.BreedStatus_Breeding && cowInfo.BreedStatus != pasturePb.BreedStatus_Pregnant {
  158. return nil, xerr.Customf("牛只: %s 未参加配种,不能进行孕检", cowInfo.EarNumber)
  159. }
  160. if cowInfo.LastMatingAt <= 0 {
  161. return nil, xerr.Customf("牛只: %s,最近一次配种数据异常", cowInfo.EarNumber)
  162. }
  163. itemEventPregnantCheck, err := s.FindEventPregnantCheckIsExIstByCowId(ctx, cowInfo)
  164. if err != nil {
  165. return nil, xerr.WithStack(err)
  166. }
  167. if itemEventPregnantCheck.Id <= 0 {
  168. return nil, xerr.Customf("未发现该牛只: %s 孕检数据", cowInfo.EarNumber)
  169. }
  170. lastEventMating, err := s.FindLastEventMatingByCowId(ctx, pastureId, cowInfo.Id)
  171. if errors.Is(err, gorm.ErrRecordNotFound) {
  172. return nil, xerr.Customf("未发现该牛只: %s 配种数据", cowInfo.EarNumber)
  173. }
  174. if lastEventMating == nil || lastEventMating.Status == pasturePb.IsShow_No {
  175. return nil, xerr.Customf("未发现该牛只: %s 配种数据", cowInfo.EarNumber)
  176. }
  177. pregnantCheckBatchModelList = append(pregnantCheckBatchModelList, &PregnantCheckBatchModel{
  178. Cow: cowInfo,
  179. OperationUser: operationUser,
  180. PregnantCheckAt: item.PregnantCheckAt,
  181. PregnantCheckMethod: item.PregnantCheckMethod,
  182. PregnantCheckResult: item.PregnantCheckResult,
  183. Remarks: item.Remarks,
  184. EventPregnancyCheck: itemEventPregnantCheck,
  185. LastMating: lastEventMating,
  186. })
  187. }
  188. return pregnantCheckBatchModelList, nil
  189. }
  190. func (s *StoreEntry) EstrusCheckDataCheck(ctx context.Context, userModel *model.UserModel, req *pasturePb.EventEstrusBatch) (*model.EventEstrusBatch, error) {
  191. pastureId := userModel.AppPasture.Id
  192. exposeEstrusType := pasturePb.ExposeEstrusType_Natural_Estrus
  193. if req.ExposeEstrusType == pasturePb.ExposeEstrusType_Neck_Ring {
  194. exposeEstrusType = pasturePb.ExposeEstrusType_Neck_Ring
  195. }
  196. unMatingReasonsMap := s.UnMatingReasonsMap()
  197. res := &model.EventEstrusBatch{
  198. ExposeEstrusType: exposeEstrusType,
  199. EventEstrusList: make([]*model.EventEstrus, 0),
  200. EventMatingList: make([]*model.EventMating, 0),
  201. EarNumbers: make([]string, 0),
  202. }
  203. for _, item := range req.Items {
  204. cowInfo, err := s.GetCowInfoByEarNumber(ctx, pastureId, item.EarNumber)
  205. if err != nil {
  206. return nil, xerr.Custom("牛只信息不存在")
  207. }
  208. if cowInfo.Sex != pasturePb.Genders_Female {
  209. return nil, xerr.Custom("该牛只不是母牛")
  210. }
  211. if item.EstrusAt <= 0 {
  212. return nil, xerr.Custom("发情时间不能为空")
  213. }
  214. if int64(item.EstrusAt) <= cowInfo.BirthAt {
  215. return nil, xerr.Customf("牛号: %s,发情时间不能小于出生时间", cowInfo.EarNumber)
  216. }
  217. if int64(item.EstrusAt) <= cowInfo.LastCalvingAt {
  218. return nil, xerr.Custom("发情时间不能小于产犊时间")
  219. }
  220. estrusAt := time.Unix(int64(item.EstrusAt), 0).Local().Format(model.LayoutDate2)
  221. estrusLocalStartTime := util.TimeParseLocalUnix(estrusAt)
  222. estrusLocalEndTime := util.TimeParseLocalEndUnix(estrusAt)
  223. isExists := s.FindEventEstrusByCowId(pastureId, cowInfo.Id, estrusLocalStartTime, estrusLocalEndTime)
  224. // 如果存在,并且当天已经提交过则跳过
  225. if isExists {
  226. return nil, xerr.Customf("该牛只:%s,今天已经提交过发情数据", item.EarNumber)
  227. }
  228. operationUser, _ := s.GetSystemUserById(ctx, int64(item.OperationId))
  229. item.OperationName = operationUser.Name
  230. newEventEstrus := model.NewEventEstrus(
  231. pastureId, cowInfo, exposeEstrusType, pasturePb.IsShow_Ok,
  232. item.IsMating, int64(item.EstrusAt), operationUser, userModel.SystemUser,
  233. )
  234. if item.IsMating == pasturePb.IsShow_Ok {
  235. isExists = s.FindEventMatingByCowId(pastureId, cowInfo.Id)
  236. if isExists {
  237. return nil, xerr.Customf("配种清单已经有该牛只数据 :%s,今天已经提交过配种数据", item.EarNumber)
  238. }
  239. newEventMating := model.NewEventMating(pastureId, cowInfo, time.Now().Local().Unix(), exposeEstrusType)
  240. res.EventMatingList = append(res.EventMatingList, newEventMating)
  241. } else {
  242. newEventEstrus.UnMatingReasonsKind = item.UnMatingReasonsKind
  243. newEventEstrus.UnMatingReasonsName = unMatingReasonsMap[item.UnMatingReasonsKind]
  244. }
  245. newEventEstrus.Remarks = item.Remarks
  246. res.EventEstrusList = append(res.EventEstrusList, newEventEstrus)
  247. res.EarNumbers = append(res.EarNumbers, item.EarNumber)
  248. }
  249. return res, nil
  250. }
  251. func (s *StoreEntry) AbortionEventDataCheck(ctx context.Context, userModel *model.UserModel, items []*pasturePb.EventAbortionItem) ([]*AbortionCheckBatchModel, error) {
  252. if len(items) <= 0 {
  253. return nil, xerr.Custom("请选择相关数据")
  254. }
  255. if len(items) > 50 {
  256. return nil, xerr.Custom("一次性最多限制提交50牛数据")
  257. }
  258. abortionCheckBatchModelList := make([]*AbortionCheckBatchModel, 0)
  259. for _, item := range items {
  260. cow, err := s.GetCowInfoByEarNumber(ctx, userModel.AppPasture.Id, item.EarNumber)
  261. if err != nil {
  262. return nil, xerr.WithStack(err)
  263. }
  264. if cow.Sex != pasturePb.Genders_Female {
  265. return nil, xerr.Customf("牛只: %s,不是母牛", cow.EarNumber)
  266. }
  267. if cow.IsPregnant != pasturePb.IsShow_Ok {
  268. return nil, xerr.Customf("牛只: %s,不是怀孕状态", cow.EarNumber)
  269. }
  270. if cow.BreedStatus != pasturePb.BreedStatus_Pregnant {
  271. return nil, xerr.Customf("牛只: %s,不是怀孕状态", cow.EarNumber)
  272. }
  273. if cow.GetEventDayAge(int64(item.AbortionAt)) < 0 {
  274. return nil, xerr.Customf("牛号: %s,流产时间不能早于牛只出生时间", cow.EarNumber)
  275. }
  276. operationUser, err := s.GetSystemUserById(ctx, int64(item.OperationId))
  277. if err != nil {
  278. return nil, xerr.WithStack(err)
  279. }
  280. item.OperationName = operationUser.Name
  281. item.AbortionReasonsName = s.AbortionReasonsMap()[item.AbortionReasons]
  282. newEventAbortion := model.NewEventAbortion(userModel.AppPasture.Id, cow, item, pasturePb.IsShow_No)
  283. abortionCheckBatchModelList = append(abortionCheckBatchModelList, &AbortionCheckBatchModel{
  284. EventAbortion: newEventAbortion,
  285. Cow: cow,
  286. OperationUser: operationUser,
  287. IsLact: item.IsLact,
  288. })
  289. }
  290. return abortionCheckBatchModelList, nil
  291. }
  292. func (s *StoreEntry) ForbiddenMatingCheck(ctx context.Context, userModel *model.UserModel, items []*pasturePb.ForbiddenMatingItem) ([]*model.EventForbiddenMatingItem, error) {
  293. res := make([]*model.EventForbiddenMatingItem, 0)
  294. forbiddenMatingReasonsMap := s.ForbiddenMatingReasonsMap()
  295. for _, v := range items {
  296. cowInfo, err := s.GetCowInfoByEarNumber(ctx, userModel.AppPasture.Id, v.EarNumber)
  297. if err != nil {
  298. return nil, xerr.WithStack(err)
  299. }
  300. if cowInfo.Sex != pasturePb.Genders_Female {
  301. return nil, xerr.Customf("牛只: %s,不是母牛", cowInfo.EarNumber)
  302. }
  303. if v.ForbiddenMatingAt < int32(cowInfo.BirthAt) {
  304. return nil, xerr.Customf("牛只: %s,禁止配种时间不能小于出生时间", cowInfo.EarNumber)
  305. }
  306. operationUser, err := s.GetSystemUserById(ctx, int64(v.OperationId))
  307. if err != nil {
  308. return nil, xerr.WithStack(err)
  309. }
  310. res = append(res, &model.EventForbiddenMatingItem{
  311. Cow: cowInfo,
  312. ForbiddenMatingAt: int64(v.ForbiddenMatingAt),
  313. ForbiddenMatingReasonsKind: v.ForbiddenMatingReasonsKind,
  314. ForbiddenMatingReasonsName: forbiddenMatingReasonsMap[v.ForbiddenMatingReasonsKind],
  315. Remarks: v.Remarks,
  316. OperationUser: operationUser,
  317. MessageUser: userModel.SystemUser,
  318. })
  319. }
  320. return res, nil
  321. }
  322. func (s *StoreEntry) DeathCheck(ctx context.Context, req *pasturePb.EventDeathBatch, userModel *model.UserModel) ([]*model.EventDeathModel, error) {
  323. newEventDeathList := make([]*model.EventDeathModel, 0)
  324. for _, item := range req.Items {
  325. cow, err := s.GetCowInfoByEarNumber(ctx, userModel.AppPasture.Id, item.EarNumber)
  326. if err != nil {
  327. zaplog.Error("DeathBatch", zap.Any("item", item), zap.Any("err", err))
  328. return nil, xerr.Customf("获取牛只信息失败: %s", item.EarNumber)
  329. }
  330. if cow.BirthAt <= int64(item.DeathAt) {
  331. return nil, xerr.Customf("牛只: %s,死亡时间不能早于出生时间", cow.EarNumber)
  332. }
  333. lastEventLog, err := s.GetEventCowLog(ctx, userModel.AppPasture.Id, cow.Id)
  334. if err != nil {
  335. zaplog.Error("DeathBatch", zap.Any("item", item), zap.Any("err", err))
  336. return nil, xerr.Customf("获取牛只信息失败: %s", item.EarNumber)
  337. }
  338. if int64(item.DeathAt) < lastEventLog.EventAt {
  339. return nil, xerr.Customf("牛只: %s,死亡时间不能早于上一次事件时间", cow.EarNumber)
  340. }
  341. if name, ok := s.DeadReasonMap()[item.DeathReasonKind]; ok {
  342. item.DeathReasonName = name
  343. }
  344. operationUser, err := s.GetSystemUserById(ctx, int64(item.OperationId))
  345. if err != nil {
  346. zaplog.Error("DeathBatch", zap.Any("item", item), zap.Any("err", err))
  347. return nil, xerr.Customf("获取操作人员信息失败: %d", item.OperationId)
  348. }
  349. if name, ok := s.CowDeathDestinationMap()[item.DeathDestinationKind]; ok {
  350. item.DeathDestinationName = name
  351. }
  352. newEventDeath := model.NewEventDeath(userModel.AppPasture.Id, cow, item, userModel.SystemUser, operationUser)
  353. eventDeathModel := &model.EventDeathModel{
  354. Cow: cow,
  355. EventDeath: newEventDeath,
  356. NeckRing: nil,
  357. CalvingCalf: nil,
  358. }
  359. // 犊牛死亡更新
  360. if cow.DayAge <= model.CalfDefaultDayAge {
  361. calvingCalf, ok := s.IsExistCalvingCalf(userModel.AppPasture.Id, cow.Id)
  362. if ok && calvingCalf != nil {
  363. eventDeathModel.CalvingCalf = calvingCalf
  364. }
  365. }
  366. if neckRing, ok := s.NeckRingIsExist(userModel.AppPasture.Id, item.EarNumber); ok && neckRing != nil {
  367. eventDeathModel.NeckRing = neckRing
  368. }
  369. newEventDeathList = append(newEventDeathList, eventDeathModel)
  370. }
  371. return newEventDeathList, nil
  372. }