work_cron.go 3.8 KB

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