event_check.go 15 KB

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