cow_cron.go 11 KB

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