|  | @@ -8,6 +8,8 @@ import (
 | 
	
		
			
				|  |  |  	"kpt-pasture/util"
 | 
	
		
			
				|  |  |  	"time"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +	"gorm.io/gorm"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 | 
	
		
			
				|  |  |  	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
 | 
	
		
			
				|  |  |  	"gitee.com/xuyiping_admin/pkg/xerr"
 | 
	
	
		
			
				|  | @@ -15,10 +17,11 @@ import (
 | 
	
		
			
				|  |  |  )
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  const (
 | 
	
		
			
				|  |  | -	UpdateCowInfo    = "UpdateCowInfo"
 | 
	
		
			
				|  |  | -	ImmunizationPlan = "ImmunizationPlan"
 | 
	
		
			
				|  |  | -	SameTimePlan     = "SameTimePlan"
 | 
	
		
			
				|  |  | -	WorkOrderMaster  = "WorkOrderMaster"
 | 
	
		
			
				|  |  | +	UpdateCowInfo      = "UpdateCowInfo"
 | 
	
		
			
				|  |  | +	ImmunizationPlan   = "ImmunizationPlan"
 | 
	
		
			
				|  |  | +	SameTimePlan       = "SameTimePlan"
 | 
	
		
			
				|  |  | +	WorkOrderMaster    = "WorkOrderMaster"
 | 
	
		
			
				|  |  | +	SystemBasicCrontab = "SystemBasicCrontab"
 | 
	
		
			
				|  |  |  )
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  // GenerateAsynqWorkOrder 异步生成工作单
 | 
	
	
		
			
				|  | @@ -44,7 +47,6 @@ func (e *Entry) GenerateAsynqWorkOrder() error {
 | 
	
		
			
				|  |  |  			continue
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  		execTime := timeUnix - nowTime
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  		task := model.NewTaskWorkOrderPayload(workOrder.Id, time.Duration(execTime)*time.Second)
 | 
	
		
			
				|  |  |  		if _, err = e.AsynqClient.CtxEnqueue(context.Background(), task); err != nil {
 | 
	
		
			
				|  |  |  			zaplog.Error("PushMessage CtxEnqueue", zap.Any("Err", err))
 | 
	
	
		
			
				|  | @@ -142,7 +144,19 @@ func (e *Entry) ImmunizationPlan() error {
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  		todayCount += int32(len(cowList))
 | 
	
		
			
				|  |  |  		newImmunizationPlanCowList := model.NewCowImmunizationPlanList(cowList, plan)
 | 
	
		
			
				|  |  | -		if err := e.DB.Create(newImmunizationPlanCowList).Error; err != nil {
 | 
	
		
			
				|  |  | +		newEventItemList := model.NewEventItemList(cowList, pasturePb.CalendarType_Immunisation)
 | 
	
		
			
				|  |  | +		if err := e.DB.Transaction(func(tx *gorm.DB) error {
 | 
	
		
			
				|  |  | +			if err := tx.Create(newImmunizationPlanCowList).Error; err != nil {
 | 
	
		
			
				|  |  | +				return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			if err := tx.Create(newEventItemList).Error; err != nil {
 | 
	
		
			
				|  |  | +				return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +			return nil
 | 
	
		
			
				|  |  | +		}); err != nil {
 | 
	
		
			
				|  |  |  			zaplog.Error("ImmunizationPlan", zap.Any("newImmunizationPlanCowList", err), zap.Any("plan", plan), zap.Any("cowList", cowList))
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  	}
 | 
	
	
		
			
				|  | @@ -256,8 +270,14 @@ func (e *Entry) UpdateSameTime() error {
 | 
	
		
			
				|  |  |  	return nil
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -// PregnancyCheck 妊娠期检查
 | 
	
		
			
				|  |  | -func (e *Entry) PregnancyCheck() error {
 | 
	
		
			
				|  |  | +// SystemBasicCrontab 基础配置计划任务
 | 
	
		
			
				|  |  | +func (e *Entry) SystemBasicCrontab() error {
 | 
	
		
			
				|  |  | +	if ok := e.IsExistCrontabLog(SystemBasicCrontab); ok {
 | 
	
		
			
				|  |  | +		return nil
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	defer func() {
 | 
	
		
			
				|  |  | +		e.CreateCrontabLog(SystemBasicCrontab)
 | 
	
		
			
				|  |  | +	}()
 | 
	
		
			
				|  |  |  	systemBasicList := make([]*model.SystemBasic, 0)
 | 
	
		
			
				|  |  |  	if err := e.DB.Model(new(model.SystemBasic)).
 | 
	
		
			
				|  |  |  		Where("is_show = ?", pasturePb.IsShow_Ok).
 | 
	
	
		
			
				|  | @@ -272,7 +292,6 @@ func (e *Entry) PregnancyCheck() error {
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	currWeekValue := time.Now().Weekday()
 | 
	
		
			
				|  |  | -	planDay := time.Now().Unix()
 | 
	
		
			
				|  |  |  	for _, v := range systemBasicList {
 | 
	
		
			
				|  |  |  		// 周执行
 | 
	
		
			
				|  |  |  		if v.WeekValue >= 0 && time.Weekday(v.WeekValue) != currWeekValue {
 | 
	
	
		
			
				|  | @@ -288,6 +307,7 @@ func (e *Entry) PregnancyCheck() error {
 | 
	
		
			
				|  |  |  		case model.PregnantCheckForSecond: // 过滤初检空怀的牛只
 | 
	
		
			
				|  |  |  			pref.Where("breed_status = ?", pasturePb.BreedStatus_Pregnant)
 | 
	
		
			
				|  |  |  		case model.WeaningAge:
 | 
	
		
			
				|  |  | +			pref.Where("day_age = ?", v.MinValue)
 | 
	
		
			
				|  |  |  		case model.PregnancyAge:
 | 
	
		
			
				|  |  |  			pref.Where("pregnancy_age >= ?", v.MinValue).
 | 
	
		
			
				|  |  |  				Where("breed_status = ?", pasturePb.BreedStatus_Pregnant)
 | 
	
	
		
			
				|  | @@ -310,27 +330,26 @@ func (e *Entry) PregnancyCheck() error {
 | 
	
		
			
				|  |  |  		if len(cowList) <= 0 {
 | 
	
		
			
				|  |  |  			continue
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		e.InitEventData(cowList, v.Name, planDay)
 | 
	
		
			
				|  |  | +		e.InitEventData(cowList, v.Name)
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	return nil
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -func (e *Entry) InitEventData(cowList []*model.Cow, systemBasicName string, planDay int64) {
 | 
	
		
			
				|  |  | +func (e *Entry) InitEventData(cowList []*model.Cow, systemBasicName string) {
 | 
	
		
			
				|  |  |  	switch systemBasicName {
 | 
	
		
			
				|  |  |  	case model.PregnantCheckForFirst, model.PregnantCheckForSecond:
 | 
	
		
			
				|  |  | -		eventPregnantCheckDataList := model.NewEventPregnantCheckList(cowList, planDay, systemBasicName)
 | 
	
		
			
				|  |  | +		eventPregnantCheckDataList := model.NewEventPregnantCheckList(cowList, systemBasicName)
 | 
	
		
			
				|  |  |  		if err := e.DB.Create(eventPregnantCheckDataList).Error; err != nil {
 | 
	
		
			
				|  |  |  			zaplog.Error("crontab", zap.Any("InitEventData", err), zap.Any("eventPregnantCheckDataList", eventPregnantCheckDataList))
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  	case model.WeaningAge:
 | 
	
		
			
				|  |  | -		eventWeaningDataList := model.NewEventWeaningList(cowList, planDay)
 | 
	
		
			
				|  |  | +		eventWeaningDataList := model.NewEventWeaningList(cowList)
 | 
	
		
			
				|  |  |  		if err := e.DB.Create(eventWeaningDataList).Error; err != nil {
 | 
	
		
			
				|  |  |  			zaplog.Error("crontab", zap.Any("InitEventData", err), zap.Any("eventWeaningDataList", eventWeaningDataList))
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  	case model.PregnancyAge:
 | 
	
		
			
				|  |  | -		eventCalvingList := model.NewEventCalvingList(cowList, planDay)
 | 
	
		
			
				|  |  | +		eventCalvingList := model.NewEventCalvingList(cowList)
 | 
	
		
			
				|  |  |  		if err := e.DB.Create(eventCalvingList).Error; err != nil {
 | 
	
		
			
				|  |  |  			zaplog.Error("crontab", zap.Any("InitEventData", err), zap.Any("eventCalvingList", eventCalvingList))
 | 
	
		
			
				|  |  |  		}
 |