other.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. Where("name != ?", NeckRingOriginal).
  34. Delete(&model.CronLog{})
  35. }
  36. }()
  37. crontabLog := model.NewCronLog(name)
  38. if err := e.DB.Model(&model.CronLog{}).Create(crontabLog).Error; err != nil {
  39. zaplog.Error("CreateCrontabLog", zap.Any("err", err), zap.String("name", name))
  40. }
  41. }
  42. func (e *Entry) DeleteCrontabLog(name string) {
  43. e.DB.Model(&model.CronLog{}).Where("name = ?", name).Delete(&model.CronLog{})
  44. }
  45. // CreatedCalendar 创建当天工单日历记录
  46. func (e *Entry) CreatedCalendar(calendarType pasturePb.CalendarType_Kind, count int32) {
  47. calendarTypeName := backend.CalendarTypeMap()[calendarType]
  48. newCalendar := model.NewCalendar(calendarTypeName, calendarType, count)
  49. historyCalendar := &model.Calendar{}
  50. historyCount := int64(0)
  51. if err := e.DB.Model(&model.Calendar{}).Where("calendar_type = ?", calendarType).
  52. Where("show_day = ?", time.Now().Format(model.LayoutDate2)).Count(&historyCount).First(historyCalendar).Error; err != nil {
  53. zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("historyCalendar", historyCalendar))
  54. }
  55. if historyCount <= 0 {
  56. if err := e.DB.Model(&model.Calendar{}).Create(newCalendar).Error; err != nil {
  57. zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("workOrderCalendar", newCalendar))
  58. }
  59. return
  60. }
  61. if historyCount > 0 {
  62. if err := e.DB.Model(&model.Calendar{}).Where("id = ?", historyCalendar.Id).
  63. Update("count", count).Error; err != nil {
  64. zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("historyCalendar", historyCalendar))
  65. }
  66. }
  67. }
  68. func (e *Entry) IsExistSameTimeCow(cow *model.Cow, sameTime *model.SameTime) bool {
  69. var count int64 = 0
  70. if err := e.DB.Model(&model.CowSameTime{}).
  71. Where("cow_id = ?", cow.Id).
  72. Where("lact = ?", cow.Lact).
  73. Where("same_time_id = ?", sameTime.Id).
  74. Where("same_time_status != ?", pasturePb.SameTimeStatus_End).
  75. Count(&count).Error; err != nil {
  76. zaplog.Error("IsExistSameTimeCow", zap.Any("err", err), zap.Any("cow", cow), zap.Any("sameTime", sameTime))
  77. return false
  78. }
  79. if count > 0 {
  80. return true
  81. }
  82. return false
  83. }