cow_cron.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package crontab
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "kpt-pasture/util"
  7. "time"
  8. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  9. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  10. "gitee.com/xuyiping_admin/pkg/xerr"
  11. "go.uber.org/zap"
  12. )
  13. const (
  14. UpdateCowInfo = "UpdateCowInfo"
  15. ImmunizationPlan = "ImmunizationPlan"
  16. SameTimePlan = "SameTimePlan"
  17. )
  18. // GenerateAsynqWorkOrder 异步生成工作单
  19. func (e *Entry) GenerateAsynqWorkOrder() error {
  20. workOrderList := make([]*model.WorkOrderMaster, 0)
  21. if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&workOrderList).Error; err != nil {
  22. return err
  23. }
  24. for _, workOrder := range workOrderList {
  25. timeUnix, err := util.ConvertParseLocalUnix(workOrder.ExecTime)
  26. if timeUnix <= 0 || err != nil {
  27. zaplog.Error("crontab", zap.Any("GenerateWorkOrder", err), zap.Any("execTime", workOrder.ExecTime))
  28. continue
  29. }
  30. execTime := time.Now().Unix() - timeUnix
  31. task := model.NewTaskWorkOrderPayload(workOrder.Id, time.Duration(execTime)*time.Second)
  32. zaplog.Info("GenerateWorkOrder", zap.Any("workOrder", workOrder), zap.Any("task", task))
  33. if _, err = e.AsynqClient.CtxEnqueue(context.Background(), task); err != nil {
  34. zaplog.Error("PushMessage CtxEnqueue", zap.Any("Err", err))
  35. }
  36. }
  37. return nil
  38. }
  39. // UpdateCowInfo 牛只基本信息维护
  40. func (e *Entry) UpdateCowInfo() error {
  41. cowList := make([]*model.Cow, 0)
  42. if err := e.DB.Where("is_remove = ?", pasturePb.IsShow_Ok).Find(&cowList).Error; err != nil {
  43. return err
  44. }
  45. if ok := e.IsExistCrontabLog(UpdateCowInfo); !ok {
  46. return nil
  47. }
  48. defer func() {
  49. e.CreateCrontabLog(UpdateCowInfo)
  50. }()
  51. for _, cow := range cowList {
  52. dayAge := cow.GetDayAge()
  53. calvingAge := cow.GetCalvingAge()
  54. pregnancyAge := cow.GetDaysPregnant()
  55. admissionAge := cow.GetAdmissionAge()
  56. if err := e.DB.Model(new(model.Cow)).Where("id = ?", cow.Id).Updates(map[string]interface{}{
  57. "day_age": dayAge,
  58. "calving_at": calvingAge,
  59. "pregnancy_age": pregnancyAge,
  60. "admission_age": admissionAge,
  61. }).Error; err != nil {
  62. zaplog.Error("Crontab", zap.Any("UpdateCowDayAge", err))
  63. }
  64. }
  65. return nil
  66. }
  67. // ImmunizationPlan 免疫计划,生成工作单
  68. func (e *Entry) ImmunizationPlan() error {
  69. if ok := e.IsExistCrontabLog(ImmunizationPlan); !ok {
  70. return nil
  71. }
  72. planList := make([]*model.ImmunizationPlan, 0)
  73. if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&planList).Error; err != nil {
  74. return xerr.WithStack(err)
  75. }
  76. var todayCount int32 = 0
  77. defer func() {
  78. var count int64 = 0
  79. if err := e.DB.Model(new(model.ImmunizationPlanCow)).Where("status = ?", pasturePb.IsShow_Ok).Count(&count).Error; err != nil {
  80. zaplog.Error("Crontab", zap.Any("ImmunizationPlanDefer", err))
  81. }
  82. todayCount += int32(count)
  83. e.CreatedWorkOrderCalendar(pasturePb.CalendarType_Immunisation, todayCount)
  84. e.CreateCrontabLog(ImmunizationPlan)
  85. }()
  86. for _, plan := range planList {
  87. if plan == nil {
  88. continue
  89. }
  90. cowList := make([]*model.Cow, 0)
  91. pref := e.DB.Table(fmt.Sprintf("%s as a", new(model.ImmunizationPlan).TableName())).
  92. Select("a.*").
  93. Where("a.is_remove = ?", pasturePb.IsShow_Ok)
  94. if plan.CowType > 0 {
  95. pref.Where("a.cow_type = ?", plan.CowType)
  96. }
  97. switch plan.Conditions {
  98. case pasturePb.ImmunizationConditions_Days_Age:
  99. pref.Where("a.day_age = ?", plan.Value)
  100. case pasturePb.ImmunizationConditions_Days_After_Delivery:
  101. pref.Where("a.calving_age = ?", plan.Value)
  102. case pasturePb.ImmunizationConditions_Days_Of_Pregnancy:
  103. pref.Where("a.pregnancy_age = ?", plan.Value).
  104. Where("a.is_pregnant = ?", pasturePb.IsShow_Ok)
  105. case pasturePb.ImmunizationConditions_Month:
  106. // todo 待实现月份
  107. case pasturePb.ImmunizationConditions_Admission_Days:
  108. pref.Where("a.admission_age = ?", plan.Value)
  109. case pasturePb.ImmunizationConditions_Other_Vaccine_After:
  110. if plan.ImmunizationPlanId > 0 {
  111. pref.Joins("INNER JOIN immunization_plan_cow as b ON b.immunization_plan_id = ?", plan.ImmunizationPlanId).
  112. Where("b.cow_id = a.id").
  113. Where("DATE_ADD(b.reality_time, INTERVAL ? DAY) = ?", plan.Value, time.Now().Format(model.LayoutDate2)).
  114. Where("b.status = ?", pasturePb.IsShow_Ok)
  115. }
  116. }
  117. if err := pref.Find(&cowList).Error; err != nil {
  118. return xerr.WithStack(err)
  119. }
  120. if len(cowList) <= 0 {
  121. continue
  122. }
  123. todayCount += int32(len(cowList))
  124. newImmunizationPlanCowList := model.NewImmunizationPlanCowList(cowList, plan)
  125. if err := e.DB.Create(newImmunizationPlanCowList).Error; err != nil {
  126. zaplog.Error("ImmunizationPlan", zap.Any("CreateImmunizationPlanCow", err), zap.Any("plan", plan), zap.Any("cowList", cowList))
  127. }
  128. }
  129. return nil
  130. }
  131. // SameTimePlan 同期计划,生成工作单
  132. func (e *Entry) SameTimePlan() error {
  133. if ok := e.IsExistCrontabLog(SameTimePlan); !ok {
  134. return nil
  135. }
  136. sameTimeList := make([]*model.SameTime, 0)
  137. if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&sameTimeList).Error; err != nil {
  138. return xerr.WithStack(err)
  139. }
  140. // 更新日历里面的数据
  141. var todayCount int32 = 0
  142. defer func() {
  143. e.CreatedWorkOrderCalendar(pasturePb.CalendarType_Immunisation, todayCount)
  144. e.CreateCrontabLog(SameTimePlan)
  145. }()
  146. currWeek := time.Now().Weekday()
  147. for _, sameTime := range sameTimeList {
  148. if sameTime == nil {
  149. continue
  150. }
  151. if time.Weekday(sameTime.WeekType) != currWeek {
  152. continue
  153. }
  154. cowList := make([]*model.Cow, 0)
  155. pref := e.DB.Where("is_remove = ?", pasturePb.IsShow_Ok)
  156. if sameTime.CowType == pasturePb.SameTimeCowType_Breeding_Calf {
  157. pref.Where("calving_age >= ?", sameTime.PostpartumDaysStart).
  158. Where("calving_age <= ?", sameTime.PostpartumDaysEnd).
  159. Where("is_pregnant = ?", pasturePb.IsShow_No)
  160. }
  161. if sameTime.CowType == pasturePb.SameTimeCowType_Empty {
  162. pref.Where(
  163. e.DB.Where("breed_status = ?", pasturePb.BreedStatus_Empty).
  164. Or("breed_status = ?", pasturePb.BreedStatus_Abort),
  165. ).Where("s_pregnant = ?", pasturePb.IsShow_No)
  166. }
  167. if err := pref.Find(&cowList).Error; err != nil {
  168. zaplog.Error("crontab", zap.Any("SameTimePlan", err), zap.Any("plan", sameTime))
  169. return xerr.WithStack(err)
  170. }
  171. if len(cowList) <= 0 {
  172. continue
  173. }
  174. currCount, err := e.GenerateCalendarBySameTimePlan(cowList, sameTime)
  175. if err != nil {
  176. zaplog.Error("crontab",
  177. zap.Any("GenerateCalendarBySameTimePlan", err),
  178. zap.Any("cowList", cowList),
  179. zap.Any("plan", sameTime),
  180. )
  181. continue
  182. }
  183. todayCount += currCount
  184. }
  185. return nil
  186. }
  187. // PregnancyCheck 妊娠期检查
  188. func (e *Entry) PregnancyCheck() error {
  189. return nil
  190. }
  191. // DiseaseCheck 疾病检查
  192. func (e *Entry) DiseaseCheck() error {
  193. return nil
  194. }