|  | @@ -1,14 +1,13 @@
 | 
	
		
			
				|  |  |  package crontab
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import (
 | 
	
		
			
				|  |  | +	"fmt"
 | 
	
		
			
				|  |  |  	"kpt-pasture/model"
 | 
	
		
			
				|  |  | -	"kpt-pasture/module/backend"
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	"gitee.com/xuyiping_admin/pkg/xerr"
 | 
	
		
			
				|  |  | -	"gorm.io/gorm"
 | 
	
		
			
				|  |  | +	"time"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 | 
	
		
			
				|  |  |  	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
 | 
	
		
			
				|  |  | +	"gitee.com/xuyiping_admin/pkg/xerr"
 | 
	
		
			
				|  |  |  	"go.uber.org/zap"
 | 
	
		
			
				|  |  |  )
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -42,36 +41,51 @@ func (e *Entry) UpdateCowInfo() error {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  // ImmunizationPlan 免疫计划,生成工作单
 | 
	
		
			
				|  |  |  func (e *Entry) ImmunizationPlan() error {
 | 
	
		
			
				|  |  | +	if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
 | 
	
		
			
				|  |  | +		return nil
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  	planList := make([]*model.ImmunizationPlan, 0)
 | 
	
		
			
				|  |  |  	if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&planList).Error; err != nil {
 | 
	
		
			
				|  |  |  		return xerr.WithStack(err)
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
 | 
	
		
			
				|  |  | -		return nil
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | +	var todayCount int32 = 0
 | 
	
		
			
				|  |  | +	defer func() {
 | 
	
		
			
				|  |  | +		e.CreatedWorkOrderCalendar(pasturePb.CalendarType_Immunisation, todayCount)
 | 
	
		
			
				|  |  | +	}()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	for _, plan := range planList {
 | 
	
		
			
				|  |  | +		if plan == nil {
 | 
	
		
			
				|  |  | +			continue
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  |  		cowList := make([]*model.Cow, 0)
 | 
	
		
			
				|  |  | -		pref := e.DB.Select("id").Where("is_remove = ?", pasturePb.IsShow_Ok).
 | 
	
		
			
				|  |  | -			Where("cow_type = ?", plan.CowType)
 | 
	
		
			
				|  |  | +		pref := e.DB.Table(fmt.Sprintf("%s as a", new(model.ImmunizationPlan).TableName())).
 | 
	
		
			
				|  |  | +			Select("a.*").
 | 
	
		
			
				|  |  | +			Where("a.is_remove = ?", pasturePb.IsShow_Ok)
 | 
	
		
			
				|  |  | +		if plan.CowType > 0 {
 | 
	
		
			
				|  |  | +			pref.Where("a.cow_type = ?", plan.CowType)
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  		switch plan.Conditions {
 | 
	
		
			
				|  |  |  		case pasturePb.ImmunizationConditions_Days_Age:
 | 
	
		
			
				|  |  | -			pref.Where("day_age >= ?", plan.Value).
 | 
	
		
			
				|  |  | -				Where("day_age <= ?", plan.Value2)
 | 
	
		
			
				|  |  | +			pref.Where("a.day_age = ?", plan.Value)
 | 
	
		
			
				|  |  |  		case pasturePb.ImmunizationConditions_Days_After_Delivery:
 | 
	
		
			
				|  |  | -			pref.Where("calving_age >= ?", plan.Value).
 | 
	
		
			
				|  |  | -				Where("calving_age <= ?", plan.Value2)
 | 
	
		
			
				|  |  | +			pref.Where("a.calving_age = ?", plan.Value)
 | 
	
		
			
				|  |  |  		case pasturePb.ImmunizationConditions_Days_Of_Pregnancy:
 | 
	
		
			
				|  |  | -			pref.Where("pregnancy_age >= ?", plan.Value).
 | 
	
		
			
				|  |  | -				Where("pregnancy_age <= ?", plan.Value2).
 | 
	
		
			
				|  |  | -				Where("is_pregnant = ?", pasturePb.IsShow_Ok)
 | 
	
		
			
				|  |  | +			pref.Where("a.pregnancy_age = ?", plan.Value).
 | 
	
		
			
				|  |  | +				Where("a.is_pregnant = ?", pasturePb.IsShow_Ok)
 | 
	
		
			
				|  |  |  		case pasturePb.ImmunizationConditions_Month:
 | 
	
		
			
				|  |  |  			// todo 待实现月份
 | 
	
		
			
				|  |  |  		case pasturePb.ImmunizationConditions_Admission_Days:
 | 
	
		
			
				|  |  | -			pref.Where("admission_age >= ?", plan.Value).
 | 
	
		
			
				|  |  | -				Where("admission_age <= ?", plan.Value2)
 | 
	
		
			
				|  |  | +			pref.Where("a.admission_age = ?", plan.Value)
 | 
	
		
			
				|  |  | +		case pasturePb.ImmunizationConditions_Other_Vaccine_After:
 | 
	
		
			
				|  |  | +			if plan.ImmunizationPlanId > 0 {
 | 
	
		
			
				|  |  | +				pref.Joins("INNER JOIN immunization_plan_cow as b ON b.immunization_plan_id = ?", plan.ImmunizationPlanId).
 | 
	
		
			
				|  |  | +					Where("b.cow_id = a.id").
 | 
	
		
			
				|  |  | +					Where("DATE_ADD(b.reality_time, INTERVAL ? DAY) = ?", plan.Value, time.Now().Format(model.LayoutDate2)).
 | 
	
		
			
				|  |  | +					Where("b.status = ?", pasturePb.IsShow_Ok)
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  		if err := pref.Find(&cowList).Error; err != nil {
 | 
	
	
		
			
				|  | @@ -80,88 +94,21 @@ func (e *Entry) ImmunizationPlan() error {
 | 
	
		
			
				|  |  |  		if len(cowList) <= 0 {
 | 
	
		
			
				|  |  |  			continue
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		if err := e.GenerateCalendarByImmunization(cowList, plan); err != nil {
 | 
	
		
			
				|  |  | -			zaplog.Error("crontab", zap.Any("GenerateWorkOrderCalendar", err), zap.Any("cowList", cowList))
 | 
	
		
			
				|  |  | +		todayCount += int32(len(cowList))
 | 
	
		
			
				|  |  | +		newImmunizationPlanCowList := model.NewImmunizationPlanCowList(cowList, plan)
 | 
	
		
			
				|  |  | +		if err := e.DB.Create(newImmunizationPlanCowList).Error; err != nil {
 | 
	
		
			
				|  |  | +			zaplog.Error("ImmunizationPlan", zap.Any("CreateImmunizationPlanCow", err), zap.Any("plan", plan), zap.Any("cowList", cowList))
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  	return nil
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -func (e *Entry) GenerateCalendarByImmunization(cowList []*model.Cow, plan *model.ImmunizationPlan) error {
 | 
	
		
			
				|  |  | -	workOrderCalendarList := e.getWorkOrderCalendar(plan.Name)
 | 
	
		
			
				|  |  | -	newCowList := make([]*model.Cow, 0)
 | 
	
		
			
				|  |  | -	if len(workOrderCalendarList) > 0 {
 | 
	
		
			
				|  |  | -		// 过滤已经存在的牛只数据,避免重复生成工作单
 | 
	
		
			
				|  |  | -		calendarIds := make([]int64, 0)
 | 
	
		
			
				|  |  | -		if err := e.DB.Model(&model.WorkOrderCalendar{}).Select("id").
 | 
	
		
			
				|  |  | -			Where("name = ?", plan.Name).
 | 
	
		
			
				|  |  | -			Where("is_show = ?", pasturePb.IsShow_Ok).
 | 
	
		
			
				|  |  | -			Order("id DESC").
 | 
	
		
			
				|  |  | -			Limit(100). // todo 默认取100条数据
 | 
	
		
			
				|  |  | -			Find(&calendarIds).Error; err != nil {
 | 
	
		
			
				|  |  | -			return xerr.WithStack(err)
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		workOrderList := make([]*model.WorkOrderList, 0)
 | 
	
		
			
				|  |  | -		if err := e.DB.Where("calendar_id IN ?", calendarIds).
 | 
	
		
			
				|  |  | -			Where("is_show  = ?", pasturePb.IsShow_Ok).
 | 
	
		
			
				|  |  | -			Where("is_completion = ?", pasturePb.IsShow_No).
 | 
	
		
			
				|  |  | -			Find(&workOrderList).Error; err != nil {
 | 
	
		
			
				|  |  | -			return xerr.WithStack(err)
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		if len(workOrderList) > 0 {
 | 
	
		
			
				|  |  | -			for _, cow := range cowList {
 | 
	
		
			
				|  |  | -				for _, workOrder := range workOrderList {
 | 
	
		
			
				|  |  | -					if workOrder.CowId == cow.Id {
 | 
	
		
			
				|  |  | -						continue
 | 
	
		
			
				|  |  | -					}
 | 
	
		
			
				|  |  | -				}
 | 
	
		
			
				|  |  | -				newCowList = append(newCowList, cow)
 | 
	
		
			
				|  |  | -			}
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -	count := len(newCowList)
 | 
	
		
			
				|  |  | -	if err := e.DB.Transaction(func(tx *gorm.DB) error {
 | 
	
		
			
				|  |  | -		calendarTypeMap := backend.CalendarTypeMap()
 | 
	
		
			
				|  |  | -		newWorkOrderCalendar := model.NewWorkOrderCalendar(
 | 
	
		
			
				|  |  | -			calendarTypeMap[pasturePb.CalendarType_Immunisation],
 | 
	
		
			
				|  |  | -			pasturePb.CalendarType_Immunisation,
 | 
	
		
			
				|  |  | -			int32(count),
 | 
	
		
			
				|  |  | -		)
 | 
	
		
			
				|  |  | -		if err := tx.Create(newWorkOrderCalendar).Error; err != nil {
 | 
	
		
			
				|  |  | -			return xerr.WithStack(err)
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -		newWorkOrderList := make([]*model.WorkOrderList, 0)
 | 
	
		
			
				|  |  | -		for _, cow := range newCowList {
 | 
	
		
			
				|  |  | -			newWorkOrderList = append(newWorkOrderList, model.NewWorkOrderList(plan.Name, newWorkOrderCalendar.Id, cow.Id))
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -		if err := tx.Create(newWorkOrderList).Error; err != nil {
 | 
	
		
			
				|  |  | -			return xerr.WithStack(err)
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | +// SameTimePlan 同期计划,生成工作单
 | 
	
		
			
				|  |  | +func (e *Entry) SameTimePlan() error {
 | 
	
		
			
				|  |  | +	if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
 | 
	
		
			
				|  |  |  		return nil
 | 
	
		
			
				|  |  | -	}); err != nil {
 | 
	
		
			
				|  |  | -		return xerr.WithStack(err)
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | -	return nil
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -// GetTowardSameTimeCowSum 获取历史未打激素牛只总数量
 | 
	
		
			
				|  |  | -func (e *Entry) GetTowardSameTimeCowSum(sameTimeId int64, showDay string) int64 {
 | 
	
		
			
				|  |  | -	res := int64(0)
 | 
	
		
			
				|  |  | -	if err := e.DB.Model(&model.SameTimeCow{}).
 | 
	
		
			
				|  |  | -		Where("status = ?", pasturePb.IsShow_Ok).
 | 
	
		
			
				|  |  | -		Where("same_time_id = ?", sameTimeId).
 | 
	
		
			
				|  |  | -		Where("show_day = ?", showDay).
 | 
	
		
			
				|  |  | -		Count(&res).Error; err != nil {
 | 
	
		
			
				|  |  | -		zaplog.Error("GetTowardSameTimeCowSum", zap.Any("err", err))
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -	return res
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -// 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)
 | 
	
	
		
			
				|  | @@ -171,9 +118,10 @@ func (e *Entry) SameTimePlan() error {
 | 
	
		
			
				|  |  |  		Where("is_remove = ?", pasturePb.IsShow_Ok).
 | 
	
		
			
				|  |  |  		Where("is_pregnant = ?", pasturePb.IsShow_No)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
 | 
	
		
			
				|  |  | -		return nil
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | +	var todayCount int32 = 0
 | 
	
		
			
				|  |  | +	defer func() {
 | 
	
		
			
				|  |  | +		e.CreatedWorkOrderCalendar(pasturePb.CalendarType_Immunisation, todayCount)
 | 
	
		
			
				|  |  | +	}()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	for _, plan := range sameTimeList {
 | 
	
		
			
				|  |  |  		cowList := make([]*model.Cow, 0)
 | 
	
	
		
			
				|  | @@ -187,6 +135,7 @@ func (e *Entry) SameTimePlan() error {
 | 
	
		
			
				|  |  |  		if len(cowList) <= 0 {
 | 
	
		
			
				|  |  |  			continue
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  | +		todayCount += int32(len(cowList))
 | 
	
		
			
				|  |  |  		if err := e.GenerateCalendarBySameTimePlan(cowList, plan); err != nil {
 | 
	
		
			
				|  |  |  			zaplog.Error("crontab", zap.Any("GenerateCalendarBySameTimePlan", err), zap.Any("cowList", cowList), zap.Any("plan", plan))
 | 
	
		
			
				|  |  |  			continue
 |