work_cron.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package crontab
  2. import (
  3. "encoding/json"
  4. "kpt-pasture/model"
  5. "kpt-pasture/module/backend"
  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) (int32, error) {
  15. if len(cowList) <= 0 {
  16. return 0, 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 0, nil
  27. }
  28. calendarList := make([]*model.Calendar, 0)
  29. newSameTimeCowDetailList := make([]*model.EventCowSameTime, 0)
  30. newEventMatingList := make([]*model.EventMating, 0)
  31. collateNodes := make([]*pasturePb.CollateNode, 0)
  32. newEventItemList := make([]*model.EventItem, 0)
  33. var _ = json.Unmarshal([]byte(sameTime.CollateNodes), &collateNodes)
  34. nowTime := time.Now()
  35. for i, collateNode := range collateNodes {
  36. showDay := nowTime.Format(model.LayoutDate2)
  37. if i > 0 {
  38. showDay = nowTime.Add(time.Hour * 24 * time.Duration(collateNode.NextNodeDay)).Format(model.LayoutDate2)
  39. }
  40. calendarName := backend.CalendarTypeMap()[pasturePb.CalendarType_PG]
  41. calendarType := pasturePb.CalendarType_PG
  42. histCount := int64(0)
  43. if collateNode.SameTimeType == pasturePb.SameTimeType_TAI {
  44. calendarName = backend.CalendarTypeMap()[pasturePb.CalendarType_Mating]
  45. calendarType = pasturePb.CalendarType_Mating
  46. histCount = e.GetTowardTaiCowSum()
  47. newEventMatingList = append(newEventMatingList, model.NewEventMatingList(newCowList, showDay)...)
  48. newEventItemList = append(newEventItemList, model.NewEventItemList(newCowList, calendarType)...)
  49. } else {
  50. if collateNode.SameTimeType == pasturePb.SameTimeType_RnGH {
  51. calendarName = backend.CalendarTypeMap()[pasturePb.CalendarType_RnGH]
  52. calendarType = pasturePb.CalendarType_RnGH
  53. }
  54. newEventItemList = append(newEventItemList, model.NewEventItemList(newCowList, calendarType)...)
  55. histCount = e.GetTowardSameTimeCowSum(sameTime.Id, collateNode.SameTimeType)
  56. newSameTimeCowDetailList = append(
  57. newSameTimeCowDetailList,
  58. model.NewCowSameTimeDetailList(newCowList, sameTime.Id, showDay, collateNode.SameTimeType)...,
  59. )
  60. }
  61. calendarList = append(calendarList, &model.Calendar{
  62. Name: calendarName,
  63. CalendarType: calendarType,
  64. Count: int32(len(newCowList)) + int32(histCount),
  65. ShowDay: showDay,
  66. IsShow: pasturePb.IsShow_Ok,
  67. })
  68. }
  69. newSameTimeCowList := model.NewCowSameTimeList(newCowList, sameTime)
  70. if err := e.DB.Transaction(func(tx *gorm.DB) error {
  71. // 创建牛只同期日历表
  72. if err := tx.Create(calendarList).Error; err != nil {
  73. return xerr.WithStack(err)
  74. }
  75. // 创建牛只同期总表
  76. if err := tx.Create(newSameTimeCowList).Error; err != nil {
  77. return xerr.WithStack(err)
  78. }
  79. // 创建牛只同期详情表
  80. if err := tx.Create(newSameTimeCowDetailList).Error; err != nil {
  81. return xerr.WithStack(err)
  82. }
  83. // 创建牛只配种表
  84. if err := tx.Create(newEventMatingList).Error; err != nil {
  85. return xerr.WithStack(err)
  86. }
  87. // 创建牛只事件表
  88. if err := tx.Create(newEventItemList).Error; err != nil {
  89. return xerr.WithStack(err)
  90. }
  91. return nil
  92. }); err != nil {
  93. return 0, xerr.WithStack(err)
  94. }
  95. return int32(len(newCowList)), nil
  96. }
  97. func (e *Entry) getWorkOrderCalendar(name string) []*model.Calendar {
  98. res := make([]*model.Calendar, 0)
  99. if err := e.DB.Where("name = ?", name).
  100. Where("is_show = ?", pasturePb.IsShow_Ok).
  101. Find(&res).Error; err != nil {
  102. zaplog.Error("getWorkOrderCalendar", zap.Any("err", err))
  103. }
  104. return res
  105. }
  106. // GetTowardSameTimeCowSum 获取历史未打激素牛只总数量
  107. func (e *Entry) GetTowardSameTimeCowSum(sameTimeId int64, sameTimeType pasturePb.SameTimeType_Kind) int64 {
  108. res := int64(0)
  109. if err := e.DB.Model(&model.EventCowSameTime{}).
  110. Where("status = ?", pasturePb.IsShow_No).
  111. Where("same_time_id = ?", sameTimeId).
  112. Where("same_time_type = ?", sameTimeType).
  113. Count(&res).Error; err != nil {
  114. zaplog.Error("GetTowardSameTimeCowSum", zap.Any("err", err))
  115. }
  116. return res
  117. }
  118. // GetTowardTaiCowSum 获取历史未配种牛只总数量
  119. func (e *Entry) GetTowardTaiCowSum() int64 {
  120. res := int64(0)
  121. if err := e.DB.Model(&model.EventMating{}).
  122. Where("status = ?", pasturePb.IsShow_No).
  123. Count(&res).Error; err != nil {
  124. zaplog.Error("GetTowardTaiCowSum", zap.Any("err", err))
  125. }
  126. return res
  127. }