1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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) {
- // 日志保留15天以内的
- nowDay := time.Now()
- 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, showDay string, count int32) {
- calendarTypeName := backend.CalendarTypeMap()[calendarType]
- newCalendar := model.NewCalendar(pastureId, calendarTypeName, calendarType, showDay, count)
- historyCalendar := &model.Calendar{}
- historyCount := int64(0)
- if err := e.DB.Model(&model.Calendar{}).
- Where("calendar_type = ?", calendarType).
- Where("show_day = ?", showDay).
- Where("pasture_id = ?", pastureId).
- Count(&historyCount).
- First(historyCalendar).Error; err != nil {
- zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("historyCalendar", historyCalendar))
- }
- if historyCount <= 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
- } else {
- if err := e.DB.Model(&model.Calendar{}).
- Where("id = ?", historyCalendar.Id).
- Update("count", count+int32(historyCount)).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.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
- }
|