You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.3 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package qatool
import (
"autogo/controllers"
"autogo/dbsql"
"net/http"
"github.com/gin-gonic/gin"
)
type Voice struct {
ID int `json:"id" gorm:"primary_key"`
FormID string `json:"form_id" gorm:"column:form_id"`
GroupID int `json:"group_id" gorm:"column:group_id"`
GroupName string `json:"group_name" gorm:"column:group_name"`
Filename string `json:"filename" gorm:"column:filename"`
FileURL string `json:"file_url" gorm:"column:file_url"`
IsDel int `json:"-" gorm:"column:is_del"`
}
func (t *Voice) TableName() string {
return "voice"
}
type VoiceGroup struct {
ID int `json:"id" gorm:"primary_key"`
Name string `json:"name"`
List []Voice `json:"list"`
}
// @Tags 工具相关 /api/tool/v1/
// @Summary 获取声音评测内容
// @Description 根据表单id获取声音评测内容返回分组和分组文件信息
// @accept x-www-form-urlencoded
// @Param form_id query string true "表单id"
// @Success 200 {object} models.Response "返回分组和分组文件信息"
// @Router /api/tool/v1/voice/list [get]
func GetVoiceTestData(c *gin.Context) {
rsp := controllers.NewResponse()
form_id := c.DefaultQuery("form_id", "")
if form_id == "" {
c.JSON(http.StatusOK, rsp.Error("form_id error"))
return
}
db, err := dbsql.GetConn(dbsql.DSN)
if err != nil {
c.JSON(http.StatusOK, rsp.Error(err.Error()))
return
}
defer dbsql.Close(db)
var list []Voice
db.Table("voice").Model(Voice{}).Where("form_id = ?", form_id).Find(&list)
if len(list) == 0 {
c.JSON(http.StatusOK, rsp.Error("该表单id下无分组数据"))
return
}
m := make(map[int]VoiceGroup)
for _, v := range list {
if group, ok := m[v.GroupID]; ok {
group.List = append(group.List, v)
m[v.GroupID] = group
} else {
var _group VoiceGroup
_group.ID = v.GroupID
_group.Name = v.GroupName
_group.List = append(_group.List, v)
m[v.GroupID] = _group
}
}
var groups []VoiceGroup
for _, v := range m {
groups = append(groups, v)
}
bubbleSort(&groups)
rsp.Success()
rsp.Data = groups
c.JSON(http.StatusOK, rsp)
}
func bubbleSort(arr *[]VoiceGroup) {
n := len(*arr)
for i := 0; i < n-1; i++ {
for j := 0; j < n-i-1; j++ {
if (*arr)[j].ID > (*arr)[j+1].ID {
(*arr)[j], (*arr)[j+1] = (*arr)[j+1], (*arr)[j]
}
}
}
}