package crontab import ( "kpt-pasture/model" "kpt-pasture/module/backend" "gitee.com/xuyiping_admin/pkg/xerr" "gorm.io/gorm" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" "gitee.com/xuyiping_admin/pkg/logger/zaplog" "go.uber.org/zap" ) // UpdateCowInfo 牛只基本信息维护 func (e *Entry) UpdateCowInfo() error { cowList := make([]*model.Cow, 0) if err := e.DB.Where("is_remove = ?", pasturePb.IsShow_Ok).Find(&cowList).Error; err != nil { return err } if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok { return nil } for _, cow := range cowList { dayAge := cow.GetDayAge() calvingAge := cow.GetCalvingAge() pregnancyAge := cow.GetDaysPregnant() admissionAge := cow.GetAdmissionAge() if err := e.DB.Model(new(model.Cow)).Where("id = ?", cow.Id).Updates(map[string]interface{}{ "day_age": dayAge, "calving_at": calvingAge, "pregnancy_age": pregnancyAge, "admission_age": admissionAge, }).Error; err != nil { zaplog.Error("Crontab", zap.Any("UpdateCowDayAge", err)) } } return nil } // ImmunizationPlan 免疫计划,生成工作单 func (e *Entry) ImmunizationPlan() error { 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 } for _, plan := range planList { cowList := make([]*model.Cow, 0) pref := e.DB.Select("id").Where("is_remove = ?", pasturePb.IsShow_Ok). Where("cow_type = ?", plan.CowType) switch plan.Conditions { case pasturePb.ImmunizationConditions_Days_Age: pref.Where("day_age >= ?", plan.Value). Where("day_age <= ?", plan.Value2) case pasturePb.ImmunizationConditions_Days_After_Delivery: pref.Where("calving_age >= ?", plan.Value). Where("calving_age <= ?", plan.Value2) case pasturePb.ImmunizationConditions_Days_Of_Pregnancy: pref.Where("pregnancy_age >= ?", plan.Value). Where("pregnancy_age <= ?", plan.Value2). Where("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) } if err := pref.Find(&cowList).Error; err != nil { return xerr.WithStack(err) } if len(cowList) <= 0 { continue } if err := e.GenerateCalendarByImmunization(cowList, plan); err != nil { zaplog.Error("crontab", zap.Any("GenerateWorkOrderCalendar", err), 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) } 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) } pref := e.DB.Select("id"). Where("is_remove = ?", pasturePb.IsShow_Ok). Where("is_pregnant = ?", pasturePb.IsShow_No) if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok { return nil } for _, plan := range sameTimeList { cowList := make([]*model.Cow, 0) pref.Where("calving_age >= ?", plan.PostpartumDaysStart). Where("calving_age <= ?", plan.PostpartumDaysEnd) if err := pref.Find(&cowList).Error; err != nil { zaplog.Error("crontab", zap.Any("SameTimePlan", err), zap.Any("plan", plan)) return xerr.WithStack(err) } 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 }