| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 | 
							- package crontab
 
- import (
 
- 	"context"
 
- 	"fmt"
 
- 	"kpt-pasture/model"
 
- 	"kpt-pasture/module/backend"
 
- 	"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"
 
- 	"go.uber.org/zap"
 
- )
 
- const (
 
- 	UpdateCowInfo      = "UpdateCowInfo"
 
- 	ImmunizationPlan   = "ImmunizationPlan"
 
- 	SameTimePlan       = "SameTimePlan"
 
- 	WorkOrderMaster    = "WorkOrderMaster"
 
- 	SystemBasicCrontab = "SystemBasicCrontab"
 
- )
 
- // GenerateAsynqWorkOrder 异步生成工作单
 
- func (e *Entry) GenerateAsynqWorkOrder() error {
 
- 	if ok := e.IsExistCrontabLog(WorkOrderMaster); ok {
 
- 		return nil
 
- 	}
 
- 	defer func() {
 
- 		e.CreateCrontabLog(WorkOrderMaster)
 
- 	}()
 
- 	workOrderList := make([]*model.WorkOrderMaster, 0)
 
- 	if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&workOrderList).Error; err != nil {
 
- 		return err
 
- 	}
 
- 	for _, workOrder := range workOrderList {
 
- 		timeUnix, err := util.ConvertParseLocalUnix(workOrder.ExecTime)
 
- 		if timeUnix <= 0 || err != nil {
 
- 			zaplog.Error("crontab", zap.Any("GenerateWorkOrder", err), zap.Any("execTime", workOrder.ExecTime))
 
- 			continue
 
- 		}
 
- 		nowTime := time.Now().Unix()
 
- 		if timeUnix < nowTime {
 
- 			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))
 
- 		}
 
- 	}
 
- 	return nil
 
- }
 
- // UpdateCowInfo 牛只基本信息维护
 
- func (e *Entry) UpdateCowInfo() error {
 
- 	cowList := make([]*model.Cow, 0)
 
- 	if err := e.DB.Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).Find(&cowList).Error; err != nil {
 
- 		return err
 
- 	}
 
- 	if ok := e.IsExistCrontabLog(UpdateCowInfo); ok {
 
- 		return nil
 
- 	}
 
- 	defer func() {
 
- 		e.CreateCrontabLog(UpdateCowInfo)
 
- 	}()
 
- 	for _, cow := range cowList {
 
- 		if err := e.DB.Model(new(model.Cow)).
 
- 			Where("id = ?", cow.Id).
 
- 			Updates(map[string]interface{}{
 
- 				"day_age":       cow.GetDayAge(),
 
- 				"calving_at":    cow.GetCalvingAge(),
 
- 				"pregnancy_age": cow.GetDaysPregnant(),
 
- 				"admission_age": cow.GetAdmissionAge(),
 
- 				"abortion_age":  cow.GetAbortionAge(),
 
- 				// todo 牛只类型待完善
 
- 			}).Error; err != nil {
 
- 			zaplog.Error("Crontab", zap.Any("UpdateCowDayAge", err))
 
- 		}
 
- 	}
 
- 	return nil
 
- }
 
- // ImmunizationPlan 免疫计划,生成工作单
 
- func (e *Entry) ImmunizationPlan() error {
 
- 	if ok := e.IsExistCrontabLog(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)
 
- 	}
 
- 	var todayCount int32 = 0
 
- 	defer func() {
 
- 		var count int64 = 0
 
- 		if err := e.DB.Model(new(model.EventImmunizationPlan)).
 
- 			Where("status = ?", pasturePb.IsShow_Ok).
 
- 			Count(&count).Error; err != nil {
 
- 			zaplog.Error("Crontab", zap.Any("ImmunizationPlanDefer", err))
 
- 		}
 
- 		todayCount += int32(count)
 
- 		if todayCount > 0 {
 
- 			e.CreatedCalendar(pasturePb.CalendarType_Immunisation, todayCount)
 
- 		}
 
- 		e.CreateCrontabLog(ImmunizationPlan)
 
- 	}()
 
