event_check.go 12 KB

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