hr.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Package postgre
  2. /**
  3. * @ File:
  4. * @ Date: 2021/5/6 11:30
  5. * @ Author: JYQ
  6. * @ Description:
  7. */
  8. package postgre
  9. import (
  10. "sportfitness/base/assembly/heartRate/model"
  11. "sync"
  12. "time"
  13. "github.com/sirupsen/logrus"
  14. )
  15. type PGHr struct {
  16. }
  17. func (PGHr) PublicSensorQuery(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  18. err = getClient().Raw("select fn_hr_sensors_public_sensors_query(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  19. Scan(&result).Error
  20. return
  21. }
  22. func (PGHr) PrivateSensorQuery(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  23. err = getClient().Raw("select fn_hr_sensors_private_sensors_query(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  24. Scan(&result).Error
  25. return
  26. }
  27. func (PGHr) PublicSensorsAdd(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  28. err = getClient().Raw("select fn_hr_sensors_public_sensors_add(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  29. Scan(&result).Error
  30. return
  31. }
  32. func (PGHr) PrivateSensorsAdd(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  33. err = getClient().Raw("select fn_hr_sensors_private_sensors_add(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  34. Scan(&result).Error
  35. return
  36. }
  37. func (PGHr) SensorsEdit(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  38. err = getClient().Raw("select fn_hr_sensors_sensors_edit(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  39. Scan(&result).Error
  40. return
  41. }
  42. func (PGHr) SensorsDel(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  43. err = getClient().Raw("select fn_hr_sensors_sensors_del(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  44. Scan(&result).Error
  45. return
  46. }
  47. func (PGHr) PkGroupChg(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  48. err = getClient().Raw("select fn_pk_group_chg(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  49. Scan(&result).Error
  50. return
  51. }
  52. func (PGHr) CrBind(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  53. err = getClient().Raw("select fn_hr_sensors_cr_bind(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  54. Scan(&result).Error
  55. return
  56. }
  57. func (PGHr) CrUnBind(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  58. err = getClient().Raw("select fn_hr_sensors_cr_unbind(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  59. Scan(&result).Error
  60. return
  61. }
  62. func (PGHr) UnBindSensorsQuery(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  63. err = getClient().Raw("select fn_hr_sensors_unbind_sensors_query(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  64. Scan(&result).Error
  65. return
  66. }
  67. func (PGHr) HrClassDetailStatusEdit(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  68. err = getClient().Raw("select fn_hr_class_detail_status_edit(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  69. Scan(&result).Error
  70. return
  71. }
  72. func (PGHr) HrClassStartPrepare(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  73. err = getClient().Raw("select fn_hr_class_start_prepare(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  74. Scan(&result).Error
  75. return
  76. }
  77. func (PGHr) ClassInfoEditBeforeStart(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  78. err = getClient().Raw("select fn_hr_class_edit_before_start(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  79. Scan(&result).Error
  80. return
  81. }
  82. func (PGHr) HrClassDetailAdd(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  83. err = getClient().Raw("select fn_hr_class_detail_add(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  84. Scan(&result).Error
  85. return
  86. }
  87. func (PGHr) TmpClassDetailAdd(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  88. err = getClient().Raw("select fn_hr_tmp_class_detail_add(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  89. Scan(&result).Error
  90. return
  91. }
  92. func (PGHr) HrClassDetailDel(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  93. err = getClient().Raw("select fn_hr_class_detail_del(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  94. Scan(&result).Error
  95. return
  96. }
  97. func (PGHr) HrClassStartConfirm(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  98. err = getClient().Raw("select fn_hr_class_start_confirm(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  99. Scan(&result).Error
  100. return
  101. }
  102. func (PGHr) HrClassGiveUpConfirm(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  103. err = getClient().Raw("select fn_hr_class_give_up_confirm(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  104. Scan(&result).Error
  105. return
  106. }
  107. const hrSaveCount = 1000
  108. const hrSaveTime = time.Second * 10
  109. // key: sn
  110. var hrSaveMap = map[string][]model.BandData{}
  111. var hrSaveMapMu = sync.Mutex{}
  112. func (PGHr) getSNByBandId(id int) (sn string) {
  113. //TODO 实现
  114. return
  115. }
  116. func (p PGHr) SaveHrBatch(data []model.BandData) {
  117. hrSaveMapMu.Lock()
  118. defer hrSaveMapMu.Unlock()
  119. for _, one := range data {
  120. sn := p.getSNByBandId(one.BandId)
  121. if list, ok := hrSaveMap[sn]; ok {
  122. hrSaveMap[sn] = append(list, one)
  123. } else {
  124. hrSaveMap[sn] = []model.BandData{one}
  125. }
  126. }
  127. p.trySaveHrSaveMap()
  128. }
  129. func (PGHr) saveOneBandToDB(sn string, data []model.BandData) {
  130. //TODO 保存
  131. }
  132. func (p PGHr) workSaveHrSaveMapLoop() {
  133. defer func() {
  134. if p := recover(); p != nil {
  135. logrus.Errorf("workSaveHrSaveMapLoop: %s", p)
  136. }
  137. }()
  138. hrSaveMapMu.Lock()
  139. defer hrSaveMapMu.Unlock()
  140. p.trySaveHrSaveMap()
  141. }
  142. func (p PGHr) workSaveHrSaveMap() {
  143. logrus.Info("心率保存循环开启")
  144. for {
  145. time.Sleep(time.Second * 30)
  146. p.workSaveHrSaveMapLoop()
  147. }
  148. }
  149. // trySaveHrSaveMap 维护R3
  150. func (p PGHr) trySaveHrSaveMap() {
  151. var deleteKeys []string
  152. for sn, v := range hrSaveMap {
  153. needSave := false
  154. if len(v) >= hrSaveCount {
  155. needSave = true
  156. }
  157. if len(v) > 0 {
  158. firstTime := time.Unix(0, v[0].TimeMiles*1000000)
  159. if time.Now().Sub(firstTime) > hrSaveTime {
  160. needSave = true
  161. }
  162. }
  163. if needSave {
  164. p.saveOneBandToDB(sn, v)
  165. deleteKeys = append(deleteKeys, sn)
  166. }
  167. }
  168. for _, sn := range deleteKeys {
  169. delete(hrSaveMap, sn)
  170. }
  171. }
  172. func (PGHr) GetStdInfoAndCrIdByUUStdID(optUserId int64, objectShopId int64, uuid string) (result string, err error) {
  173. err = getClient().Raw("select fn_hr_class_start_query_for_redis(? ,? ,? )",
  174. optUserId, objectShopId, uuid).
  175. Scan(&result).Error
  176. return
  177. }