work_cron.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. zaplog.Info("GenerateCalendarBySameTimePlan", zap.Any("sameTime", sameTime), zap.Any("cow", cow))
  22. continue
  23. }
  24. newCowList = append(newCowList, cow)
  25. }
  26. calendarList := make([]*model.Calendar, 0)
  27. newSameTimeCowDetailList := make([]*model.SameTimeCowDetail, 0)
  28. if len(sameTime.CollateNodes) > 0 {
  29. collateNodes := make([]*pasturePb.CollateNode, 0)
  30. var _ = json.Unmarshal([]byte(sameTime.CollateNodes), &collateNodes)
  31. nowTime := time.Now()
  32. for i, collateNode := range collateNodes {
  33. showDay := nowTime.Format(model.LayoutDate2)
  34. if i > 0 {
  35. showDay = nowTime.Add(time.Hour * 24 * time.Duration(collateNode.NextNodeDay)).Format(model.LayoutDate2)
  36. }
  37. calendarName := backend.CalendarTypeMap()[pasturePb.CalendarType_PG]
  38. calendarType := pasturePb.CalendarType_PG
  39. if collateNode.SameTimeType == pasturePb.SameTimeType_RnGH {
  40. calendarName = backend.CalendarTypeMap()[pasturePb.CalendarType_RnGH]
  41. calendarType = pasturePb.CalendarType_RnGH
  42. }
  43. if collateNode.SameTimeType == pasturePb.SameTimeType_TAI {
  44. calendarName = backend.CalendarTypeMap()[pasturePb.CalendarType_Mating]
  45. calendarType = pasturePb.CalendarType_Mating
  46. }
  47. histCount := e.GetTowardSameTimeCowSum(sameTime.Id, collateNode.SameTimeType)
  48. calendarList = append(calendarList, &model.Calendar{
  49. Name: calendarName,
  50. CalendarType: calendarType,
  51. Count: int32(len(newCowList)) + int32(histCount),
  52. ShowDay: showDay,
  53. IsShow: pasturePb.IsShow_Ok,
  54. })
  55. newSameTimeCowDetailList = append(
  56. newSameTimeCowDetailList,
  57. model.NewSameTimeCowDetailList(newCowList, sameTime.Id, showDay, collateNode.SameTimeType)...,
  58. )
  59. }
  60. }
  61. newSameTimeCowList := model.NewSameTimeCowList(newCowList, sameTime)
  62. if err := e.DB.Transaction(func(tx *gorm.DB) error {
  63. // 创建牛只同期日历表
  64. if err := tx.Create(calendarList).Error; err != nil {
  65. return xerr.WithStack(err)
  66. }
  67. // 创建牛只同期总表
  68. if err := tx.Create(newSameTimeCowList).Error; err != nil {
  69. return xerr.WithStack(err)
  70. }
  71. if err := tx.Create(newSameTimeCowDetailList).Error; err != nil {
  72. return xerr.WithStack(err)
  73. }
  74. return nil
  75. }); err != nil {
  76. return 0, xerr.WithStack(err)
  77. }
  78. return int32(len(newCowList)), nil
  79. }
  80. func (e *Entry) getWorkOrderCalendar(name string) []*model.Calendar {
  81. res := make([]*model.Calendar, 0)
  82. if err := e.DB.Where("name = ?", name).
  83. Where("is_show = ?", pasturePb.IsShow_Ok).
  84. Find(&res).Error; err != nil {
  85. zaplog.Error("getWorkOrderCalendar", zap.Any("err", err))
  86. }
  87. return res
  88. }
  89. // GetTowardSameTimeCowSum 获取历史未打激素牛只总数量
  90. func (e *Entry) GetTowardSameTimeCowSum(sameTimeId int64, sameTimeType pasturePb.SameTimeType_Kind) int64 {
  91. res := int64(0)
  92. if err := e.DB.Model(&model.SameTimeCowDetail{}).
  93. Where("status = ?", pasturePb.IsShow_No).
  94. Where("same_time_id = ?", sameTimeId).
  95. Where("same_time_type = ?", sameTimeType).
  96. Count(&res).Error; err != nil {
  97. zaplog.Error("GetTowardSameTimeCowSum", zap.Any("err", err))
  98. }
  99. return res
  100. }