other.go 3.2 KB

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