other.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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().Local().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().Local()
  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. }
  70. func (e *Entry) NeckRingErrorMap() map[pasturePb.NeckRingNumberError_Kind]string {
  71. res := make(map[pasturePb.NeckRingNumberError_Kind]string)
  72. for _, v := range e.NeckRingErrorEnumList("") {
  73. res[pasturePb.NeckRingNumberError_Kind(v.Value)] = v.Label
  74. }
  75. return res
  76. }
  77. func (e *Entry) NeckRingErrorEnumList(isAll string) []*pasturePb.ConfigOptionsList {
  78. configOptions := make([]*pasturePb.ConfigOptionsList, 0)
  79. if isAll == model.IsAllYes {
  80. configOptions = append(configOptions,
  81. &pasturePb.ConfigOptionsList{
  82. Value: int32(0),
  83. Label: "全部",
  84. Disabled: true,
  85. })
  86. }
  87. configOptions = append(configOptions, &pasturePb.ConfigOptionsList{
  88. Value: int32(pasturePb.NeckRingNumberError_Suspected_Fall_Off),
  89. Label: "疑似脱落",
  90. Disabled: true,
  91. }, &pasturePb.ConfigOptionsList{
  92. Value: int32(pasturePb.NeckRingNumberError_No_Signal),
  93. Label: "无信号",
  94. Disabled: true,
  95. }, &pasturePb.ConfigOptionsList{
  96. Value: int32(pasturePb.NeckRingNumberError_Receiving_Less),
  97. Label: "接受率低",
  98. Disabled: true,
  99. }, &pasturePb.ConfigOptionsList{
  100. Value: int32(pasturePb.NeckRingNumberError_Data_Latency),
  101. Label: "数据延迟",
  102. Disabled: true,
  103. }, &pasturePb.ConfigOptionsList{
  104. Value: int32(pasturePb.NeckRingNumberError_Low_Activity_Level),
  105. Label: "活动量低",
  106. Disabled: true,
  107. }, &pasturePb.ConfigOptionsList{
  108. Value: int32(pasturePb.NeckRingNumberError_Abnormal_Wearing),
  109. Label: "佩戴异常",
  110. Disabled: true,
  111. }, &pasturePb.ConfigOptionsList{
  112. Value: int32(pasturePb.NeckRingNumberError_Should_Associated),
  113. Label: "应关联",
  114. Disabled: true,
  115. })
  116. return configOptions
  117. }