package dto import ( "fmt" "knowledge/api/model" ) // 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"` // 二级标签 } // 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, QuestionAnswer: m.QuestionAnswer, 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, QuestionAnswer: v.QuestionAnswer, 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) } // 将转换后的结构体添加到新切片中 responses[i] = response } } return responses } // 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 }