neck_ring_merge.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. package crontab
  2. import (
  3. "fmt"
  4. "kpt-pasture/model"
  5. "kpt-pasture/util"
  6. "math"
  7. "sort"
  8. "strings"
  9. "time"
  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. MinChangeFilter = -99
  17. MinRuminaFilter = -99
  18. MinChewFilter = -99
  19. MinChangeHigh = -99
  20. DefaultNb = 30
  21. DefaultScore = 100
  22. )
  23. var (
  24. defaultLimit = int32(1000)
  25. calculateIsRunning bool
  26. )
  27. // NeckRingOriginalMerge 把脖环数据合并成2个小时的
  28. func (e *Entry) NeckRingOriginalMerge() (err error) {
  29. if ok := e.IsExistCrontabLog(NeckRingOriginal); !ok {
  30. newTime := time.Now()
  31. e.CreateCrontabLog(NeckRingOriginal)
  32. // 原始数据删除15天前的
  33. e.DB.Model(new(model.NeckRingOriginal)).
  34. Where("created_at < ?", newTime.AddDate(0, 0, -7).Unix()).
  35. Delete(new(model.NeckRingOriginal))
  36. // 活动数据删除6个月前的数据
  37. e.DB.Model(new(model.NeckActiveHabit)).
  38. Where("created_at < ?", newTime.AddDate(0, -2, 0).Unix()).
  39. Delete(new(model.NeckActiveHabit))
  40. }
  41. pastureList := e.FindPastureList()
  42. if pastureList == nil || len(pastureList) == 0 {
  43. return nil
  44. }
  45. for _, pasture := range pastureList {
  46. if err = e.OriginalMergeData(pasture.Id); err != nil {
  47. zaplog.Error("NeckRingOriginalMerge", zap.Any("OriginalMergeData", err), zap.Any("pasture", pasture))
  48. }
  49. }
  50. return nil
  51. }
  52. func (e *Entry) OriginalMergeData(pastureId int64) error {
  53. limit := e.Cfg.NeckRingLimit
  54. if limit <= 0 {
  55. limit = defaultLimit
  56. }
  57. neckRingList := make([]*model.NeckRingOriginal, 0)
  58. if err := e.DB.Model(new(model.NeckRingOriginal)).
  59. Where("is_show = ?", pasturePb.IsShow_No).
  60. Where("pasture_id = ?", pastureId).
  61. Limit(int(limit)).Find(&neckRingList).Error; err != nil {
  62. return xerr.WithStack(err)
  63. }
  64. if len(neckRingList) <= 0 {
  65. return nil
  66. }
  67. // 去重
  68. neckRingList = RemoveDuplicates(neckRingList)
  69. // 计算合并
  70. neckActiveHabitList := Recalculate(neckRingList)
  71. if len(neckActiveHabitList) <= 0 {
  72. return nil
  73. }
  74. for _, habit := range neckActiveHabitList {
  75. //更新脖环牛只相关信息 新数据直接插入
  76. historyNeckActiveHabit, ct := e.IsExistNeckActiveHabit(pastureId, habit.NeckRingNumber, habit.HeatDate, habit.Frameid)
  77. if ct <= 0 {
  78. if err := e.DB.Create(habit).Error; err != nil {
  79. zaplog.Info("NeckRingOriginalMergeData-1",
  80. zap.Any("err", err),
  81. zap.Any("neckActiveHabit", habit),
  82. )
  83. }
  84. } else {
  85. // 重新计算
  86. newNeckActiveHabit := e.againRecalculate(historyNeckActiveHabit)
  87. if newNeckActiveHabit == nil {
  88. continue
  89. }
  90. if err := e.DB.Model(new(model.NeckActiveHabit)).
  91. Select("rumina", "intake", "inactive", "gasp", "other", "high", "active", "is_show", "record_count").
  92. Where("id = ?", historyNeckActiveHabit.Id).
  93. Updates(newNeckActiveHabit).Error; err != nil {
  94. zaplog.Error("NeckRingOriginalMergeData-2",
  95. zap.Any("err", err),
  96. zap.Any("ct", ct),
  97. zap.Any("historyNeckActiveHabit", historyNeckActiveHabit),
  98. zap.Any("newNeckActiveHabit", newNeckActiveHabit),
  99. )
  100. }
  101. }
  102. if err := e.UpdateNeckRingOriginalIsShow(habit); err != nil {
  103. zaplog.Error("NeckRingOriginalMergeData-4",
  104. zap.Any("err", err),
  105. zap.Any("neckActiveHabit", habit),
  106. )
  107. }
  108. }
  109. return nil
  110. }
  111. func (e *Entry) NeckRingCalculate() error {
  112. pastureList := e.FindPastureList()
  113. if pastureList == nil || len(pastureList) == 0 {
  114. return nil
  115. }
  116. if calculateIsRunning {
  117. return nil
  118. }
  119. defer func() {
  120. calculateIsRunning = false
  121. }()
  122. calculateIsRunning = true
  123. for _, pasture := range pastureList {
  124. if err := e.EntryUpdateActiveHabit(pasture.Id); err != nil {
  125. zaplog.Error("NeckRingCalculate", zap.Any("err", err), zap.Any("pasture", pasture))
  126. }
  127. zaplog.Info(fmt.Sprintf("NeckRingCalculate Success %d", pasture.Id))
  128. }
  129. return nil
  130. }
  131. func (e *Entry) EntryUpdateActiveHabit(pastureId int64) (err error) {
  132. // 获取这段执行数据内最大日期和最小日期
  133. xToday := &XToday{}
  134. systemConfigureList, err := e.GetSystemNeckRingConfigure(pastureId)
  135. if err != nil {
  136. return xerr.WithStack(err)
  137. }
  138. for _, v := range systemConfigureList {
  139. switch v.Name {
  140. case model.MaxHabit:
  141. xToday.LastMaxHabitId = v.Value
  142. case model.High:
  143. xToday.High = int32(v.Value)
  144. case model.Rumina:
  145. xToday.Rumina = int32(v.Value)
  146. case model.XRuminaDisc:
  147. xToday.XRuminaDisc = int32(v.Value)
  148. case model.XChangeDiscount:
  149. xToday.XChangeDiscount = int32(v.Value)
  150. case model.WeeklyActive:
  151. xToday.WeeklyActive = int32(v.Value)
  152. }
  153. }
  154. currMaxHabit := &model.NeckActiveHabit{}
  155. if err = e.DB.Model(new(model.NeckActiveHabit)).
  156. Where("id > ?", xToday.LastMaxHabitId).
  157. Where("pasture_id = ?", pastureId).
  158. Where("is_show = ?", pasturePb.IsShow_No).
  159. Order("id desc").
  160. First(currMaxHabit).Error; err != nil {
  161. return xerr.WithStack(err)
  162. }
  163. if currMaxHabit.Id <= 0 || currMaxHabit.Id <= xToday.LastMaxHabitId {
  164. return nil
  165. }
  166. xToday.CurrMaxHabitId = currMaxHabit.Id
  167. var processIds []int64
  168. // 更新活动滤波
  169. processIds, err = e.FirstFilterUpdate(pastureId, xToday)
  170. if err != nil {
  171. zaplog.Error("NeckRingCalculate", zap.Any("FirstFilterUpdate", err), zap.Any("xToday", xToday))
  172. }
  173. zaplog.Info("NeckRingCalculate", zap.Any("xToday", xToday), zap.Any("processIds", processIds))
  174. if len(processIds) <= 0 {
  175. return nil
  176. }
  177. if err = e.WeeklyUpdateActiveHabit(pastureId, processIds, xToday); err != nil {
  178. zaplog.Error("NeckRingCalculate", zap.Any("WeeklyUpdateActiveHabit", err), zap.Any("xToday", xToday))
  179. }
  180. if err = e.Before3DaysNeckActiveHabit(pastureId, processIds, xToday); err != nil {
  181. zaplog.Error("NeckRingCalculate", zap.Any("Before3DaysNeckActiveHabit", err), zap.Any("xToday", xToday))
  182. }
  183. // 二次更新滤波
  184. if err = e.SecondUpdateChangeFilter(pastureId, xToday); err != nil {
  185. zaplog.Error("NeckRingCalculate", zap.Any("SecondUpdateChangeFilter", err), zap.Any("xToday", xToday))
  186. }
  187. // 活动量校正系数和健康评分
  188. if err = e.FilterCorrectAndScoreUpdate(pastureId, xToday); err != nil {
  189. zaplog.Error("NeckRingCalculate", zap.Any("ActivityVolumeChanges", err), zap.Any("xToday", xToday))
  190. }
  191. if err = e.DB.Model(new(model.NeckActiveHabit)).
  192. Where("id IN (?)", processIds).
  193. Where("pasture_id = ?", pastureId).
  194. Where("is_show = ?", pasturePb.IsShow_No).
  195. Where("change_filter = ?", model.InitChangeFilter).
  196. Updates(map[string]interface{}{
  197. "change_filter": model.DefaultChangeFilter,
  198. "rumina_filter": model.DefaultRuminaFilter,
  199. "chew_filter": model.DefaultChewFilter,
  200. }).Error; err != nil {
  201. zaplog.Error("EntryUpdateActiveHabit", zap.Any("change_filter", err), zap.Any("xToday", xToday))
  202. }
  203. if err = e.DB.Model(new(model.NeckActiveHabit)).
  204. Where("id IN (?)", processIds).
  205. Where("pasture_id = ?", pastureId).
  206. Where("change_filter < ?", 0).
  207. Where("filter_correct < ?", model.DefaultFilterCorrect).
  208. Updates(map[string]interface{}{
  209. "filter_correct": model.DefaultFilterCorrect,
  210. }).Error; err != nil {
  211. zaplog.Error("EntryUpdateActiveHabit", zap.Any("filter_correct", err), zap.Any("xToday", xToday))
  212. }
  213. // 插入群体校正表
  214. if err = e.UpdateChangeAdJust(pastureId, xToday); err != nil {
  215. zaplog.Error("EntryUpdateActiveHabit", zap.Any("UpdateChangeAdJust", err), zap.Any("xToday", xToday))
  216. }
  217. // 更新所有的显示状态为否的记录为是
  218. if err = e.DB.Model(new(model.NeckActiveHabit)).
  219. Where("id IN (?)", processIds).
  220. Where("pasture_id = ?", pastureId).
  221. Update("is_show", pasturePb.IsShow_Ok).Error; err != nil {
  222. zaplog.Error("UpdateChangeAdJust-2", zap.Any("error", err), zap.Any("xToday", xToday))
  223. }
  224. // 更新日志
  225. if err = e.DB.Model(new(model.NeckRingConfigure)).
  226. Where("name = ?", model.MaxHabit).
  227. Where("pasture_id = ?", pastureId).
  228. Update("value", processIds[len(processIds)-1]).Error; err != nil {
  229. zaplog.Error("NeckRingCalculate", zap.Any("MaxHabit", err), zap.Any("xToday", xToday))
  230. }
  231. // 健康预警
  232. if err = e.HealthWarning(pastureId, processIds); err != nil {
  233. zaplog.Error("EntryUpdateActiveHabit", zap.Any("HealthWarning", err))
  234. }
  235. return nil
  236. }
  237. // FirstFilterUpdate 首次更新活动滤波
  238. func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds []int64, err error) {
  239. newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  240. if err = e.DB.Model(new(model.NeckActiveHabit)).
  241. Where("id > ?", xToDay.LastMaxHabitId).
  242. Where("pasture_id = ?", pastureId).
  243. Where("is_show = ?", pasturePb.IsShow_No).
  244. //Where("record_count = ?", model.DefaultRecordCount).
  245. Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)).
  246. Order("heat_date,neck_ring_number,frameid").
  247. Limit(int(defaultLimit)).
  248. Find(&newNeckActiveHabitList).Error; err != nil {
  249. return nil, xerr.WithStack(err)
  250. }
  251. // 活动量滤波
  252. for _, v := range newNeckActiveHabitList {
  253. // 过滤牛只未绑定的脖环的数据
  254. cowInfo := e.GetCowInfoByNeckRingNumber(v.PastureId, v.NeckRingNumber)
  255. if cowInfo == nil || cowInfo.Id <= 0 {
  256. continue
  257. }
  258. frameId := v.Frameid
  259. heatDate := v.HeatDate
  260. if v.Frameid == 0 {
  261. frameId = 11
  262. heatDateParse, _ := time.Parse(model.LayoutDate2, heatDate)
  263. heatDate = heatDateParse.AddDate(0, 0, -1).Format(model.LayoutDate2)
  264. } else {
  265. frameId -= 1
  266. }
  267. firstFilterData := e.FindFirstFilter(pastureId, v.NeckRingNumber, heatDate, frameId)
  268. if v.FilterHigh > 0 {
  269. firstFilterData.FilterHigh = v.FilterHigh
  270. } else {
  271. if v.NeckRingNumber == firstFilterData.NeckRingNumber {
  272. firstFilterData.FilterHigh = int32(computeIfPositiveElse(float64(v.High), float64(firstFilterData.FilterHigh), 0.23, 0.77))
  273. } else {
  274. firstFilterData.FilterHigh = v.High
  275. }
  276. }
  277. if v.FilterRumina > 0 {
  278. firstFilterData.FilterRumina = v.FilterRumina
  279. } else {
  280. if v.NeckRingNumber == firstFilterData.NeckRingNumber {
  281. firstFilterData.FilterRumina = int32(computeIfPositiveElse(float64(v.Rumina), float64(firstFilterData.FilterRumina), 0.33, 0.67))
  282. } else {
  283. firstFilterData.FilterRumina = v.Rumina
  284. }
  285. }
  286. if v.FilterChew > 0 {
  287. firstFilterData.FilterChew = v.FilterChew
  288. } else {
  289. if v.NeckRingNumber == firstFilterData.NeckRingNumber {
  290. firstFilterData.FilterChew = int32(computeIfPositiveElse(float64(v.Rumina+v.Intake), float64(firstFilterData.FilterChew), 0.33, 0.67))
  291. } else {
  292. firstFilterData.FilterChew = v.Rumina + v.Intake
  293. }
  294. }
  295. processIds = append(processIds, v.Id)
  296. // 更新过滤值 // todo 记得更新胎次为牛只胎次,现在为了测试特意改成0
  297. if err = e.DB.Model(new(model.NeckActiveHabit)).
  298. Select("filter_high", "filter_rumina", "filter_chew", "cow_id", "lact", "calving_age", "ear_number").
  299. Where("id = ?", v.Id).
  300. Updates(map[string]interface{}{
  301. "filter_high": firstFilterData.FilterHigh,
  302. "filter_rumina": firstFilterData.FilterRumina,
  303. "filter_chew": firstFilterData.FilterChew,
  304. "cow_id": cowInfo.Id,
  305. "lact": 0,
  306. "calving_age": cowInfo.CalvingAge,
  307. "ear_number": cowInfo.EarNumber,
  308. }).Error; err != nil {
  309. zaplog.Error("FirstFilterUpdate",
  310. zap.Any("error", err),
  311. zap.Any("firstFilterData", firstFilterData),
  312. zap.Any("NeckActiveHabit", v),
  313. zap.Any("cowInfo", cowInfo),
  314. zap.Any("xToday", xToDay),
  315. )
  316. }
  317. }
  318. return processIds, nil
  319. }
  320. // WeeklyUpdateActiveHabit 时间点周平均值计算
  321. func (e *Entry) WeeklyUpdateActiveHabit(pastureId int64, processIds []int64, xToDay *XToday) (err error) {
  322. newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  323. if err = e.DB.Model(new(model.NeckActiveHabit)).
  324. Where("id IN (?)", processIds).
  325. Order("heat_date,neck_ring_number,frameid").
  326. Find(&newNeckActiveHabitList).Error; err != nil {
  327. return xerr.WithStack(err)
  328. }
  329. for _, v := range newNeckActiveHabitList {
  330. // 前七天的
  331. weekHabitData := e.FindWeekHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
  332. // 更新过滤值
  333. if err = e.DB.Model(new(model.NeckActiveHabit)).
  334. Select("week_high_habit", "week_rumina_habit", "week_chew_habit", "week_intake_habit", "week_inactive_habit").
  335. Where("id = ?", v.Id).
  336. Updates(map[string]interface{}{
  337. "week_high_habit": weekHabitData.WeekHighHabit,
  338. "week_rumina_habit": weekHabitData.WeekRuminaHabit,
  339. "week_chew_habit": weekHabitData.WeekChewHabit,
  340. "week_intake_habit": weekHabitData.WeekIntakeHabit,
  341. "week_inactive_habit": weekHabitData.WeekInactiveHabit,
  342. }).Error; err != nil {
  343. zaplog.Error("WeeklyUpdateActiveHabit",
  344. zap.Error(err),
  345. zap.Any("NeckActiveHabit", v),
  346. zap.Any("pastureId", pastureId),
  347. )
  348. }
  349. }
  350. if err = e.SumUpdateActiveHabit(pastureId, newNeckActiveHabitList, xToDay); err != nil {
  351. zaplog.Error("WeeklyUpdateActiveHabit",
  352. zap.Any("SumUpdateActiveHabit", err),
  353. zap.Any("newNeckActiveHabitList", newNeckActiveHabitList),
  354. zap.Any("pastureId", pastureId),
  355. )
  356. }
  357. if err = e.ActiveChange(pastureId, processIds, xToDay); err != nil {
  358. zaplog.Error("WeeklyUpdateActiveHabit",
  359. zap.Any("ActiveChange", err),
  360. zap.Any("processIds", processIds),
  361. zap.Any("xToDay", xToDay),
  362. zap.Any("pastureId", pastureId),
  363. )
  364. }
  365. return nil
  366. }
  367. // SumUpdateActiveHabit -- 累计24小时数值
  368. func (e *Entry) SumUpdateActiveHabit(pastureId int64, newNeckActiveHabitList []*model.NeckActiveHabit, xToDay *XToday) (err error) {
  369. for _, v := range newNeckActiveHabitList {
  370. sumHabitData := e.FindSumHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
  371. // 更新过滤值
  372. if err = e.DB.Model(new(model.NeckActiveHabit)).
  373. Select("sum_rumina", "sum_intake", "sum_inactive", "sum_active", "sum_max_high", "sum_min_high", "sum_min_chew").
  374. Where("id = ?", v.Id).
  375. Updates(map[string]interface{}{
  376. "sum_rumina": sumHabitData.SumRumina,
  377. "sum_intake": sumHabitData.SumIntake,
  378. "sum_inactive": sumHabitData.SumInactive,
  379. "sum_active": sumHabitData.SumActive,
  380. "sum_max_high": sumHabitData.SumMaxHigh,
  381. "sum_min_high": sumHabitData.SumMinHigh,
  382. "sum_min_chew": sumHabitData.SumMinChew,
  383. }).Error; err != nil {
  384. zaplog.Error("WeeklyUpdateActiveHabit",
  385. zap.Any("err", err),
  386. zap.Any("NeckActiveHabit", v),
  387. zap.Any("pastureId", pastureId),
  388. )
  389. }
  390. }
  391. return err
  392. }
  393. // ActiveChange -- 变化百分比
  394. func (e *Entry) ActiveChange(pastureId int64, processIds []int64, xToDay *XToday) (err error) {
  395. newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  396. if err = e.DB.Model(new(model.NeckActiveHabit)).
  397. Where("id IN (?)", processIds).
  398. Where("week_high_habit > ?", 0).
  399. Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)).
  400. Find(&newNeckActiveHabitList).Error; err != nil {
  401. return xerr.WithStack(err)
  402. }
  403. for _, v := range newNeckActiveHabitList {
  404. highDiff := v.FilterHigh - v.WeekHighHabit
  405. denominator := float64(v.WeekHigh)*0.6 + float64(v.WeekHighHabit)*0.2 + float64(xToDay.WeeklyActive)*0.2
  406. if highDiff > 0 {
  407. v.ChangeHigh = int32(math.Round((float64(highDiff) / denominator / float64(v.WeekHighHabit)) * 100))
  408. } else {
  409. v.ChangeHigh = int32(math.Round(float64(highDiff) / float64(v.WeekHighHabit) * 100))
  410. }
  411. if v.WeekRuminaHabit != 0 {
  412. v.ChangeRumina = int32(math.Round(float64(v.FilterRumina-v.WeekRuminaHabit) / float64(v.WeekRuminaHabit) * 100))
  413. }
  414. if v.WeekChewHabit != 0 {
  415. v.ChangeChew = int32(math.Round(float64(v.FilterChew-v.WeekChewHabit) / float64(v.WeekChewHabit) * 100))
  416. }
  417. // 更新过滤值
  418. if err = e.DB.Model(new(model.NeckActiveHabit)).
  419. Select("change_high", "change_rumina", "change_chew").
  420. Where("id = ?", v.Id).
  421. Updates(map[string]interface{}{
  422. "change_high": v.ChangeHigh,
  423. "change_rumina": v.ChangeRumina,
  424. "change_chew": v.ChangeChew,
  425. }).Error; err != nil {
  426. zaplog.Error("WeeklyUpdateActiveHabit",
  427. zap.Error(err),
  428. zap.Any("NeckActiveHabit", v),
  429. zap.Any("pastureId", pastureId),
  430. )
  431. }
  432. }
  433. return err
  434. }
  435. func (e *Entry) WeeklyUpdateActiveHabitOld(pastureId int64, newNeckActiveHabitList []*model.NeckActiveHabit, xToDay *XToday) (err error) {
  436. for _, v := range newNeckActiveHabitList {
  437. // 前七天的
  438. weekHabitData := e.FindWeekHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
  439. highDiff := v.FilterHigh - weekHabitData.WeekHighHabit
  440. denominator := float64(v.WeekHigh)*0.6 + float64(weekHabitData.WeekHighHabit)*0.2 + float64(xToDay.WeeklyActive)*0.2
  441. if highDiff > 0 {
  442. v.ChangeHigh = int32(math.Round((float64(highDiff) / denominator) * 100))
  443. } else {
  444. v.ChangeHigh = int32(math.Round(float64(highDiff) / denominator * 100))
  445. }
  446. if weekHabitData.WeekRuminaHabit != 0 {
  447. v.ChangeRumina = int32(math.Round(float64(v.FilterRumina-weekHabitData.WeekRuminaHabit) / float64(weekHabitData.WeekRuminaHabit) * 100))
  448. } else {
  449. v.ChangeRumina = 0
  450. }
  451. if weekHabitData.WeekChewHabit != 0 {
  452. v.ChangeChew = int32(math.Round(float64(v.FilterChew-weekHabitData.WeekChewHabit) / float64(weekHabitData.WeekChewHabit) * 100))
  453. } else {
  454. v.ChangeChew = 0
  455. }
  456. sumHabitData := e.FindSumHabitData(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid, xToDay)
  457. // 更新过滤值
  458. if err = e.DB.Model(new(model.NeckActiveHabit)).
  459. Select(
  460. "week_high_habit", "week_rumina_habit", "week_chew_habit", "week_intake_habit", "week_inactive_habit",
  461. "sum_rumina", "sum_intake", "sum_inactive", "sum_active", "sum_max_high", "sum_min_high", "sum_min_chew",
  462. "change_high", "change_rumina", "change_chew", "before_three_sum_rumina", "before_three_sum_intake",
  463. ).Where("id = ?", v.Id).
  464. Updates(map[string]interface{}{
  465. "week_high_habit": weekHabitData.WeekHighHabit,
  466. "week_rumina_habit": weekHabitData.WeekRuminaHabit,
  467. "week_chew_habit": weekHabitData.WeekChewHabit,
  468. "week_intake_habit": weekHabitData.WeekIntakeHabit,
  469. "week_inactive_habit": weekHabitData.WeekInactiveHabit,
  470. "sum_rumina": sumHabitData.SumRumina,
  471. "sum_intake": sumHabitData.SumIntake,
  472. "sum_inactive": sumHabitData.SumInactive,
  473. "sum_active": sumHabitData.SumActive,
  474. "sum_max_high": sumHabitData.SumMaxHigh,
  475. "sum_min_high": sumHabitData.SumMinHigh,
  476. "sum_min_chew": sumHabitData.SumMinChew,
  477. "change_high": v.ChangeHigh,
  478. "change_rumina": v.ChangeRumina,
  479. "change_chew": v.ChangeChew,
  480. }).Error; err != nil {
  481. zaplog.Error("WeeklyUpdateActiveHabit",
  482. zap.Error(err),
  483. zap.Any("NeckActiveHabit", v),
  484. zap.Any("pastureId", pastureId),
  485. )
  486. }
  487. }
  488. return err
  489. }
  490. func (e *Entry) Before3DaysNeckActiveHabit(pastureId int64, processIds []int64, xToDay *XToday) (err error) {
  491. newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  492. if err = e.DB.Model(new(model.NeckActiveHabit)).
  493. Where("id IN (?)", processIds).
  494. Order("heat_date,neck_ring_number,frameid").
  495. Find(&newNeckActiveHabitList).Error; err != nil {
  496. return xerr.WithStack(err)
  497. }
  498. for _, v := range newNeckActiveHabitList {
  499. before3DaysNeckActiveHabit := e.FindBefore3DaysNeckActiveHabit(pastureId, v.NeckRingNumber, v.HeatDate, v.Frameid)
  500. // 更新过滤值
  501. if err = e.DB.Model(new(model.NeckActiveHabit)).
  502. Select("before_three_sum_rumina", "before_three_sum_intake").
  503. Where("id = ?", v.Id).
  504. Updates(map[string]interface{}{
  505. "before_three_sum_rumina": before3DaysNeckActiveHabit.SumRumina,
  506. "before_three_sum_intake": before3DaysNeckActiveHabit.SumIntake,
  507. }).Error; err != nil {
  508. zaplog.Error("Before3DaysNeckActiveHabit",
  509. zap.Error(err),
  510. zap.Any("NeckActiveHabit", v),
  511. zap.Any("pastureId", pastureId),
  512. )
  513. }
  514. }
  515. return nil
  516. }
  517. // SecondUpdateChangeFilter 第二次更新变化趋势滤波
  518. func (e *Entry) SecondUpdateChangeFilter(pastureId int64, xToday *XToday) (err error) {
  519. newChangeFilterList := make([]*ChangeFilterData, 0)
  520. if err = e.DB.Model(new(model.NeckActiveHabit)).
  521. Select("id", "neck_ring_number", "change_high", "change_filter", "rumina_filter", "change_rumina",
  522. "chew_filter", "change_chew", "heat_date", "frameid", "IF(lact = 0, 0.8, 1) as xlc_dis_count").
  523. Where("id > ?", xToday.LastMaxHabitId).
  524. Where("heat_date >= ?", time.Now().AddDate(0, 0, -2).Format(model.LayoutDate2)).
  525. Where("pasture_id = ?", pastureId).
  526. Where("change_filter = ?", model.InitChangeFilter).
  527. Where("change_high > ?", MinChangeHigh).
  528. Order("neck_ring_number,heat_date,frameid").
  529. Limit(int(defaultLimit)).
  530. Find(&newChangeFilterList).Error; err != nil {
  531. return xerr.WithStack(err)
  532. }
  533. for _, v := range newChangeFilterList {
  534. secondFilterData := &SecondFilterData{}
  535. frameId := v.FrameId
  536. heatDate := v.HeatDate
  537. if v.FrameId == 0 {
  538. frameId = 11
  539. heatDateParse, _ := time.Parse(model.LayoutDate2, heatDate)
  540. heatDate = heatDateParse.AddDate(0, 0, -1).Format(model.LayoutDate2)
  541. }
  542. if err = e.DB.Model(new(model.NeckActiveHabit)).
  543. Select("neck_ring_number", "filter_high", "filter_rumina", "filter_chew").
  544. Where("neck_ring_number = ?", v.NeckRingNumber).
  545. Where("heat_date = ?", heatDate).
  546. Where("frameid = ?", frameId).
  547. First(&secondFilterData).Error; err != nil {
  548. zaplog.Error("EntryUpdateActiveHabit", zap.Any("FirstFilterUpdate", err))
  549. }
  550. if v.ChangeFilter > MinChangeFilter {
  551. secondFilterData.ChangeFilter = float64(v.ChangeFilter)
  552. } else {
  553. if v.NeckRingNumber == secondFilterData.NeckRingNumber {
  554. secondFilterData.ChangeFilter = secondFilterData.ChangeFilter*(1-(float64(xToday.XChangeDiscount)/10)*v.XlcDisCount) +
  555. math.Min(float64(v.ChangeHigh), secondFilterData.ChangeFilter+135)*(float64(xToday.XChangeDiscount)/10)*v.XlcDisCount
  556. } else {
  557. secondFilterData.ChangeFilter = 0
  558. }
  559. }
  560. if v.RuminaFilter > MinRuminaFilter {
  561. secondFilterData.RuminaFilter = float64(v.ChangeFilter)
  562. } else {
  563. if v.NeckRingNumber == secondFilterData.NeckRingNumber {
  564. discount := float64(xToday.XRuminaDisc) / 10 * v.XlcDisCount
  565. if math.Abs(float64(v.ChangeRumina)) > 60 {
  566. discount *= 0.5
  567. }
  568. secondFilterData.RuminaFilter = secondFilterData.RuminaFilter*(1-discount) + float64(v.ChangeRumina)*discount
  569. } else {
  570. secondFilterData.RuminaFilter = 0
  571. }
  572. }
  573. secondFilterData.RuminaFilter = math.Min(50, secondFilterData.RuminaFilter)
  574. if v.ChewFilter > MinChewFilter {
  575. secondFilterData.ChewFilter = float64(v.ChangeChew)
  576. } else {
  577. if v.NeckRingNumber == secondFilterData.NeckRingNumber {
  578. discount := float64(xToday.XRuminaDisc) / 10
  579. if math.Abs(float64(v.ChangeChew)) > 60 {
  580. discount *= 0.5
  581. }
  582. secondFilterData.ChewFilter = secondFilterData.ChewFilter*(1-discount) + float64(v.ChangeChew)*discount
  583. } else {
  584. secondFilterData.ChewFilter = 0
  585. }
  586. }
  587. secondFilterData.ChewFilter = math.Min(50, secondFilterData.ChewFilter)
  588. if err = e.DB.Model(new(model.NeckActiveHabit)).
  589. Select("change_filter", "rumina_filter", "chew_filter").
  590. Where("id = ?", v.Id).
  591. Updates(map[string]interface{}{
  592. "change_filter": secondFilterData.ChangeFilter,
  593. "rumina_filter": secondFilterData.RuminaFilter,
  594. "chew_filter": secondFilterData.ChewFilter,
  595. }).Error; err != nil {
  596. zaplog.Error("SecondUpdateChangeFilter-1", zap.Any("error", err), zap.Any("xToday", xToday))
  597. }
  598. }
  599. return nil
  600. }
  601. // FilterCorrectAndScoreUpdate 计算活动量变化趋势校正值(活跃度校正)和健康评分
  602. func (e *Entry) FilterCorrectAndScoreUpdate(pastureId int64, xToday *XToday) error {
  603. beginDayDate := time.Now()
  604. before7DayDate := beginDayDate.AddDate(0, 0, -7).Format(model.LayoutDate2)
  605. before1DayDate := beginDayDate.AddDate(0, 0, -1).Format(model.LayoutDate2)
  606. activityVolumeList := make([]*ActivityVolume, 0)
  607. activityVolumeMap := make(map[string]*ActivityVolume)
  608. if err := e.DB.Model(new(model.NeckActiveHabit)).
  609. Select("neck_ring_number", "AVG(IF(change_filter>=60, 60, change_filter)) as avg_filter",
  610. "ROUND(STD(IF(change_filter>=60, 60, change_filter))) as std_filter", "COUNT(1) as nb").
  611. Where("heat_date BETWEEN ? AND ?", before7DayDate, before1DayDate).
  612. Where("pasture_id = ?", pastureId).
  613. Where(e.DB.Where("high > ?", xToday.High).Or("rumina >= ?", xToday.Rumina)).
  614. Where("active_time <= ?", beginDayDate.Add(-12*time.Hour).Format(model.LayoutTime)).
  615. Where("change_filter > ?", MinChangeFilter).
  616. Having("nb > ?", DefaultNb).
  617. Group("neck_ring_number").
  618. Find(&activityVolumeList).Error; err != nil {
  619. zaplog.Error("ActivityVolumeChanges-0", zap.Any("error", err), zap.Any("xToday", xToday))
  620. }
  621. if len(activityVolumeList) > 0 {
  622. for _, v := range activityVolumeList {
  623. activityVolumeMap[v.NeckRingNumber] = v
  624. }
  625. }
  626. neckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  627. if err := e.DB.Model(new(model.NeckActiveHabit)).
  628. Where("id <= ?", xToday.CurrMaxHabitId).
  629. Where("heat_date >= ?", before1DayDate).
  630. Where("pasture_id = ?", pastureId).
  631. Where(e.DB.Where("high > ?", xToday.High).Or("rumina > ?", xToday.Rumina)).
  632. Find(&neckActiveHabitList).Error; err != nil {
  633. zaplog.Error("ActivityVolumeChanges-1", zap.Any("error", err), zap.Any("xToday", xToday))
  634. return xerr.WithStack(err)
  635. }
  636. for _, v := range neckActiveHabitList {
  637. if filterCorrectMap, ok := activityVolumeMap[v.NeckRingNumber]; ok {
  638. filterCorrect := model.DefaultFilterCorrect - int(math.Floor(filterCorrectMap.AvgFilter/3+float64(filterCorrectMap.StdFilter)/2))
  639. // 活动量校正系数
  640. if err := e.DB.Model(new(model.NeckActiveHabit)).
  641. Where("id = ?", v.Id).
  642. Where("neck_ring_number = ?", v.NeckRingNumber).
  643. Update("filter_correct", filterCorrect).Error; err != nil {
  644. zaplog.Error("ActivityVolumeChanges-2", zap.Any("error", err), zap.Any("xToday", xToday))
  645. continue
  646. }
  647. }
  648. cowScore := calculateScore(v)
  649. if err := e.DB.Model(new(model.NeckActiveHabit)).
  650. Where("id = ?", v.Id).
  651. Update("score", cowScore).Error; err != nil {
  652. zaplog.Error("ActivityVolumeChanges-2", zap.Any("error", err), zap.Any("xToday", xToday))
  653. continue
  654. }
  655. }
  656. return nil
  657. }
  658. // UpdateChangeAdJust 更新群体校正数据
  659. func (e *Entry) UpdateChangeAdJust(pastureId int64, xToday *XToday) error {
  660. /*-- 插入群体校正表
  661. INSERT INTO data_bar_change(heatdate, frameid, intCurBar, intCurBarName, nb, highchange, changefilter)
  662. SELECT h.heatdate, h.frameid, c.intCurBar, c.intCurBarName, COUNT(*) nb, ROUND(AVG(h.highchange)) highchange, ROUND(AVG(h.changefilter) ) changefilter
  663. FROM h_activehabit h JOIN cow c ON h.intCowId=c.intCowId
  664. WHERE h.heatdate>=(CURDATE() -INTERVAL 1 DAY )
  665. GROUP BY h.heatdate, h.frameid, c.intCurBar
  666. ORDER BY h.heatdate, h.frameid, c.intCurBarName
  667. ON DUPLICATE KEY UPDATE nb = VALUES(nb), highchange = VALUES(highchange), changefilter = VALUES(changefilter);
  668. UPDATE h_activehabit h
  669. JOIN cow c ON h.intCowId=c.intCowId
  670. JOIN data_bar_change cg ON h.heatdate=cg.heatdate AND h.frameid=cg.frameid AND c.intCurBar=cg.intCurBar
  671. SET h.changeadjust=cg.changefilter
  672. WHERE h.id>xBeg_update_act_Id AND h.heatdate>=CURRENT_DATE() - INTERVAL 1 DAY AND ABS(cg.changefilter)>=10;
  673. */
  674. res := make([]*model.NeckRingBarChange, 0)
  675. oneDayAgo := time.Now().AddDate(0, 0, -1).Format(model.LayoutDate2)
  676. if err := e.DB.Table(fmt.Sprintf("%s as h", new(model.NeckActiveHabit).TableName())).
  677. 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").
  678. Joins("JOIN cow as c ON h.neck_ring_number = c.neck_ring_number").
  679. Where("h.pasture_id = ?", pastureId).
  680. Where("h.heat_date >= ?", oneDayAgo).
  681. Where("h.cow_id >= ?", 0).
  682. Where("h.cow_id >= ?", 0).
  683. Group("h.heat_date, h.frameid, c.pen_id").
  684. Order("h.heat_date, h.frameid, c.pen_name").
  685. Find(&res).Error; err != nil {
  686. return xerr.WithStack(err)
  687. }
  688. // todo ABS(cg.changefilter)>=10;
  689. for _, v := range res {
  690. if err := e.DB.Model(new(model.NeckActiveHabit)).
  691. Where("id > ?", xToday.LastMaxHabitId).
  692. Where("heat_date = ?", v.HeatDate).
  693. Where("frameid = ?", v.FrameId).
  694. Where("neck_ring_number = ?", v.NeckRingNumber).
  695. Update("change_adjust", v.ChangeHigh).Error; err != nil {
  696. zaplog.Error("UpdateChangeAdJust-1", zap.Any("error", err), zap.Any("xToday", xToday))
  697. }
  698. }
  699. return nil
  700. }
  701. func (e *Entry) UpdateNeckRingOriginalIsShow(habit *model.NeckActiveHabit) error {
  702. if err := e.DB.Model(new(model.NeckRingOriginal)).
  703. Where("pasture_id = ?", habit.PastureId).
  704. Where("neck_ring_number = ?", habit.NeckRingNumber).
  705. Where("active_date = ?", habit.HeatDate).
  706. Where("frameid IN (?)", util.FrameIds(habit.Frameid)).
  707. Update("is_show", pasturePb.IsShow_Ok).Error; err != nil {
  708. return xerr.WithStack(err)
  709. }
  710. return nil
  711. }
  712. // RemoveDuplicates 清洗一下数据,去掉重复的,如果有重复的,取最新的一条数据
  713. func RemoveDuplicates(records []*model.NeckRingOriginal) []*model.NeckRingOriginal {
  714. uniqueRecords := make(map[string]*model.NeckRingOriginal)
  715. // 遍历原始数组
  716. for _, record := range records {
  717. mapKey := fmt.Sprintf("%s%s%s%s%d", record.NeckRingNumber, model.JoinKey, record.ActiveDate, model.JoinKey, record.Frameid) // 0001/2023-12-04/0 0001/2023-12-03/4
  718. if existing, exists := uniqueRecords[mapKey]; exists {
  719. if record.CreatedAt > existing.CreatedAt {
  720. uniqueRecords[mapKey] = record
  721. }
  722. } else {
  723. uniqueRecords[mapKey] = record
  724. }
  725. }
  726. // 将 map 中的记录转换为切片
  727. result := make([]*model.NeckRingOriginal, 0, len(uniqueRecords))
  728. for _, record := range uniqueRecords {
  729. result = append(result, record)
  730. }
  731. return result
  732. }
  733. // Recalculate 合并计算
  734. func Recalculate(neckRingList []*model.NeckRingOriginal) []*model.NeckActiveHabit {
  735. originalMapData := make(map[string]*model.NeckRingOriginalMerge)
  736. // 合并成2个小时的
  737. for _, v := range neckRingList {
  738. xframeId := util.XFrameId(v.Frameid)
  739. mapKey := fmt.Sprintf("%s%s%s%s%d", v.NeckRingNumber, model.JoinKey, v.ActiveDate, model.JoinKey, xframeId) // 0001/2023-12-04/0 0001/2023-12-03/4
  740. if originalMapData[mapKey] == nil {
  741. originalMapData[mapKey] = new(model.NeckRingOriginalMerge)
  742. }
  743. originalMapData[mapKey].IsMageData(v, xframeId)
  744. }
  745. currTime := time.Now()
  746. res := make([]*model.NeckActiveHabit, 0)
  747. // 算平均值
  748. for k, v := range originalMapData {
  749. // 过滤掉合并后<6条数据,如果时间太短就晚点再算
  750. if v.RecordCount < model.DefaultRecordCount {
  751. currMaxXframeId := util.FrameIdMapReverse[int32(currTime.Hour())]
  752. activeDateString := fmt.Sprintf("%s %02d:00:00", v.ActiveDate, v.XframeId*2+1)
  753. activeDate, _ := time.Parse(model.LayoutTime, activeDateString)
  754. if currMaxXframeId-v.XframeId <= 1 && currTime.Add(-1*time.Hour).Unix() < activeDate.Unix() {
  755. delete(originalMapData, k)
  756. continue
  757. }
  758. }
  759. v.SumAvg()
  760. }
  761. if len(originalMapData) <= 0 {
  762. return res
  763. }
  764. res = model.NeckRingOriginalMap(originalMapData).ForMatData()
  765. sort.Sort(model.NeckActiveHabitSlice(res))
  766. return res
  767. }
  768. func (e *Entry) againRecalculate(data *model.NeckActiveHabit) *model.NeckActiveHabit {
  769. originalList := make([]*model.NeckRingOriginal, 0)
  770. frameIds := util.FrameIds(data.Frameid)
  771. sql := ""
  772. for _, frameId := range frameIds {
  773. sql += fmt.Sprintf(`SELECT * FROM neck_ring_original WHERE pasture_id = %d AND neck_ring_number = '%s' AND active_date = '%s' AND frameid = %d UNION ALL `, data.PastureId, data.NeckRingNumber, data.HeatDate, frameId)
  774. }
  775. if len(sql) > 0 {
  776. sql = strings.TrimSuffix(sql, "UNION ALL ")
  777. }
  778. if err := e.DB.Raw(sql).Find(&originalList).Error; err != nil {
  779. return nil
  780. }
  781. /*if err := e.DB.Model(new(model.NeckRingOriginal)).
  782. Where("pasture_id = ?", data.PastureId).
  783. Where("neck_ring_number = ?", data.NeckRingNumber).
  784. Where("active_date = ?", data.HeatDate).
  785. Where("frameid IN (?)", frameIds).
  786. Find(&originalList).Error; err != nil {
  787. return nil
  788. }*/
  789. originalList = RemoveDuplicates(originalList)
  790. newDataList := Recalculate(originalList)
  791. if len(newDataList) != 1 {
  792. return nil
  793. }
  794. res := newDataList[0]
  795. res.IsShow = pasturePb.IsShow_No
  796. return res
  797. }
  798. // computeIfPositiveElse 辅助函数来计算过滤值
  799. func computeIfPositiveElse(newValue, prevFilterValue float64, weightPrev, weightNew float64) float64 {
  800. return math.Ceil((prevFilterValue * weightPrev) + (weightNew * newValue))
  801. }
  802. // 计算 score 的逻辑
  803. func calculateScore(habit *model.NeckActiveHabit) int {
  804. // 第一部分逻辑
  805. var part1 float64
  806. switch {
  807. case (habit.CalvingAge <= 1 && habit.Lact >= 1) ||
  808. (habit.CalvingAge >= 2 && habit.CalvingAge <= 13 && (habit.SumRumina+habit.SumIntake) == 0) ||
  809. ((habit.Lact == 0 || habit.CalvingAge >= 14) && habit.ChangeFilter == -99):
  810. part1 = -199
  811. case habit.CalvingAge >= 2 && habit.CalvingAge <= 13:
  812. part1 = math.Min((float64(habit.SumRumina+habit.SumIntake)-(100+math.Min(7, float64(habit.CalvingAge))*60))/10*2, 0)
  813. case habit.ChangeFilter > -99:
  814. part1 = math.Min(0, math.Min(getValueOrDefault(float64(habit.ChangeFilter), 0), getValueOrDefault(float64(habit.SumMinHigh), 0)))*0.2 +
  815. math.Min(0, math.Min(getValueOrDefault(float64(habit.ChangeFilter), 0), getValueOrDefault(float64(habit.SumMinChew), 0)))*0.2 +
  816. getRuminaSumIntakeSumScore(float64(habit.SumRumina+habit.SumIntake)) + getAdditionalScore(habit)
  817. default:
  818. part1 = -299
  819. }
  820. // 第二部分逻辑
  821. var part2 float64
  822. switch {
  823. case habit.FirmwareVersion%100 >= 52:
  824. part2 = 1
  825. case habit.FirmwareVersion%100 >= 30 && habit.FirmwareVersion%100 <= 43:
  826. part2 = 0.8
  827. default:
  828. part2 = 0.6
  829. }
  830. // 最终 score
  831. return DefaultScore + int(math.Floor(part1*part2))
  832. }
  833. // 获取值或默认值
  834. func getValueOrDefault(value, defaultValue float64) float64 {
  835. if value > -99 {
  836. return value
  837. }
  838. return defaultValue
  839. }
  840. // 计算累计反刍得分
  841. func getRuminaSumIntakeSumScore(sum float64) float64 {
  842. switch {
  843. case sum < 80:
  844. return -30
  845. case sum < 180:
  846. return -20
  847. case sum < 280:
  848. return -10
  849. default:
  850. return 0
  851. }
  852. }
  853. // 计算额外得分
  854. func getAdditionalScore(habit *model.NeckActiveHabit) float64 {
  855. var score float64
  856. if (habit.SumRumina+habit.SumIntake < 280 || habit.SumMinHigh+habit.SumMinChew < -50) && habit.SumMaxHigh > 50 {
  857. score += 10
  858. }
  859. if habit.ChangeFilter < -30 && habit.ChangeFilter <= habit.SumMinHigh && habit.ChewFilter < -30 && habit.ChewFilter <= habit.SumMinChew {
  860. score -= 5
  861. }
  862. return score
  863. }