| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- // Package postgre
- /**
- * @ File:
- * @ Date: 2021/5/6 11:30
- * @ Author: JYQ
- * @ Description:
- */
- package postgre
- import (
- "sportfitness/base/assembly/heartRate/model"
- "sync"
- "time"
- "github.com/sirupsen/logrus"
- )
- type PGHr struct {
- }
- func (PGHr) PublicSensorQuery(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_sensors_public_sensors_query(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) PrivateSensorQuery(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_sensors_private_sensors_query(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) PublicSensorsAdd(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_sensors_public_sensors_add(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) PrivateSensorsAdd(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_sensors_private_sensors_add(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) SensorsEdit(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_sensors_sensors_edit(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) SensorsDel(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_sensors_sensors_del(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) PkGroupChg(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_pk_group_chg(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) CrBind(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_sensors_cr_bind(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) CrUnBind(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_sensors_cr_unbind(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) UnBindSensorsQuery(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_sensors_unbind_sensors_query(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) HrClassDetailStatusEdit(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_class_detail_status_edit(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) HrClassStartPrepare(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_class_start_prepare(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) ClassInfoEditBeforeStart(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_class_edit_before_start(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) HrClassDetailAdd(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_class_detail_add(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) TmpClassDetailAdd(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_tmp_class_detail_add(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) HrClassDetailDel(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_class_detail_del(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) HrClassStartConfirm(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_class_start_confirm(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- func (PGHr) HrClassGiveUpConfirm(optUserId int64, objectShopId int64, jsonStr string, verfStr string) (result string, err error) {
- err = getClient().Raw("select fn_hr_class_give_up_confirm(? ,? ,?, ? )", optUserId, objectShopId, jsonStr, verfStr).
- Scan(&result).Error
- return
- }
- const hrSaveCount = 1000
- const hrSaveTime = time.Second * 10
- // key: sn
- var hrSaveMap = map[string][]model.BandData{}
- var hrSaveMapMu = sync.Mutex{}
- func (PGHr) getSNByBandId(id int) (sn string) {
- //TODO 实现
- return
- }
- func (p PGHr) SaveHrBatch(data []model.BandData) {
- hrSaveMapMu.Lock()
- defer hrSaveMapMu.Unlock()
- for _, one := range data {
- sn := p.getSNByBandId(one.BandId)
- if list, ok := hrSaveMap[sn]; ok {
- hrSaveMap[sn] = append(list, one)
- } else {
- hrSaveMap[sn] = []model.BandData{one}
- }
- }
- p.trySaveHrSaveMap()
- }
- func (PGHr) saveOneBandToDB(sn string, data []model.BandData) {
- //TODO 保存
- }
- func (p PGHr) workSaveHrSaveMapLoop() {
- defer func() {
- if p := recover(); p != nil {
- logrus.Errorf("workSaveHrSaveMapLoop: %s", p)
- }
- }()
- hrSaveMapMu.Lock()
- defer hrSaveMapMu.Unlock()
- p.trySaveHrSaveMap()
- }
- func (p PGHr) workSaveHrSaveMap() {
- logrus.Info("心率保存循环开启")
- for {
- time.Sleep(time.Second * 30)
- p.workSaveHrSaveMapLoop()
- }
- }
- // trySaveHrSaveMap 维护R3
- func (p PGHr) trySaveHrSaveMap() {
- var deleteKeys []string
- for sn, v := range hrSaveMap {
- needSave := false
- if len(v) >= hrSaveCount {
- needSave = true
- }
- if len(v) > 0 {
- firstTime := time.Unix(0, v[0].TimeMiles*1000000)
- if time.Now().Sub(firstTime) > hrSaveTime {
- needSave = true
- }
- }
- if needSave {
- p.saveOneBandToDB(sn, v)
- deleteKeys = append(deleteKeys, sn)
- }
- }
- for _, sn := range deleteKeys {
- delete(hrSaveMap, sn)
- }
- }
- func (PGHr) GetStdInfoAndCrIdByUUStdID(optUserId int64, objectShopId int64, uuid string) (result string, err error) {
- err = getClient().Raw("select fn_hr_class_start_query_for_redis(? ,? ,? )",
- optUserId, objectShopId, uuid).
- Scan(&result).Error
- return
- }
|