cow_cron.go 11 KB

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