cow_cron.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package crontab
  2. import (
  3. "kpt-pasture/model"
  4. "kpt-pasture/module/backend"
  5. "gitee.com/xuyiping_admin/pkg/xerr"
  6. "gorm.io/gorm"
  7. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  8. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  9. "go.uber.org/zap"
  10. )
  11. // UpdateCowInfo 牛只基本信息维护
  12. func (e *Entry) UpdateCowInfo() error {
  13. cowList := make([]*model.Cow, 0)
  14. if err := e.DB.Where("is_remove = ?", pasturePb.IsShow_Ok).Find(&cowList).Error; err != nil {
  15. return err
  16. }
  17. if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
  18. return nil
  19. }
  20. for _, cow := range cowList {
  21. dayAge := cow.GetDayAge()
  22. calvingAge := cow.GetCalvingAge()
  23. pregnancyAge := cow.GetDaysPregnant()
  24. admissionAge := cow.GetAdmissionAge()
  25. if err := e.DB.Model(new(model.Cow)).Where("id = ?", cow.Id).Updates(map[string]interface{}{
  26. "day_age": dayAge,
  27. "calving_at": calvingAge,
  28. "pregnancy_age": pregnancyAge,
  29. "admission_age": admissionAge,
  30. }).Error; err != nil {
  31. zaplog.Error("Crontab", zap.Any("UpdateCowDayAge", err))
  32. }
  33. }
  34. return nil
  35. }
  36. // ImmunizationPlan 免疫计划,生成工作单
  37. func (e *Entry) ImmunizationPlan() error {
  38. planList := make([]*model.ImmunizationPlan, 0)
  39. if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&planList).Error; err != nil {
  40. return xerr.WithStack(err)
  41. }
  42. if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
  43. return nil
  44. }
  45. for _, plan := range planList {
  46. cowList := make([]*model.Cow, 0)
  47. pref := e.DB.Select("id").Where("is_remove = ?", pasturePb.IsShow_Ok).
  48. Where("cow_type = ?", plan.CowType)
  49. switch plan.Conditions {
  50. case pasturePb.ImmunizationConditions_Days_Age:
  51. pref.Where("day_age >= ?", plan.Value).
  52. Where("day_age <= ?", plan.Value2)
  53. case pasturePb.ImmunizationConditions_Days_After_Delivery:
  54. pref.Where("calving_age >= ?", plan.Value).
  55. Where("calving_age <= ?", plan.Value2)
  56. case pasturePb.ImmunizationConditions_Days_Of_Pregnancy:
  57. pref.Where("pregnancy_age >= ?", plan.Value).
  58. Where("pregnancy_age <= ?", plan.Value2).
  59. Where("is_pregnant = ?", pasturePb.IsShow_Ok)
  60. case pasturePb.ImmunizationConditions_Month:
  61. // todo 待实现月份
  62. case pasturePb.ImmunizationConditions_Admission_Days:
  63. pref.Where("admission_age >= ?", plan.Value).
  64. Where("admission_age <= ?", plan.Value2)
  65. }
  66. if err := pref.Find(&cowList).Error; err != nil {
  67. return xerr.WithStack(err)
  68. }
  69. if len(cowList) <= 0 {
  70. continue
  71. }
  72. if err := e.GenerateCalendarByImmunization(cowList, plan); err != nil {
  73. zaplog.Error("crontab", zap.Any("GenerateWorkOrderCalendar", err), zap.Any("cowList", cowList))
  74. }
  75. }
  76. return nil
  77. }
  78. func (e *Entry) GenerateCalendarByImmunization(cowList []*model.Cow, plan *model.ImmunizationPlan) error {
  79. workOrderCalendarList := e.getWorkOrderCalendar(plan.Name)
  80. newCowList := make([]*model.Cow, 0)
  81. if len(workOrderCalendarList) > 0 {
  82. // 过滤已经存在的牛只数据,避免重复生成工作单
  83. calendarIds := make([]int64, 0)
  84. if err := e.DB.Model(&model.WorkOrderCalendar{}).Select("id").
  85. Where("name = ?", plan.Name).
  86. Where("is_show = ?", pasturePb.IsShow_Ok).
  87. Order("id DESC").
  88. Limit(100). // todo 默认取100条数据
  89. Find(&calendarIds).Error; err != nil {
  90. return xerr.WithStack(err)
  91. }
  92. workOrderList := make([]*model.WorkOrderList, 0)
  93. if err := e.DB.Where("calendar_id IN ?", calendarIds).
  94. Where("is_show = ?", pasturePb.IsShow_Ok).
  95. Where("is_completion = ?", pasturePb.IsShow_No).
  96. Find(&workOrderList).Error; err != nil {
  97. return xerr.WithStack(err)
  98. }
  99. if len(workOrderList) > 0 {
  100. for _, cow := range cowList {
  101. for _, workOrder := range workOrderList {
  102. if workOrder.CowId == cow.Id {
  103. continue
  104. }
  105. }
  106. newCowList = append(newCowList, cow)
  107. }
  108. }
  109. }
  110. count := len(newCowList)
  111. if err := e.DB.Transaction(func(tx *gorm.DB) error {
  112. calendarTypeMap := backend.CalendarTypeMap()
  113. newWorkOrderCalendar := model.NewWorkOrderCalendar(
  114. calendarTypeMap[pasturePb.CalendarType_Immunisation],
  115. pasturePb.CalendarType_Immunisation,
  116. int32(count),
  117. )
  118. if err := tx.Create(newWorkOrderCalendar).Error; err != nil {
  119. return xerr.WithStack(err)
  120. }
  121. newWorkOrderList := make([]*model.WorkOrderList, 0)
  122. for _, cow := range newCowList {
  123. newWorkOrderList = append(newWorkOrderList, model.NewWorkOrderList(plan.Name, newWorkOrderCalendar.Id, cow.Id))
  124. }
  125. if err := tx.Create(newWorkOrderList).Error; err != nil {
  126. return xerr.WithStack(err)
  127. }
  128. return nil
  129. }); err != nil {
  130. return xerr.WithStack(err)
  131. }
  132. return nil
  133. }
  134. // GetTowardSameTimeCowSum 获取历史未打激素牛只总数量
  135. func (e *Entry) GetTowardSameTimeCowSum(sameTimeId int64, showDay string) int64 {
  136. res := int64(0)
  137. if err := e.DB.Model(&model.SameTimeCow{}).
  138. Where("status = ?", pasturePb.IsShow_Ok).
  139. Where("same_time_id = ?", sameTimeId).
  140. Where("show_day = ?", showDay).
  141. Count(&res).Error; err != nil {
  142. zaplog.Error("GetTowardSameTimeCowSum", zap.Any("err", err))
  143. }
  144. return res
  145. }
  146. // SameTimePlan 同期计划,生成工作单
  147. func (e *Entry) SameTimePlan() error {
  148. sameTimeList := make([]*model.SameTime, 0)
  149. if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&sameTimeList).Error; err != nil {
  150. return xerr.WithStack(err)
  151. }
  152. pref := e.DB.Select("id").
  153. Where("is_remove = ?", pasturePb.IsShow_Ok).
  154. Where("is_pregnant = ?", pasturePb.IsShow_No)
  155. if ok := e.CreateCrontabLog("ImmunizationPlan"); !ok {
  156. return nil
  157. }
  158. for _, plan := range sameTimeList {
  159. cowList := make([]*model.Cow, 0)
  160. pref.Where("calving_age >= ?", plan.PostpartumDaysStart).
  161. Where("calving_age <= ?", plan.PostpartumDaysEnd)
  162. if err := pref.Find(&cowList).Error; err != nil {
  163. zaplog.Error("crontab", zap.Any("SameTimePlan", err), zap.Any("plan", plan))
  164. return xerr.WithStack(err)
  165. }
  166. if len(cowList) <= 0 {
  167. continue
  168. }
  169. if err := e.GenerateCalendarBySameTimePlan(cowList, plan); err != nil {
  170. zaplog.Error("crontab", zap.Any("GenerateCalendarBySameTimePlan", err), zap.Any("cowList", cowList), zap.Any("plan", plan))
  171. continue
  172. }
  173. }
  174. return nil
  175. }