other.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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, startDay, endDay string, count int32) {
  46. calendarTypeName := backend.CalendarTypeMap()[calendarType]
  47. newCalendar := model.NewCalendar(pastureId, calendarTypeName, calendarType, startDay, endDay, count)
  48. if err := e.DB.Model(&model.Calendar{}).
  49. Create(newCalendar).Error; err != nil {
  50. zaplog.Error("CreatedCalendar", zap.Any("err", err), zap.Any("workOrderCalendar", newCalendar))
  51. }
  52. }
  53. func (e *Entry) IsExistSameTimeCow(cow *model.Cow, sameTime *model.SameTime) bool {
  54. var count int64 = 0
  55. if err := e.DB.Model(&model.EventCowSameTime{}).
  56. Where("pasture_id = ?", sameTime.PastureId).
  57. Where("cow_id = ?", cow.Id).
  58. Where("lact = ?", cow.Lact).
  59. Where("same_time_id = ?", sameTime.Id).
  60. Where("status = ?", pasturePb.IsShow_Ok).
  61. Count(&count).Error; err != nil {
  62. zaplog.Error("IsExistSameTimeCow", zap.Any("err", err), zap.Any("cow", cow), zap.Any("sameTime", sameTime))
  63. return false
  64. }
  65. if count > 0 {
  66. return true
  67. }
  68. return false
  69. }