other.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // 日志保留15天以内的
  26. nowDay := time.Now()
  27. defer func() {
  28. if nowDay.Day()%15 == 0 {
  29. beforeDay := nowDay.AddDate(0, 0, -15)
  30. beforeDayFormat := beforeDay.Format(model.LayoutDate2)
  31. e.DB.Model(&model.CronLog{}).
  32. Where("date < ?", beforeDayFormat).
  33. Delete(&model.CronLog{})
  34. }
  35. }()
  36. crontabLog := model.NewCronLog(name)
  37. if err := e.DB.Model(&model.CronLog{}).Create(crontabLog).Error; err != nil {
  38. zaplog.Error("CreateCrontabLog", zap.Any("err", err), zap.String("name", name))
  39. }
  40. }
  41. func (e *Entry) DeleteCrontabLog(name string) {
  42. e.DB.Model(&model.CronLog{}).Where("name = ?", name).Delete(&model.CronLog{})
  43. }
  44. // CreatedCalendar 创建当天工单日历记录
  45. func (e *Entry) CreatedCalendar(pastureId int64, calendarType pasturePb.CalendarType_Kind, showDay string, count int32) {
  46. calendarTypeName := backend.CalendarTypeMap()[calendarType]
  47. newCalendar := model.NewCalendar(pastureId, calendarTypeName, calendarType, showDay, count)
  48. historyCalendar := &model.Calendar{}
  49. historyCount := int64(0)
  50. if err := e.DB.Model(&model.Calendar{}).
  51. Where("calendar_type = ?", calendarType).
  52. Where("show_day = ?", showDay).
  53. Where("pasture_id = ?", pastureId).
  54. Count(&historyCount).
  55. First(historyCalendar).Error; err != nil {
  56. zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("historyCalendar", historyCalendar))
  57. }
  58. if historyCount <= 0 {
  59. if err := e.DB.Model(&model.Calendar{}).
  60. Create(newCalendar).Error; err != nil {
  61. zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("workOrderCalendar", newCalendar))
  62. }
  63. return
  64. } else {
  65. if err := e.DB.Model(&model.Calendar{}).
  66. Where("id = ?", historyCalendar.Id).
  67. Update("count", count+int32(historyCount)).Error; err != nil {
  68. zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("historyCalendar", historyCalendar))
  69. }
  70. }
  71. }
  72. func (e *Entry) IsExistSameTimeCow(cow *model.Cow, sameTime *model.SameTime) bool {
  73. var count int64 = 0
  74. if err := e.DB.Model(&model.EventCowSameTime{}).
  75. Where("pasture_id = ?", sameTime.PastureId).
  76. Where("cow_id = ?", cow.Id).
  77. Where("lact = ?", cow.Lact).
  78. Where("same_time_id = ?", sameTime.Id).
  79. Where("status = ?", pasturePb.IsShow_Ok).
  80. Count(&count).Error; err != nil {
  81. zaplog.Error("IsExistSameTimeCow", zap.Any("err", err), zap.Any("cow", cow), zap.Any("sameTime", sameTime))
  82. return false
  83. }
  84. if count > 0 {
  85. return true
  86. }
  87. return false
  88. }