neck_ring_calculate.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. package crontab
  2. import (
  3. "fmt"
  4. "kpt-pasture/model"
  5. "math"
  6. "time"
  7. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  8. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  9. "gitee.com/xuyiping_admin/pkg/xerr"
  10. "go.uber.org/zap"
  11. )
  12. func (e *Entry) NeckRingCalculate() error {
  13. pastureList := e.FindPastureList()
  14. if pastureList == nil || len(pastureList) == 0 {
  15. return nil
  16. }
  17. if calculateIsRunning {
  18. return nil
  19. }
  20. defer func() {
  21. calculateIsRunning = false
  22. }()
  23. calculateIsRunning = true
  24. for _, pasture := range pastureList {
  25. if err := e.EntryUpdateActiveHabit(pasture.Id); err != nil {
  26. zaplog.Error("NeckRingCalculate", zap.Any("err", err), zap.Any("pasture", pasture))
  27. }
  28. zaplog.Info(fmt.Sprintf("NeckRingCalculate Success %d", pasture.Id))
  29. }
  30. return nil
  31. }
  32. func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
  33. // 获取这段执行数据内最大日期和最小日期
  34. xToday, err := e.XToday(pastureId)
  35. if err != nil {
  36. return xerr.WithStack(err)
  37. }
  38. var processIds []int64
  39. // 更新活动滤波
  40. processIds, err = e.FirstFilterUpdate(pastureId, xToday)
  41. if err != nil {
  42. zaplog.Error("NeckRingCalculate", zap.Any("FirstFilterUpdate", err), zap.Any("xToday", xToday))
  43. }
  44. zaplog.Info("NeckRingCalculate", zap.Any("xToday", xToday), zap.Any("processIds", processIds))
  45. if len(processIds) <= 0 {
  46. return nil
  47. }
  48. e.WeeklyUpdateActiveHabit(pastureId, processIds, xToday)
  49. // 二次更新滤波
  50. e.SecondUpdateChangeFilter(pastureId, xToday)
  51. // 活动量校正系数和健康评分
  52. e.FilterCorrectAndScoreUpdate(pastureId, processIds, xToday)
  53. // 更新 ChangeFilter
  54. e.UpdateChangeFilter(pastureId, processIds)
  55. // 更新 FilterCorrect
  56. e.UpdateFilterCorrect(pastureId, processIds)
  57. // 插入群体校正表
  58. e.UpdateChangeAdJust(pastureId, xToday)
  59. // 更新所有的显示状态为否的记录为是
  60. e.UpdateIsShow(pastureId, processIds)
  61. // 健康预警
  62. e.HealthWarning(pastureId, processIds)
  63. return nil
  64. }
  65. // FirstFilterUpdate 首次更新活动滤波
  66. func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds []int64, err error) {
  67. newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  68. if err = e.DB.Model(new(model.NeckActiveHabit)).
  69. Where("heat_date >= ?", "2025-03-01").
  70. Where("pasture_id = ?", pastureId).
  71. Where("is_show = ?", pasturePb.IsShow_No).
  72. //Where("record_count = ?", model.DefaultRecordCount).
  73. Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)).
  74. Where("cow_id > ?", 0).
  75. Order("heat_date,neck_ring_number,frameid").
  76. Limit(int(defaultLimit)).
  77. Find(&newNeckActiveHabitList).Error; err != nil {
  78. return nil, xerr.WithStack(err)
  79. }
  80. // 活动量滤波
  81. for _, v := range newNeckActiveHabitList {
  82. // 过滤牛只未绑定的脖环的数据
  83. cowInfo := e.GetCowInfoByNeckRingNumber(v.PastureId, v.NeckRingNumber)
  84. if cowInfo == nil || cowInfo.Id <= 0 {
  85. v.UpdateIsShowOk()
  86. if err = e.DB.Model(new(model.NeckActiveHabit)).
  87. Select("is_show").
  88. Where("id = ?", v.Id).
  89. Updates(v).Error; err != nil {
  90. zaplog.Error("EntryUpdateActiveHabit", zap.Any("error", err))
  91. }
  92. continue
  93. }
  94. // 8小时数据不全的不参与滤波
  95. activeTime, _ := time.Parse(model.LayoutTime, v.ActiveTime)
  96. if v.RecordCount != model.DefaultRecordCount && activeTime.Sub(time.Now()).Hours() < 8 {
  97. continue
  98. }
  99. frameId := v.Frameid
  100. heatDate := v.HeatDate
  101. if v.Frameid == 0 {
  102. frameId = 11
  103. heatDateParse, _ := time.Parse(model.LayoutDate2, heatDate)
  104. heatDate = heatDateParse.AddDate(0, 0, -1).Format(model.LayoutDate2)
  105. } else {
  106. frameId -= 1
  107. }
  108. firstFilterData := e.FindFilterData(pastureId, v.NeckRingNumber, heatDate, frameId)
  109. if v.FilterHigh > 0 {
  110. firstFilterData.FilterHigh = v.FilterHigh
  111. } else {
  112. if v.NeckRingNumber == firstFilterData.NeckRingNumber {
  113. firstFilterData.FilterHigh = int32(computeIfPositiveElse(float64(v.High), float64(firstFilterData.FilterHigh), 0.23, 0.77))
  114. } else {
  115. firstFilterData.FilterHigh = v.High
  116. }
  117. }
  118. if v.FilterRumina > 0 {
  119. firstFilterData.FilterRumina = v.FilterRumina
  120. } else {
  121. if v.NeckRingNumber == firstFilterData.NeckRingNumber {
  122. firstFilterData.FilterRumina = int32(computeIfPositiveElse(float64(v.Rumina), float64(firstFilterData.FilterRumina), 0.33, 0.67))
  123. } else {
  124. firstFilterData.FilterRumina = v.Rumina
  125. }
  126. }
  127. if v.FilterChew > 0 {
  128. firstFilterData.FilterChew = v.FilterChew
  129. } else {
  130. if v.NeckRingNumber == firstFilterData.NeckRingNumber {
  131. firstFilterData.FilterChew = int32(computeIfPositiveElse(float64(v.Rumina+v.Intake), float64(firstFilterData.FilterChew), 0.33, 0.67))
  132. } else {
  133. firstFilterData.FilterChew = v.Rumina + v.Intake
  134. }
  135. }
  136. processIds = append(processIds, v.Id)
  137. // 更新过滤值 // todo 记得更新胎次为牛只胎次,现在为了测试特意改成0
  138. if err = e.DB.Model(new(model.NeckActiveHabit)).
  139. Select("filter_high", "filter_rumina", "filter_chew", "cow_id", "lact", "calving_age", "ear_number").
  140. Where("id = ?", v.Id).
  141. Updates(map[string]interface{}{
  142. "filter_high": firstFilterData.FilterHigh,
  143. "filter_rumina": firstFilterData.FilterRumina,
  144. "filter_chew": firstFilterData.FilterChew,
  145. "cow_id": cowInfo.Id,
  146. "lact": 0,
  147. "calving_age": cowInfo.CalvingAge,
  148. "ear_number": cowInfo.EarNumber,
  149. }).Error; err != nil {
  150. zaplog.Error("FirstFilterUpdate",
  151. zap.Any("error", err),
  152. zap.Any("firstFilterData", firstFilterData),
  153. zap.Any("NeckActiveHabit", v),
  154. zap.Any("cowInfo", cowInfo),
  155. zap.Any("xToday", xToDay),
  156. )
  157. }
  158. }
  159. return processIds, nil
  160. }
  161. // SecondUpdateChangeFilter 第二次更新变化趋势滤波
  162. func (e *Entry) SecondUpdateChangeFilter(pastureId int64, xToday *XToday) {
  163. newChangeFilterList := make([]*ChangeFilterData, 0)
  164. if err := e.DB.Model(new(model.NeckActiveHabit)).
  165. Select("id", "neck_ring_number", "change_high", "change_filter", "rumina_filter", "change_rumina",
  166. "chew_filter", "change_chew", "heat_date", "frameid", "IF(lact = 0, 0.8, 1) as xlc_dis_count").
  167. Where("heat_date >= ?", time.Now().AddDate(0, 0, -2).Format(model.LayoutDate2)).
  168. Where("pasture_id = ?", pastureId).
  169. Where("change_filter = ?", model.InitChangeFilter).
  170. Where("change_high > ?", MinChangeHigh).
  171. Order("neck_ring_number,heat_date,frameid").
  172. Limit(int(defaultLimit)).
  173. Find(&newChangeFilterList).Error; err != nil {
  174. zaplog.Error("SecondUpdateChangeFilter", zap.Any("error", err), zap.Any("xToday", xToday))
  175. return
  176. }
  177. for _, v := range newChangeFilterList {
  178. frameId := v.FrameId
  179. heatDate := v.HeatDate
  180. if v.FrameId == 0 {
  181. frameId = 11
  182. heatDateParse, _ := time.Parse(model.LayoutDate2, heatDate)
  183. heatDate = heatDateParse.AddDate(0, 0, -1).Format(model.LayoutDate2)
  184. } else {
  185. frameId -= 1
  186. }
  187. secondFilterData := e.FindFilterData(pastureId, v.NeckRingNumber, heatDate, frameId)
  188. if v.ChangeFilter > MinChangeFilter {
  189. secondFilterData.ChangeFilter = v.ChangeFilter
  190. } else {
  191. if v.NeckRingNumber == secondFilterData.NeckRingNumber {
  192. changeFilter := float64(secondFilterData.ChangeFilter)*(1-(float64(xToday.XChangeDiscount)/10)*v.XlcDisCount) +
  193. math.Min(float64(v.ChangeHigh), float64(secondFilterData.ChangeFilter)+135)*(float64(xToday.XChangeDiscount)/10)*v.XlcDisCount
  194. secondFilterData.ChangeFilter = int32(changeFilter)
  195. } else {
  196. secondFilterData.ChangeFilter = 0
  197. }
  198. }
  199. if v.RuminaFilter > MinRuminaFilter {
  200. secondFilterData.RuminaFilter = v.ChangeFilter
  201. } else {
  202. if v.NeckRingNumber == secondFilterData.NeckRingNumber {
  203. discount := float64(xToday.XRuminaDisc) / 10 * v.XlcDisCount
  204. if math.Abs(float64(v.ChangeRumina)) > 60 {
  205. discount *= 0.5
  206. }
  207. secondFilterData.RuminaFilter = int32(float64(secondFilterData.RuminaFilter)*(1-discount) + float64(v.ChangeRumina)*discount)
  208. } else {
  209. secondFilterData.RuminaFilter = 0
  210. }
  211. }
  212. secondFilterData.RuminaFilter = int32(math.Min(50, float64(secondFilterData.RuminaFilter)))
  213. if v.ChewFilter > MinChewFilter {
  214. secondFilterData.ChewFilter = v.ChangeChew
  215. } else {
  216. if v.NeckRingNumber == secondFilterData.NeckRingNumber {
  217. discount := float64(xToday.XRuminaDisc) / 10
  218. if math.Abs(float64(v.ChangeChew)) > 60 {
  219. discount *= 0.5
  220. }
  221. secondFilterData.ChewFilter = int32(float64(secondFilterData.ChewFilter)*(1-discount) + float64(v.ChangeChew)*discount)
  222. } else {
  223. secondFilterData.ChewFilter = 0
  224. }
  225. }
  226. secondFilterData.ChewFilter = int32(math.Min(50, float64(secondFilterData.ChewFilter)))
  227. if err := e.DB.Model(new(model.NeckActiveHabit)).
  228. Select("change_filter", "rumina_filter", "chew_filter").
  229. Where("id = ?", v.Id).
  230. Updates(map[string]interface{}{
  231. "change_filter": secondFilterData.ChangeFilter,
  232. "rumina_filter": secondFilterData.RuminaFilter,
  233. "chew_filter": secondFilterData.ChewFilter,
  234. }).Error; err != nil {
  235. zaplog.Error("SecondUpdateChangeFilter", zap.Any("error", err), zap.Any("secondFilterData", secondFilterData))
  236. }
  237. }
  238. }
  239. // FilterCorrectAndScoreUpdate 计算活动量变化趋势校正值(活跃度校正)和健康评分
  240. func (e *Entry) FilterCorrectAndScoreUpdate(pastureId int64, processIds []int64, xToday *XToday) {
  241. beginDayDate := time.Now()
  242. before7DayDate := beginDayDate.AddDate(0, 0, -7).Format(model.LayoutDate2)
  243. before1DayDate := beginDayDate.AddDate(0, 0, -1).Format(model.LayoutDate2)
  244. neckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  245. if err := e.DB.Model(new(model.NeckActiveHabit)).
  246. Where("id IN (?)", processIds).
  247. Where("pasture_id = ?", pastureId).
  248. Find(&neckActiveHabitList).Error; err != nil {
  249. zaplog.Error("ActivityVolumeChanges-1", zap.Any("error", err), zap.Any("xToday", xToday))
  250. return
  251. }
  252. for _, v := range neckActiveHabitList {
  253. activityVolume := &ActivityVolume{}
  254. if err := e.DB.Model(new(model.NeckActiveHabit)).
  255. Select("neck_ring_number", "AVG(IF(change_filter>=60, 60, change_filter)) as avg_filter",
  256. "ROUND(STD(IF(change_filter>=60, 60, change_filter))) as std_filter", "COUNT(1) as nb").
  257. Where("heat_date BETWEEN ? AND ?", before7DayDate, before1DayDate).
  258. Where("pasture_id = ?", pastureId).
  259. Where(e.DB.Where("high > ?", xToday.High).Or("rumina >= ?", xToday.Rumina)).
  260. Where("active_time <= ?", beginDayDate.Add(-12*time.Hour).Format(model.LayoutTime)).
  261. Where("change_filter > ?", MinChangeFilter).
  262. Where("neck_ring_number = ?").
  263. Having("nb > ?", DefaultNb).
  264. First(&activityVolume).Error; err != nil {
  265. zaplog.Error("ActivityVolumeChanges-0", zap.Any("error", err), zap.Any("xToday", xToday))
  266. continue
  267. }
  268. if activityVolume != nil && activityVolume.NeckRingNumber != "" {
  269. filterCorrect := model.DefaultFilterCorrect - int(math.Floor(activityVolume.AvgFilter/3+float64(activityVolume.StdFilter)/2))
  270. // 活动量校正系数
  271. if err := e.DB.Model(new(model.NeckActiveHabit)).
  272. Where("id = ?", v.Id).
  273. Where("neck_ring_number = ?", v.NeckRingNumber).
  274. Update("filter_correct", filterCorrect).Error; err != nil {
  275. zaplog.Error("ActivityVolumeChanges-2", zap.Any("error", err), zap.Any("xToday", xToday))
  276. continue
  277. }
  278. }
  279. cowScore := calculateScore(v)
  280. if err := e.DB.Model(new(model.NeckActiveHabit)).
  281. Where("id = ?", v.Id).
  282. Update("score", cowScore).Error; err != nil {
  283. zaplog.Error("ActivityVolumeChanges-2", zap.Any("error", err), zap.Any("xToday", xToday))
  284. continue
  285. }
  286. }
  287. }
  288. func (e *Entry) UpdateChangeFilter(pastureId int64, processIds []int64) {
  289. if err := e.DB.Model(new(model.NeckActiveHabit)).
  290. Where("id IN (?)", processIds).
  291. Where("pasture_id = ?", pastureId).
  292. Where("is_show = ?", pasturePb.IsShow_No).
  293. Where("change_filter = ?", model.InitChangeFilter).
  294. Updates(map[string]interface{}{
  295. "change_filter": model.DefaultChangeFilter,
  296. "rumina_filter": model.DefaultRuminaFilter,
  297. "chew_filter": model.DefaultChewFilter,
  298. }).Error; err != nil {
  299. zaplog.Error("UpdateChangeFilter", zap.Any("change_filter", err))
  300. }
  301. }
  302. func (e *Entry) UpdateFilterCorrect(pastureId int64, processIds []int64) {
  303. if err := e.DB.Model(new(model.NeckActiveHabit)).
  304. Where("id IN (?)", processIds).
  305. Where("pasture_id = ?", pastureId).
  306. Where("change_filter < ?", 0).
  307. Where("filter_correct < ?", model.DefaultFilterCorrect).
  308. Updates(map[string]interface{}{
  309. "filter_correct": model.DefaultFilterCorrect,
  310. }).Error; err != nil {
  311. zaplog.Error("UpdateFilterCorrect", zap.Any("filter_correct", err))
  312. }
  313. }
  314. // UpdateChangeAdJust 更新群体校正数据
  315. func (e *Entry) UpdateChangeAdJust(pastureId int64, xToday *XToday) {
  316. /*-- 插入群体校正表
  317. INSERT INTO data_bar_change(heatdate, frameid, intCurBar, intCurBarName, nb, highchange, changefilter)
  318. SELECT h.heatdate, h.frameid, c.intCurBar, c.intCurBarName, COUNT(*) nb, ROUND(AVG(h.highchange)) highchange, ROUND(AVG(h.changefilter) ) changefilter
  319. FROM h_activehabit h JOIN cow c ON h.intCowId=c.intCowId
  320. WHERE h.heatdate>=(CURDATE() -INTERVAL 1 DAY )
  321. GROUP BY h.heatdate, h.frameid, c.intCurBar
  322. ORDER BY h.heatdate, h.frameid, c.intCurBarName
  323. ON DUPLICATE KEY UPDATE nb = VALUES(nb), highchange = VALUES(highchange), changefilter = VALUES(changefilter);
  324. UPDATE h_activehabit h
  325. JOIN cow c ON h.intCowId=c.intCowId
  326. JOIN data_bar_change cg ON h.heatdate=cg.heatdate AND h.frameid=cg.frameid AND c.intCurBar=cg.intCurBar
  327. SET h.changeadjust=cg.changefilter
  328. WHERE h.id>xBeg_update_act_Id AND h.heatdate>=CURRENT_DATE() - INTERVAL 1 DAY AND ABS(cg.changefilter)>=10;
  329. */
  330. res := make([]*model.NeckRingBarChange, 0)
  331. oneDayAgo := time.Now().AddDate(0, 0, -1).Format(model.LayoutDate2)
  332. if err := e.DB.Table(fmt.Sprintf("%s as h", new(model.NeckActiveHabit).TableName())).
  333. Select("h.neck_ring_number,h.heat_date, h.frameid, c.pen_id, c.pen_name, COUNT(*) as nb, ROUND(AVG(h.change_high)) as change_high, ROUND(AVG(h.change_filter)) as change_filter").
  334. Joins("JOIN cow as c ON h.neck_ring_number = c.neck_ring_number").
  335. Where("h.pasture_id = ?", pastureId).
  336. Where("h.heat_date >= ?", oneDayAgo).
  337. Where("h.cow_id >= ?", 0).
  338. Group("h.heat_date, h.frameid, c.pen_id").
  339. Order("h.heat_date, h.frameid, c.pen_name").
  340. Find(&res).Error; err != nil {
  341. zaplog.Error("UpdateChangeAdJust", zap.Any("error", err), zap.Any("xToday", xToday))
  342. }
  343. // todo ABS(cg.changefilter)>=10;
  344. for _, v := range res {
  345. if err := e.DB.Model(new(model.NeckActiveHabit)).
  346. Where("id > ?", xToday.LastMaxHabitId).
  347. Where("heat_date = ?", v.HeatDate).
  348. Where("frameid = ?", v.FrameId).
  349. Where("neck_ring_number = ?", v.NeckRingNumber).
  350. Update("change_adjust", v.ChangeHigh).Error; err != nil {
  351. zaplog.Error("UpdateChangeAdJust-1", zap.Any("error", err), zap.Any("xToday", xToday))
  352. }
  353. }
  354. }
  355. func (e *Entry) UpdateIsShow(pastureId int64, processIds []int64) {
  356. if err := e.DB.Model(new(model.NeckActiveHabit)).
  357. Where("id IN (?)", processIds).
  358. Where("pasture_id = ?", pastureId).
  359. Update("is_show", pasturePb.IsShow_Ok).Error; err != nil {
  360. zaplog.Error("UpdateChangeAdJust-2", zap.Any("error", err))
  361. }
  362. }
  363. func (e *Entry) XToday(pastureId int64) (*XToday, error) {
  364. xToday := &XToday{}
  365. systemConfigureList, err := e.GetSystemNeckRingConfigure(pastureId)
  366. if err != nil {
  367. return nil, xerr.WithStack(err)
  368. }
  369. for _, v := range systemConfigureList {
  370. switch v.Name {
  371. case model.MaxHabit:
  372. xToday.LastMaxHabitId = v.Value
  373. case model.High:
  374. xToday.High = int32(v.Value)
  375. case model.Rumina:
  376. xToday.Rumina = int32(v.Value)
  377. case model.XRuminaDisc:
  378. xToday.XRuminaDisc = int32(v.Value)
  379. case model.XChangeDiscount:
  380. xToday.XChangeDiscount = int32(v.Value)
  381. case model.WeeklyActive:
  382. xToday.WeeklyActive = int32(v.Value)
  383. }
  384. }
  385. return xToday, nil
  386. }
  387. // WeeklyUpdateActiveHabit 时间点周平均值计算
  388. func (e *Entry) WeeklyUpdateActiveHabit(pastureId int64, processIds []int64, xToDay *XToday) {
  389. newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  390. if err := e.DB.Model(new(model.NeckActiveHabit)).
  391. Where("id IN (?)", processIds).
  392. Order("heat_date,neck_ring_number,frameid").
  393. Find(&newNeckActiveHabitList).Error; err != nil {
  394. zaplog.Error("WeeklyUpdateActiveHabit", zap.Any("error", err), zap.Any("processIds", processIds))
  395. }
  396. if len(newNeckActiveHabitList) <= 0 {
  397. return
  398. }
  399. e.HabitUpdateActiveHabit(pastureId, newNeckActiveHabitList, xToDay)
  400. e.SumUpdateActiveHabit(pastureId, newNeckActiveHabitList, xToDay)
  401. e.ActiveChange(pastureId, processIds, xToDay)
  402. e.Before3DaysNeckActiveHabit(pastureId, processIds)
  403. }
  404. func (e *Entry) HabitUpdateActiveHabit(pastureId int64, newNeckActiveHabitList []*model.NeckActiveHabit, xToDay *XToday) {
  405. for _, v := range newNeckActiveHabitList {
  406. // 前七天的
  407. weekHabitData := e.FindWeekHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
  408. // 更新过滤值
  409. if err := e.DB.Model(new(model.NeckActiveHabit)).
  410. Select("high_habit", "rumina_habit", "chew_habit", "intake_habit", "inactive_habit").
  411. Where("id = ?", v.Id).
  412. Updates(map[string]interface{}{
  413. "high_habit": weekHabitData.HighHabit,
  414. "rumina_habit": weekHabitData.RuminaHabit,
  415. "chew_habit": weekHabitData.ChewHabit,
  416. "intake_habit": weekHabitData.IntakeHabit,
  417. "inactive_habit": weekHabitData.InactiveHabit,
  418. }).Error; err != nil {
  419. zaplog.Error("WeeklyUpdateActiveHabit",
  420. zap.Error(err),
  421. zap.Any("NeckActiveHabit", v),
  422. zap.Any("pastureId", pastureId),
  423. )
  424. }
  425. }
  426. }
  427. // SumUpdateActiveHabit -- 累计24小时数值
  428. func (e *Entry) SumUpdateActiveHabit(pastureId int64, newNeckActiveHabitList []*model.NeckActiveHabit, xToDay *XToday) {
  429. for _, v := range newNeckActiveHabitList {
  430. sumHabitData := e.FindSumHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
  431. // 更新过滤值
  432. if err := e.DB.Model(new(model.NeckActiveHabit)).
  433. Select("sum_rumina", "sum_intake", "sum_inactive", "sum_active", "sum_max_high", "sum_min_high", "sum_min_chew").
  434. Where("id = ?", v.Id).
  435. Updates(map[string]interface{}{
  436. "sum_rumina": sumHabitData.SumRumina,
  437. "sum_intake": sumHabitData.SumIntake,
  438. "sum_inactive": sumHabitData.SumInactive,
  439. "sum_active": sumHabitData.SumActive,
  440. "sum_max_high": sumHabitData.SumMaxHigh,
  441. "sum_min_high": sumHabitData.SumMinHigh,
  442. "sum_min_chew": sumHabitData.SumMinChew,
  443. }).Error; err != nil {
  444. zaplog.Error("WeeklyUpdateActiveHabit",
  445. zap.Any("err", err),
  446. zap.Any("NeckActiveHabit", v),
  447. zap.Any("pastureId", pastureId),
  448. )
  449. }
  450. }
  451. }
  452. // ActiveChange -- 变化百分比
  453. func (e *Entry) ActiveChange(pastureId int64, processIds []int64, xToDay *XToday) {
  454. newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  455. if err := e.DB.Model(new(model.NeckActiveHabit)).
  456. Where("id IN (?)", processIds).
  457. Where("high_habit > ?", 0).
  458. Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)).
  459. Find(&newNeckActiveHabitList).Error; err != nil {
  460. zaplog.Error("ActiveChange", zap.Any("error", err), zap.Any("processIds", processIds))
  461. }
  462. for _, v := range newNeckActiveHabitList {
  463. highDiff := v.FilterHigh - v.HighHabit
  464. denominator := float64(v.WeekHigh)*0.6 + float64(v.HighHabit)*0.2 + float64(xToDay.WeeklyActive)*0.2
  465. if highDiff > 0 {
  466. v.ChangeHigh = int32(math.Round((float64(highDiff) / denominator / float64(v.HighHabit)) * 100))
  467. } else {
  468. v.ChangeHigh = int32(math.Round(float64(highDiff) / float64(v.HighHabit) * 100))
  469. }
  470. if v.RuminaHabit != 0 {
  471. v.ChangeRumina = int32(math.Round(float64(v.FilterRumina-v.RuminaHabit) / float64(v.RuminaHabit) * 100))
  472. }
  473. if v.ChewHabit != 0 {
  474. v.ChangeChew = int32(math.Round(float64(v.FilterChew-v.ChewHabit) / float64(v.ChewHabit) * 100))
  475. }
  476. // 更新过滤值
  477. if err := e.DB.Model(new(model.NeckActiveHabit)).
  478. Select("change_high", "change_rumina", "change_chew").
  479. Where("id = ?", v.Id).
  480. Updates(map[string]interface{}{
  481. "change_high": v.ChangeHigh,
  482. "change_rumina": v.ChangeRumina,
  483. "change_chew": v.ChangeChew,
  484. }).Error; err != nil {
  485. zaplog.Error("WeeklyUpdateActiveHabit",
  486. zap.Error(err),
  487. zap.Any("NeckActiveHabit", v),
  488. zap.Any("pastureId", pastureId),
  489. )
  490. }
  491. }
  492. }
  493. func (e *Entry) Before3DaysNeckActiveHabit(pastureId int64, processIds []int64) {
  494. newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  495. if err := e.DB.Model(new(model.NeckActiveHabit)).
  496. Where("id IN (?)", processIds).
  497. Order("heat_date,neck_ring_number,frameid").
  498. Find(&newNeckActiveHabitList).Error; err != nil {
  499. zaplog.Error("Before3DaysNeckActiveHabit", zap.Any("error", err), zap.Any("processIds", processIds))
  500. }
  501. for _, v := range newNeckActiveHabitList {
  502. before3DaysNeckActiveHabit := e.FindBefore3DaysNeckActiveHabit(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid)
  503. if before3DaysNeckActiveHabit.SumRumina == 0 && before3DaysNeckActiveHabit.SumIntake == 0 {
  504. continue
  505. }
  506. // 更新过滤值
  507. if err := e.DB.Model(new(model.NeckActiveHabit)).
  508. Select("before_three_sum_rumina", "before_three_sum_intake").
  509. Where("id = ?", v.Id).
  510. Updates(map[string]interface{}{
  511. "before_three_sum_rumina": before3DaysNeckActiveHabit.SumRumina,
  512. "before_three_sum_intake": before3DaysNeckActiveHabit.SumIntake,
  513. }).Error; err != nil {
  514. zaplog.Error("Before3DaysNeckActiveHabit",
  515. zap.Error(err),
  516. zap.Any("NeckActiveHabit", v),
  517. zap.Any("pastureId", pastureId),
  518. )
  519. }
  520. }
  521. }