123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- 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"
- )
- 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
- }
- 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:
-
- 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).
- 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
- }
- 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
- }
- 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
- }
|