1
This commit is contained in:
parent
9e26a28cb8
commit
da1a866cd8
@ -9,6 +9,7 @@ import (
|
||||
"knowledge/api/service"
|
||||
"knowledge/global"
|
||||
"knowledge/utils"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Question struct{}
|
||||
@ -83,3 +84,107 @@ func (r *Question) AddQuestion(c *gin.Context) {
|
||||
|
||||
responses.Ok(c)
|
||||
}
|
||||
|
||||
// AddQuestionTest 新增题目
|
||||
func (r *Question) AddQuestionTest(c *gin.Context) {
|
||||
questionRequest := requests.QuestionRequest{}
|
||||
req := questionRequest.AddQuestionTest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := global.Validate.Struct(req); err != nil {
|
||||
responses.FailWithMessage(utils.Translate(err), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 业务处理
|
||||
questionService := service.QuestionService{}
|
||||
_, err := questionService.AddQuestionTest(req)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
responses.Ok(c)
|
||||
}
|
||||
|
||||
// PutQuestion 修改题目
|
||||
func (r *Question) PutQuestion(c *gin.Context) {
|
||||
couponRequest := requests.QuestionRequest{}
|
||||
req := couponRequest.PutQuestion
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := global.Validate.Struct(req); err != nil {
|
||||
responses.FailWithMessage(utils.Translate(err), c)
|
||||
return
|
||||
}
|
||||
|
||||
id := c.Param("question_id")
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 将 id 转换为 int64 类型
|
||||
questionId, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
responses.Fail(c)
|
||||
return
|
||||
}
|
||||
|
||||
// 业务处理
|
||||
questionService := service.QuestionService{}
|
||||
_, err = questionService.PutQuestion(questionId, req)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
responses.Ok(c)
|
||||
}
|
||||
|
||||
// PutQuestionTest 修改题目
|
||||
func (r *Question) PutQuestionTest(c *gin.Context) {
|
||||
couponRequest := requests.QuestionRequest{}
|
||||
req := couponRequest.PutQuestionTest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
// 参数验证
|
||||
if err := global.Validate.Struct(req); err != nil {
|
||||
responses.FailWithMessage(utils.Translate(err), c)
|
||||
return
|
||||
}
|
||||
|
||||
id := c.Param("question_id")
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
// 将 id 转换为 int64 类型
|
||||
questionId, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
responses.Fail(c)
|
||||
return
|
||||
}
|
||||
|
||||
// 业务处理
|
||||
questionService := service.QuestionService{}
|
||||
_, err = questionService.PutQuestionTest(questionId, req)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
responses.Ok(c)
|
||||
}
|
||||
|
||||
@ -12,9 +12,8 @@ import (
|
||||
type QuestionDao struct {
|
||||
}
|
||||
|
||||
// GetQuestionFirstById 获取数据-id
|
||||
// roleId 用户id
|
||||
func (r *QuestionDao) GetQuestionFirstById(questionId int64) (m *model.Question, err error) {
|
||||
// GetQuestionById 获取数据-id
|
||||
func (r *QuestionDao) GetQuestionById(questionId int64) (m *model.Question, err error) {
|
||||
err = global.Db.First(&m, questionId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -36,6 +36,14 @@ func (r *QuestionOptionDao) DeleteQuestionOptionById(tx *gorm.DB, OptionId int64
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteQuestionOptionByQuestionId 删除-题目id
|
||||
func (r *QuestionOptionDao) DeleteQuestionOptionByQuestionId(tx *gorm.DB, questionId int64) error {
|
||||
if err := tx.Where("question_id = ?", questionId).Delete(&model.QuestionOption{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditQuestionOption 修改
|
||||
func (r *QuestionOptionDao) EditQuestionOption(tx *gorm.DB, maps interface{}, data interface{}) error {
|
||||
err := tx.Model(&model.QuestionOption{}).Where(maps).Updates(data).Error
|
||||
|
||||
@ -3,6 +3,9 @@ package requests
|
||||
type QuestionRequest struct {
|
||||
GetQuestionPage // 获取题目列表-分页
|
||||
AddQuestion // 新增题目
|
||||
AddQuestionTest // 新增题目
|
||||
PutQuestion // 修改题目
|
||||
PutQuestionTest // 修改题目
|
||||
}
|
||||
|
||||
// GetQuestionPage 获取题目列表-分页
|
||||
@ -41,3 +44,55 @@ type AddQuestion struct {
|
||||
QuestionImage []string `json:"question_image" form:"question_image" label:"图片"`
|
||||
QuestionOption []string `json:"question_option" form:"question_option" label:"选项"`
|
||||
}
|
||||
|
||||
// AddQuestionTest 新增题目
|
||||
type AddQuestionTest struct {
|
||||
QuestionName string `json:"question_name" form:"question_name" validate:"required" label:"题目名称"`
|
||||
QuestionType string `json:"question_type" form:"question_type" validate:"required,oneof=1 2 3 4" label:"题目类型"` // 题目类型(1:单选 2:多选 3:问答 4:判断)
|
||||
QuestionStatus string `json:"question_status" form:"question_status" validate:"required,oneof=1 2" label:"状态"` // 状态(1:正常 2:禁用)
|
||||
QuestionAnswer string `json:"question_answer" form:"question_answer" validate:"required" label:"答案"`
|
||||
QuestionAnalysis string `json:"question_analysis" form:"question_analysis" label:"解析"`
|
||||
Difficulty string `json:"difficulty" form:"difficulty" validate:"required,oneof=1 2 3" label:"难度"`
|
||||
FirstLabelId string `json:"first_label_id" form:"first_label_id" validate:"required" label:"一级标签id"`
|
||||
SecondLabelId string `json:"second_label_id" form:"second_label_id" label:"二级标签id"`
|
||||
QuestionImage *string `json:"question_image" form:"question_image" label:"图片"`
|
||||
QuestionOption []string `json:"question_option" form:"question_option" label:"选项"`
|
||||
Input1 *string `json:"input1" form:"input1" label:"选项1"`
|
||||
Input2 *string `json:"input2" form:"input2" label:"选项2"`
|
||||
Input3 *string `json:"input3" form:"input3" label:"选项3"`
|
||||
Input4 *string `json:"input4" form:"input4" label:"选项4"`
|
||||
Input5 *string `json:"input5" form:"input5" label:"选项5"`
|
||||
}
|
||||
|
||||
// PutQuestion 修改题目
|
||||
type PutQuestion struct {
|
||||
QuestionName string `json:"question_name" form:"question_name" validate:"required" label:"题目名称"`
|
||||
QuestionType int `json:"question_type" form:"question_type" validate:"required,oneof=1 2 3 4" label:"题目类型"` // 题目类型(1:单选 2:多选 3:问答 4:判断)
|
||||
QuestionStatus int `json:"question_status" form:"question_status" validate:"required,oneof=1 2" label:"状态"` // 状态(1:正常 2:禁用)
|
||||
QuestionAnswer string `json:"question_answer" form:"question_answer" validate:"required" label:"答案"`
|
||||
QuestionAnalysis string `json:"question_analysis" form:"question_analysis" label:"解析"`
|
||||
Difficulty int `json:"difficulty" form:"difficulty" validate:"required,oneof=1 2 3" label:"难度"`
|
||||
FirstLabelId string `json:"first_label_id" form:"first_label_id" validate:"required" label:"一级标签id"`
|
||||
SecondLabelId string `json:"second_label_id" form:"second_label_id" label:"二级标签id"`
|
||||
QuestionImage []string `json:"question_image" form:"question_image" label:"图片"`
|
||||
QuestionOption []string `json:"question_option" form:"question_option" label:"选项"`
|
||||
}
|
||||
|
||||
// PutQuestionTest 修改题目
|
||||
type PutQuestionTest struct {
|
||||
QuestionName string `json:"question_name" form:"question_name" validate:"required" label:"题目名称"`
|
||||
QuestionType string `json:"question_type" form:"question_type" validate:"required,oneof=1 2 3 4" label:"题目类型"` // 题目类型(1:单选 2:多选 3:问答 4:判断)
|
||||
QuestionStatus string `json:"question_status" form:"question_status" validate:"required,oneof=1 2" label:"状态"` // 状态(1:正常 2:禁用)
|
||||
QuestionAnswer string `json:"question_answer" form:"question_answer" validate:"required" label:"答案"`
|
||||
QuestionAnalysis string `json:"question_analysis" form:"question_analysis" label:"解析"`
|
||||
Difficulty string `json:"difficulty" form:"difficulty" validate:"required,oneof=1 2 3" label:"难度"`
|
||||
FirstLabelId string `json:"first_label_id" form:"first_label_id" validate:"required" label:"一级标签id"`
|
||||
SecondLabelId string `json:"second_label_id" form:"second_label_id" label:"二级标签id"`
|
||||
QuestionImage *string `json:"question_image" form:"question_image" label:"图片"`
|
||||
QuestionOption []string `json:"question_option" form:"question_option" label:"选项"`
|
||||
Input1 *string `json:"input1" form:"input1" label:"选项1"`
|
||||
Input2 *string `json:"input2" form:"input2" label:"选项2"`
|
||||
Input3 *string `json:"input3" form:"input3" label:"选项3"`
|
||||
Input4 *string `json:"input4" form:"input4" label:"选项4"`
|
||||
Input5 *string `json:"input5" form:"input5" label:"选项5"`
|
||||
}
|
||||
|
||||
@ -84,6 +84,15 @@ func publicRouter(r *gin.Engine, api controller.Api) {
|
||||
|
||||
// 登陆
|
||||
adminGroup.POST("/login", api.AdminUser.Login)
|
||||
|
||||
// 新增题目
|
||||
adminGroup.POST("/login2", api.AdminUser.Login)
|
||||
|
||||
// 新增题目
|
||||
adminGroup.POST("/question/test", api.Question.AddQuestionTest)
|
||||
|
||||
// 修改题目
|
||||
adminGroup.PUT("/question/test/:question_id", api.Question.PutQuestionTest)
|
||||
}
|
||||
|
||||
// adminRouter 公共路由-验证权限
|
||||
@ -108,6 +117,9 @@ func privateRouter(r *gin.Engine, api controller.Api) {
|
||||
|
||||
// 新增题目
|
||||
questionGroup.POST("", api.Question.AddQuestion)
|
||||
|
||||
// 修改题目
|
||||
questionGroup.PUT("/:question_id", api.Question.PutQuestion)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,6 +2,8 @@ package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"knowledge/api/dao"
|
||||
"knowledge/api/model"
|
||||
"knowledge/api/requests"
|
||||
@ -120,3 +122,586 @@ func (r *QuestionService) AddQuestion(req requests.AddQuestion) (bool, error) {
|
||||
//tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// AddQuestionTest 新增题目
|
||||
func (r *QuestionService) AddQuestionTest(req requests.AddQuestionTest) (bool, error) {
|
||||
// 验证一级标签
|
||||
firstLabelId, err := strconv.ParseInt(req.FirstLabelId, 10, 64)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
labelDao := dao.LabelDao{}
|
||||
_, err = labelDao.GetLabelFirstById(firstLabelId)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// 验证二级标签
|
||||
var secondLabelId int64
|
||||
if req.SecondLabelId != "" {
|
||||
secondLabelId, err = strconv.ParseInt(req.SecondLabelId, 10, 64)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
_, err = labelDao.GetLabelFirstById(secondLabelId)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
// 处理图片
|
||||
var questionImage string
|
||||
if req.QuestionImage != nil {
|
||||
questionImage = *req.QuestionImage
|
||||
|
||||
if strings.HasSuffix(questionImage, "|") {
|
||||
questionImage = questionImage[:len(questionImage)-1]
|
||||
}
|
||||
|
||||
if strings.HasSuffix(questionImage, "|") {
|
||||
questionImage = questionImage[:len(questionImage)-1]
|
||||
}
|
||||
|
||||
questionImages := strings.Split(questionImage, "|")
|
||||
if len(questionImages) > 0 {
|
||||
result := make([]string, len(questionImages))
|
||||
for i, url := range questionImages {
|
||||
result[i] = utils.RemoveOssDomain(url)
|
||||
}
|
||||
|
||||
questionImage = strings.Join(result, ",")
|
||||
}
|
||||
}
|
||||
|
||||
// 判断选项
|
||||
if req.QuestionType == "1" || req.QuestionType == "2" {
|
||||
if req.Input1 == nil {
|
||||
return false, errors.New("请填入选项")
|
||||
}
|
||||
}
|
||||
|
||||
// 验证重复
|
||||
questionDao := dao.QuestionDao{}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
maps["question_name"] = req.QuestionName
|
||||
question, _ := questionDao.GetQuestion(maps)
|
||||
if question != nil {
|
||||
return false, errors.New("题目名称重复")
|
||||
}
|
||||
|
||||
questionType, err := strconv.Atoi(req.QuestionType)
|
||||
if err != nil {
|
||||
return false, errors.New("题目类型错误")
|
||||
}
|
||||
|
||||
questionStatus, err := strconv.Atoi(req.QuestionStatus)
|
||||
if err != nil {
|
||||
return false, errors.New("题目状态错误")
|
||||
}
|
||||
|
||||
difficulty, err := strconv.Atoi(req.Difficulty)
|
||||
if err != nil {
|
||||
return false, errors.New("题目难度错误")
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
// 新增题目
|
||||
question = &model.Question{
|
||||
QuestionName: req.QuestionName,
|
||||
QuestionType: questionType,
|
||||
QuestionStatus: questionStatus,
|
||||
QuestionSource: 1,
|
||||
QuestionImage: questionImage,
|
||||
QuestionAnswer: req.QuestionAnswer,
|
||||
QuestionAnalysis: req.QuestionAnalysis,
|
||||
Difficulty: difficulty,
|
||||
FirstLabelId: &firstLabelId,
|
||||
SecondLabelId: nil,
|
||||
}
|
||||
|
||||
if req.SecondLabelId != "" {
|
||||
question.SecondLabelId = &secondLabelId
|
||||
}
|
||||
|
||||
question, err = questionDao.AddQuestion(tx, question)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
|
||||
// 新增选项
|
||||
questionOptionDao := dao.QuestionOptionDao{}
|
||||
if req.QuestionType == "1" || req.QuestionType == "2" {
|
||||
if req.Input1 != nil {
|
||||
questionOption := &model.QuestionOption{
|
||||
QuestionId: question.QuestionId,
|
||||
OptionValue: *req.Input1,
|
||||
}
|
||||
|
||||
questionOption, err := questionOptionDao.AddQuestionOption(tx, questionOption)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
}
|
||||
|
||||
if req.Input2 != nil {
|
||||
questionOption := &model.QuestionOption{
|
||||
QuestionId: question.QuestionId,
|
||||
OptionValue: *req.Input2,
|
||||
}
|
||||
|
||||
questionOption, err := questionOptionDao.AddQuestionOption(tx, questionOption)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
}
|
||||
|
||||
if req.Input3 != nil {
|
||||
questionOption := &model.QuestionOption{
|
||||
QuestionId: question.QuestionId,
|
||||
OptionValue: *req.Input3,
|
||||
}
|
||||
|
||||
questionOption, err := questionOptionDao.AddQuestionOption(tx, questionOption)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
}
|
||||
|
||||
if req.Input4 != nil {
|
||||
questionOption := &model.QuestionOption{
|
||||
QuestionId: question.QuestionId,
|
||||
OptionValue: *req.Input4,
|
||||
}
|
||||
|
||||
questionOption, err := questionOptionDao.AddQuestionOption(tx, questionOption)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
}
|
||||
|
||||
if req.Input5 != nil {
|
||||
questionOption := &model.QuestionOption{
|
||||
QuestionId: question.QuestionId,
|
||||
OptionValue: *req.Input5,
|
||||
}
|
||||
|
||||
questionOption, err := questionOptionDao.AddQuestionOption(tx, questionOption)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// PutQuestion 修改题目
|
||||
func (r *QuestionService) PutQuestion(questionId int64, req requests.PutQuestion) (bool, error) {
|
||||
questionDao := dao.QuestionDao{}
|
||||
|
||||
question, err := questionDao.GetQuestionById(questionId)
|
||||
if err != nil {
|
||||
return false, errors.New("题目不存在")
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
questionData := make(map[string]interface{})
|
||||
|
||||
// 题目名称
|
||||
if req.QuestionName != question.QuestionName {
|
||||
questionData["question_name"] = req.QuestionName
|
||||
}
|
||||
|
||||
// 题目类型
|
||||
if req.QuestionType != question.QuestionType {
|
||||
questionData["question_type"] = req.QuestionType
|
||||
}
|
||||
|
||||
// 状态
|
||||
if req.QuestionStatus != question.QuestionStatus {
|
||||
questionData["question_status"] = req.QuestionStatus
|
||||
}
|
||||
|
||||
// 答案
|
||||
if req.QuestionAnswer != question.QuestionAnswer {
|
||||
questionData["question_answer"] = req.QuestionAnswer
|
||||
}
|
||||
|
||||
// 解析
|
||||
if req.QuestionAnalysis != question.QuestionAnalysis {
|
||||
questionData["question_analysis"] = req.QuestionAnalysis
|
||||
}
|
||||
|
||||
// 难度
|
||||
if req.Difficulty != question.Difficulty {
|
||||
questionData["difficulty"] = req.Difficulty
|
||||
}
|
||||
|
||||
// 验证一级标签
|
||||
firstLabelId, err := strconv.ParseInt(req.FirstLabelId, 10, 64)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if firstLabelId != *question.FirstLabelId {
|
||||
labelDao := dao.LabelDao{}
|
||||
_, err = labelDao.GetLabelFirstById(firstLabelId)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
questionData["first_label_id"] = firstLabelId
|
||||
}
|
||||
|
||||
// 验证二级标签
|
||||
if req.SecondLabelId == "" {
|
||||
if question.SecondLabelId != nil {
|
||||
questionData["second_label_id"] = gorm.Expr("NULL")
|
||||
}
|
||||
} else {
|
||||
// 验证二级标签
|
||||
if req.SecondLabelId != "" {
|
||||
secondLabelId, err := strconv.ParseInt(req.SecondLabelId, 10, 64)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if &firstLabelId != question.FirstLabelId {
|
||||
labelDao := dao.LabelDao{}
|
||||
_, err = labelDao.GetLabelFirstById(secondLabelId)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
questionData["first_label_id"] = firstLabelId
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// 图片
|
||||
if len(req.QuestionImage) > 0 {
|
||||
result := make([]string, len(req.QuestionImage))
|
||||
for i, url := range req.QuestionImage {
|
||||
result[i] = utils.RemoveOssDomain(url)
|
||||
}
|
||||
|
||||
questionData["question_image"] = strings.Join(result, ",")
|
||||
}
|
||||
|
||||
// 验证重复
|
||||
if req.QuestionName != question.QuestionName {
|
||||
maps := make(map[string]interface{})
|
||||
maps["question_name"] = req.QuestionName
|
||||
res, _ := questionDao.GetQuestion(maps)
|
||||
if res != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("题目名称重复")
|
||||
}
|
||||
}
|
||||
|
||||
// 删除所有选项
|
||||
if question.QuestionType == 1 || question.QuestionType == 2 {
|
||||
questionOptionDao := dao.QuestionOptionDao{}
|
||||
err = questionOptionDao.DeleteQuestionOptionByQuestionId(tx, question.QuestionId)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if req.QuestionType == 1 || req.QuestionType == 2 {
|
||||
// 新增选项
|
||||
questionOptionDao := dao.QuestionOptionDao{}
|
||||
for _, s := range req.QuestionOption {
|
||||
questionOption := &model.QuestionOption{
|
||||
QuestionId: question.QuestionId,
|
||||
OptionValue: s,
|
||||
}
|
||||
|
||||
questionOption, err := questionOptionDao.AddQuestionOption(tx, questionOption)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("修改失败")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = questionDao.EditQuestionById(tx, question.QuestionId, questionData)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New(err.Error())
|
||||
}
|
||||
|
||||
//tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// PutQuestionTest 修改题目
|
||||
func (r *QuestionService) PutQuestionTest(questionId int64, req requests.PutQuestionTest) (bool, error) {
|
||||
questionDao := dao.QuestionDao{}
|
||||
|
||||
question, err := questionDao.GetQuestionById(questionId)
|
||||
if err != nil {
|
||||
return false, errors.New("题目不存在")
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
fmt.Println(r)
|
||||
}
|
||||
}()
|
||||
|
||||
questionData := make(map[string]interface{})
|
||||
|
||||
// 题目名称
|
||||
if req.QuestionName != question.QuestionName {
|
||||
questionData["question_name"] = req.QuestionName
|
||||
}
|
||||
|
||||
// 题目类型
|
||||
if req.QuestionType != "" {
|
||||
questionType, err := strconv.Atoi(req.QuestionType)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("题目类型错误")
|
||||
}
|
||||
|
||||
if questionType != question.QuestionType {
|
||||
questionData["question_type"] = req.QuestionType
|
||||
}
|
||||
}
|
||||
|
||||
// 状态
|
||||
if req.QuestionStatus != "" {
|
||||
questionStatus, err := strconv.Atoi(req.QuestionStatus)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("题目状态错误")
|
||||
}
|
||||
|
||||
if questionStatus != question.QuestionStatus {
|
||||
questionData["question_status"] = req.QuestionStatus
|
||||
}
|
||||
}
|
||||
|
||||
// 答案
|
||||
if req.QuestionAnswer != question.QuestionAnswer {
|
||||
questionData["question_answer"] = req.QuestionAnswer
|
||||
}
|
||||
|
||||
// 解析
|
||||
if req.QuestionAnalysis != question.QuestionAnalysis {
|
||||
questionData["question_analysis"] = req.QuestionAnalysis
|
||||
}
|
||||
|
||||
// 难度
|
||||
if req.Difficulty != "" {
|
||||
difficulty, err := strconv.Atoi(req.Difficulty)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("题目难度错误")
|
||||
}
|
||||
|
||||
if difficulty != question.Difficulty {
|
||||
questionData["difficulty"] = req.Difficulty
|
||||
}
|
||||
}
|
||||
|
||||
// 验证一级标签
|
||||
firstLabelId, err := strconv.ParseInt(req.FirstLabelId, 10, 64)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
if firstLabelId != *question.FirstLabelId {
|
||||
labelDao := dao.LabelDao{}
|
||||
_, err = labelDao.GetLabelFirstById(firstLabelId)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
questionData["first_label_id"] = firstLabelId
|
||||
}
|
||||
|
||||
// 验证二级标签
|
||||
if req.SecondLabelId == "" {
|
||||
if question.SecondLabelId != nil {
|
||||
questionData["second_label_id"] = gorm.Expr("NULL")
|
||||
}
|
||||
} else {
|
||||
// 验证二级标签
|
||||
if req.SecondLabelId != "" {
|
||||
secondLabelId, err := strconv.ParseInt(req.SecondLabelId, 10, 64)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
if &secondLabelId != question.SecondLabelId {
|
||||
labelDao := dao.LabelDao{}
|
||||
_, err := labelDao.GetLabelFirstById(secondLabelId)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
questionData["second_label_id"] = secondLabelId
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 图片
|
||||
if req.QuestionImage != nil {
|
||||
questionImage := *req.QuestionImage
|
||||
|
||||
if strings.HasSuffix(questionImage, "|") {
|
||||
questionImage = questionImage[:len(questionImage)-1]
|
||||
}
|
||||
|
||||
if strings.HasSuffix(questionImage, ",") {
|
||||
questionImage = questionImage[:len(questionImage)-1]
|
||||
}
|
||||
|
||||
questionImages := strings.Split(questionImage, "|")
|
||||
if len(questionImages) > 0 {
|
||||
result := make([]string, len(questionImages))
|
||||
for i, url := range questionImages {
|
||||
result[i] = utils.RemoveOssDomain(url)
|
||||
}
|
||||
|
||||
questionImage = strings.Join(result, ",")
|
||||
if questionImage != question.QuestionImage {
|
||||
questionData["question_image"] = questionImage
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 验证重复
|
||||
if req.QuestionName != question.QuestionName {
|
||||
maps := make(map[string]interface{})
|
||||
maps["question_name"] = req.QuestionName
|
||||
res, _ := questionDao.GetQuestion(maps)
|
||||
if res != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("题目名称重复")
|
||||
}
|
||||
}
|
||||
|
||||
// 删除所有选项
|
||||
if question.QuestionType == 1 || question.QuestionType == 2 {
|
||||
questionOptionDao := dao.QuestionOptionDao{}
|
||||
err = questionOptionDao.DeleteQuestionOptionByQuestionId(tx, question.QuestionId)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if req.QuestionType == "1" || req.QuestionType == "2" {
|
||||
// 新增选项
|
||||
questionOptionDao := dao.QuestionOptionDao{}
|
||||
if req.Input1 != nil {
|
||||
questionOption := &model.QuestionOption{
|
||||
QuestionId: question.QuestionId,
|
||||
OptionValue: *req.Input1,
|
||||
}
|
||||
|
||||
questionOption, err := questionOptionDao.AddQuestionOption(tx, questionOption)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("修改失败")
|
||||
}
|
||||
}
|
||||
|
||||
if req.Input2 != nil {
|
||||
questionOption := &model.QuestionOption{
|
||||
QuestionId: question.QuestionId,
|
||||
OptionValue: *req.Input2,
|
||||
}
|
||||
|
||||
questionOption, err := questionOptionDao.AddQuestionOption(tx, questionOption)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("修改失败")
|
||||
}
|
||||
}
|
||||
|
||||
if req.Input3 != nil {
|
||||
questionOption := &model.QuestionOption{
|
||||
QuestionId: question.QuestionId,
|
||||
OptionValue: *req.Input3,
|
||||
}
|
||||
|
||||
questionOption, err := questionOptionDao.AddQuestionOption(tx, questionOption)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("修改失败")
|
||||
}
|
||||
}
|
||||
|
||||
if req.Input4 != nil {
|
||||
questionOption := &model.QuestionOption{
|
||||
QuestionId: question.QuestionId,
|
||||
OptionValue: *req.Input4,
|
||||
}
|
||||
|
||||
questionOption, err := questionOptionDao.AddQuestionOption(tx, questionOption)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("修改失败")
|
||||
}
|
||||
}
|
||||
|
||||
if req.Input5 != nil {
|
||||
questionOption := &model.QuestionOption{
|
||||
QuestionId: question.QuestionId,
|
||||
OptionValue: *req.Input5,
|
||||
}
|
||||
|
||||
questionOption, err := questionOptionDao.AddQuestionOption(tx, questionOption)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("修改失败")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = questionDao.EditQuestionById(tx, question.QuestionId, questionData)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New(err.Error())
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ mysql:
|
||||
username: root
|
||||
password: 'gdxz123456^%$d'
|
||||
port: 30001
|
||||
db-name: knowledge_base
|
||||
db-name: knowledge_admin
|
||||
max-idle-cons: 5
|
||||
max-open-cons: 20
|
||||
debug: true
|
||||
@ -38,4 +38,4 @@ oss:
|
||||
oss-access-key-secret: q1aiIZCJJuf92YbKk2cSXnPES4zx26
|
||||
oss-bucket: dev-knowledge
|
||||
oss-endpoint: oss-cn-chengdu.aliyuncs.com
|
||||
oss-custom-domain-name: https://img.applets.igandanyiyuan.com
|
||||
oss-custom-domain-name: https://dev-knowledge.oss-cn-beijing.aliyuncs.com
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user