milk_daily.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package crontab
  2. import (
  3. "kpt-pasture/model"
  4. "kpt-pasture/util"
  5. "time"
  6. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  7. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  8. "go.uber.org/zap"
  9. )
  10. func (e *Entry) InsertMilkDaily() error {
  11. pastureList := e.FindPastureList()
  12. if pastureList == nil || len(pastureList) == 0 {
  13. return nil
  14. }
  15. for _, pasture := range pastureList {
  16. // 获取最大记录日期或默认日期
  17. pastureId := pasture.Id
  18. var maxDateTime string
  19. if err := e.DB.Model(new(model.MilkDaily)).
  20. Where("pasture_id = ?", pastureId).
  21. Select("IFNULL(MAX(heat_date) + INTERVAL 1 DAY, CURDATE() - INTERVAL 1 MONTH) AS max_date_time").
  22. Scan(&maxDateTime).Error; err != nil {
  23. zaplog.Error("InsertMilkDaily", zap.Any("pastureId", pastureId), zap.Any("err", err))
  24. continue
  25. }
  26. maxDate, err := util.TimeParseLocal(model.LayoutDate2, maxDateTime)
  27. if err != nil {
  28. zaplog.Error("InsertMilkDaily", zap.Any("pastureId", pastureId), zap.Any("err", err))
  29. continue
  30. }
  31. e.ProcessMilkDaily(pastureId, maxDate)
  32. }
  33. return nil
  34. }
  35. func (e *Entry) ProcessMilkDaily(pastureId int64, maxDate time.Time) {
  36. nowTime := time.Now().Local()
  37. // 处理每一天的数据
  38. for maxDate.Before(nowTime) {
  39. // 处理有胎次的奶牛
  40. if err := e.processCowsWithLact(maxDate); err != nil {
  41. zaplog.Error("ProcessMilkDaily", zap.Any("processCowsWithFetal", err))
  42. }
  43. // 处理无胎次的奶牛
  44. if err := e.processCowsWithNoLact(maxDate); err != nil {
  45. zaplog.Error("ProcessMilkDaily", zap.Any("processCowsWithoutFetal", err))
  46. }
  47. // 日期加1天
  48. maxDate = maxDate.AddDate(0, 0, 1)
  49. }
  50. }
  51. func (e *Entry) UpdateMilkDaily(pastureId int64, maxDateTime string) error {
  52. //yesterday := time.Now().Local().AddDate(0, 0, -1).Format(model.LayoutDate2)
  53. return nil
  54. }
  55. // 处理有胎次的奶牛
  56. func (e *Entry) processCowsWithLact(recordDate time.Time) error {
  57. // 查询有胎次且在记录日期前有记录的奶牛
  58. cowList := make([]*model.Cow, 0)
  59. if err := e.DB.Model(new(model.Cow)).
  60. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  61. Where("lact > ?", 0).
  62. Where("last_calving_at <= ?", recordDate.Local().Unix()).
  63. Find(&cowList).Error; err != nil {
  64. return err
  65. }
  66. // 批量插入数据
  67. milkDailyList := make([]*model.MilkDaily, 0)
  68. for _, cow := range cowList {
  69. calvingDate := ""
  70. if cow.LastCalvingAt > 0 {
  71. calvingDate = time.Unix(cow.LastCalvingAt, 0).Local().Format(model.LayoutDate2)
  72. }
  73. milkDaily := &model.MilkDaily{
  74. CowId: cow.Id,
  75. Lact: cow.Lact,
  76. PastureId: cow.PastureId,
  77. EarNumber: cow.EarNumber,
  78. CalvingDate: calvingDate,
  79. HeatDate: recordDate.Format(model.LayoutDate2),
  80. LactationAge: cow.LactationAge,
  81. PenId: cow.PenId,
  82. PenName: cow.PenName,
  83. BreedStatus: cow.BreedStatus,
  84. }
  85. milkDailyList = append(milkDailyList, milkDaily)
  86. }
  87. if len(milkDailyList) > 0 {
  88. // 分批次插入数据
  89. for i := 0; i < len(milkDailyList); i += 100 {
  90. end := i + 100
  91. if end > len(milkDailyList) {
  92. end = len(milkDailyList)
  93. }
  94. if err := e.DB.Model(new(model.MilkDaily)).
  95. Create(milkDailyList[i:end]).Error; err != nil {
  96. return err
  97. }
  98. }
  99. }
  100. return nil
  101. }
  102. // 处理无胎次的奶牛
  103. func (e *Entry) processCowsWithNoLact(recordDate time.Time) error {
  104. // 查询无胎次且EID1>0的奶牛
  105. cowList := make([]*model.Cow, 0)
  106. err := e.DB.Model(new(model.Cow)).
  107. //Select("c.intCowId as cow_id, c.intPastureId as pasture_id, c.varCowCode as cow_code,c.dateBirthDate as birth_date, c.intCurBar as cur_bar").
  108. //Where("(c.dateLeave IS NULL OR c.dateLeave > ?) AND c.intCurFetal = 0 AND c.EID1 > 0", recordDate).
  109. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  110. Where("lact = ?", 0).
  111. Where("neck_ring_number != ?", "").
  112. Find(&cowList).Error
  113. if err != nil {
  114. return err
  115. }
  116. // 批量插入数据
  117. milkDailyList := make([]*model.MilkDaily, 0)
  118. for _, cow := range cowList {
  119. birthDate, calvingDate := "", ""
  120. if cow.BirthAt > 0 {
  121. birthDate = time.Unix(cow.BirthAt, 0).Local().Format(model.LayoutDate2)
  122. }
  123. if cow.LastCalvingAt > 0 {
  124. calvingDate = time.Unix(cow.LastCalvingAt, 0).Local().Format(model.LayoutDate2)
  125. }
  126. milkDaily := &model.MilkDaily{
  127. CowId: cow.Id,
  128. Lact: cow.Lact,
  129. PastureId: cow.PastureId,
  130. EarNumber: cow.EarNumber,
  131. BirthDate: birthDate,
  132. CalvingDate: calvingDate,
  133. LactationAge: cow.LactationAge,
  134. HeatDate: recordDate.Format(model.LayoutDate2),
  135. PenId: cow.PenId,
  136. PenName: cow.PenName,
  137. BreedStatus: cow.BreedStatus,
  138. }
  139. milkDailyList = append(milkDailyList, milkDaily)
  140. }
  141. if len(milkDailyList) > 0 {
  142. if err = e.DB.Model(new(model.MilkDaily)).Create(&milkDailyList).Error; err != nil {
  143. return err
  144. }
  145. }
  146. return nil
  147. }