cow_cron.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. if err := e.DB.Model(new(model.Cow)).Where("id = ?", cow.Id).Updates(map[string]interface{}{
  60. "day_age": cow.GetDayAge(),
  61. "calving_at": cow.GetCalvingAge(),
  62. "pregnancy_age": cow.GetDaysPregnant(),
  63. "admission_age": cow.GetAdmissionAge(),
  64. "abortion_age": cow.GetAbortionAge(),
  65. }).Error; err != nil {
  66. zaplog.Error("Crontab", zap.Any("UpdateCowDayAge", err))
  67. }
  68. }
  69. return nil
  70. }
  71. // ImmunizationPlan 免疫计划,生成工作单
  72. func (e *Entry) ImmunizationPlan() error {
  73. if ok := e.IsExistCrontabLog(ImmunizationPlan); ok {
  74. return nil
  75. }
  76. planList := make([]*model.ImmunizationPlan, 0)
  77. if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&planList).Error; err != nil {
  78. return xerr.WithStack(err)
  79. }
  80. var todayCount int32 = 0
  81. defer func() {
  82. var count int64 = 0
  83. if err := e.DB.Model(new(model.CowImmunizationPlan)).Where("status = ?", pasturePb.IsShow_Ok).Count(&count).Error; err != nil {
  84. zaplog.Error("Crontab", zap.Any("ImmunizationPlanDefer", err))
  85. }
  86. todayCount += int32(count)
  87. if todayCount > 0 {
  88. e.CreatedCalendar(pasturePb.CalendarType_Immunisation, todayCount)
  89. }
  90. e.CreateCrontabLog(ImmunizationPlan)
  91. }()
  92. for _, plan := range planList {
  93. if plan == nil {
  94. continue
  95. }
  96. cowList := make([]*model.Cow, 0)
  97. pref := e.DB.Table(fmt.Sprintf("%s as a", new(model.ImmunizationPlan).TableName())).
  98. Select("a.*").
  99. Where("a.is_show = ?", pasturePb.IsShow_Ok)
  100. if plan.CowType > 0 {
  101. pref.Where("a.cow_type = ?", plan.CowType)
  102. }
  103. switch plan.Conditions {
  104. case pasturePb.ImmunizationConditions_Days_Age:
  105. pref.Where("a.day_age = ?", plan.Value)
  106. case pasturePb.ImmunizationConditions_Days_After_Delivery:
  107. pref.Where("a.calving_age = ?", plan.Value)
  108. case pasturePb.ImmunizationConditions_Days_Of_Pregnancy:
  109. pref.Where("a.pregnancy_age = ?", plan.Value).
  110. Where("a.is_pregnant = ?", pasturePb.IsShow_Ok)
  111. case pasturePb.ImmunizationConditions_Month:
  112. // todo 待实现月份
  113. case pasturePb.ImmunizationConditions_Admission_Days:
  114. pref.Where("a.admission_age = ?", plan.Value)
  115. case pasturePb.ImmunizationConditions_Other_Vaccine_After:
  116. if plan.ImmunizationPlanId > 0 {
  117. pref.Joins("INNER JOIN immunization_plan_cow as b ON b.immunization_plan_id = ?", plan.ImmunizationPlanId).
  118. Where("b.cow_id = a.id").
  119. Where("DATE_ADD(b.reality_time, INTERVAL ? DAY) = ?", plan.Value, time.Now().Format(model.LayoutDate2)).
  120. Where("b.status = ?", pasturePb.IsShow_Ok)
  121. }
  122. }
  123. if err := pref.Find(&cowList).Error; err != nil {
  124. return xerr.WithStack(err)
  125. }
  126. if len(cowList) <= 0 {
  127. continue
  128. }
  129. todayCount += int32(len(cowList))
  130. newImmunizationPlanCowList := model.NewCowImmunizationPlanList(cowList, plan)
  131. if err := e.DB.Create(newImmunizationPlanCowList).Error; err != nil {
  132. zaplog.Error("ImmunizationPlan", zap.Any("newImmunizationPlanCowList", err), zap.Any("plan", plan), zap.Any("cowList", cowList))
  133. }
  134. }
  135. zaplog.Info("ImmunizationPlan", zap.Any("todayCount", todayCount))
  136. return nil
  137. }
  138. // SameTimePlan 同期计划,生成工作单
  139. func (e *Entry) SameTimePlan() error {
  140. if ok := e.IsExistCrontabLog(SameTimePlan); ok {
  141. return nil
  142. }
  143. sameTimeList := make([]*model.SameTime, 0)
  144. if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&sameTimeList).Error; err != nil {
  145. return xerr.WithStack(err)
  146. }
  147. // 更新日历里面的数据
  148. var todayCount int32 = 0
  149. defer func() {
  150. if todayCount > 0 {
  151. e.CreatedCalendar(pasturePb.CalendarType_Immunisation, todayCount)
  152. }
  153. e.CreateCrontabLog(SameTimePlan)
  154. }()
  155. currWeek := time.Now().Weekday()
  156. for _, sameTime := range sameTimeList {
  157. if sameTime == nil {
  158. continue
  159. }
  160. if time.Weekday(sameTime.WeekType) != currWeek {
  161. continue
  162. }
  163. cowList := make([]*model.Cow, 0)
  164. pref := e.DB.Where("is_remove = ?", pasturePb.IsShow_Ok)
  165. if sameTime.CowType == pasturePb.SameTimeCowType_Breeding_Calf {
  166. pref.Where("calving_age >= ?", sameTime.PostpartumDaysStart).
  167. Where("calving_age <= ?", sameTime.PostpartumDaysEnd).
  168. Where("is_pregnant = ?", pasturePb.IsShow_No)
  169. }
  170. if sameTime.CowType == pasturePb.SameTimeCowType_Empty {
  171. pref.Where(
  172. e.DB.Where("breed_status = ?", pasturePb.BreedStatus_Empty).
  173. Or("breed_status = ?", pasturePb.BreedStatus_Abort),
  174. ).Where("is_pregnant = ?", pasturePb.IsShow_No)
  175. }
  176. if err := pref.Find(&cowList).Error; err != nil {
  177. zaplog.Error("crontab", zap.Any("SameTimePlan", err), zap.Any("plan", sameTime))
  178. return xerr.WithStack(err)
  179. }
  180. if len(cowList) <= 0 {
  181. continue
  182. }
  183. currCount, err := e.GenerateCalendarBySameTimePlan(cowList, sameTime)
  184. if err != nil {
  185. zaplog.Error("crontab",
  186. zap.Any("GenerateCalendarBySameTimePlan", err),
  187. zap.Any("cowList", cowList),
  188. zap.Any("plan", sameTime),
  189. )
  190. continue
  191. }
  192. todayCount += currCount
  193. }
  194. return nil
  195. }
  196. // UpdateSameTime 更新每天同情数据
  197. func (e *Entry) UpdateSameTime() error {
  198. calendarTypeList := backend.CalendarTypeEnumList()
  199. showDay := time.Now().Format(model.LayoutDate2)
  200. for _, v := range calendarTypeList {
  201. count := int64(0)
  202. if err := e.DB.Model(new(model.EventCowSameTime)).
  203. Where("same_time_type = ?", v.Value).
  204. Where("status = ?", pasturePb.IsShow_No).
  205. Count(&count).Error; err != nil {
  206. zaplog.Error("crontab", zap.Any("UpdateSameTime", err), zap.Any("count", count))
  207. }
  208. if count >= 0 {
  209. continue
  210. }
  211. isExist := int64(0)
  212. if err := e.DB.Model(new(model.Calendar)).
  213. Where("calendar_type = ?", v.Value).
  214. Where("show_day = ?", showDay).
  215. Count(&isExist).Error; err != nil {
  216. continue
  217. }
  218. if isExist <= 0 {
  219. continue
  220. }
  221. if err := e.DB.Model(new(model.Calendar)).
  222. Where("calendar_type = ?", v.Value).
  223. Where("show_day = ?", showDay).
  224. Updates(map[string]interface{}{
  225. "count": count,
  226. }).Error; err != nil {
  227. continue
  228. }
  229. }
  230. return nil
  231. }
  232. // PregnancyCheck 妊娠期检查
  233. func (e *Entry) PregnancyCheck() error {
  234. systemBasicList := make([]*model.SystemBasic, 0)
  235. if err := e.DB.Model(new(model.SystemBasic)).
  236. Where("is_show = ?", pasturePb.IsShow_Ok).
  237. Where("name IN ?", []string{
  238. model.PregnantCheckForFirst,
  239. model.PregnantCheckForSecond,
  240. model.WeaningAge,
  241. model.PregnancyAge,
  242. }).Find(&systemBasicList).Error; err != nil {
  243. zaplog.Error("crontab", zap.Any("PregnancyCheck", err))
  244. return xerr.WithStack(err)
  245. }
  246. currWeekValue := time.Now().Weekday()
  247. planDay := time.Now().Unix()
  248. for _, v := range systemBasicList {
  249. // 周执行
  250. if v.WeekValue >= 0 && time.Weekday(v.WeekValue) != currWeekValue {
  251. continue
  252. }
  253. cowList := make([]*model.Cow, 0)
  254. pref := e.DB.Model(new(model.Cow)).Where("is_remove = ? ", pasturePb.IsShow_Ok)
  255. switch v.Name {
  256. case model.PregnantCheckForFirst:
  257. pref.Where("breed_status = ?", pasturePb.BreedStatus_Breeding)
  258. case model.PregnantCheckForSecond: // 过滤初检空怀的牛只
  259. pref.Where("breed_status = ?", pasturePb.BreedStatus_Pregnant)
  260. case model.WeaningAge:
  261. case model.PregnancyAge:
  262. pref.Where("pregnancy_age >= ?", v.MinValue).
  263. Where("breed_status = ?", pasturePb.BreedStatus_Pregnant)
  264. default:
  265. continue
  266. }
  267. if v.ValueType == model.ValueTypeFixed {
  268. pref.Where("day_age = ?", v.MinValue)
  269. }
  270. if v.ValueType == model.ValueTypeRange {
  271. pref.Where("day_age >= ?", v.MinValue).Where("day_age <= ?", v.MaxValue)
  272. }
  273. if err := pref.Find(&cowList).Error; err != nil {
  274. zaplog.Error("crontab", zap.Any("PregnancyCheck", err), zap.Any("cowList", cowList))
  275. continue
  276. }
  277. if len(cowList) <= 0 {
  278. continue
  279. }
  280. e.InitEventData(cowList, v.Name, planDay)
  281. }
  282. return nil
  283. }
  284. func (e *Entry) InitEventData(cowList []*model.Cow, systemBasicName string, planDay int64) {
  285. switch systemBasicName {
  286. case model.PregnantCheckForFirst, model.PregnantCheckForSecond:
  287. eventPregnantCheckDataList := model.NewEventPregnantCheckList(cowList, planDay, systemBasicName)
  288. if err := e.DB.Create(eventPregnantCheckDataList).Error; err != nil {
  289. zaplog.Error("crontab", zap.Any("InitEventData", err), zap.Any("eventPregnantCheckDataList", eventPregnantCheckDataList))
  290. }
  291. case model.WeaningAge:
  292. eventWeaningDataList := model.NewEventWeaningList(cowList, planDay)
  293. if err := e.DB.Create(eventWeaningDataList).Error; err != nil {
  294. zaplog.Error("crontab", zap.Any("InitEventData", err), zap.Any("eventWeaningDataList", eventWeaningDataList))
  295. }
  296. case model.PregnancyAge:
  297. eventCalvingList := model.NewEventCalvingList(cowList, planDay)
  298. if err := e.DB.Create(eventCalvingList).Error; err != nil {
  299. zaplog.Error("crontab", zap.Any("InitEventData", err), zap.Any("eventCalvingList", eventCalvingList))
  300. }
  301. }
  302. }
  303. // DiseaseCheck 疾病检查
  304. func (e *Entry) DiseaseCheck() error {
  305. return nil
  306. }