hr.go 7.1 KB

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