- 	for _, plan := range planList {
 
- 		cowList := make([]*model.Cow, 0)
 
- 		pref := e.DB.Table(fmt.Sprintf("%s as a", new(model.Cow).TableName())).
 
- 			Select("a.*").
 
- 			Where("a.admission_status = ?", pasturePb.AdmissionStatus_Admission)
 
- 		if plan.CowType > 0 {
 
- 			pref.Where("a.cow_type = ?", plan.CowType)
 
- 		}
 
- 		switch plan.Conditions {
 
- 		case pasturePb.ImmunizationConditions_Days_Age:
 
- 			pref.Where("a.day_age = ?", plan.Value)
 
- 		case pasturePb.ImmunizationConditions_Days_After_Delivery:
 
- 			pref.Where("a.calving_age = ?", plan.Value)
 
- 		case pasturePb.ImmunizationConditions_Days_Of_Pregnancy:
 
- 			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("a.admission_age = ?", plan.Value)
 
- 		case pasturePb.ImmunizationConditions_Other_Vaccine_After:
 
- 			if plan.ImmunizationPlanId > 0 {
 
- 				pref.Joins("INNER JOIN event_immunization_plan 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 {
 
- 			return xerr.WithStack(err)
 
- 		}
 
- 		if len(cowList) <= 0 {
 
- 			continue
 
- 		}
 
- 		todayCount += int32(len(cowList))
 
- 		penList, _ := e.GetPenMapList()
 
- 		newImmunizationPlanCowList := model.NewCowImmunizationPlanList(cowList, penList, plan)
 
- 		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))
 
- 		}
 
- 	}
 
- 	zaplog.Info("ImmunizationPlan", zap.Any("todayCount", todayCount))
 
- 	return nil
 
- }
 
- // SameTimePlan 同期计划,生成工作单
 
- func (e *Entry) SameTimePlan() error {
 
- 	if ok := e.IsExistCrontabLog(SameTimePlan); ok {
 
- 		return nil
 
- 	}
 
- 	sameTimeList := make([]*model.SameTime, 0)
 
- 	if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&sameTimeList).Error; err != nil {
 
- 		return xerr.WithStack(err)
 
- 	}
 
- 	// 更新日历里面的数据
 
- 	var todayCount int32 = 0
 
- 	defer func() {
 
- 		if todayCount > 0 {
 
- 			e.CreatedCalendar(pasturePb.CalendarType_Immunisation, todayCount)
 
- 		}
 
- 		e.CreateCrontabLog(SameTimePlan)
 
- 	}()
 
- 	currWeek := time.Now().Weekday()
 
- 	for _, sameTime := range sameTimeList {
 
- 		if sameTime == nil {
 
- 			continue
 
- 		}
 
- 		if time.Weekday(sameTime.WeekType) != currWeek {
 
- 			continue
 
- 		}
 
- 		cowList := make([]*model.Cow, 0)
 
- 		pref := e.DB.Model(new(model.Cow)).
 
- 			Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
 
- 			Where("sex = ?", pasturePb.Genders_Male)
 
- 		switch sameTime.CowType {
 
- 		case pasturePb.SameTimeCowType_Breeding_Calf:
 
- 			pref.Where("calving_age >= ?", sameTime.PostpartumDaysStart).
 
- 				Where("calving_age <= ?", sameTime.PostpartumDaysEnd).
 
- 				Where("is_pregnant = ?", pasturePb.IsShow_No)
 
- 		case pasturePb.SameTimeCowType_Empty:
 
- 			pref.Where(
 
- 				e.DB.Where("breed_status = ?", pasturePb.BreedStatus_Empty).
 
- 					Or("breed_status = ?", pasturePb.BreedStatus_Abort),
 
- 			).Where("is_pregnant = ?", pasturePb.IsShow_No)
 
- 		default:
 
- 			continue
 
- 		}
 
- 		if err := pref.Find(&cowList).Error; err != nil {
 
- 			zaplog.Error("crontab", zap.Any("err", err), zap.Any("sameTime", sameTime))
 
- 			return xerr.WithStack(err)
 
- 		}
 
- 		if len(cowList) <= 0 {
 
- 			continue
 
- 		}
 
- 		currCount, err := e.GenerateCalendarBySameTimePlan(cowList, sameTime)
 
- 		if err != nil {
 
- 			zaplog.Error("crontab",
 
- 				zap.Any("GenerateCalendarBySameTimePlan", err),
 
- 				zap.Any("cowList", cowList),
 
- 				zap.Any("plan", sameTime),
 
- 			)
 
- 			continue
 
- 		}
 
- 		todayCount += currCount
 
- 	}
 
- 	return nil
 
- }
 
