cow_cron.go 7.7 KB

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