work_cron.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package crontab
  2. import (
  3. "encoding/json"
  4. "kpt-pasture/model"
  5. "time"
  6. "gorm.io/gorm"
  7. "gitee.com/xuyiping_admin/pkg/xerr"
  8. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  9. "go.uber.org/zap"
  10. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  11. )
  12. // GenerateCalendarBySameTimePlan 生成同期计划的牛只
  13. func (e *Entry) GenerateCalendarBySameTimePlan(cowList []*model.Cow, sameTime *model.SameTime) (err error) {
  14. if len(cowList) <= 0 {
  15. return nil
  16. }
  17. newCowList := make([]*model.Cow, 0)
  18. for _, cow := range cowList {
  19. if ok := e.IsExistSameTimeCow(cow, sameTime); ok {
  20. continue
  21. }
  22. newCowList = append(newCowList, cow)
  23. }
  24. if len(newCowList) <= 0 || len(sameTime.CollateNodes) <= 0 {
  25. return nil
  26. }
  27. newSameTimeCowDetailList := make([]*model.EventCowSameTime, 0)
  28. newEventMatingList := make([]*model.EventMating, 0)
  29. collateNodes := make([]*pasturePb.CollateNode, 0)
  30. newEventItemList := make([]*model.EventItem, 0)
  31. var _ = json.Unmarshal([]byte(sameTime.CollateNodes), &collateNodes)
  32. nowTime := time.Now()
  33. for i, collateNode := range collateNodes {
  34. showDay := nowTime
  35. if i > 0 {
  36. showDay = nowTime.Add(time.Hour * 24 * time.Duration(collateNode.NextNodeDay))
  37. }
  38. calendarType := pasturePb.CalendarType_PG
  39. histCount := int64(0)
  40. if collateNode.SameTimeType == pasturePb.SameTimeType_TAI {
  41. calendarType = pasturePb.CalendarType_Mating
  42. histCount = e.GetTowardTaiCowSum(sameTime.PastureId)
  43. newEventMatingList = append(newEventMatingList, model.NewEventMatingList(sameTime.PastureId, newCowList, showDay.Unix(), pasturePb.ExposeEstrusType_Same_Time)...)
  44. newEventItemList = append(newEventItemList, model.NewEventItemList(newCowList, calendarType)...)
  45. } else {
  46. if collateNode.SameTimeType == pasturePb.SameTimeType_RnGH {
  47. calendarType = pasturePb.CalendarType_RnGH
  48. }
  49. histCount = e.GetTowardSameTimeCowSum(sameTime, collateNode.SameTimeType)
  50. newEventCowSameTimeList := model.NewEventCowSameTimeList(sameTime.PastureId, newCowList, sameTime, showDay.Unix(), collateNode.SameTimeType)
  51. newSameTimeCowDetailList = append(newSameTimeCowDetailList, newEventCowSameTimeList...)
  52. newEventItemList = append(newEventItemList, model.NewEventItemList(newCowList, calendarType)...)
  53. }
  54. allCount := int32(len(newCowList)) + int32(histCount)
  55. e.CreatedCalendar(sameTime.PastureId, calendarType, showDay.Format(model.LayoutDate2), allCount)
  56. }
  57. if err = e.DB.Transaction(func(tx *gorm.DB) error {
  58. // 创建牛只同期详情表
  59. if err = tx.Create(newSameTimeCowDetailList).Error; err != nil {
  60. return xerr.WithStack(err)
  61. }
  62. // 创建牛只配种表
  63. if err = tx.Create(newEventMatingList).Error; err != nil {
  64. return xerr.WithStack(err)
  65. }
  66. // 创建牛只事件表
  67. if err = tx.Create(newEventItemList).Error; err != nil {
  68. return xerr.WithStack(err)
  69. }
  70. return nil
  71. }); err != nil {
  72. return xerr.WithStack(err)
  73. }
  74. return nil
  75. }
  76. // GetTowardSameTimeCowSum 获取历史未打激素牛只总数量
  77. func (e *Entry) GetTowardSameTimeCowSum(sameTime *model.SameTime, sameTimeType pasturePb.SameTimeType_Kind) int64 {
  78. res := int64(0)
  79. if err := e.DB.Model(&model.EventCowSameTime{}).
  80. Where("status = ?", pasturePb.IsShow_No).
  81. Where("pasture_id = ?", sameTime.PastureId).
  82. Where("same_time_id = ?", sameTime.Id).
  83. Where("same_time_type = ?", sameTimeType).
  84. Count(&res).Error; err != nil {
  85. zaplog.Error("GetTowardSameTimeCowSum", zap.Any("err", err))
  86. }
  87. return res
  88. }
  89. // GetTowardTaiCowSum 获取历史未配种牛只总数量
  90. func (e *Entry) GetTowardTaiCowSum(pastureId int64) int64 {
  91. res := int64(0)
  92. if err := e.DB.Model(&model.EventMating{}).
  93. Where("pasture_id = ?", pastureId).
  94. Where("status = ?", pasturePb.IsShow_No).
  95. Count(&res).Error; err != nil {
  96. zaplog.Error("GetTowardTaiCowSum", zap.Any("err", err))
  97. }
  98. return res
  99. }