Jelajahi Sumber

HrSensorsUpdate

lhs 4 tahun lalu
induk
melakukan
7c93599313
7 mengubah file dengan 212 tambahan dan 385 penghapusan
  1. 33 331
      controller/hrSensors.go
  2. 50 54
      controller/test.go
  3. 4 0
      errors/error.go
  4. 1 0
      main.go
  5. 6 0
      model/duInfo.go
  6. 91 0
      repository/http/heartrate_server.go
  7. 27 0
      service/hrsensors.go

+ 33 - 331
controller/hrSensors.go

@@ -6,338 +6,40 @@
  */
 package controller
 
+import (
+	"net/http"
+	"video_course/service"
+)
+
 type HrSensors struct {
 	BaseController
 }
+type UpdateHrSensorsInfo struct {
+	ResponseBase
+	HrId int
+}
 
-//
-//// AddHrSensors godoc
-//// @Summary 添加商家公共心率设备
-//// @tags HrSensors
-//// @Description 添加商家公共心率设备
-//// @Accept  x-www-form-urlencoded
-//// @Produce  json
-//// @Param token formData string true "Token"
-//// @Param shopId formData int true "商家ID"
-//// @Param sn formData int true "心率带Sn"
-//// @Param venueNo formData string false "场馆内编号  01 02"
-//// @Success 200 {object} controller.ResponseBase
-//// @Router /HrSensors/AddHrSensors [post]
-//func (h *HrSensors) AddHrSensors() (err error) {
-//	//sess := h.Ctx().PostForm("Token")
-//	//sn := h.postString("sn", true)
-//	//venueNo := h.Ctx().PostForm("venueNo")
-//	//shopId := h.postIntNecessary("shopId")
-//	//
-//	//logInfo, err := service.HrSensors{}.AddHrSensors(sess, sn, venueNo, shopId)
-//	//if err != nil {
-//	//	return
-//	//}
-//	//h.saveOptLogInfo(logInfo)
-//	//h.json(newResponseBase())
-//	return
-//}
-//
-//// AddPvtHrSensors godoc
-//// @Summary 添加会员私有心率设备
-//// @tags HrSensors
-//// @Description 添加会员私有心率设备
-//// @Accept  x-www-form-urlencoded
-//// @Produce  json
-//// @Param token formData string true "Token"
-//// @Param shopId formData int true "商家ID"
-//// @Param sn formData int true "心率带Sn"
-//// @Param userId formData int true "用户ID"
-//// @Success 200 {object} controller.ResponseBase
-//// @Router /HrSensors/AddPvtHrSensors [post]
-//func (h *HrSensors) AddPvtHrSensors() (err error) {
-//	//sess := h.Ctx().PostForm("Token")
-//	//sn := h.postString("sn", true)
-//	//shopId := h.postIntNecessary("shopId")
-//	//userId := h.postIntNecessary("userId")
-//	//
-//	//logInfo, err := service.HrSensors{}.AddPvtHrSensors(sess, sn, userId, shopId)
-//	//if err != nil {
-//	//	return
-//	//}
-//	//h.saveOptLogInfo(logInfo)
-//	//h.json(newResponseBase())
-//	return
-//}
-//
-//// EditHrSensors godoc
-//// @Summary 修改商家心率设备信息
-//// @tags HrSensors
-//// @Description 修改商家心率设备信息
-//// @Accept  x-www-form-urlencoded
-//// @Produce  json
-//// @Param token formData string true "Token"
-//// @Param hrId formData int true "心率带ID"
-//// @Param sn formData string true "心率带Sn"
-//// @Param shopId formData string true "商家ID"
-//// @Param venueNo formData string false "场馆内编号  01 02"
-//// @Success 200 {object} controller.ResponseBase
-//// @Router /HrSensors/EditHrSensors [post]
-//func (h *HrSensors) EditHrSensors() (err error) {
-//	//sess := h.getSession()
-//	//sn := h.postString("sn", true)
-//	//venueNo := h.Ctx().PostForm("venueNo")
-//	//hrId := h.postIntNecessary("hrId")
-//	//shopId := h.postIntNecessary("shopId")
-//	//
-//	//logInfo, err := service.HrSensors{}.EditHrSensors(sess, sn, venueNo, shopId, hrId)
-//	//if err != nil {
-//	//	return
-//	//}
-//	//h.saveOptLogInfo(logInfo)
-//	//h.json(newResponseBase())
-//	return
-//}
-//
-//// HrSensorsStatusEdit godoc
-//// @Summary 修改商家心率设备状态
-//// @tags HrSensors
-//// @Description 修改商家心率设备状态
-//// @Accept  x-www-form-urlencoded
-//// @Produce  json
-//// @Param token formData string true "Token"
-//// @Param hrId formData int true "心率带ID"
-//// @Param status formData int true "状态, 1:启用,8:暂停,9:删除"
-//// @Success 200 {object} controller.ResponseBase
-//// @Router /HrSensors/HrSensorsStatusEdit [post]
-//func (h *HrSensors) HrSensorsStatusEdit() (err error) {
-//	//sess := h.getSession()
-//	//hrId := h.postIntNecessary("hrId")
-//	//status := h.postIntNecessary("status")
-//	//
-//	//logInfo, err := service.HrSensors{}.HrSensorsStatusEdit(sess, hrId, status)
-//	//if err != nil {
-//	//	return
-//	//}
-//	//h.saveOptLogInfo(logInfo)
-//	//h.json(newResponseBase())
-//	return
-//}
-//
-//type QueryHrSensorsInfo struct {
-//	ResponseBase
-//	Rs string
-//}
-//
-//// QueryHrSensors godoc
-//// @Summary 查询商家公共心率设备
-//// @tags HrSensors
-//// @Description 查询商家公共心率设备
-//// @Accept  x-www-form-urlencoded
-//// @Produce  json
-//// @Param token formData string true "Token"
-//// @Param shopId formData int true "商家ID"
-//// @Param status formData int false "设备状态"
-//// @Param str formData string false "模糊查询sn与场内编号"
-//// @Success 200 {object} controller.ResponseBase
-//// @Router /HrSensors/QueryHrSensors [post]
-//func (h *HrSensors) QueryHrSensors() (err error) {
-//	//sess := h.getSession()
-//	//str := h.Ctx().PostForm("str")
-//	//shopId := h.postIntNecessary("shopId")
-//	//status := h.postInt("status")
-//	//hr, err := service.HrSensors{}.QueryHrSensors(sess, shopId, str, status)
-//	//if err != nil {
-//	//	return
-//	//}
-//	//h.json(QueryHrSensorsInfo{
-//	//	ResponseBase: ResponseBase{},
-//	//	Rs:           hr,
-//	//})
-//	return
-//}
-//
-//// QueryPvtHrSensors godoc
-//// @Summary 查询商家会员私有心率设备
-//// @tags HrSensors
-//// @Description 查询商家会员私有心率设备
-//// @Accept  x-www-form-urlencoded
-//// @Produce  json
-//// @Param token formData string true "Token"
-//// @Param shopId formData int true "商家ID"
-//// @Param status formData int false "设备状态"
-//// @Param userName formData string false "用户姓名"
-//// @Param str formData string false "模糊查询sn与场内编号"
-//// @Success 200 {object} controller.ResponseBase
-//// @Router /HrSensors/QueryPvtHrSensors [post]
-//func (h *HrSensors) QueryPvtHrSensors() (err error) {
-//	//sess := h.getSession()
-//	//str := h.Ctx().PostForm("str")
-//	//userName := h.Ctx().PostForm("userName")
-//	//shopId := h.postIntNecessary("shopId")
-//	//status := h.postInt("status")
-//	//hr, err := service.HrSensors{}.QueryPvtHrSensors(sess, shopId, str, userName, status)
-//	//if err != nil {
-//	//	return
-//	//}
-//	//h.json(QueryHrSensorsInfo{
-//	//	ResponseBase: ResponseBase{},
-//	//	Rs:           hr,
-//	//})
-//	return
-//}
-//
-//// BindHrSensorsToUser godoc
-//// @Summary 心率带绑定用户
-//// @tags HrSensors
-//// @Description 心率带绑定用户
-//// @Accept  x-www-form-urlencoded
-//// @Produce  json
-//// @Param token formData string true "Token"
-//// @Param sn formData string true "心率带Sn"
-//// @Param cfId formData int true "课堂记录Id,必传字段"
-//// @Param userId formData int true "用户Id"
-//// @Param shopId formData int true "商家ID"
-//// @Param hrId formData int true "心率带ID"
-//// @Success 200 {object} controller.ResponseBase
-//// @Router /HrSensors/BindHrSensorsToUser [post]
-//func (h *HrSensors) BindHrSensorsToUser() (err error) {
-//	//sess := h.getSession()
-//	//sn := h.postString("sn", true)
-//	//cfId := h.postIntNecessary("cfId")
-//	//userId := h.postIntNecessary("userId")
-//	//shopId := h.postIntNecessary("shopId")
-//	//hrId := h.postIntNecessary("hrId")
-//	////dpId := h.postInt("dpId")  // 用来判断是否上课的依据,目前不需要前台传递,根据cfId进行反查。且未确认上课时cf表中dpId=0
-//	////oldSn := h.Ctx().PostForm("oldSn")
-//	//
-//	//logInfo, err := service.HrSensors{}.HrSensorsBindUser(sess, sn, cfId, userId, shopId, hrId)
-//	//if err != nil {
-//	//	return
-//	//}
-//	//h.saveOptLogInfo(logInfo)
-//	//h.json(newResponseBase())
-//	return
-//}
-//
-//// UnBindHrSensorsToUser godoc
-//// @Summary 心率带解绑用户
-//// @tags HrSensors
-//// @Description 心率带解绑用户
-//// @Accept  x-www-form-urlencoded
-//// @Produce  json
-//// @Param token formData string true "Token"
-//// @Param cfId formData int true "课堂记录Id,必传字段"
-//// @Param bindId formData int true "绑定ID"
-//// @Param userId formData int true "用户ID"
-//// @Param sn formData string true "心率带Sn"
-//// @Success 200 {object} controller.ResponseBase
-//// @Router /HrSensors/UnBindHrSensorsToUser [post]
-//func (h *HrSensors) UnBindHrSensorsToUser() (err error) {
-//	//sess := h.getSession()
-//	//cfId := h.postIntNecessary("cfId")
-//	//bindId := h.postIntNecessary("bindId")
-//	//userId := h.postIntNecessary("userId")
-//	//sn := h.postString("sn", true)
-//	//
-//	//logInfo, err := service.HrSensors{}.UnBindHrSensorsToUser(sess, sn, cfId, bindId, userId)
-//	//if err != nil {
-//	//	return
-//	//}
-//	//h.saveOptLogInfo(logInfo)
-//	//h.json(newResponseBase())
-//	return
-//}
-//
-//type SelectHrSensorsRs struct {
-//	ResponseBase
-//	Rs string
-//	//Rs []*model.HrSensors
-//}
-//
-//// SelectHrSensors godoc
-//// @Summary 查询未绑定的心率带--下拉框用
-//// @tags HrSensors
-//// @Description 查询未绑定的心率带--下拉框用
-//// @Accept  x-www-form-urlencoded
-//// @Produce  json
-//// @Param token formData string true "Token"
-//// @Param userId formData int false "用户ID"
-//// @Success 200 {object} controller.ResponseBase
-//// @Router /HrSensors/SelectHrSensors [post]
-//func (h *HrSensors) SelectHrSensors() (err error) {
-//	//sess := h.getSession()
-//	//userId := h.postInt("userId")
-//	//rs, err := service.HrSensors{}.SelectHrSensors(sess, userId)
-//	//if err != nil {
-//	//	return
-//	//}
-//	//h.json(SelectHrSensorsRs{
-//	//	ResponseBase: ResponseBase{},
-//	//	Rs:           rs,
-//	//})
-//	return
-//}
-//
-//type HrSensorsBindHistoryRs struct {
-//	ResponseBase
-//	Rs string
-//}
-//
-//// HrSensorsBindHistory godoc
-//// @Summary 查询心率带绑定历史记录
-//// @tags HrSensors
-//// @Description 查询心率带绑定历史记录
-//// @Accept  x-www-form-urlencoded
-//// @Produce  json
-//// @Param token formData string true "Token"
-//// @Param hrId formData int false "心率带ID"
-//// @Param bt formData string true "开始时间  2020-10-10 00:00:01"
-//// @Param et formData string true "结束时间  2020-10-10 23:59:59"
-//// @Success 200 {object} controller.ResponseBase
-//// @Router /HrSensors/HrSensorsBindHistory [post]
-//func (h *HrSensors) HrSensorsBindHistory() (err error) {
-//	//sess := h.getSession()
-//	//bt, err := h.getPostFromTime("bt")
-//	//if err != nil {
-//	//	return
-//	//}
-//	//et, err := h.getPostFromTime("et")
-//	//if err != nil {
-//	//	return
-//	//}
-//	//hrId := h.postInt("hrId")
-//	//
-//	//rs, err := service.HrSensors{}.HrSensorsBindHistory(sess, hrId, bt, et)
-//	//if err != nil {
-//	//	return
-//	//}
-//	//h.json(HrSensorsBindHistoryRs{
-//	//	ResponseBase: ResponseBase{},
-//	//	Rs:           rs,
-//	//})
-//	return
-//}
-//
-//// GetHrSensorsPowerPercent godoc
-//// @Summary 获取心率带电量
-//// @tags HrSensors
-//// @Description 获取心率带电量
-//// @Accept  x-www-form-urlencoded
-//// @Produce  json
-//// @Param token formData string true "Token"
-//// @Param shopId formData int true "商家ID"
-//// @Param hrId formData int false "心率带Id"
-//// @Success 200 {object} controller.ResponseBase
-//// @Router /HrSensors/GetHrSensorsPowerPercent [post]
-//func (h *HrSensors) GetHrSensorsPowerPercent() (err error) {
-//	//sess := h.getSession()
-//	//shopId := h.postIntNecessary("shopId")
-//	//hrId := h.postInt("hrId")
-//	////jsonStr := h.postString("jsonStr", false)
-//	////dpId := h.postInt("dpId")  // 用来判断是否上课的依据,目前不需要前台传递,根据cfId进行反查。且未确认上课时cf表中dpId=0
-//	////oldSn := h.Ctx().PostForm("oldSn")
-//	//
-//	//logInfo, err := service.HrSensors{}.GetHrSensorsPowerPercent(sess, shopId, hrId)
-//	//if err != nil {
-//	//	return
-//	//}
-//	//h.saveOptLogInfo(logInfo)
-//	//h.json(newResponseBase())
-//	return
-//}
+// HrSensorsUpdate godoc
+// @Summary 心率带信息查询和更新
+// @tags HrSensors
+// @Description  心率带信息查询和更新
+// @Accept  x-www-form-urlencoded
+// @Produce  json
+// @Param token formData string true "Token"
+// @Param sn formData string true "心率带编号"
+// @Success 200 {object} controller.ResponseBase
+// @Router /HrSensors/HrSensorsUpdate [post]
+func (h *HrSensors) HrSensorsUpdate() (err error) {
+	token := h.Ctx().PostForm("token")
+	sn := h.Ctx().PostForm("sn")
+	hrId, err := service.HrSensors{}.HrSensorsUpdate(token, sn)
+	if err != nil {
+		return
+	}
+	r := UpdateHrSensorsInfo{
+		newResponseBase(),
+		hrId,
+	}
+	h.Ctx().JSON(http.StatusOK, r)
+	return
+}

