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().Local().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) { // 日志保留15天以内的 nowDay := time.Now().Local() defer func() { if nowDay.Day()%15 == 0 { beforeDay := nowDay.AddDate(0, 0, -15) beforeDayFormat := beforeDay.Format(model.LayoutDate2) e.DB.Model(&model.CronLog{}). Where("date < ?", beforeDayFormat). Delete(&model.CronLog{}) } }() 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)) } } func (e *Entry) DeleteCrontabLog(name string) { e.DB.Model(&model.CronLog{}).Where("name = ?", name).Delete(&model.CronLog{}) } // CreatedCalendar 创建当天工单日历记录 func (e *Entry) CreatedCalendar(pastureId int64, calendarType pasturePb.CalendarType_Kind, startDay, endDay string, count int32) { calendarTypeName := backend.CalendarTypeMap()[calendarType] newCalendar := model.NewCalendar(pastureId, calendarTypeName, calendarType, startDay, endDay, count) if err := e.DB.Model(&model.Calendar{}). Create(newCalendar).Error; err != nil { zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("workOrderCalendar", newCalendar)) } } func (e *Entry) IsExistSameTimeCow(cow *model.Cow, sameTime *model.SameTime) bool { var count int64 = 0 if err := e.DB.Model(&model.EventCowSameTime{}). Where("pasture_id = ?", sameTime.PastureId). Where("cow_id = ?", cow.Id). Where("lact = ?", cow.Lact). Where("same_time_id = ?", sameTime.Id). Where("status = ?", pasturePb.IsShow_Ok). 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 } func (e *Entry) NeckRingErrorMap() map[pasturePb.NeckRingNumberError_Kind]string { res := make(map[pasturePb.NeckRingNumberError_Kind]string) for _, v := range e.NeckRingErrorEnumList("") { res[pasturePb.NeckRingNumberError_Kind(v.Value)] = v.Label } return res } func (e *Entry) NeckRingErrorEnumList(isAll string) []*pasturePb.ConfigOptionsList { configOptions := make([]*pasturePb.ConfigOptionsList, 0) if isAll == model.IsAllYes { configOptions = append(configOptions, &pasturePb.ConfigOptionsList{ Value: int32(0), Label: "全部", Disabled: true, }) } configOptions = append(configOptions, &pasturePb.ConfigOptionsList{ Value: int32(pasturePb.NeckRingNumberError_Suspected_Fall_Off), Label: "疑似脱落", Disabled: true, }, &pasturePb.ConfigOptionsList{ Value: int32(pasturePb.NeckRingNumberError_No_Signal), Label: "无信号", Disabled: true, }, &pasturePb.ConfigOptionsList{ Value: int32(pasturePb.NeckRingNumberError_Receiving_Less), Label: "接受率低", Disabled: true, }, &pasturePb.ConfigOptionsList{ Value: int32(pasturePb.NeckRingNumberError_Data_Latency), Label: "数据延迟", Disabled: true, }, &pasturePb.ConfigOptionsList{ Value: int32(pasturePb.NeckRingNumberError_Low_Activity_Level), Label: "活动量低", Disabled: true, }, &pasturePb.ConfigOptionsList{ Value: int32(pasturePb.NeckRingNumberError_Abnormal_Wearing), Label: "佩戴异常", Disabled: true, }, &pasturePb.ConfigOptionsList{ Value: int32(pasturePb.NeckRingNumberError_Should_Associated), Label: "应关联", Disabled: true, }) return configOptions }