cow_cron.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. "gorm.io/gorm"
  10. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  11. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  12. "gitee.com/xuyiping_admin/pkg/xerr"
  13. "go.uber.org/zap"
  14. )
  15. const (
  16. UpdateCowInfo = "UpdateCowInfo"
  17. ImmunizationPlan = "ImmunizationPlan"
  18. SameTimePlan = "SameTimePlan"
  19. WorkOrderMaster = "WorkOrderMaster"
  20. SystemBasicCrontab = "SystemBasicCrontab"
  21. )
  22. // GenerateAsynqWorkOrder 异步生成工作单
  23. func (e *Entry) GenerateAsynqWorkOrder() error {
  24. if ok := e.IsExistCrontabLog(WorkOrderMaster); ok {
  25. return nil
  26. }
  27. defer func() {
  28. e.CreateCrontabLog(WorkOrderMaster)
  29. }()
  30. workOrderList := make([]*model.WorkOrderMaster, 0)
  31. if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&workOrderList).Error; err != nil {
  32. return err
  33. }
  34. for _, workOrder := range workOrderList {
  35. timeUnix, err := util.ConvertParseLocalUnix(workOrder.ExecTime)
  36. if timeUnix <= 0 || err != nil {
  37. zaplog.Error("crontab", zap.Any("GenerateWorkOrder", err), zap.Any("execTime", workOrder.ExecTime))
  38. continue
  39. }
  40. nowTime := time.Now().Unix()
  41. if timeUnix < nowTime {
  42. continue
  43. }
  44. execTime := timeUnix - nowTime
  45. task := model.NewTaskWorkOrderPayload(workOrder.Id, time.Duration(execTime)*time.Second)
  46. if _, err = e.AsynqClient.CtxEnqueue(context.Background(), task); err != nil {
  47. zaplog.Error("PushMessage CtxEnqueue", zap.Any("Err", err))
  48. }
  49. }
  50. return nil
  51. }
  52. // UpdateCowInfo 牛只基本信息维护
  53. func (e *Entry) UpdateCowInfo() error {
  54. cowList := make([]*model.Cow, 0)
  55. if err := e.DB.Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).Find(&cowList).Error; err != nil {
  56. return err
  57. }
  58. if ok := e.IsExistCrontabLog(UpdateCowInfo); ok {
  59. return nil
  60. }
  61. defer func() {
  62. e.CreateCrontabLog(UpdateCowInfo)
  63. }()
  64. for _, cow := range cowList {
  65. if err := e.DB.Model(new(model.Cow)).Where("id = ?", cow.Id).Updates(map[string]interface{}{
  66. "day_age": cow.GetDayAge(),
  67. "calving_at": cow.GetCalvingAge(),
  68. "pregnancy_age": cow.GetDaysPregnant(),
  69. "admission_age": cow.GetAdmissionAge(),
  70. "abortion_age": cow.GetAbortionAge(),
  71. }).Error; err != nil {
  72. zaplog.Error("Crontab", zap.Any("UpdateCowDayAge", err))
  73. }
  74. }
  75. return nil
  76. }
  77. // ImmunizationPlan 免疫计划,生成工作单
  78. func (e *Entry) ImmunizationPlan() error {
  79. if ok := e.IsExistCrontabLog(ImmunizationPlan); ok {
  80. return nil
  81. }
  82. planList := make([]*model.ImmunizationPlan, 0)
  83. if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&planList).Error; err != nil {
  84. return xerr.WithStack(err)
  85. }
  86. var todayCount int32 = 0
  87. defer func() {
  88. var count int64 = 0
  89. if err := e.DB.Model(new(model.CowImmunizationPlan)).Where("status = ?", pasturePb.IsShow_Ok).Count(&count).Error; err != nil {
  90. zaplog.Error("Crontab", zap.Any("ImmunizationPlanDefer", err))
  91. }
  92. todayCount += int32(count)
  93. if todayCount > 0 {
  94. e.CreatedCalendar(pasturePb.CalendarType_Immunisation, todayCount)
  95. }
  96. e.CreateCrontabLog(ImmunizationPlan)
  97. }()
  98. for _, plan := range planList {
  99. if plan == nil {
  100. continue
  101. }
  102. cowList := make([]*model.Cow, 0)
  103. pref := e.DB.Table(fmt.Sprintf("%s as a", new(model.ImmunizationPlan).TableName())).
  104. Select("a.*").
  105. Where("a.is_show = ?", pasturePb.IsShow_Ok)
  106. if plan.CowType > 0 {
  107. pref.Where("a.cow_type = ?", plan.CowType)
  108. }
  109. switch plan.Conditions {
  110. case pasturePb.ImmunizationConditions_Days_Age:
  111. pref.Where("a.day_age = ?", plan.Value)
  112. case pasturePb.ImmunizationConditions_Days_After_Delivery:
  113. pref.Where("a.calving_age = ?", plan.Value)
  114. case pasturePb.ImmunizationConditions_Days_Of_Pregnancy:
  115. pref.Where("a.pregnancy_age = ?", plan.Value).
  116. Where("a.is_pregnant = ?", pasturePb.IsShow_Ok)
  117. case pasturePb.ImmunizationConditions_Month:
  118. // todo 待实现月份
  119. case pasturePb.ImmunizationConditions_Admission_Days:
  120. pref.Where("a.admission_age = ?", plan.Value)
  121. case pasturePb.ImmunizationConditions_Other_Vaccine_After:
  122. if plan.ImmunizationPlanId > 0 {
  123. pref.Joins("INNER JOIN immunization_plan_cow as b ON b.immunization_plan_id = ?", plan.ImmunizationPlanId).
  124. Where("b.cow_id = a.id").
  125. Where("DATE_ADD(b.reality_time, INTERVAL ? DAY) = ?", plan.Value, time.Now().Format(model.LayoutDate2)).
  126. Where("b.status = ?", pasturePb.IsShow_Ok)
  127. }
  128. }
  129. if err := pref.Find(&cowList).Error; err != nil {
  130. return xerr.WithStack(err)
  131. }
  132. if len(cowList) <= 0 {
  133. continue
  134. }
  135. todayCount += int32(len(cowList))
  136. newImmunizationPlanCowList := model.NewCowImmunizationPlanList(cowList, plan)
  137. newEventItemList := model.NewEventItemList(cowList, pasturePb.CalendarType_Immunisation)
  138. if err := e.DB.Transaction(func(tx *gorm.DB) error {
  139. if err := tx.Create(newImmunizationPlanCowList).Error; err != nil {
  140. return xerr.WithStack(err)
  141. }
  142. if err := tx.Create(newEventItemList).Error; err != nil {
  143. return xerr.WithStack(err)
  144. }
  145. return nil
  146. }); err != nil {
  147. zaplog.Error("ImmunizationPlan", zap.Any("newImmunizationPlanCowList", err), zap.Any("plan", plan), zap.Any("cowList", cowList))
  148. }
  149. }
  150. zaplog.Info("ImmunizationPlan", zap.Any("todayCount", todayCount))
  151. return nil
  152. }
  153. // SameTimePlan 同期计划,生成工作单
  154. func (e *Entry) SameTimePlan() error {
  155. if ok := e.IsExistCrontabLog(SameTimePlan); ok {
  156. return nil
  157. }
  158. sameTimeList := make([]*model.SameTime, 0)
  159. if err := e.DB.Where("is_show = ?", pasturePb.IsShow_Ok).Find(&sameTimeList).Error; err != nil {
  160. return xerr.WithStack(err)
  161. }
  162. // 更新日历里面的数据
  163. var todayCount int32 = 0
  164. defer func() {
  165. if todayCount > 0 {
  166. e.CreatedCalendar(pasturePb.CalendarType_Immunisation, todayCount)
  167. }
  168. e.CreateCrontabLog(SameTimePlan)
  169. }()
  170. currWeek := time.Now().Weekday()
  171. for _, sameTime := range sameTimeList {
  172. if sameTime == nil {
  173. continue
  174. }
  175. if time.Weekday(sameTime.WeekType) != currWeek {
  176. continue
  177. }
  178. cowList := make([]*model.Cow, 0)
  179. pref := e.DB.Where("admission_status = ?", pasturePb.AdmissionStatus_Admission)
  180. if sameTime.CowType == pasturePb.SameTimeCowType_Breeding_Calf {
  181. pref.Where("calving_age >= ?", sameTime.PostpartumDaysStart).
  182. Where("calving_age <= ?", sameTime.PostpartumDaysEnd).
  183. Where("is_pregnant = ?", pasturePb.IsShow_No)
  184. }
  185. if sameTime.CowType == pasturePb.SameTimeCowType_Empty {
  186. pref.Where(
  187. e.DB.Where("breed_status = ?", pasturePb.BreedStatus_Empty).
  188. Or("breed_status = ?", pasturePb.BreedStatus_Abort),
  189. ).Where("is_pregnant = ?", pasturePb.IsShow_No)
  190. }
  191. if err := pref.Find(&cowList).Error; err != nil {
  192. zaplog.Error("crontab", zap.Any("SameTimePlan", err), zap.Any("plan", sameTime))
  193. return xerr.WithStack(err)
  194. }
  195. if len(cowList) <= 0 {
  196. continue
  197. }
  198. currCount, err := e.GenerateCalendarBySameTimePlan(cowList, sameTime)
  199. if err != nil {
  200. zaplog.Error("crontab",
  201. zap.Any("GenerateCalendarBySameTimePlan", err),
  202. zap.Any("cowList", cowList),
  203. zap.Any("plan", sameTime),
  204. )
  205. continue
  206. }
  207. todayCount += currCount
  208. }
  209. return nil
  210. }
  211. // UpdateSameTime 更新每天同情数据
  212. func (e *Entry) UpdateSameTime() error {
  213. calendarTypeList := backend.CalendarTypeEnumList("")
  214. showDay := time.Now().Format(model.LayoutDate2)
  215. for _, v := range calendarTypeList {
  216. count := int64(0)
  217. if err := e.DB.Model(new(model.EventCowSameTime)).
  218. Where("same_time_type = ?", v.Value).
  219. Where("status = ?", pasturePb.IsShow_No).
  220. Count(&count).Error; err != nil {
  221. zaplog.Error("crontab", zap.Any("UpdateSameTime", err), zap.Any("count", count))
  222. }
  223. if count >= 0 {
  224. continue
  225. }
  226. isExist := int64(0)
  227. if err := e.DB.Model(new(model.Calendar)).
  228. Where("calendar_type = ?", v.Value).
  229. Where("show_day = ?", showDay).
  230. Count(&isExist).Error; err != nil {
  231. continue
  232. }
  233. if isExist <= 0 {
  234. continue
  235. }
  236. if err := e.DB.Model(new(model.Calendar)).
  237. Where("calendar_type = ?", v.Value).
  238. Where("show_day = ?", showDay).
  239. Updates(map[string]interface{}{
  240. "count": count,
  241. }).Error; err != nil {
  242. continue
  243. }
  244. }
  245. return nil
  246. }
  247. // SystemBasicCrontab 基础配置计划任务
  248. func (e *Entry) SystemBasicCrontab() error {
  249. if ok := e.IsExistCrontabLog(SystemBasicCrontab); ok {
  250. return nil
  251. }
  252. defer func() {
  253. e.CreateCrontabLog(SystemBasicCrontab)
  254. }()
  255. systemBasicList := make([]*model.SystemBasic, 0)
  256. if err := e.DB.Model(new(model.SystemBasic)).
  257. Where("is_show = ?", pasturePb.IsShow_Ok).
  258. Where("name IN ?", []string{
  259. model.PregnantCheckForFirst,
  260. model.PregnantCheckForSecond,
  261. model.WeaningAge,
  262. model.PregnancyAge,
  263. }).Find(&systemBasicList).Error; err != nil {
  264. zaplog.Error("crontab", zap.Any("PregnancyCheck", err))
  265. return xerr.WithStack(err)
  266. }
  267. currWeekValue := time.Now().Weekday()
  268. for _, v := range systemBasicList {
  269. // 周执行
  270. if v.WeekValue >= 0 && time.Weekday(v.WeekValue) != currWeekValue {
  271. continue
  272. }
  273. cowList := make([]*model.Cow, 0)
  274. pref := e.DB.Model(new(model.Cow)).Where("admission_status = ? ", pasturePb.AdmissionStatus_Admission)
  275. switch v.Name {
  276. case model.PregnantCheckForFirst:
  277. pref.Where("breed_status = ?", pasturePb.BreedStatus_Breeding)
  278. case model.PregnantCheckForSecond: // 过滤初检空怀的牛只
  279. pref.Where("breed_status = ?", pasturePb.BreedStatus_Pregnant)
  280. case model.WeaningAge:
  281. pref.Where("day_age = ?", v.MinValue)
  282. case model.PregnancyAge:
  283. pref.Where("pregnancy_age >= ?", v.MinValue).
  284. Where("breed_status = ?", pasturePb.BreedStatus_Pregnant)
  285. default:
  286. continue
  287. }
  288. if v.ValueType == model.ValueTypeFixed {
  289. pref.Where("day_age = ?", v.MinValue)
  290. }
  291. if v.ValueType == model.ValueTypeRange {
  292. pref.Where("day_age >= ?", v.MinValue).Where("day_age <= ?", v.MaxValue)
  293. }
  294. if err := pref.Find(&cowList).Error; err != nil {
  295. zaplog.Error("crontab", zap.Any("PregnancyCheck", err), zap.Any("cowList", cowList))
  296. continue
  297. }
  298. if len(cowList) <= 0 {
  299. continue
  300. }
  301. e.InitEventData(cowList, v.Name)
  302. }
  303. return nil
  304. }
  305. func (e *Entry) InitEventData(cowList []*model.Cow, systemBasicName string) {
  306. switch systemBasicName {
  307. case model.PregnantCheckForFirst, model.PregnantCheckForSecond:
  308. eventPregnantCheckDataList := model.NewEventPregnantCheckList(cowList, systemBasicName)
  309. if err := e.DB.Create(eventPregnantCheckDataList).Error; err != nil {
  310. zaplog.Error("crontab", zap.Any("InitEventData", err), zap.Any("eventPregnantCheckDataList", eventPregnantCheckDataList))
  311. }
  312. case model.WeaningAge:
  313. eventWeaningDataList := model.NewEventWeaningList(cowList)
  314. if err := e.DB.Create(eventWeaningDataList).Error; err != nil {
  315. zaplog.Error("crontab", zap.Any("InitEventData", err), zap.Any("eventWeaningDataList", eventWeaningDataList))
  316. }
  317. case model.PregnancyAge:
  318. eventCalvingList := model.NewEventCalvingList(cowList)
  319. if err := e.DB.Create(eventCalvingList).Error; err != nil {
  320. zaplog.Error("crontab", zap.Any("InitEventData", err), zap.Any("eventCalvingList", eventCalvingList))
  321. }
  322. }
  323. }
  324. // DiseaseCheck 疾病检查
  325. func (e *Entry) DiseaseCheck() error {
  326. return nil
  327. }