event_breed_more.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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, 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, 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. // MatingBatch 牛只配种
  280. func (s *StoreEntry) MatingBatch(ctx context.Context, req *pasturePb.EventMatingBatch) (err error) {
  281. userModel, err := s.GetUserModel(ctx)
  282. if err != nil {
  283. return xerr.WithStack(err)
  284. }
  285. eventMatingCheckModelList, err := s.MatingCreateCheck(ctx, userModel.AppPasture.Id, req)
  286. if err != nil {
  287. return xerr.WithStack(err)
  288. }
  289. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  290. for _, item := range eventMatingCheckModelList {
  291. // 脖环发情揭发数据
  292. if item.ExposeEstrusType == pasturePb.ExposeEstrusType_Neck_Ring {
  293. // 新增发情事件数据
  294. newEstrus := model.NewEventEstrus(
  295. userModel.AppPasture.Id, item.Cow, pasturePb.ExposeEstrusType_Neck_Ring,
  296. pasturePb.IsShow_Ok, pasturePb.IsShow_Ok, item.MatingAt,
  297. item.OperationUser, userModel.SystemUser,
  298. )
  299. if err = tx.Create(newEstrus).Error; err != nil {
  300. return xerr.WithStack(err)
  301. }
  302. // 新增配种事件数据
  303. newMating := model.NewEventMating(userModel.AppPasture.Id, item.Cow, item.MatingAt, pasturePb.ExposeEstrusType_Neck_Ring)
  304. newMating.EventUpdate(item.MatingAt, item.FrozenSemen.BullId, false, item.OperationUser, userModel.SystemUser)
  305. if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, item, false); err != nil {
  306. return xerr.WithStack(err)
  307. }
  308. if err = tx.Model(newMating).Create(newMating).Error; err != nil {
  309. return xerr.WithStack(err)
  310. }
  311. } else {
  312. // 获取牛只最近一次配种信息
  313. lastEventMating, err := s.FindLastEventMatingByCowId(ctx, userModel.AppPasture.Id, item.Cow.Id)
  314. if err != nil {
  315. // 1. 没有配种信息,(第一次参加配种的牛只,并且没有参与同期的牛只 )
  316. if errors.Is(err, gorm.ErrRecordNotFound) {
  317. newMating := model.NewEventMatingNaturalEstrus(userModel.AppPasture.Id, item, userModel.SystemUser)
  318. if err = tx.Create(newMating).Error; err != nil {
  319. return xerr.WithStack(err)
  320. }
  321. if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, item, false); err != nil {
  322. return xerr.WithStack(err)
  323. }
  324. continue
  325. } else {
  326. return xerr.WithStack(err)
  327. }
  328. }
  329. // 2. 所有有配种数据的牛只
  330. if lastEventMating == nil || lastEventMating.Id <= 0 || lastEventMating.Status != pasturePb.IsShow_No {
  331. zaplog.Error("MatingCreate", zap.Any("cow", item.Cow), zap.Any("UserModel", userModel))
  332. return xerr.Customf("牛只配种数据异常: %d", item.Cow.EarNumber)
  333. }
  334. // 2.1 复配 => 本胎次配次不加1
  335. isReMating := lastEventMating.IsReMating(item.Cow, item.MatingAt)
  336. if isReMating {
  337. lastEventMating.EventReMatingUpdate(item.MatingAt)
  338. if err = tx.Model(lastEventMating).
  339. Select("mating_result", "mating_result_at", "status").
  340. Where("id = ?", lastEventMating.Id).
  341. Updates(lastEventMating).Error; err != nil {
  342. }
  343. }
  344. // 2.2. 同期初配
  345. IsMatingUpdate := lastEventMating.IsMatingUpdate()
  346. if IsMatingUpdate {
  347. lastEventMating.EventUpdate(item.MatingAt, item.FrozenSemen.BullId, isReMating, item.OperationUser, userModel.SystemUser)
  348. if err = tx.Model(lastEventMating).
  349. Select("mating_at", "status", "reality_day", "frozen_semen_number", "operation_id", "operation_name", "message_id", "message_name").
  350. Where("id = ?", lastEventMating.Id).
  351. Updates(lastEventMating).Error; err != nil {
  352. return xerr.WithStack(err)
  353. }
  354. }
  355. // 2.3. 提交的配种数据中,定位出空怀的牛只,
  356. isEmptyMating := lastEventMating.IsEmptyMating(item.Cow, item.MatingAt)
  357. if isEmptyMating {
  358. // 把上次配种结果信息更新成空怀
  359. lastEventMating.EventMatingResultUpdate(pasturePb.MatingResult_Empty, item.MatingAt)
  360. if err = tx.Model(lastEventMating).
  361. Select("mating_result", "mating_result_at").
  362. Where("id = ?", lastEventMating.Id).
  363. Updates(lastEventMating).Error; err != nil {
  364. return xerr.WithStack(err)
  365. }
  366. // 先创建一条新的配种数据
  367. newMating := model.NewEventMating(userModel.AppPasture.Id, item.Cow, item.MatingAt, pasturePb.ExposeEstrusType_Natural_Estrus)
  368. newMating.EventUpdate(item.MatingAt, item.FrozenSemen.BullId, isReMating, item.OperationUser, userModel.SystemUser)
  369. if err = tx.Model(newMating).Create(newMating).Error; err != nil {
  370. return xerr.WithStack(err)
  371. }
  372. // 更新牛只配种结果日志
  373. if err = s.UpdateMatingResultEventCowLogByCowId(ctx, item.Cow.Id, s.MatingResultMap()[pasturePb.MatingResult_Empty]); err != nil {
  374. zaplog.Error("MatingCreate", zap.Any("UpdateEventCowLogByCowId", err), zap.Any("cow", item.Cow))
  375. }
  376. }
  377. // 牛只基本中配种信息更新
  378. if err = s.MatingCowUpdate(ctx, userModel.AppPasture.Id, item, isReMating); err != nil {
  379. return xerr.WithStack(err)
  380. }
  381. }
  382. // 减去精液的数量
  383. item.FrozenSemen.EventQuantityUpdate(item.FrozenSemenCount)
  384. if err = tx.Model(item.FrozenSemen).
  385. Select("quantity").
  386. Where("id = ?", item.FrozenSemen.Id).
  387. Updates(item.FrozenSemen).Error; err != nil {
  388. return xerr.WithStack(err)
  389. }
  390. // 创建冻精使用记录日志
  391. itemFrozenSemenLog := model.NewEventFrozenSemenLog(userModel.AppPasture.Id, item)
  392. if err = tx.Create(itemFrozenSemenLog).Error; err != nil {
  393. return xerr.WithStack(err)
  394. }
  395. }
  396. return nil
  397. }); err != nil {
  398. return xerr.WithStack(err)
  399. }
  400. return nil
  401. }
  402. func (s *StoreEntry) MatingCowUpdate(ctx context.Context, pastureId int64, item *model.EventMatingCheckBatchModel, isReMating bool) error {
  403. // 牛只基本中配种信息更新
  404. item.Cow.EventMatingUpdate(item.MatingAt, item.FrozenSemen.BullId, isReMating)
  405. if err := s.DB.Model(item.Cow).
  406. Select("last_mating_at", "mating_times", "last_bull_number", "first_mating_at", "is_pregnant", "breed_status").
  407. Where("id = ?", item.Cow.Id).
  408. Updates(item.Cow).Error; err != nil {
  409. return xerr.WithStack(err)
  410. }
  411. // 记录日志
  412. cowLogs := s.SubmitEventLog(ctx, pastureId, item.Cow, pasturePb.EventType_Mating, item)
  413. if err := s.DB.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  414. return xerr.WithStack(err)
  415. }
  416. return nil
  417. }
  418. func (s *StoreEntry) WeaningBatch(ctx context.Context, req *pasturePb.EventWeaningBatch) (err error) {
  419. userModel, err := s.GetUserModel(ctx)
  420. if err != nil {
  421. return xerr.WithStack(err)
  422. }
  423. if len(req.Items) <= 0 {
  424. return nil
  425. }
  426. cowIds := make([]int64, 0)
  427. cowWeightMap := make(map[int64]float64)
  428. for _, item := range req.Items {
  429. cowIds = append(cowIds, int64(item.CowId))
  430. cowWeightMap[int64(item.CowId)] = float64(item.Weight)
  431. }
  432. eventWeaningList := make([]*model.EventWeaning, 0)
  433. if err = s.DB.Model(new(model.EventWeaning)).
  434. Where("cow_id IN ?", cowIds).
  435. Where("status = ?", pasturePb.IsShow_No).
  436. Find(&eventWeaningList).Error; err != nil {
  437. return xerr.WithStack(err)
  438. }
  439. operation, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  440. if err != nil {
  441. return xerr.WithStack(err)
  442. }
  443. req.OperationName = operation.Name
  444. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  445. cowInfo := &model.Cow{}
  446. for _, eventWeaning := range eventWeaningList {
  447. eventWeaning.EventUpdate(int64(req.WeaningAt), int32(cowWeightMap[cowInfo.Id]*1000), req.Remarks, req.PenId, operation, userModel.SystemUser)
  448. if err = tx.Model(new(model.EventWeaning)).
  449. Select("status", "reality_day", "operation_id", "operation_name", "message_id", "message_name", "remarks", "after_pen_id").
  450. Where("id = ?", eventWeaning.Id).
  451. Updates(eventWeaning).Error; err != nil {
  452. return xerr.WithStack(err)
  453. }
  454. cowInfo, err = s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, eventWeaning.CowId)
  455. if err != nil {
  456. return xerr.WithStack(err)
  457. }
  458. cowInfo.EventWeaningUpdate(int64(req.WeaningAt), req.PenId, int64(cowWeightMap[cowInfo.Id]*1000))
  459. if err = tx.Model(new(model.Cow)).
  460. Select("pen_id", "current_weight", "weaning_at", "last_weight_at").
  461. Where("id = ?", cowInfo.Id).
  462. Updates(cowInfo).Error; err != nil {
  463. return xerr.WithStack(err)
  464. }
  465. // 创建牛只的体重记录
  466. eventWeight := &pasturePb.EventWeight{
  467. WeightAt: req.WeaningAt,
  468. OperationId: req.OperationId,
  469. OperationName: req.OperationName,
  470. Remarks: req.Remarks,
  471. Weight: float32(cowWeightMap[cowInfo.Id]),
  472. Height: 0,
  473. }
  474. newEventWeight := model.NewEventWeight(userModel.AppPasture.Id, cowInfo, userModel.SystemUser, eventWeight)
  475. if err = tx.Create(newEventWeight).Error; err != nil {
  476. return xerr.WithStack(err)
  477. }
  478. // 记录事件日志
  479. cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cowInfo, pasturePb.EventType_Weaning, req)
  480. if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  481. return xerr.WithStack(err)
  482. }
  483. }
  484. return nil
  485. }); err != nil {
  486. return xerr.WithStack(err)
  487. }
  488. return nil
  489. }