event_check.go 14 KB

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