package crontab import ( "encoding/json" "kpt-pasture/model" "kpt-pasture/module/backend" "time" "gorm.io/gorm" "gitee.com/xuyiping_admin/pkg/xerr" "gitee.com/xuyiping_admin/pkg/logger/zaplog" "go.uber.org/zap" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) // GenerateCalendarBySameTimePlan 生成同期计划的牛只 func (e *Entry) GenerateCalendarBySameTimePlan(cowList []*model.Cow, sameTime *model.SameTime) (int32, error) { if len(cowList) <= 0 { return 0, nil } newCowList := make([]*model.Cow, 0) for _, cow := range cowList { if ok := e.IsExistSameTimeCow(cow, sameTime); ok { continue } newCowList = append(newCowList, cow) } if len(newCowList) <= 0 || len(sameTime.CollateNodes) <= 0 { return 0, nil } calendarList := make([]*model.Calendar, 0) newSameTimeCowDetailList := make([]*model.EventCowSameTime, 0) newEventMatingList := make([]*model.EventMating, 0) collateNodes := make([]*pasturePb.CollateNode, 0) newEventItemList := make([]*model.EventItem, 0) var _ = json.Unmarshal([]byte(sameTime.CollateNodes), &collateNodes) nowTime := time.Now() for i, collateNode := range collateNodes { showDay := nowTime if i > 0 { showDay = nowTime.Add(time.Hour * 24 * time.Duration(collateNode.NextNodeDay)) } calendarName := backend.CalendarTypeMap()[pasturePb.CalendarType_PG] calendarType := pasturePb.CalendarType_PG histCount := int64(0) if collateNode.SameTimeType == pasturePb.SameTimeType_TAI { calendarName = backend.CalendarTypeMap()[pasturePb.CalendarType_Mating] calendarType = pasturePb.CalendarType_Mating histCount = e.GetTowardTaiCowSum() newEventMatingList = append(newEventMatingList, model.NewEventMatingList(newCowList, showDay.Unix())...) newEventItemList = append(newEventItemList, model.NewEventItemList(newCowList, calendarType)...) } else { if collateNode.SameTimeType == pasturePb.SameTimeType_RnGH { calendarName = backend.CalendarTypeMap()[pasturePb.CalendarType_RnGH] calendarType = pasturePb.CalendarType_RnGH } histCount = e.GetTowardSameTimeCowSum(sameTime.Id, collateNode.SameTimeType) newEventCowSameTimeList := model.NewEventCowSameTimeList(newCowList, sameTime, showDay.Unix(), collateNode.SameTimeType) newSameTimeCowDetailList = append(newSameTimeCowDetailList, newEventCowSameTimeList...) newEventItemList = append(newEventItemList, model.NewEventItemList(newCowList, calendarType)...) } calendarList = append(calendarList, &model.Calendar{ Name: calendarName, CalendarType: calendarType, Count: int32(len(newCowList)) + int32(histCount), ShowDay: time.Unix(showDay.Unix(), 0).Format(model.LayoutDate2), IsShow: pasturePb.IsShow_Ok, }) } newSameTimeCowList := model.NewCowSameTimeList(newCowList, sameTime) if err := e.DB.Transaction(func(tx *gorm.DB) error { // 创建牛只同期日历表 if err := tx.Create(calendarList).Error; err != nil { return xerr.WithStack(err) } // 创建牛只同期总表 if err := tx.Create(newSameTimeCowList).Error; err != nil { return xerr.WithStack(err) } // 创建牛只同期详情表 if err := tx.Create(newSameTimeCowDetailList).Error; err != nil { return xerr.WithStack(err) } // 创建牛只配种表 if err := tx.Create(newEventMatingList).Error; err != nil { return xerr.WithStack(err) } // 创建牛只事件表 if err := tx.Create(newEventItemList).Error; err != nil { return xerr.WithStack(err) } return nil }); err != nil { return 0, xerr.WithStack(err) } return int32(len(newCowList)), nil } func (e *Entry) getWorkOrderCalendar(name string) []*model.Calendar { res := make([]*model.Calendar, 0) if err := e.DB.Where("name = ?", name). Where("is_show = ?", pasturePb.IsShow_Ok). Find(&res).Error; err != nil { zaplog.Error("getWorkOrderCalendar", zap.Any("err", err)) } return res } // GetTowardSameTimeCowSum 获取历史未打激素牛只总数量 func (e *Entry) GetTowardSameTimeCowSum(sameTimeId int64, sameTimeType pasturePb.SameTimeType_Kind) int64 { res := int64(0) if err := e.DB.Model(&model.EventCowSameTime{}). Where("status = ?", pasturePb.IsShow_No). Where("same_time_id = ?", sameTimeId). Where("same_time_type = ?", sameTimeType). Count(&res).Error; err != nil { zaplog.Error("GetTowardSameTimeCowSum", zap.Any("err", err)) } return res } // GetTowardTaiCowSum 获取历史未配种牛只总数量 func (e *Entry) GetTowardTaiCowSum() int64 { res := int64(0) if err := e.DB.Model(&model.EventMating{}). Where("status = ?", pasturePb.IsShow_No). Count(&res).Error; err != nil { zaplog.Error("GetTowardTaiCowSum", zap.Any("err", err)) } return res } // CowPregnant 月度牛只怀孕清单 func (e *Entry) CowPregnant() error { cowList := make([]*model.Cow, 0) if err := e.DB.Model(new(model.Cow)). Where("admission_status = ?", pasturePb.AdmissionStatus_Admission). Where("is_pregnant = ?", pasturePb.IsShow_Ok).Find(&cowList).Error; err != nil { return xerr.WithStack(err) } cowPregnantList := model.NewCowPregnantList(cowList) if err := e.DB.Create(cowPregnantList).Error; err != nil { return xerr.WithStack(err) } return nil }