- // UpdateSameTime 更新每天同情数据
 
- func (e *Entry) UpdateSameTime() error {
 
- 	calendarTypeList := backend.CalendarTypeEnumList("")
 
- 	showDay := time.Now().Format(model.LayoutDate2)
 
- 	for _, v := range calendarTypeList {
 
- 		count := int64(0)
 
- 		if err := e.DB.Model(new(model.EventCowSameTime)).
 
- 			Where("same_time_type = ?", v.Value).
 
- 			Where("status = ?", pasturePb.IsShow_No).
 
- 			Count(&count).Error; err != nil {
 
- 			zaplog.Error("crontab", zap.Any("UpdateSameTime", err), zap.Any("count", count))
 
- 		}
 
- 		if count >= 0 {
 
- 			continue
 
- 		}
 
- 		isExist := int64(0)
 
- 		if err := e.DB.Model(new(model.Calendar)).
 
- 			Where("calendar_type = ?", v.Value).
 
- 			Where("show_day = ?", showDay).
 
- 			Count(&isExist).Error; err != nil {
 
- 			continue
 
- 		}
 
- 		if isExist <= 0 {
 
- 			continue
 
- 		}
 
- 		if err := e.DB.Model(new(model.Calendar)).
 
- 			Where("calendar_type = ?", v.Value).
 
- 			Where("show_day = ?", showDay).
 
- 			Updates(map[string]interface{}{
 
- 				"count": count,
 
- 			}).Error; err != nil {
 
- 			continue
 
- 		}
 
- 	}
 
- 	return nil
 
- }
 
- // 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).
 
- 		Where("name IN ?", []string{
 
- 			model.PregnantCheckForFirst,
 
- 			model.PregnantCheckForSecond,
 
- 			model.WeaningAge,
 
- 			model.PregnancyAge,
 
- 		}).Find(&systemBasicList).Error; err != nil {
 
- 		zaplog.Error("crontab", zap.Any("PregnancyCheck", err))
 
- 		return xerr.WithStack(err)
 
- 	}
 
- 	currWeekValue := time.Now().Weekday()
 
- 	for _, v := range systemBasicList {
 
- 		// 周执行
 
- 		if v.WeekValue >= 0 && time.Weekday(v.WeekValue) != currWeekValue {
 
- 			continue
 
- 		}
 
- 		cowList := make([]*model.Cow, 0)
 
- 		pref := e.DB.Model(new(model.Cow)).Where("admission_status = ? ", pasturePb.AdmissionStatus_Admission)
 
- 		switch v.Name {
 
- 		case model.PregnantCheckForFirst:
 
- 			pref.Where("breed_status = ?", pasturePb.BreedStatus_Breeding)
 
- 		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)
 
- 		default:
 
- 			continue
 
- 		}
 
- 		if v.ValueType == model.ValueTypeFixed {
 
- 			pref.Where("day_age = ?", v.MinValue)
 
- 		}
 
- 		if v.ValueType == model.ValueTypeRange {
 
- 			pref.Where("day_age >= ?", v.MinValue).Where("day_age <= ?", v.MaxValue)
 
- 		}
 
- 		if err := pref.Find(&cowList).Error; err != nil {
 
- 			zaplog.Error("crontab", zap.Any("PregnancyCheck", err), zap.Any("cowList", cowList))
 
- 			continue
 
- 		}
 
- 		if len(cowList) <= 0 {
 
- 			continue
 
- 		}
 
- 		e.InitEventData(cowList, v.Name)
 
- 	}
 
- 	return nil
 
- }
 
- func (e *Entry) InitEventData(cowList []*model.Cow, systemBasicName string) {
 
- 	switch systemBasicName {
 
- 	case model.PregnantCheckForFirst, model.PregnantCheckForSecond:
 
- 		penMap, _ := e.GetPenMapList()
 
- 		eventPregnantCheckDataList := model.NewEventPregnantCheckList(cowList, penMap, 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)
 
- 		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)
 
- 		if err := e.DB.Create(eventCalvingList).Error; err != nil {
 
- 			zaplog.Error("crontab", zap.Any("InitEventData", err), zap.Any("eventCalvingList", eventCalvingList))
 
- 		}
 
- 	}
 
- }
 
- // DiseaseCheck 疾病检查
 
- func (e *Entry) DiseaseCheck() error {
 
- 	return nil
 
- }
 
 
  |