cow_cron.go 11 KB

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