|
@@ -2,6 +2,7 @@ package crontab
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "errors"
|
|
|
"kpt-pasture/model"
|
|
|
"kpt-pasture/util"
|
|
|
"time"
|
|
@@ -52,9 +53,10 @@ func (e *Entry) GenerateAsynqWorkOrder() error {
|
|
|
continue
|
|
|
}
|
|
|
execTime := time.Now().Unix() - timeUnix
|
|
|
- if _, err := e.AsynqClient.CtxEnqueue(
|
|
|
+
|
|
|
+ if _, err = e.AsynqClient.CtxEnqueue(
|
|
|
context.Background(),
|
|
|
- model.NewTaskWorkOrderPayload(e.Cfg.FarmName, workOrder.Id, time.Duration(execTime)*time.Second),
|
|
|
+ model.NewTaskWorkOrderPayload(workOrder.Id, time.Duration(execTime)*time.Second),
|
|
|
); err != nil {
|
|
|
zaplog.Error("PushMessage CtxEnqueue", zap.Any("Err", err))
|
|
|
}
|
|
@@ -108,6 +110,47 @@ func (e *Entry) ImmunizationPlan() error {
|
|
|
|
|
|
// SameTimePlan 同期计划,生成工作单
|
|
|
func (e *Entry) SameTimePlan() error {
|
|
|
+ sameTimeList := make([]*model.SameTime, 0)
|
|
|
+ if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&sameTimeList).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ cowSameTimeList := make([]*model.SameTimeCow, 0)
|
|
|
+ if err := e.DB.Select("cow_id").Where("status = ?", pasturePb.IsShow_Ok).Find(&cowSameTimeList).Error; err != nil {
|
|
|
+ if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cowIds := make([]int64, 0)
|
|
|
+ if len(cowSameTimeList) > 0 {
|
|
|
+ for _, cowSameTime := range cowSameTimeList {
|
|
|
+ cowIds = append(cowIds, cowSameTime.CowId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, plan := range sameTimeList {
|
|
|
+ cowList := make([]*model.Cow, 0)
|
|
|
+ pref := e.DB.Select("id").
|
|
|
+ Where("is_remove = ?", pasturePb.IsShow_Ok).
|
|
|
+ Where("is_pregnant = ?", pasturePb.IsShow_No).
|
|
|
+ Where("calving_age >= ?", plan.PostpartumDaysStart).
|
|
|
+ Where("calving_age <= ?", plan.PostpartumDaysEnd)
|
|
|
+ if len(cowIds) > 0 {
|
|
|
+ pref.Where("id NOT IN ?", cowIds)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := pref.Find(&cowList); err != nil {
|
|
|
+ zaplog.Error("crontab", zap.Any("SameTimePlan", err), zap.Any("plan", plan))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if len(cowList) <= 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if err := e.GenerateCalendarBySameTimePlan(cowList, plan); err != nil {
|
|
|
+ zaplog.Error("crontab", zap.Any("GenerateCalendarBySameTimePlan", err), zap.Any("cowList", cowList), zap.Any("plan", plan))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -165,6 +208,29 @@ func (e *Entry) GenerateCalendarByImmunization(cowList []*model.Cow, name string
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+func (e *Entry) GenerateCalendarBySameTimePlan(cowList []*model.Cow, sameTime *model.SameTime) error {
|
|
|
+ if len(cowList) <= 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ cowSameTimeList := make([]*model.SameTimeCow, 0)
|
|
|
+ for _, cow := range cowList {
|
|
|
+ cowSameTimeList = append(cowSameTimeList, &model.SameTimeCow{
|
|
|
+ CowId: cow.Id,
|
|
|
+ SameTimeId: sameTime.Id,
|
|
|
+ Status: pasturePb.IsShow_Ok,
|
|
|
+ StartAt: time.Now().Unix(),
|
|
|
+ EndAt: 0,
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := e.DB.Create(cowSameTimeList).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
func (e *Entry) getWorkOrderCalendar(name string) []*model.WorkOrderCalendar {
|
|
|
res := make([]*model.WorkOrderCalendar, 0)
|
|
|
if err := e.DB.Where("name = ?", name).
|