other.go 3.2 KB

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