hcmp/weapp/plan.go
2024-07-31 15:48:23 +08:00

947 lines
26 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package weapp
import (
"fmt"
"hc/dbop"
"math"
"net/http"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/shockliu/logger"
)
func UpdatePlan(c *gin.Context) {
//uid := c.MustGet("TK_User").(int)
uid := 10000014
type plancation struct {
ActionId int `json:"action_id"`
Seri int `json:"action_seri"`
Dur int `json:"action_dur"`
}
type tmp struct {
PlanId int `json:"plan_id"`
FreqType string `json:"freq_type"`
Plist []plancation `json:"plist"`
}
var data tmp
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
logger.Debugf(err.Error())
return
}
ts, err := dbop.MDb.Begin()
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
logger.Debugf(err.Error())
return
}
if len(data.FreqType) > 0 {
_, err = ts.Exec("update user_actplan set freq_type = ?,modify_time = now() where action_type = 2 and user_id = ?;", data.FreqType, uid)
if err != nil {
ts.Rollback()
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
logger.Debugf(err.Error())
return
}
}
if len(data.Plist) > 0 {
_, err = ts.Exec("delete from user_plan where user_id=? and play_type = 2;", uid)
if err != nil {
ts.Rollback()
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
logger.Debugf(err.Error())
return
}
for _, v := range data.Plist {
action_id := v.ActionId
if v.ActionId < 0 { //新增的动作
}
_, err = ts.Exec("insert into user_plan (user_id,play_type,action_id,action_seri,creat_time,action_dur) values (?,2,?,?,now(),?);", uid, action_id, v.Seri, v.Dur)
if err != nil {
ts.Rollback()
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
logger.Debugf(err.Error())
return
}
}
}
ts.Commit()
c.JSON(http.StatusOK, gin.H{
"code": 0,
"msg": "succ",
})
}
func PlanList(c *gin.Context) {
uid := c.MustGet("TK_User").(int)
//uid := 10000014
code := 0
//BEGIN:
var record_id, grade int
var ts, optime string
err := dbop.MDb.QueryRow("select id,IFNULL(grade,-1),timeSimp,create_time from record where DATE(NOW())=DATE(create_time) and action_type = 2 and user_id = ? ORDER BY create_time DESC limit 1", uid).Scan(&record_id, &grade, &ts, &optime)
type tmp struct {
ScoreId int `json:"score_id"`
Status int `json:"status"`
}
uncmp_plan := make(map[int]tmp)
if grade != -1 { //今日最后一次正常结束
record_id = 0
} else {
rows, err := dbop.MDb.Query("select id, action_id,result,`status` from action_score where record_id=?;", record_id)
if err != nil {
logger.Warnf("数据查询错误%s\n", err)
c.JSON(http.StatusOK, gin.H{
"code": -1,
"message": err.Error(),
})
return
}
defer rows.Close()
for rows.Next() {
var action_id, result, status, score_id int
err = rows.Scan(&score_id, &action_id, &result, &status)
vk, ok := uncmp_plan[action_id]
var cur tmp
cur.ScoreId = score_id
if ok {
if vk.Status == 0 {
uncmp_plan[action_id] = cur
}
} else {
uncmp_plan[action_id] = cur
}
}
// if len(uncmp_plan) == 0 { //无动作的记录,可删除,重新查找
// logger.Debugf("删除无效记录 %d", record_id)
// code = 1
// dbop.MDb.Exec("delete from record where id = ?", record_id)
// goto BEGIN
// }
}
var plan_id, relax_time int
var freq_type string
err = dbop.MDb.QueryRow("select plan_id,freq_type,relax_time from user_actplan where user_id = ? and action_type = 2;", uid).Scan(&plan_id, &freq_type, &relax_time)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"message": err.Error(),
})
return
}
rows, err := dbop.MDb.Query("select p.action_seri,b.action_id,b.url_type,b.action_name,b.action_url,b.isonline,ifnull(p.action_dur,0),b.action_dur,b.author from user_plan p LEFT JOIN brain_action b on p.action_id=b.action_id where p.user_id = ? and p.play_type = 2 and b.`show`=1 ORDER BY p.action_seri;", uid)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"message": err.Error(),
})
return
}
defer rows.Close()
var rst []gin.H
for rows.Next() {
var action_url, action_name, author string
var action_seri, action_id, url_type, isonline, action_dur, def_dur int
err = rows.Scan(&action_seri, &action_id, &url_type, &action_name, &action_url, &isonline, &action_dur, &def_dur, &author)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
if action_dur == 0 {
action_dur = def_dur
}
act_status := 0
score_id := 0
if record_id != 0 {
kv, ok := uncmp_plan[action_id]
if ok == true { //值存在
act_status = kv.Status
score_id = kv.ScoreId
delete(uncmp_plan, action_id)
}
}
rst = append(rst, gin.H{"action_seri": action_seri, "action_id": action_id, "url_type": url_type, "action_name": action_name, "action_url": action_url, "isonline": isonline, "action_dur": action_dur, "author": author, "status": act_status, "score_id": score_id})
}
logger.Debugf("%#v", rst)
c.JSON(http.StatusOK, gin.H{
"code": code,
"data": gin.H{
"plan_id": plan_id,
"freq_type": freq_type,
"record_id": record_id,
"timestamp": ts,
"optime": optime,
"relax_time": relax_time,
"plist": rst,
},
"msg": "succ",
})
}
func TrainHistory(c *gin.Context) {
//uid := c.MustGet("TK_User").(int)
uid := 10000014
type tmp struct {
Month string `json:"month"`
}
var data tmp
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
rows, err := dbop.MDb.Query("select id,action_type,ifnull(grade,-1),ifnull(duration,0),DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') as create_time,ifnull(`desc`,'') from record where user_id=? and ? = DATE_FORMAT(create_time, '%Y%m') ORDER BY create_time DESC", uid, data.Month)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
defer rows.Close()
var rst []gin.H
for rows.Next() {
var create_time, desc string
var record_id, action_type, grade, dur int
err = rows.Scan(&record_id, &action_type, &grade, &dur, &create_time, &desc)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
if action_type == 1 {
desc = "快速检测"
} else if action_type == 2 {
desc = "日常训练"
}
rst = append(rst, gin.H{"record_id": record_id, "action_type": action_type, "grade": grade, "dur": dur, "create_time": create_time, "desc": desc})
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": rst,
"msg": "succ",
})
}
func TrainResult(c *gin.Context) {
type tmp struct {
RecordId int `json:"record_id"`
}
var data tmp
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
rows, err := dbop.MDb.Query("select b.url_type, b.action_name,ifnull(s.beginTime,''),IFNULL(s.endTime,''),b.action_url,ifnull(s.result,-1) rs,s.status,s.actionDur from action_score s LEFT JOIN brain_action b on b.action_id = s.action_id where s.record_id = ? ORDER BY rs DESC;", data.RecordId)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
defer rows.Close()
var rst []gin.H
var tol_result, tol_times, action_type int
var all_beg, all_end string
for rows.Next() {
var action_name, action_url, beg_time, end_time string
var result, status, dur int
err = rows.Scan(&action_type, &action_name, &beg_time, &end_time, &action_url, &result, &status, &dur)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
var act_type string
switch action_type { //视觉1、听觉2触觉3味觉4嗅觉5
case 1:
act_type = "视觉听觉"
break
case 2:
act_type = "听觉"
break
case 3:
act_type = "触觉"
break
case 4:
act_type = "味觉"
break
case 5:
act_type = "嗅觉"
break
case 6:
act_type = "自定动作"
break
}
rst = append(rst, gin.H{"action_name": action_name, "action_type": act_type, "action_url": action_url, "result": result, "dur": dur, "status": status, "beg_time": beg_time, "end_time": end_time})
if status == 1 {
tol_result += result
tol_times++
}
if len(all_beg) > 0 {
if beg_time < all_beg {
all_beg = beg_time
}
} else {
all_beg = beg_time
}
if len(all_end) > 0 {
if end_time > all_end {
all_end = end_time
}
} else {
all_end = end_time
}
}
grade := 0
if tol_times != 0 {
grade = tol_result / tol_times
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": gin.H{
"plist": rst,
"grade": grade,
"beg_time": all_beg,
"end_time": all_end,
},
"msg": "succ",
})
}
func ActionList(c *gin.Context) {
uid := c.MustGet("TK_User").(int)
rows, err := dbop.MDb.Query("select action_id,url_type,action_name,action_url,action_dur,author from brain_action where user_id in (0,?) and `show` = 1 ORDER BY url_type;", uid)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
defer rows.Close()
var vlist, mlist, olist, rst []gin.H
for rows.Next() {
var action_name, action_url, author string
var action_id, url_type, action_dur int
err = rows.Scan(&action_id, &url_type, &action_name, &action_url, &action_dur, &author)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
switch url_type { //视觉1、听觉2触觉3味觉4嗅觉5
case 1:
//vlist = append(vlist, gin.H{"action_id": action_id, "action_name": action_name, "action_url": action_url, "author": author, "action_dur": action_dur})
vlist = append(vlist, gin.H{"value": action_id, "text": action_name})
break
case 2:
//mlist = append(mlist, gin.H{"action_id": action_id, "action_name": action_name, "action_url": action_url, "author": author, "action_dur": action_dur})
mlist = append(mlist, gin.H{"value": action_id, "text": action_name})
break
case 3, 4, 5, 6:
//olist = append(olist, gin.H{"action_id": action_id, "action_name": action_name, "action_url": action_url, "author": author, "action_dur": action_dur})
olist = append(olist, gin.H{"value": action_id, "text": action_name})
break
}
}
rst = append(rst, gin.H{"value": 0, "text": "视频", "children": vlist})
rst = append(rst, gin.H{"value": 1, "text": "音乐", "children": mlist})
rst = append(rst, gin.H{"value": 2, "text": "感官", "children": olist})
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": rst,
"msg": "succ",
})
}
func ActionDetail(c *gin.Context) {
type tmp struct {
ActionId int `json:"action_id"`
}
var data tmp
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
var action_name, action_url, author string
var action_id, url_type, action_dur int
err := dbop.MDb.QueryRow("select action_id,url_type,action_name,action_url,action_dur,author from brain_action where action_id = ? and `show` = 1 ORDER BY url_type;", data.ActionId).Scan(&action_id, &url_type, &action_name, &action_url, &action_dur, &author)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": gin.H{
"action_id": action_id,
"url_type": url_type,
"action_name": action_name,
"action_url": action_url,
"action_dur": action_dur,
"author": author,
},
"msg": "succ",
})
}
func CreateTrain(c *gin.Context) {
uid := c.MustGet("TK_User").(int)
type tmp struct {
Ts string `json:"timestamp"`
Action_type int `json:"action_type"`
DevMac string `json:"devmac,omitempty"`
DevSn string `json:"devsn,omitempty"`
}
var data tmp
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
_, err := dbop.MDb.Exec("insert into record (action_type,user_id,timeSimp,create_time,status,facility_sn,facility_mac) values (?,?,?,now(),0,?,?)", data.Action_type, uid, data.Ts, data.DevSn, data.DevMac)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
var record_id int
err = dbop.MDb.QueryRow("select id from record where timeSimp = ?;", data.Ts).Scan(&record_id)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
logger.Debugf("用户 %d 创建新训练,id %d,timestamp:%s", uid, record_id, data.Ts)
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": gin.H{
"record_id": record_id,
"timestamp": data.Ts,
},
"msg": "succ",
})
}
// 计算给定数据集的方差
func variance(data []float64) (int, int, int, int, int) {
sum := 0.0
avg := 0.0
variance := 0.0
max := 0.0
min := 100000.0
// 计算数据集的均值
for _, val := range data {
sum += val
if val > max {
max = val
}
if val < min {
min = val
}
}
avg = sum / float64(len(data))
// 计算方差
for _, val := range data {
variance += math.Pow((val - avg), 2)
}
variance = variance / float64(len(data)-1)
return int(variance * 100), int(sum * 100), int(avg * 100), int(max * 100), int(min * 100)
}
func updateActionValue(act_rel, act_abs []float64, score_id, band_type int) {
undulate_rel, _, avg_rel, max_rel, min_rel := variance(act_rel) //相对
undulate_abs, eng_abs, avg_abs, max_abs, min_abs := variance(act_abs) //绝对
strsql := fmt.Sprintf("insert into action_band_info (score_id,band_type,status,undulate_rel, avg_rel, max_rel, min_rel,undulate_abs, eng_abs, avg_abs, max_abs, min_abs) values (%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", score_id, band_type, 1, undulate_rel, avg_rel, max_rel, min_rel, undulate_abs, eng_abs, avg_abs, max_abs, min_abs)
logger.Debugf("%s", strsql)
_, err := dbop.MDb.Exec(strsql)
if err != nil {
logger.Warnf("%s", err.Error())
}
}
func updateTotalValue(rest_rel, rest_abs, act_rel, act_abs []float64, score_id, band_type int) int {
base_undul_rel, _, base_rel, _, _ := variance(rest_rel) //空闲期相对值
base_undul_abs, base_eng_abs, base_abs, _, _ := variance(rest_abs) //空闲期绝对值
undulate_rel, _, avg_rel, max_rel, min_rel := variance(act_rel) //相对
undulate_abs, eng_abs, avg_abs, max_abs, min_abs := variance(act_abs) //绝对
strsql := fmt.Sprintf("insert into action_band_info (score_id,band_type,status,base_undul_rel,base_rel,base_undul_abs, base_eng_abs, base_abs,undulate_rel, avg_rel, max_rel, min_rel,undulate_abs, eng_abs, avg_abs, max_abs, min_abs) values (%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", score_id, band_type, 0, base_undul_rel, base_rel, base_undul_abs, base_eng_abs, base_abs, undulate_rel, avg_rel, max_rel, min_rel, undulate_abs, eng_abs, avg_abs, max_abs, min_abs)
logger.Debugf("%s", strsql)
_, err := dbop.MDb.Exec(strsql)
if err != nil {
logger.Warnf("%s", err.Error())
}
return int(undulate_abs * 100 / base_undul_abs)
}
func UploadAction(c *gin.Context) {
uid := c.MustGet("TK_User").(int)
type tmp struct {
Ts string `json:"timestamp"`
RecordId int `json:"record_id"`
ActionId int `json:"action_id"`
ScoreId int `json:"score_id"`
RestBeg string `json:"rest_beg"`
ActionBeg string `json:"action_beg"`
ActionEnd string `json:"action_end"`
Dur int `json:"dur"`
Last bool `json:"last,omitempty"`
}
var data tmp
if err := c.ShouldBindJSON(&data); err != nil {
logger.Warnf("%s", err.Error())
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
logger.Debugf("上传训练记录 %#v", data)
scoreId := data.ScoreId
if scoreId == 0 {
_, err := dbop.MDb.Exec("insert into action_score (user_id,record_id,action_id,result,status,timeSimp,beginTime,endTime,actionDur) values (?,?,?,0,0,?,?,?,?)", uid, data.RecordId, data.ActionId, data.Ts, data.ActionBeg, data.ActionEnd, data.Dur)
if err != nil {
logger.Warnf("%s", err.Error())
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
err = dbop.MDb.QueryRow("select id from action_score where timeSimp = ? and beginTime = ?;", data.Ts, data.ActionBeg).Scan(&scoreId)
if err != nil {
logger.Warnf("%s", err.Error())
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
logger.Debugf("创建新 action_score id %d", scoreId)
}
if data.Last == false {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"msg": "succ",
})
}
//下面进行计算和更新
logger.Debugf("开始计算,beg:%s,end:%s,split:%s,ts:%s", data.RestBeg, data.ActionEnd, data.ActionBeg, data.Ts)
//strsql := fmt.Sprintf("select data,DATE_FORMAT(create_time,'%s') from url_band where is_error = 0 and create_time > '%s' and create_time < '%s' and timeSimp = '%s';", "%Y-%m-%d %h:%m:%s", data.RestBeg, data.ActionEnd, data.Ts)
//logger.Debugf("sql:%s", strsql)
//rows, err := dbop.MDb.Query(strsql)
rows, err := dbop.MDb.Query("select data,create_time from url_band where is_error = 0 and create_time > ? and create_time < ? and timeSimp = ?;", data.RestBeg, data.ActionEnd, data.Ts)
if err != nil {
logger.Warnf("%s", err.Error())
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
defer rows.Close()
var c_time, banddata string
r_deltaRelative := make([]float64, 0)
r_thetaRelative := make([]float64, 0)
r_alphaRelative := make([]float64, 0)
r_betaRelative := make([]float64, 0)
r_gammaRelative := make([]float64, 0)
r_deltaAbsolute := make([]float64, 0)
r_thetaAbsolute := make([]float64, 0)
r_alphaAbsolute := make([]float64, 0)
r_betaAbsolute := make([]float64, 0)
r_gammaAbsolute := make([]float64, 0)
a_deltaRelative := make([]float64, 0)
a_thetaRelative := make([]float64, 0)
a_alphaRelative := make([]float64, 0)
a_betaRelative := make([]float64, 0)
a_gammaRelative := make([]float64, 0)
a_deltaAbsolute := make([]float64, 0)
a_thetaAbsolute := make([]float64, 0)
a_alphaAbsolute := make([]float64, 0)
a_betaAbsolute := make([]float64, 0)
a_gammaAbsolute := make([]float64, 0)
var had_rest, had_action bool
for rows.Next() {
err = rows.Scan(&banddata, &c_time)
if err != nil {
logger.Debugf("%s", err.Error())
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
c_time = strings.Replace(c_time, "T", " ", -1)
c_time = strings.Replace(c_time, "Z", "", -1)
/*
target.energy.deltaRelative, target.energy.thetaRelative, target.energy.alphaRelative, target.energy.betaRelative, target.energy.gammaRelative,
target.mindfullness, target.focus, target.emotion, target.fatigue, target.stress,
target.energy.deltaAbsolute, target.energy.thetaAbsolute, target.energy.alphaAbsolute, target.energy.betaAbsolute, target.energy.gammaAbsolute,
*/
ds := strings.Split(banddata, "|")
//logger.Debugf("%#v", ds)
//logger.Debugf("%s %s", c_time, data.ActionBeg)
if c_time < data.ActionBeg { //空闲期
had_rest = true
f_data, err := strconv.ParseFloat(ds[0], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
r_deltaRelative = append(r_deltaRelative, f_data)
f_data, err = strconv.ParseFloat(ds[1], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
r_thetaRelative = append(r_thetaRelative, f_data)
f_data, err = strconv.ParseFloat(ds[2], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
r_alphaRelative = append(r_alphaRelative, f_data)
f_data, err = strconv.ParseFloat(ds[3], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
r_betaRelative = append(r_betaRelative, f_data)
f_data, err = strconv.ParseFloat(ds[4], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
r_gammaRelative = append(r_gammaRelative, f_data)
f_data, err = strconv.ParseFloat(ds[10], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
r_deltaAbsolute = append(r_deltaAbsolute, f_data)
f_data, err = strconv.ParseFloat(ds[11], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
r_thetaAbsolute = append(r_thetaAbsolute, f_data)
f_data, err = strconv.ParseFloat(ds[12], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
r_alphaAbsolute = append(r_alphaAbsolute, f_data)
f_data, err = strconv.ParseFloat(ds[13], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
r_betaAbsolute = append(r_betaAbsolute, f_data)
f_data, err = strconv.ParseFloat(ds[14], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
r_gammaAbsolute = append(r_gammaAbsolute, f_data)
} else { //动作期
had_action = true
f_data, err := strconv.ParseFloat(ds[0], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
a_deltaRelative = append(a_deltaRelative, f_data)
f_data, err = strconv.ParseFloat(ds[1], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
a_thetaRelative = append(a_thetaRelative, f_data)
f_data, err = strconv.ParseFloat(ds[2], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
a_alphaRelative = append(a_alphaRelative, f_data)
f_data, err = strconv.ParseFloat(ds[3], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
a_betaRelative = append(a_betaRelative, f_data)
f_data, err = strconv.ParseFloat(ds[4], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
a_gammaRelative = append(a_gammaRelative, f_data)
f_data, err = strconv.ParseFloat(ds[10], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
a_deltaAbsolute = append(a_deltaAbsolute, f_data)
f_data, err = strconv.ParseFloat(ds[11], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
a_thetaAbsolute = append(a_thetaAbsolute, f_data)
f_data, err = strconv.ParseFloat(ds[12], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
a_alphaAbsolute = append(a_alphaAbsolute, f_data)
f_data, err = strconv.ParseFloat(ds[13], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
a_betaAbsolute = append(a_betaAbsolute, f_data)
f_data, err = strconv.ParseFloat(ds[14], 64)
if err != nil {
logger.Debugf("%s", err.Error())
}
a_gammaAbsolute = append(a_gammaAbsolute, f_data)
}
}
var sql_update_action_score string
if had_action && had_rest { //rest+action期都有
logger.Debugf(" action_score %d 有数据,%s", scoreId, data.Ts)
result_delta := updateTotalValue(r_deltaRelative, r_deltaAbsolute, a_deltaRelative, a_deltaAbsolute, scoreId, 1)
result_theta := updateTotalValue(r_thetaRelative, r_thetaAbsolute, a_thetaRelative, a_thetaAbsolute, scoreId, 2)
result_alpha := updateTotalValue(r_alphaRelative, r_alphaAbsolute, a_alphaRelative, a_alphaAbsolute, scoreId, 3)
result_beta := updateTotalValue(r_betaRelative, r_betaAbsolute, a_betaRelative, a_betaAbsolute, scoreId, 4)
result_gamma := updateTotalValue(r_gammaRelative, r_gammaAbsolute, a_gammaRelative, a_gammaAbsolute, scoreId, 5)
sql_update_action_score = fmt.Sprintf("update action_score set result_delta = %d,result_theta = %d,result_alpha = %d,result_beta = %d,result_gamma = %d,result = %d,`status` = 1 where id = %d", result_delta, result_theta, result_alpha, result_beta, result_gamma, result_alpha, scoreId)
} else if had_action { //action期
logger.Warnf(" action_score %d 只有动作期数据,%s", scoreId, data.Ts)
updateActionValue(a_deltaRelative, a_deltaAbsolute, scoreId, 1)
updateActionValue(a_thetaRelative, a_thetaAbsolute, scoreId, 2)
updateActionValue(a_alphaRelative, a_alphaAbsolute, scoreId, 3)
updateActionValue(a_betaRelative, a_betaAbsolute, scoreId, 4)
updateActionValue(a_gammaRelative, a_gammaAbsolute, scoreId, 5)
sql_update_action_score = fmt.Sprintf("update action_score set result = 0,`status` = 1 where id = %d", scoreId)
} else {
//这就没法用了
logger.Warnf(" action_score %d 无可用数据,%s", scoreId, data.Ts)
sql_update_action_score = fmt.Sprintf("update action_score set result = 0,`status` = 0 where id = %d", scoreId)
}
logger.Debugf("%s", sql_update_action_score)
_, err = dbop.MDb.Exec(sql_update_action_score)
if err != nil {
logger.Warnf("%s", err.Error())
}
if data.Last {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"msg": "succ",
})
}
}
/*
计算总体成绩
*/
func AnalyseResult(c *gin.Context) {
type tmp struct {
RecordId int `json:"record_id"`
Desc string `json:"desc,omitempty"`
}
var data tmp
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
logger.Debugf("上传训练记录 %#v", data)
rows, err := dbop.MDb.Query("select result,status,beginTime,endTime from action_score where record_id = ?", data.RecordId)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
defer rows.Close()
var result, status, total_score, total_count int
var beginTime, endTime, s_beg, s_end string
for rows.Next() {
err = rows.Scan(&result, &status, &beginTime, &endTime)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
total_count++
total_score += result
if len(s_beg) == 0 {
s_beg = beginTime
} else {
if beginTime < s_beg {
s_beg = beginTime
}
}
if len(s_end) == 0 {
s_end = endTime
} else {
if endTime > s_end {
s_end = endTime
}
}
}
var grade int
var desc string
if total_count > 0 {
grade = total_score / total_count
} else {
logger.Warnf("record %d 训练全为空", data.RecordId)
}
loc, _ := time.LoadLocation("Local") //重要:获取时区
//logger.Debugf("beg %s,end %s", s_beg, s_end)
var t_beg, t_end int64
if len(s_beg) > 0 {
theTime, _ := time.ParseInLocation("2006-01-02T15:04:05Z", s_beg, loc) //使用模板在对应时区转化为time.time类型
t_beg = theTime.Unix()
}
if len(s_end) > 0 {
theTime, _ := time.ParseInLocation("2006-01-02T15:04:05Z", s_end, loc) //使用模板在对应时区转化为time.time类型
t_end = theTime.Unix()
}
if len(data.Desc) > 0 {
desc = data.Desc
}
logger.Debugf("grade %d,duration:%d,desc:%s,id:%d", grade, t_end-t_beg, desc, data.RecordId)
_, err = dbop.MDb.Exec("update record set `status` = ?,grade = ?,duration=?,`desc`=? where id = ?", 1, grade, t_end-t_beg, desc, data.RecordId)
if err != nil {
logger.Debug(err.Error())
c.JSON(http.StatusOK, gin.H{
"code": -1,
"msg": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"msg": "succ",
})
}