181 lines
5.6 KiB
Go
181 lines
5.6 KiB
Go
package dto
|
||
|
||
import (
|
||
"fmt"
|
||
"knowledge/api/model"
|
||
"knowledge/utils"
|
||
"strings"
|
||
)
|
||
|
||
// QuestionDto 题目表(单选-多选-问答-判断)
|
||
type QuestionDto struct {
|
||
QuestionId string `json:"question_id"` // 主键id
|
||
QuestionName string `json:"question_name"` // 题目名称
|
||
QuestionType int `json:"question_type"` // 题目类型(1:单选 2:多选 3:问答 4:判断)
|
||
QuestionStatus int `json:"question_status"` // 状态(1:正常 2:禁用)
|
||
IsDelete int `json:"is_delete"` // 是否删除(0:否 1:是)
|
||
QuestionSource int `json:"question_source"` // 题目来源(1:本题库 2:外部数据)
|
||
QuestionImage []string `json:"question_image"` // 题目图片(逗号分隔)
|
||
QuestionAnswer []string `json:"question_answer"` // 答案
|
||
QuestionAnalysis string `json:"question_analysis"` // 解析
|
||
Difficulty int `json:"difficulty"` // 难度(0:未知 1:低 2:中 3:高)
|
||
FirstLabelId string `json:"first_label_id"` // 一级标签id
|
||
SecondLabelId string `json:"second_label_id"` // 二级标签id
|
||
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||
UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间
|
||
FirstLabel *LabelDto `json:"first_label"` // 一级标签
|
||
SecondLabel *LabelDto `json:"second_label"` // 二级标签
|
||
QuestionOption []*QuestionOptionDto `json:"question_option"` // 选项
|
||
}
|
||
|
||
// GetQuestionDto 题目详情
|
||
func GetQuestionDto(m *model.Question) *QuestionDto {
|
||
return &QuestionDto{
|
||
QuestionId: fmt.Sprintf("%d", m.QuestionId),
|
||
QuestionName: m.QuestionName,
|
||
QuestionType: m.QuestionType,
|
||
QuestionStatus: m.QuestionStatus,
|
||
IsDelete: m.IsDelete,
|
||
QuestionSource: m.QuestionSource,
|
||
QuestionImage: []string{},
|
||
QuestionAnswer: []string{},
|
||
QuestionAnalysis: m.QuestionAnalysis,
|
||
Difficulty: m.Difficulty,
|
||
FirstLabelId: fmt.Sprintf("%d", *m.FirstLabelId),
|
||
SecondLabelId: fmt.Sprintf("%d", *m.SecondLabelId),
|
||
CreatedAt: m.CreatedAt,
|
||
UpdatedAt: m.UpdatedAt,
|
||
}
|
||
}
|
||
|
||
// GetQuestionListDto 题目列表
|
||
func GetQuestionListDto(m []*model.Question) []*QuestionDto {
|
||
// 处理返回值
|
||
responses := make([]*QuestionDto, len(m))
|
||
|
||
if len(m) > 0 {
|
||
for i, v := range m {
|
||
response := &QuestionDto{
|
||
QuestionId: fmt.Sprintf("%d", v.QuestionId),
|
||
QuestionName: v.QuestionName,
|
||
QuestionType: v.QuestionType,
|
||
QuestionStatus: v.QuestionStatus,
|
||
IsDelete: v.IsDelete,
|
||
QuestionSource: v.QuestionSource,
|
||
QuestionImage: []string{},
|
||
QuestionAnswer: []string{},
|
||
QuestionAnalysis: v.QuestionAnalysis,
|
||
Difficulty: v.Difficulty,
|
||
FirstLabelId: fmt.Sprintf("%d", *v.FirstLabelId),
|
||
SecondLabelId: fmt.Sprintf("%d", *v.SecondLabelId),
|
||
CreatedAt: v.CreatedAt,
|
||
UpdatedAt: v.UpdatedAt,
|
||
}
|
||
|
||
// 加载一级标签
|
||
if v.FirstLabel != nil {
|
||
response = response.LoadFirstLabel(v.FirstLabel)
|
||
}
|
||
|
||
// 加载二级标签
|
||
if v.SecondLabel != nil {
|
||
response = response.LoadSecondLabel(v.SecondLabel)
|
||
}
|
||
|
||
// 加载图片
|
||
if v.QuestionImage != "" {
|
||
response = response.LoadQuestionImage(v.QuestionImage)
|
||
}
|
||
|
||
// 加载答案
|
||
if v.QuestionAnswer != "" {
|
||
response = response.LoadQuestionAnswer(v)
|
||
}
|
||
|
||
// 将转换后的结构体添加到新切片中
|
||
responses[i] = response
|
||
}
|
||
}
|
||
|
||
return responses
|
||
}
|
||
|
||
// GetShareQuestionDto 题目详情-分享
|
||
func GetShareQuestionDto(m *model.Question) *QuestionDto {
|
||
return &QuestionDto{
|
||
QuestionId: fmt.Sprintf("%d", m.QuestionId),
|
||
QuestionName: m.QuestionName,
|
||
QuestionType: m.QuestionType,
|
||
QuestionSource: m.QuestionSource,
|
||
QuestionImage: []string{},
|
||
QuestionAnswer: []string{},
|
||
QuestionAnalysis: m.QuestionAnalysis,
|
||
Difficulty: m.Difficulty,
|
||
CreatedAt: m.CreatedAt,
|
||
UpdatedAt: m.UpdatedAt,
|
||
}
|
||
}
|
||
|
||
// LoadFirstLabel 加载一级标签
|
||
func (r *QuestionDto) LoadFirstLabel(m *model.Label) *QuestionDto {
|
||
if m != nil {
|
||
r.FirstLabel = GetLabelDto(m)
|
||
}
|
||
return r
|
||
}
|
||
|
||
// LoadSecondLabel 加载二级标签
|
||
func (r *QuestionDto) LoadSecondLabel(m *model.Label) *QuestionDto {
|
||
if m != nil {
|
||
r.SecondLabel = GetLabelDto(m)
|
||
}
|
||
return r
|
||
}
|
||
|
||
// LoadQuestionOption 加载选项
|
||
func (r *QuestionDto) LoadQuestionOption(m []*model.QuestionOption) *QuestionDto {
|
||
if len(m) > 0 {
|
||
r.QuestionOption = GetQuestionOptionListDto(m)
|
||
}
|
||
return r
|
||
}
|
||
|
||
// LoadQuestionOptionSimplify 加载选项-简化
|
||
func (r *QuestionDto) LoadQuestionOptionSimplify(m []*model.QuestionOption) *QuestionDto {
|
||
if len(m) > 0 {
|
||
r.QuestionOption = GetQuestionOptionSimplifyListDto(m)
|
||
}
|
||
return r
|
||
}
|
||
|
||
// LoadQuestionImage 加载图片
|
||
func (r *QuestionDto) LoadQuestionImage(s string) *QuestionDto {
|
||
if s != "" {
|
||
result := strings.Split(s, ",")
|
||
if len(result) > 0 {
|
||
for _, v := range result {
|
||
v = utils.AddOssDomain(v)
|
||
r.QuestionImage = append(r.QuestionImage, v)
|
||
}
|
||
}
|
||
}
|
||
return r
|
||
}
|
||
|
||
// LoadQuestionAnswer 加载答案
|
||
func (r *QuestionDto) LoadQuestionAnswer(m *model.Question) *QuestionDto {
|
||
if m.QuestionAnswer != "" {
|
||
if m.QuestionType == 1 || m.QuestionType == 2 {
|
||
result := strings.Split(m.QuestionAnswer, ",")
|
||
if len(result) > 0 {
|
||
for _, v := range result {
|
||
r.QuestionAnswer = append(r.QuestionAnswer, v)
|
||
}
|
||
}
|
||
} else {
|
||
r.QuestionAnswer[0] = m.QuestionAnswer
|
||
}
|
||
}
|
||
return r
|
||
}
|