event_breed_more.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "kpt-pasture/model"
  7. "kpt-pasture/util"
  8. "net/http"
  9. "strings"
  10. "go.uber.org/zap"
  11. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  12. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  13. "gitee.com/xuyiping_admin/pkg/xerr"
  14. "gorm.io/gorm"
  15. )
  16. func (s *StoreEntry) PregnantCheckList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventPregnantCheckResponse, error) {
  17. userModel, err := s.GetUserModel(ctx)
  18. if err != nil {
  19. return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
  20. }
  21. pregnantCheckList := make([]*model.EventPregnantCheck, 0)
  22. var count int64 = 0
  23. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventPregnantCheck).TableName())).
  24. Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
  25. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  26. Where("a.status = ?", pasturePb.IsShow_Ok)
  27. if len(req.CowId) > 0 {
  28. cowIds := strings.Split(req.CowId, ",")
  29. pref.Where("a.cow_id IN ?", cowIds)
  30. }
  31. if req.EarNumber != "" {
  32. pref.Where("a.ear_number = ?", req.EarNumber)
  33. }
  34. if req.PenId > 0 {
  35. pref.Where("b.pen_id = ?", req.PenId)
  36. }
  37. if err = pref.Order("a.id desc").
  38. Count(&count).Limit(int(pagination.PageSize)).
  39. Offset(int(pagination.PageOffset)).
  40. Find(&pregnantCheckList).Error; err != nil {
  41. return nil, xerr.WithStack(err)
  42. }
  43. pregnantCheckResultMap := s.PregnantCheckResultMap()
  44. pregnantCheckMethodMap := s.PregnantCheckMethodMap()
  45. return &pasturePb.EventPregnantCheckResponse{
  46. Code: http.StatusOK,
  47. Msg: "ok",
  48. Data: &pasturePb.SearchPregnantCheckData{
  49. List: model.EventPregnantCheckSlice(pregnantCheckList).ToPB(pregnantCheckResultMap, pregnantCheckMethodMap),
  50. Total: int32(count),
  51. PageSize: pagination.PageSize,
  52. Page: pagination.Page,
  53. },
  54. }, nil
  55. }
  56. func (s *StoreEntry) PregnantCheckCreateBatch(ctx context.Context, req *pasturePb.EventPregnantCheckBatch) (err error) {
  57. userModel, err := s.GetUserModel(ctx)
  58. if err != nil {
  59. return xerr.WithStack(err)
  60. }
  61. pregnantCheckBatchModelList, err := s.PregnantCheckDataCheck(ctx, userModel.AppPasture.Id, req)
  62. if err != nil {
  63. return xerr.WithStack(err)
  64. }
  65. abortionReasonsMap := s.AbortionReasonsMap()
  66. matingResultMap := s.MatingResultMap()
  67. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  68. for _, item := range pregnantCheckBatchModelList {
  69. breedStatus := pasturePb.BreedStatus_Pregnant
  70. isPregnant := pasturePb.IsShow_Ok
  71. matingResult := pasturePb.MatingResult_Pregnant
  72. if item.PregnantCheckResult == pasturePb.PregnantCheckResult_UnPregnant {
  73. breedStatus = pasturePb.BreedStatus_Empty
  74. isPregnant = pasturePb.IsShow_No
  75. matingResult = pasturePb.MatingResult_Empty
  76. }
  77. // 更新上一次配种结果数据,如何是复检无胎则更新为流产
  78. if item.EventPregnancyCheck.PregnantCheckName == model.PregnantCheckForSecond {
  79. breedStatus = pasturePb.BreedStatus_Abort
  80. matingResult = pasturePb.MatingResult_Abort
  81. }
  82. item.Cow.EventPregnantCheckUpdate(breedStatus, int64(item.PregnantCheckAt), isPregnant)
  83. // 更新牛只基本信息
  84. if err = tx.Model(item.Cow).
  85. Select("breed_status", "last_pregnant_check_at", "is_pregnant").
  86. Where("id = ?", item.Cow.Id).
  87. Updates(item.Cow).Error; err != nil {
  88. return xerr.WithStack(err)
  89. }
  90. // 更新最近一次配种结果数据
  91. item.LastMating.EventMatingResultUpdate(matingResult, int64(item.PregnantCheckAt))
  92. if err = tx.Model(item.LastMating).
  93. Where("id = ?", item.LastMating.Id).
  94. Select("mating_result", "mating_result_at").
  95. Updates(item.LastMating).Error; err != nil {
  96. return xerr.WithStack(err)
  97. }
  98. // 更新孕检事件表
  99. item.EventPregnancyCheck.EventUpdate(int64(item.PregnantCheckAt), item.PregnantCheckResult, item.PregnantCheckMethod,
  100. item.OperationUser, userModel.SystemUser, item.Remarks)
  101. if err = tx.Model(new(model.EventPregnantCheck)).
  102. Select("reality_day", "pregnant_check_result", "pregnant_check_method", "operation_id", "operation_name", "message_id", "message_name", "remarks", "status").
  103. Where("id = ?", item.EventPregnancyCheck.Id).
  104. Updates(item.EventPregnancyCheck).Error; err != nil {
  105. return xerr.WithStack(err)
  106. }
  107. // 更新上次配种结果
  108. matingResultName := ""
  109. if matingRName, ok := matingResultMap[matingResult]; ok {
  110. matingResultName = matingRName
  111. }
  112. if err = s.UpdateMatingResultEventCowLogByCowId(ctx, item.Cow.Id, matingResultName); err != nil {
  113. zaplog.Error("PregnantCheckCreateBatch", zap.Any("UpdateMatingResultEventCowLogByCowId", err))
  114. return xerr.WithStack(err)
  115. }
  116. // 如果确定是流产
  117. if matingResult == pasturePb.MatingResult_Abort {
  118. newEventAbortion := model.NewEventAbortion(userModel.AppPasture.Id, item.Cow, &pasturePb.EventAbortionItem{
  119. CowId: int32(item.Cow.Id),
  120. AbortionAt: item.PregnantCheckAt,
  121. IsAfterbirth: pasturePb.IsShow_No,
  122. Photos: []string{},
  123. AbortionReasons: pasturePb.AbortionReasons_Unknown,
  124. AbortionReasonsName: abortionReasonsMap[pasturePb.AbortionReasons_Unknown],
  125. Remarks: "追加流产数据",
  126. OperationId: int32(item.OperationUser.Id),
  127. OperationName: item.OperationUser.Name,
  128. }, pasturePb.IsShow_Ok)
  129. if err = tx.Create(newEventAbortion).Error; err != nil {
  130. return xerr.WithStack(err)
  131. }
  132. }
  133. // 记录日志
  134. cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, item.Cow, pasturePb.EventType_Pregnancy_Check, pasturePb.ExposeEstrusType_Invalid, item)
  135. if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  136. return xerr.WithStack(err)
  137. }
  138. }
  139. return nil
  140. }); err != nil {
  141. return xerr.WithStack(err)
  142. }
  143. return nil
  144. }
  145. func (s *StoreEntry) AbortionCreateBatch(ctx context.Context, req *pasturePb.EventAbortionBatch) error {
  146. if len(req.Items) <= 0 {
  147. return nil
  148. }
  149. userModel, err := s.GetUserModel(ctx)
  150. if err != nil {
  151. return xerr.WithStack(err)
  152. }
  153. eventAbortionModelList, err := s.AbortionEventDataCheck(ctx, userModel, req.Items)
  154. if err != nil {
  155. return xerr.WithStack(err)
  156. }
  157. if len(eventAbortionModelList) <= 0 {
  158. return xerr.Custom("数据异常")
  159. }
  160. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  161. for _, item := range eventAbortionModelList {
  162. if err = tx.Create(item.EventAbortion).Error; err != nil {
  163. return xerr.WithStack(err)
  164. }
  165. item.Cow.EventAbortionUpdate(item.EventAbortion.AbortionAt, item.IsLact)
  166. // 更新牛只状态
  167. if err = tx.Model(new(model.Cow)).
  168. Select("is_pregnant", "last_abortion_at", "breed_status").
  169. Where("id = ?", item.Cow.Id).
  170. Updates(item.Cow).Error; err != nil {
  171. return xerr.WithStack(err)
  172. }
  173. lastCowMating := &model.EventMating{}
  174. if err = s.DB.Model(new(model.EventMating)).
  175. Where("cow_id = ?", item.Cow.Id).
  176. Where("mating_result = ?", pasturePb.MatingResult_Pregnant).
  177. Order("id desc").First(lastCowMating).Error; err != nil {
  178. return xerr.WithStack(err)
  179. }
  180. // 更新最近一次配种结果为流产
  181. lastCowMating.EventMatingResultUpdate(pasturePb.MatingResult_Abort, item.EventAbortion.AbortionAt)
  182. // 更新最近一次配种结果为流产
  183. if err = tx.Model(new(model.EventMating)).
  184. Select("mating_result", "mating_result_at").
  185. Where("id = ?", lastCowMating.Id).
  186. Updates(lastCowMating).Error; err != nil {
  187. }
  188. // 记录日志
  189. cowLog := s.SubmitEventLog(ctx, userModel.AppPasture.Id, item.Cow, pasturePb.EventType_Abort, pasturePb.ExposeEstrusType_Invalid, item)
  190. if err = tx.Table(cowLog.TableName()).Create(cowLog).Error; err != nil {
  191. return xerr.WithStack(err)
  192. }
  193. }
  194. return nil
  195. }); err != nil {
  196. return xerr.WithStack(err)
  197. }
  198. return nil
  199. }
  200. func (s *StoreEntry) AbortionList(
  201. ctx context.Context,
  202. req *pasturePb.SearchEventRequest,
  203. pagination *pasturePb.PaginationModel,
  204. ) (*pasturePb.EventAbortionResponse, error) {
  205. userModel, err := s.GetUserModel(ctx)
  206. if err != nil {
  207. return nil, xerr.WithStack(err)
  208. }
  209. abortionList := make([]*model.EventAbortion, 0)
  210. var count int64 = 0
  211. pref := s.DB.Model(new(model.EventAbortion)).
  212. Where("pasture_id = ?", userModel.AppPasture.Id)
  213. if len(req.CowId) > 0 {
  214. cowIds, err := util.ConvertCowIdsToInt64Slice(req.CowId)
  215. if err != nil {
  216. return nil, xerr.Custom("cowId转换失败")
  217. }
  218. pref.Where("cow_id IN (?)", cowIds)
  219. }
  220. if req.StartDayAt > 0 && req.EndDayAt > 0 && req.StartDayAt <= req.EndDayAt {
  221. pref.Where("abortion_at BETWEEN ? AND ?", req.StartDayAt, req.EndDayAt)
  222. }
  223. if req.EarNumber != "" {
  224. pref.Where("ear_number = ?", req.EarNumber)
  225. }
  226. if err = pref.Order("id desc").
  227. Count(&count).Limit(int(pagination.PageSize)).
  228. Offset(int(pagination.PageOffset)).
  229. Find(&abortionList).Error; err != nil {
  230. return nil, xerr.WithStack(err)
  231. }
  232. return &pasturePb.EventAbortionResponse{
  233. Code: http.StatusOK,
  234. Msg: "ok",
  235. Data: &pasturePb.EventAbortionData{
  236. List: model.AbortionSlice(abortionList).ToPB(),
  237. Total: int32(count),
  238. PageSize: pagination.PageSize,
  239. Page: pagination.Page,
  240. },
  241. }, nil
  242. }
  243. func (s *StoreEntry) MatingList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventMatingResponse, error) {
  244. userModel, err := s.GetUserModel(ctx)
  245. if err != nil {
  246. return nil, xerr.WithStack(err)
  247. }
  248. matingList := make([]*model.EventMating, 0)
  249. var count int64 = 0
  250. pref := s.DB.Model(new(model.EventMating)).
  251. Where("mating_result > ?", pasturePb.MatingResult_Invalid).
  252. Where("status = ?", pasturePb.IsShow_Ok).
  253. Where("pasture_id = ?", userModel.AppPasture.Id)
  254. if len(req.CowId) > 0 {
  255. cowIds := strings.Split(req.CowId, ",")
  256. pref.Where("cow_id IN ?", cowIds)
  257. }
  258. if req.EarNumber != "" {
  259. pref.Where("ear_number = ?", req.EarNumber)
  260. }
  261. if err = pref.Order("id desc").
  262. Count(&count).Limit(int(pagination.PageSize)).
  263. Offset(int(pagination.PageOffset)).
  264. Find(&matingList).Error; err != nil {
  265. return nil, xerr.WithStack(err)
  266. }
  267. exposeEstrusTypeMap := s.ExposeEstrusTypeMap()
  268. return &pasturePb.EventMatingResponse{
  269. Code: http.StatusOK,
  270. Msg: "ok",
  271. Data: &pasturePb.SearchMatingData{
  272. List: model.EventMatingSlice(matingList).ToPB(exposeEstrusTypeMap),
  273. Total: int32(count),
  274. PageSize: pagination.PageSize,
  275. Page: pagination.Page,
  276. },
  277. }, nil
  278. }
  279. // MatingCreate 牛只配种
  280. func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.EventMating) (err error) {
  281. userModel, err := s.GetUserModel(ctx)
  282. if err != nil {
  283. return xerr.WithStack(err)
  284. }
  285. eventMatingCheckModel, err := s.MatingCreateCheck(ctx, userModel.AppPasture.Id, req)
  286. if err != nil {
  287. return xerr.WithStack(err)
  288. }
  289. req.OperationName = eventMatingCheckModel.OperationUser.Name
  290. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  291. for _, cow := range eventMatingCheckModel.CowList {
  292. // 获取牛只最近一次配种信息
  293. lastEventMating, err := s.FindLastEventMatingByCowId(ctx, userModel.AppPasture.Id, cow.Id)
  294. if err != nil {
  295. // 1. 没有配种信息,(第一次参加配种的牛只,并且没有参与同期的牛只 )
  296. if errors.Is(err, gorm.ErrRecordNotFound) {
  297. newMating := model.NewEventMatingNaturalEstrus(userModel.AppPasture.Id, cow, req, userModel.SystemUser)
  298. if err = tx.Create(newMating).Error; err != nil {
  299. return xerr.WithStack(err)
  300. }
  301. if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, cow, req, false); err != nil {
  302. return xerr.WithStack(err)
  303. }
  304. continue
  305. } else {
  306. return xerr.WithStack(err)
  307. }
  308. }
  309. // 2. 所有有配种数据的牛只
  310. if lastEventMating == nil || lastEventMating.Id <= 0 || lastEventMating.Status != pasturePb.IsShow_No {
  311. zaplog.Error("MatingCreate", zap.Any("cow", cow), zap.Any("UserModel", userModel))
  312. return xerr.Customf("牛只配种数据异常: %d", cow.EarNumber)
  313. }
  314. // 2.1 复配 => 本胎次配次不加1
  315. isReMating := lastEventMating.IsReMating(cow, int64(req.MatingAt))
  316. if isReMating {
  317. lastEventMating.EventReMatingUpdate(int64(req.MatingAt))
  318. if err = tx.Model(lastEventMating).
  319. Select("mating_result", "mating_result_at", "status").
  320. Where("id = ?", lastEventMating.Id).
  321. Updates(lastEventMating).Error; err != nil {
  322. }
  323. }
  324. // 2.2. 同期初配
  325. IsMatingUpdate := lastEventMating.IsMatingUpdate()
  326. if IsMatingUpdate {
  327. lastEventMating.EventUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating, eventMatingCheckModel.OperationUser, userModel.SystemUser)
  328. if err = tx.Model(lastEventMating).
  329. Select("mating_at", "status", "reality_day", "frozen_semen_number", "operation_id", "operation_name", "message_id", "message_name").
  330. Where("id = ?", lastEventMating.Id).
  331. Updates(lastEventMating).Error; err != nil {
  332. return xerr.WithStack(err)
  333. }
  334. }
  335. // 2.3. 提交的配种数据中,定位出空怀的牛只,
  336. isEmptyMating := lastEventMating.IsEmptyMating(cow, int64(req.MatingAt))
  337. if isEmptyMating {
  338. // 把上次配种结果信息更新成空怀
  339. lastEventMating.EventMatingResultUpdate(pasturePb.MatingResult_Empty, int64(req.MatingAt))
  340. if err = tx.Model(lastEventMating).
  341. Select("mating_result", "mating_result_at").
  342. Where("id = ?", lastEventMating.Id).
  343. Updates(lastEventMating).Error; err != nil {
  344. return xerr.WithStack(err)
  345. }
  346. // 先创建一条新的配种数据
  347. newMating := model.NewEventMating(userModel.AppPasture.Id, cow, int64(req.MatingAt), req.ExposeEstrusType)
  348. newMating.EventUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating, eventMatingCheckModel.OperationUser, userModel.SystemUser)
  349. if err = tx.Model(newMating).Create(newMating).Error; err != nil {
  350. return xerr.WithStack(err)
  351. }
  352. // 更新牛只配种结果日志
  353. if err = s.UpdateMatingResultEventCowLogByCowId(ctx, cow.Id, s.MatingResultMap()[pasturePb.MatingResult_Empty]); err != nil {
  354. zaplog.Error("MatingCreate", zap.Any("UpdateEventCowLogByCowId", err), zap.Any("cow", cow))
  355. }
  356. }
  357. // 牛只基本中配种信息更新
  358. if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, cow, req, isReMating); err != nil {
  359. return xerr.WithStack(err)
  360. }
  361. }
  362. // 创建冻精使用记录日志
  363. itemFrozenSemenLog := model.NewEventFrozenSemenLog(eventMatingCheckModel.FrozenSemen.PastureId, req)
  364. if err = tx.Create(itemFrozenSemenLog).Error; err != nil {
  365. return xerr.WithStack(err)
  366. }
  367. // 减去精液的数量
  368. eventMatingCheckModel.FrozenSemen.EventQuantityUpdate(req.FrozenSemenCount)
  369. if err = tx.Model(eventMatingCheckModel.FrozenSemen).
  370. Select("quantity").
  371. Where("id = ?", eventMatingCheckModel.FrozenSemen.Id).
  372. Updates(eventMatingCheckModel.FrozenSemen).Error; err != nil {
  373. return xerr.WithStack(err)
  374. }
  375. return nil
  376. }); err != nil {
  377. return xerr.WithStack(err)
  378. }
  379. return nil
  380. }
  381. func (s *StoreEntry) MatingCowUpdate(ctx context.Context, pastureId int64, cow *model.Cow, req *pasturePb.EventMating, isReMating bool) error {
  382. // 牛只基本中配种信息更新
  383. cow.EventMatingUpdate(int64(req.MatingAt), req.FrozenSemenNumber, isReMating)
  384. if err := s.DB.Model(cow).
  385. Select("last_mating_at", "mating_times", "last_bull_number", "first_mating_at", "is_pregnant", "breed_status").
  386. Where("id = ?", cow.Id).
  387. Updates(cow).Error; err != nil {
  388. return xerr.WithStack(err)
  389. }
  390. // 记录日志
  391. cowLogs := s.SubmitEventLog(ctx, pastureId, cow, pasturePb.EventType_Mating, req.ExposeEstrusType, req)
  392. if err := s.DB.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  393. return xerr.WithStack(err)
  394. }
  395. return nil
  396. }
  397. func (s *StoreEntry) WeaningBatch(ctx context.Context, req *pasturePb.EventWeaningBatch) (err error) {
  398. userModel, err := s.GetUserModel(ctx)
  399. if err != nil {
  400. return xerr.WithStack(err)
  401. }
  402. if len(req.Items) <= 0 {
  403. return nil
  404. }
  405. cowIds := make([]int64, 0)
  406. cowWeightMap := make(map[int64]float64)
  407. for _, item := range req.Items {
  408. cowIds = append(cowIds, int64(item.CowId))
  409. cowWeightMap[int64(item.CowId)] = float64(item.Weight)
  410. }
  411. eventWeaningList := make([]*model.EventWeaning, 0)
  412. if err = s.DB.Model(new(model.EventWeaning)).
  413. Where("cow_id IN ?", cowIds).
  414. Where("status = ?", pasturePb.IsShow_No).
  415. Find(&eventWeaningList).Error; err != nil {
  416. return xerr.WithStack(err)
  417. }
  418. operation, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  419. if err != nil {
  420. return xerr.WithStack(err)
  421. }
  422. req.OperationName = operation.Name
  423. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  424. cowInfo := &model.Cow{}
  425. for _, eventWeaning := range eventWeaningList {
  426. eventWeaning.EventUpdate(int64(req.WeaningAt), int32(cowWeightMap[cowInfo.Id]*1000), req.Remarks, req.PenId, operation, userModel.SystemUser)
  427. if err = tx.Model(new(model.EventWeaning)).
  428. Select("status", "reality_day", "operation_id", "operation_name", "message_id", "message_name", "remarks", "after_pen_id").
  429. Where("id = ?", eventWeaning.Id).
  430. Updates(eventWeaning).Error; err != nil {
  431. return xerr.WithStack(err)
  432. }
  433. cowInfo, err = s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, eventWeaning.CowId)
  434. if err != nil {
  435. return xerr.WithStack(err)
  436. }
  437. cowInfo.EventWeaningUpdate(int64(req.WeaningAt), req.PenId, int64(cowWeightMap[cowInfo.Id]*1000))
  438. if err = tx.Model(new(model.Cow)).
  439. Select("pen_id", "current_weight", "weaning_at", "last_weight_at").
  440. Where("id = ?", cowInfo.Id).
  441. Updates(cowInfo).Error; err != nil {
  442. return xerr.WithStack(err)
  443. }
  444. // 创建牛只的体重记录
  445. eventWeight := &pasturePb.EventWeight{
  446. WeightAt: req.WeaningAt,
  447. OperationId: req.OperationId,
  448. OperationName: req.OperationName,
  449. Remarks: req.Remarks,
  450. Weight: float32(cowWeightMap[cowInfo.Id]),
  451. Height: 0,
  452. }
  453. newEventWeight := model.NewEventWeight(userModel.AppPasture.Id, cowInfo, userModel.SystemUser, eventWeight)
  454. if err = tx.Create(newEventWeight).Error; err != nil {
  455. return xerr.WithStack(err)
  456. }
  457. // 记录事件日志
  458. cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cowInfo, pasturePb.EventType_Weaning, pasturePb.ExposeEstrusType_Invalid, req)
  459. if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  460. return xerr.WithStack(err)
  461. }
  462. }
  463. return nil
  464. }); err != nil {
  465. return xerr.WithStack(err)
  466. }
  467. return nil
  468. }