other.go 2.5 KB

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