+ 50 - 54
controller/test.go

@@ -1,60 +1,56 @@
 package controller
 
-import (
-	"net/http"
-	"video_course/service"
-)
-
 type Test struct {
 	BaseController
 }
 
-// UserAdd godoc
-// @Summary 会员用户添加
-// @tags Test
-// @Description 会员用户添加
-// @Accept  x-www-form-urlencoded
-// @Produce  json
-// @Param name formData string true "姓名"
-// @Success 200 {object} controller.ResponseBase
-// @Router /Test/UserAdd [post]
-func (t *Test) UserAdd() (err error) {
-
-	name := t.postString("name", true)
-
-	err = service.Test{}.UserAdd(name)
-	if err != nil {
-		return
-	}
-
-	t.Ctx().JSON(http.StatusOK, newResponseBase())
-	return
-}
-
-type UserList struct {
-	ResponseBase
-	Rs []interface{}
-	//Rs []*gorm.ShopUserSimpleInfo
-}
-
-// UserListQuery godoc
-// @Summary 会员用户查询
-// @tags Test
-// @Description 会员用户查询
-// @Accept  x-www-form-urlencoded
-// @Produce  json
-// @Success 200 {object} controller.UserList
-// @Router /Test/UserListQuery [post]
-func (t *Test) UserListQuery() (err error) {
-
-	rs, err := service.Test{}.UserListQuery()
-	if err != nil {
-		return
-	}
-	rp := UserList{
-		newResponseBase(),
-		rs,
-	}
-	t.Ctx().JSON(http.StatusOK, rp)
-	return
-}
+//
+//// UserAdd godoc
+//// @Summary 会员用户添加
+//// @tags Test
+//// @Description 会员用户添加
+//// @Accept  x-www-form-urlencoded
+//// @Produce  json
+//// @Param name formData string true "姓名"
+//// @Success 200 {object} controller.ResponseBase
+//// @Router /Test/UserAdd [post]
+//func (t *Test) UserAdd() (err error) {
+//
+//	name := t.postString("name", true)
+//
+//	err = service.Test{}.UserAdd(name)
+//	if err != nil {
+//		return
+//	}
+//
+//	t.Ctx().JSON(http.StatusOK, newResponseBase())
+//	return
+//}
+//
+//type UserList struct {
+//	ResponseBase
+//	Rs []interface{}
+//	//Rs []*gorm.ShopUserSimpleInfo
+//}
+//
+//// UserListQuery godoc
+//// @Summary 会员用户查询
+//// @tags Test
+//// @Description 会员用户查询
+//// @Accept  x-www-form-urlencoded
+//// @Produce  json
+//// @Success 200 {object} controller.UserList
+//// @Router /Test/UserListQuery [post]
+//func (t *Test) UserListQuery() (err error) {
+//
+//	rs, err := service.Test{}.UserListQuery()
+//	if err != nil {
+//		return
+//	}
+//	rp := UserList{
+//		newResponseBase(),
+//		rs,
+//	}
+//	t.Ctx().JSON(http.StatusOK, rp)
+//	return
+//}

