other.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package crontab
  2. import (
  3. "kpt-pasture/model"
  4. "kpt-pasture/module/backend"
  5. "time"
  6. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  7. "go.uber.org/zap"
  8. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  9. )
  10. // IsExistCrontabLog 定时任务今日是否已经执行过
  11. func (e *Entry) IsExistCrontabLog(name string) bool {
  12. currDateTime := time.Now().Format(model.LayoutDate2)
  13. var count int64 = 0
  14. if err := e.DB.Model(&model.CronLog{}).Where("name = ?", name).
  15. Where("date = ?", currDateTime).Count(&count).Error; err != nil {
  16. zaplog.Error("CreateCrontabLog", zap.Any("err", err), zap.String("name", name))
  17. return false
  18. }
  19. if count > 0 {
  20. return true
  21. }
  22. return false
  23. }
  24. func (e *Entry) CreateCrontabLog(name string) {
  25. crontabLog := model.NewCronLog(name)
  26. if err := e.DB.Model(&model.CronLog{}).Create(crontabLog).Error; err != nil {
  27. zaplog.Error("CreateCrontabLog", zap.Any("err", err), zap.String("name", name))
  28. }
  29. // 日志保留15天以内的
  30. nowDay := time.Now()
  31. beforeDay := nowDay.AddDate(0, 0, -15)
  32. beforeDayFormat := beforeDay.Format(model.LayoutDate2)
  33. if err := e.DB.Model(&model.CronLog{}).Where("date < ?", beforeDayFormat).Delete(&model.CronLog{}).Error; err != nil {
  34. zaplog.Error("CreateCrontabLog", zap.Any("err", err), zap.String("name", name))
  35. }
  36. }
  37. // CreatedCalendar 创建当天工单日历记录
  38. func (e *Entry) CreatedCalendar(calendarType pasturePb.CalendarType_Kind, count int32) {
  39. calendarTypeName := backend.CalendarTypeMap()[calendarType]
  40. newCalendar := model.NewCalendar(calendarTypeName, calendarType, count)
  41. historyCalendar := &model.Calendar{}
  42. if err := e.DB.Model(&model.Calendar{}).Where("calendar_type = ?", calendarType).
  43. Where("show_day = ?", time.Now().Format(model.LayoutDate2)).First(historyCalendar).Error; err != nil {
  44. zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("historyCalendar", historyCalendar))
  45. }
  46. if historyCalendar.Id <= 0 {
  47. if err := e.DB.Model(&model.Calendar{}).Create(newCalendar).Error; err != nil {
  48. zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("workOrderCalendar", newCalendar))
  49. }
  50. return
  51. }
  52. if err := e.DB.Model(&model.Calendar{}).Where("id = ?", historyCalendar.Id).
  53. Update("count", count).Error; err != nil {
  54. zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("historyCalendar", historyCalendar))
  55. }
  56. }
  57. func (e *Entry) IsExistSameTimeCow(cow *model.Cow, sameTime *model.SameTime) bool {
  58. var count int64 = 0
  59. if err := e.DB.Model(&model.CowSameTime{}).
  60. Where("cow_id = ?", cow.Id).
  61. Where("lact = ?", cow.Lact).
  62. Where("same_time_id = ?", sameTime.Id).
  63. Where("same_time_status != ?", pasturePb.SameTimeStatus_End).
  64. Count(&count).Error; err != nil {
  65. zaplog.Error("IsExistSameTimeCow", zap.Any("err", err), zap.Any("cow", cow), zap.Any("sameTime", sameTime))
  66. return false
  67. }
  68. if count > 0 {
  69. return true
  70. }
  71. return false
  72. }