package crontab import ( "kpt-pasture/model" "kpt-pasture/module/backend" "time" "gitee.com/xuyiping_admin/pkg/logger/zaplog" "go.uber.org/zap" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) // IsExistCrontabLog 定时任务今日是否已经执行过 func (e *Entry) IsExistCrontabLog(name string) bool { currDateTime := time.Now().Format(model.LayoutDate2) var count int64 = 0 if err := e.DB.Model(&model.CronLog{}).Where("name = ?", name). Where("date = ?", currDateTime).Count(&count).Error; err != nil { zaplog.Error("CreateCrontabLog", zap.Any("err", err), zap.String("name", name)) return false } if count > 0 { return true } return false } func (e *Entry) CreateCrontabLog(name string) { crontabLog := model.NewCronLog(name) if err := e.DB.Model(&model.CronLog{}).Create(crontabLog).Error; err != nil { zaplog.Error("CreateCrontabLog", zap.Any("err", err), zap.String("name", name)) } // 日志保留15天以内的 nowDay := time.Now() beforeDay := nowDay.AddDate(0, 0, -15) beforeDayFormat := beforeDay.Format(model.LayoutDate2) if err := e.DB.Model(&model.CronLog{}).Where("date < ?", beforeDayFormat).Delete(&model.CronLog{}).Error; err != nil { zaplog.Error("CreateCrontabLog", zap.Any("err", err), zap.String("name", name)) } } // CreatedCalendar 创建当天工单日历记录 func (e *Entry) CreatedCalendar(calendarType pasturePb.CalendarType_Kind, count int32) { calendarTypeName := backend.CalendarTypeMap()[calendarType] newCalendar := model.NewCalendar(calendarTypeName, calendarType, count) historyCalendar := &model.Calendar{} if err := e.DB.Model(&model.Calendar{}).Where("calendar_type = ?", calendarType). Where("show_day = ?", time.Now().Format(model.LayoutDate2)).First(historyCalendar).Error; err != nil { zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("historyCalendar", historyCalendar)) } if historyCalendar.Id <= 0 { if err := e.DB.Model(&model.Calendar{}).Create(newCalendar).Error; err != nil { zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("workOrderCalendar", newCalendar)) } return } if err := e.DB.Model(&model.Calendar{}).Where("id = ?", historyCalendar.Id). Update("count", count).Error; err != nil { zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("historyCalendar", historyCalendar)) } } func (e *Entry) IsExistSameTimeCow(cow *model.Cow, sameTime *model.SameTime) bool { var count int64 = 0 if err := e.DB.Model(&model.CowSameTime{}). Where("cow_id = ?", cow.Id). Where("lact = ?", cow.Lact). Where("same_time_id = ?", sameTime.Id). Where("same_time_status != ?", pasturePb.SameTimeStatus_End). Count(&count).Error; err != nil { zaplog.Error("IsExistSameTimeCow", zap.Any("err", err), zap.Any("cow", cow), zap.Any("sameTime", sameTime)) return false } if count > 0 { return true } return false }