+ 4 - 0
errors/error.go

@@ -34,6 +34,8 @@ const (
 	CodeREDIS               ErrorCode = 10000
 	HrSensorsTimeOutErr     ErrorCode = 30012
 	JsonToMaptErr           ErrorCode = 30013
+	HrSensorsUpdateErr      ErrorCode = 30014
+	GetDuInfoUpdateErr      ErrorCode = 30015
 	CodeParam                         = ErrorCode(errors.CodeParam)
 )
 
@@ -45,6 +47,8 @@ var (
 	ErrBirthday         = NewServiceErr(BirthdayErr, "用户生日不得大于当前年份,请重新输入")
 	ErrJsonToMap        = NewServiceErr(JsonToMaptErr, "Json转换Map对象失败")
 	ErrHrSensorsTimeOut = NewServiceErr(HrSensorsTimeOutErr, "心率系统连接超时")
+	ErrHrSensorsUpdate  = NewServiceErr(HrSensorsUpdateErr, "心率系统更新Sn报错")
+	ErrGetDuInfo        = NewServiceErr(GetDuInfoUpdateErr, "心率系统查询用户当前显示单元和对应身体信息报错")
 )
 
 func (e ErrorCode) ShowMsg() string {

+ 1 - 0
main.go

@@ -89,6 +89,7 @@ func main() {
 		middleware.Session(),
 	)
 	r.AutoRegisterController("/v1", &controller.Auth{})
+	r.AutoRegisterController("/v1", &controller.HrSensors{})
 	r.AutoRegisterController("/v1", &controller.User{})
 	//debug模式下启用swag文档
 	if global.Project.Debug {

+ 6 - 0
model/duInfo.go

@@ -0,0 +1,6 @@
+package model
+
+type DuInfo struct {
+	DpName string
+	DuId   int
+}

+ 91 - 0
repository/http/heartrate_server.go

@@ -7,6 +7,7 @@ import (
 	"net/http"
 	"net/url"
 	"strings"
+	"video_course/errors"
 	"video_course/global"
 	"video_course/model"
 	"video_course/utils"
@@ -59,3 +60,93 @@ func (h HeartRateServer) VipUserQueryByUserMd5(userMd5 string) (rtnCode int, rtn
 
 	return
 }
+
+//线上心率带更新
+func (h HeartRateServer) HrSensorsUpdate(userMd5 string, sn string) (hrId int, err error) {
+	api := strings.Join([]string{global.Project.HeartrateUrl, "v1/OutService/HrSensorsUpdate"}, "/")
+
+	resp, err := http.PostForm(api, url.Values{
+		"userMd5": {userMd5},
+		"sn":      {sn},
+	})
+	if err != nil {
+		panic(err)
+	}
+
+	defer resp.Body.Close()
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
+		panic(err)
+	}
+
+	var responseBase struct {
+		Code int
+		Memo string
+		HrId int
+	}
+	err = json.Unmarshal(body, &responseBase)
+	if err != nil {
+		err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
+		panic(err)
+	}
+	rtnCode := responseBase.Code
+	hrId = responseBase.HrId
+	if rtnCode != 0 {
+		err = errors.ErrHrSensorsUpdate
+	}
+
+	return
+}
+
+//查询用户是否正在上课接口
+func (h HeartRateServer) GetDuInfoAndUserInfoByUserMd5(userMd5 string) (inClass int, duInfo *model.DuInfo, userInfo *model.UserBodyInfo, err error) {
+	api := strings.Join([]string{global.Project.HeartrateUrl, "v1/OutService/GetDuInfoAndUserInfoByUserMd5"}, "/")
+
+	resp, err := http.PostForm(api, url.Values{
+		"userMd5": {userMd5},
+	})
+	if err != nil {
+		panic(err)
+	}
+
+	defer resp.Body.Close()
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
+		panic(err)
+	}
+
+	var responseBase struct {
+		Code     int
+		Memo     string
+		DuInfo   map[string]interface{}
+		UserInfo map[string]interface{}
+	}
+	err = json.Unmarshal(body, &responseBase)
+	if err != nil {
+		err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
+		panic(err)
+	}
+	rtnCode := responseBase.Code
+	if rtnCode == 0 {
+		duInfo = &model.DuInfo{
+			DpName: utils.MapToString(responseBase.DuInfo, "DpName"),
+			DuId:   utils.MapToInt32(responseBase.DuInfo, "DuId"),
+		}
+		userInfo = &model.UserBodyInfo{
+			UserName: utils.MapToString(responseBase.UserInfo, "Name"),
+			Birthday: utils.MapToTime(responseBase.UserInfo, "Birthday"),
+			Height:   utils.MapToInt32(responseBase.UserInfo, "Height"),
+			Weight:   utils.MapToInt32(responseBase.UserInfo, "Weight"),
+			Sex:      utils.MapToInt32(responseBase.UserInfo, "Sex"),
+			StaticHr: utils.MapToInt32(responseBase.UserInfo, "StaticHr"),
+			UserMd5:  utils.MapToString(responseBase.UserInfo, "UserMd5"),
+			Head:     utils.MapToString(responseBase.UserInfo, "Head"),
+		}
+	} else {
+		err = errors.ErrGetDuInfo
+	}
+
+	return
+}

+ 27 - 0
service/hrsensors.go

@@ -0,0 +1,27 @@
+package service
+
+import (
+	"video_course/lib/session"
+	h "video_course/repository/http"
+)
+
+type HrSensors struct {
+	base
+}
+
+//心率带更新(
+func (hr HrSensors) HrSensorsUpdate(
+	token string, sn string) (hrId int, err error) {
+	sm := session.GetSessionManager()
+	sess, err_ := sm.FindByToken(token)
+	if err_ != nil {
+		err = err_
+		return
+	}
+	userMd5 := sess.UserId
+
+	//首先查询心率系统,如果查询失败查询小飞龙系统
+	hrId, err = h.HeartRateServer{}.HrSensorsUpdate(userMd5, sn)
+
+	return
+}