hr.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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) HrClassDetailDel(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  88. err = getClient().Raw("select fn_hr_class_detail_del(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  89. Scan(&result).Error
  90. return
  91. }
  92. func (PGHr) HrClassStartConfirm(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  93. err = getClient().Raw("select fn_hr_class_start_confirm(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  94. Scan(&result).Error
  95. return
  96. }
  97. func (PGHr) HrClassGiveUpConfirm(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
  98. err = getClient().Raw("select fn_hr_class_give_up_confirm(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
  99. Scan(&result).Error
  100. return
  101. }
  102. const hrSaveCount = 1000
  103. const hrSaveTime = time.Second * 10
  104. // key: sn
  105. var hrSaveMap = map[string][]model.BandData{}
  106. var hrSaveMapMu = sync.Mutex{}
  107. func (PGHr) getSNByBandId(id int) (sn string) {
  108. //TODO 实现
  109. return
  110. }
  111. func (p PGHr) SaveHrBatch(data []model.BandData) {
  112. hrSaveMapMu.Lock()
  113. defer hrSaveMapMu.Unlock()
  114. for _, one := range data {
  115. sn := p.getSNByBandId(one.BandId)
  116. if list, ok := hrSaveMap[sn]; ok {
  117. hrSaveMap[sn] = append(list, one)
  118. } else {
  119. hrSaveMap[sn] = []model.BandData{one}
  120. }
  121. }
  122. p.trySaveHrSaveMap()
  123. }
  124. func (PGHr) saveOneBandToDB(sn string, data []model.BandData) {
  125. //TODO 保存
  126. }
  127. func (p PGHr) workSaveHrSaveMapLoop() {
  128. defer func() {
  129. if p := recover(); p != nil {
  130. logrus.Errorf("workSaveHrSaveMapLoop: %s", p)
  131. }
  132. }()
  133. hrSaveMapMu.Lock()
  134. defer hrSaveMapMu.Unlock()
  135. p.trySaveHrSaveMap()
  136. }
  137. func (p PGHr) workSaveHrSaveMap() {
  138. logrus.Info("心率保存循环开启")
  139. for {
  140. time.Sleep(time.Second * 30)
  141. p.workSaveHrSaveMapLoop()
  142. }
  143. }
  144. // trySaveHrSaveMap 维护R3
  145. func (p PGHr) trySaveHrSaveMap() {
  146. var deleteKeys []string
  147. for sn, v := range hrSaveMap {
  148. needSave := false
  149. if len(v) >= hrSaveCount {
  150. needSave = true
  151. }
  152. if len(v) > 0 {
  153. firstTime := time.Unix(0, v[0].TimeMiles*1000000)
  154. if time.Now().Sub(firstTime) > hrSaveTime {
  155. needSave = true
  156. }
  157. }
  158. if needSave {
  159. p.saveOneBandToDB(sn, v)
  160. deleteKeys = append(deleteKeys, sn)
  161. }
  162. }
  163. for _, sn := range deleteKeys {
  164. delete(hrSaveMap, sn)
  165. }
  166. }
  167. func (PGHr) GetStdInfoAndCrIdByUUStdID(optUserId int64, objectShopId int64, uuid string) (result string, err error) {
  168. err = getClient().Raw("select fn_hr_class_start_query_for_redis(? ,? ,? )",
  169. optUserId, objectShopId, uuid).
  170. Scan(&result).Error
  171. return
  172. }