1
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"knowledge/api/dao"
|
||||
"knowledge/api/model"
|
||||
"knowledge/api/requests"
|
||||
"knowledge/global"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type LabelService struct {
|
||||
}
|
||||
|
||||
// AddLabel 新增标签
|
||||
func (r *LabelService) AddLabel(req requests.AddLabel) (bool, error) {
|
||||
labelDao := dao.LabelDao{}
|
||||
|
||||
var parentId int64
|
||||
if req.LabelLevel != 1 {
|
||||
if req.ParentId == "" || req.ParentId == "0" {
|
||||
return false, errors.New("请填写父级标签")
|
||||
}
|
||||
|
||||
parentId, err := strconv.ParseInt(req.ParentId, 10, 64)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
_, err = labelDao.GetLabelFirstById(parentId)
|
||||
if err != nil {
|
||||
return false, errors.New("父级标签错误")
|
||||
}
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
// 新增题目
|
||||
label := &model.Label{
|
||||
LabelName: req.LabelName,
|
||||
ParentId: parentId,
|
||||
LabelLevel: req.LabelLevel,
|
||||
}
|
||||
|
||||
label, err := labelDao.AddLabel(tx, label)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// PutLabel 修改标签
|
||||
func (r *LabelService) PutLabel(labelId int64, req requests.PutLabel) (bool, error) {
|
||||
labelDao := dao.LabelDao{}
|
||||
_, err := labelDao.GetLabelFirstById(labelId)
|
||||
if err != nil {
|
||||
return false, errors.New("标签不存在")
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
labelData := make(map[string]interface{})
|
||||
labelData["label_name"] = req.LabelName
|
||||
|
||||
err = labelDao.EditLabelById(tx, labelId, labelData)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New(err.Error())
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"knowledge/api/dao"
|
||||
"knowledge/api/dto"
|
||||
"knowledge/api/model"
|
||||
"knowledge/api/requests"
|
||||
"knowledge/global"
|
||||
@@ -119,6 +120,29 @@ func (r *QuestionService) AddQuestion(req requests.AddQuestion) (bool, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// 处理题库数据
|
||||
if req.QaId != "" {
|
||||
qaId, err := strconv.ParseInt(req.QaId, 10, 64)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
// 新增题库明细
|
||||
questionQaItemDao := dao.QuestionQaItemDao{}
|
||||
questionQaItem := &model.QuestionQaItem{
|
||||
QaId: qaId,
|
||||
QuestionId: question.QuestionId,
|
||||
IsMustSelect: 0,
|
||||
}
|
||||
|
||||
_, err = questionQaItemDao.AddQuestionQaItem(tx, questionQaItem)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
@@ -746,3 +770,34 @@ func (r *QuestionService) DeleteQuestion(req requests.DeleteQuestion) (bool, err
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GetQuestion 获取题目详情
|
||||
func (r *QuestionService) GetQuestion(questionId int64) (g *dto.QuestionDto, err error) {
|
||||
questionDao := dao.QuestionDao{}
|
||||
question, err := questionDao.GetQuestionPreloadById(questionId)
|
||||
if err != nil {
|
||||
return nil, errors.New("题目不存在")
|
||||
}
|
||||
|
||||
if question.QuestionStatus == 2 {
|
||||
return nil, errors.New("题目已禁用")
|
||||
}
|
||||
|
||||
if question.IsDelete == 1 {
|
||||
return nil, errors.New("题目已删除")
|
||||
}
|
||||
|
||||
// 处理返回值
|
||||
g = dto.GetQuestionDto(question)
|
||||
|
||||
// 加载一级标签
|
||||
g.LoadFirstLabel(question.FirstLabel)
|
||||
|
||||
// 加载二级标签
|
||||
g.LoadSecondLabel(question.SecondLabel)
|
||||
|
||||
// 加载选项
|
||||
g.LoadQuestionOption(question.QuestionOption)
|
||||
|
||||
return g, nil
|
||||
}
|
||||
|
||||
@@ -2,7 +2,10 @@ package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/goccy/go-json"
|
||||
"knowledge/api/dao"
|
||||
"knowledge/api/dto"
|
||||
"knowledge/api/model"
|
||||
"knowledge/api/requests"
|
||||
"knowledge/global"
|
||||
@@ -39,8 +42,8 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
|
||||
return false, errors.New("过期时间需大于当前时间")
|
||||
}
|
||||
|
||||
// 生成分享链接
|
||||
qaLink := ""
|
||||
// 处理明细选题规则
|
||||
itemContent, _ := json.Marshal(req.Item)
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
@@ -58,10 +61,10 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
|
||||
QaRuleContent: req.QaRuleContent,
|
||||
QaDisplayType: req.QaDisplayType,
|
||||
QaExpireTime: model.LocalTime(qaExpireTime),
|
||||
QaLink: qaLink,
|
||||
QaPassword: req.QaPassword,
|
||||
OpenNumber: 0,
|
||||
Image: req.Image,
|
||||
ItemContent: string(itemContent),
|
||||
}
|
||||
|
||||
questionQaDao := dao.QuestionQaDao{}
|
||||
@@ -71,6 +74,21 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
|
||||
// 生成分享链接
|
||||
qaShareId := utils.HashString(fmt.Sprintf("%d", questionQa.QaId))
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
}
|
||||
|
||||
questionQaData := make(map[string]interface{})
|
||||
questionQaData["qa_share_id"] = qaShareId
|
||||
err = questionQaDao.EditQuestionQaById(tx, questionQa.QaId, questionQaData)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("新增失败")
|
||||
}
|
||||
|
||||
// 新增问答题库题目列表
|
||||
questionDao := dao.QuestionDao{}
|
||||
questionQaItemDao := dao.QuestionQaItemDao{}
|
||||
@@ -129,7 +147,7 @@ func (r *QuestionQaService) AddQuestionQa(req requests.AddQuestionQa) (bool, err
|
||||
}
|
||||
|
||||
// 获取随机明细题目
|
||||
questions, err := questionDao.GetQuestionListRand(maps, req.QaQuantity)
|
||||
questions, err := questionDao.GetQuestionListRand(maps, item.Quantity*3)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
@@ -223,6 +241,12 @@ func (r *QuestionQaService) PutQuestionQa(qaId int64, req requests.PutQuestionQa
|
||||
questionQaData["image"] = image
|
||||
}
|
||||
|
||||
// 处理明细选题规则
|
||||
itemContent, _ := json.Marshal(req.Item)
|
||||
if string(itemContent) != questionQa.ItemContent {
|
||||
questionQaData["item_content"] = string(itemContent)
|
||||
}
|
||||
|
||||
// 题目数量
|
||||
if req.QaQuantity != questionQa.QaQuantity {
|
||||
if req.Action == 1 {
|
||||
@@ -298,7 +322,7 @@ func (r *QuestionQaService) PutQuestionQa(qaId int64, req requests.PutQuestionQa
|
||||
}
|
||||
|
||||
// 获取随机明细题目
|
||||
questions, err := questionDao.GetQuestionListRand(maps, req.QaQuantity)
|
||||
questions, err := questionDao.GetQuestionListRand(maps, req.QaQuantity*3)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, err
|
||||
@@ -418,3 +442,62 @@ func (r *QuestionQaService) PutQuestionQaExpire(qaId int64, req requests.PutQues
|
||||
tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// PutQuestionQaRule 修改问答题库规则解释
|
||||
func (r *QuestionQaService) PutQuestionQaRule(qaId int64, req requests.PutQuestionQaRule) (bool, error) {
|
||||
questionQaDao := dao.QuestionQaDao{}
|
||||
questionQa, err := questionQaDao.GetQuestionQaById(qaId)
|
||||
if err != nil {
|
||||
return false, errors.New("题库不存在")
|
||||
}
|
||||
|
||||
if questionQa.QaStatus == 2 {
|
||||
return false, errors.New("题库已失效")
|
||||
}
|
||||
|
||||
// 分享密码
|
||||
if req.QaRuleContent == questionQa.QaRuleContent {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
questionQaData := make(map[string]interface{})
|
||||
questionQaData["qa_rule_content"] = req.QaRuleContent
|
||||
|
||||
err = questionQaDao.EditQuestionQaById(tx, questionQa.QaId, questionQaData)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New(err.Error())
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GetQuestionQa 获取问答题库详情
|
||||
func (r *QuestionQaService) GetQuestionQa(qaId int64) (g *dto.QuestionQaDto, err error) {
|
||||
questionQaDao := dao.QuestionQaDao{}
|
||||
questionQa, err := questionQaDao.GetQuestionQaById(qaId)
|
||||
if err != nil {
|
||||
return nil, errors.New("题库不存在")
|
||||
}
|
||||
|
||||
if questionQa.QaStatus == 2 {
|
||||
return nil, errors.New("题库已失效")
|
||||
}
|
||||
|
||||
// 处理返回值
|
||||
g = dto.GetQuestionQaDto(questionQa)
|
||||
|
||||
// 加载明细选题规则
|
||||
g = g.LoadItemContent(questionQa.ItemContent)
|
||||
|
||||
return g, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"knowledge/api/dao"
|
||||
"knowledge/api/requests"
|
||||
"knowledge/global"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type QuestionQaItemService struct {
|
||||
}
|
||||
|
||||
// DeleteQuestionQaItem 删除问答题库明细
|
||||
func (r *QuestionQaItemService) DeleteQuestionQaItem(req requests.DeleteQuestionQaItem) (bool, error) {
|
||||
questionQaItemDao := dao.QuestionQaItemDao{}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
for _, itemId := range req.ItemId {
|
||||
// 将字符串转换为int64类型
|
||||
itemId, err := strconv.ParseInt(itemId, 10, 64)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("删除失败")
|
||||
}
|
||||
|
||||
_, err = questionQaItemDao.GetQuestionQaItemById(itemId)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("明细不存在")
|
||||
}
|
||||
|
||||
err = questionQaItemDao.DeleteQuestionQaItemById(tx, itemId)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New("删除失败")
|
||||
}
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// PutQuestionQaItemMust 修改必选
|
||||
func (r *QuestionQaItemService) PutQuestionQaItemMust(itemId int64, req requests.PutQuestionQaItemMust) (bool, error) {
|
||||
questionQaItemDao := dao.QuestionQaItemDao{}
|
||||
questionQaItem, err := questionQaItemDao.GetQuestionQaItemById(itemId)
|
||||
if err != nil {
|
||||
return false, errors.New("题目不存在")
|
||||
}
|
||||
|
||||
// 分享密码
|
||||
if req.IsMustSelect == questionQaItem.IsMustSelect {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
tx := global.Db.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
questionQaItemData := make(map[string]interface{})
|
||||
questionQaItemData["is_must_select"] = req.IsMustSelect
|
||||
|
||||
err = questionQaItemDao.EditQuestionQaItemById(tx, itemId, questionQaItemData)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return false, errors.New(err.Error())
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return true, nil
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"knowledge/api/dao"
|
||||
"knowledge/api/dto"
|
||||
"knowledge/api/requests"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ShareService struct {
|
||||
}
|
||||
|
||||
func (r *ShareService) GetShare(req requests.GetShare) (g *dto.ShareDto, err error) {
|
||||
questionQaDao := dao.QuestionQaDao{}
|
||||
questionQaItemDao := dao.QuestionQaItemDao{}
|
||||
questionDao := dao.QuestionDao{}
|
||||
|
||||
// 获取题库数据
|
||||
maps := make(map[string]interface{})
|
||||
maps["qa_share_id"] = req.QaShareId
|
||||
questionQa, err := questionQaDao.GetQuestionQa(maps)
|
||||
if err != nil || questionQa == nil {
|
||||
return nil, errors.New("题库不存在")
|
||||
}
|
||||
|
||||
if questionQa.QaStatus == 2 {
|
||||
return nil, errors.New("题库已失效")
|
||||
}
|
||||
|
||||
if questionQa.QaPassword != req.QaPassword {
|
||||
return nil, errors.New("密码错误")
|
||||
}
|
||||
|
||||
// 检测过期时间
|
||||
now := time.Now()
|
||||
qaExpireTime := time.Time(questionQa.QaExpireTime)
|
||||
if qaExpireTime.Before(now) {
|
||||
return nil, errors.New("题库已失效")
|
||||
}
|
||||
|
||||
// 加载题库数据
|
||||
g = &dto.ShareDto{}
|
||||
g.QuestionQa = dto.GetQuestionQaDto(questionQa)
|
||||
|
||||
// 获取题库明细数据-必备选中
|
||||
maps = make(map[string]interface{})
|
||||
maps["qa_id"] = questionQa.QaId
|
||||
maps["is_must_select"] = 1
|
||||
questionQaItems, err := questionQaItemDao.GetQuestionQaItemList(maps)
|
||||
if err == nil && len(questionQaItems) > 0 {
|
||||
shareQuestionDtos := make([]*dto.ShareQuestionDto, len(questionQaItems))
|
||||
|
||||
for i, item := range questionQaItems {
|
||||
// 获取题目数据
|
||||
question, err := questionDao.GetQuestionById(item.QuestionId)
|
||||
if err != nil {
|
||||
return nil, errors.New("题目错误")
|
||||
}
|
||||
|
||||
shareQuestionDto := dto.GetShareQuestionDto(question)
|
||||
|
||||
shareQuestionDto.IsMustSelect = item.IsMustSelect
|
||||
|
||||
// 将转换后的结构体添加到新切片中
|
||||
shareQuestionDtos[i] = shareQuestionDto
|
||||
}
|
||||
|
||||
g.Question = shareQuestionDtos
|
||||
}
|
||||
|
||||
// 计算剩余题目数量
|
||||
remainingQuantity := questionQa.QaQuantity - len(questionQaItems)
|
||||
|
||||
if remainingQuantity > 0 {
|
||||
// 随机获取剩余题目
|
||||
maps = make(map[string]interface{})
|
||||
maps["qa_id"] = questionQa.QaId
|
||||
maps["is_must_select"] = 0
|
||||
questionQaItems, err = questionQaItemDao.GetQuestionQaItemListRand(maps, 1)
|
||||
if err == nil && len(questionQaItems) > 0 {
|
||||
for _, item := range questionQaItems {
|
||||
// 获取题目数据
|
||||
question, err := questionDao.GetQuestionById(item.QuestionId)
|
||||
if err != nil {
|
||||
return nil, errors.New("题目错误")
|
||||
}
|
||||
|
||||
shareQuestionDto := dto.GetShareQuestionDto(question)
|
||||
|
||||
shareQuestionDto.IsMustSelect = item.IsMustSelect
|
||||
|
||||
// 将转换后的结构体添加到新切片中
|
||||
g.Question = append(g.Question, shareQuestionDto)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 按照难度重新排序题目顺序
|
||||
sort.SliceStable(g.Question, func(i, j int) bool {
|
||||
return g.Question[i].Difficulty < g.Question[j].Difficulty
|
||||
})
|
||||
|
||||
return g, nil
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"knowledge/api/dao"
|
||||
"knowledge/api/dto"
|
||||
)
|
||||
|
||||
type StaticService struct {
|
||||
}
|
||||
|
||||
func (r *StaticService) GetStatic() (g []*dto.StaticDto, err error) {
|
||||
questionDao := dao.QuestionDao{}
|
||||
// 处理返回值
|
||||
var responses []*dto.StaticDto
|
||||
|
||||
// 获取每个题目类型题目数量
|
||||
maps := make(map[string]interface{})
|
||||
maps["question_type"] = 1
|
||||
maps["question_status"] = 1
|
||||
maps["question_source"] = 1
|
||||
total, err := questionDao.GetQuestionCount(maps)
|
||||
response := &dto.StaticDto{
|
||||
Name: "单选题",
|
||||
Total: int(total),
|
||||
}
|
||||
|
||||
responses = append(responses, response)
|
||||
|
||||
// 获取每个题目类型题目数量
|
||||
maps = make(map[string]interface{})
|
||||
maps["question_type"] = 2
|
||||
maps["question_status"] = 1
|
||||
maps["question_source"] = 1
|
||||
total, err = questionDao.GetQuestionCount(maps)
|
||||
response = &dto.StaticDto{
|
||||
Name: "多选题",
|
||||
Total: int(total),
|
||||
}
|
||||
|
||||
responses = append(responses, response)
|
||||
|
||||
// 获取每个题目类型题目数量
|
||||
maps = make(map[string]interface{})
|
||||
maps["question_type"] = 3
|
||||
maps["question_status"] = 1
|
||||
maps["question_source"] = 1
|
||||
total, err = questionDao.GetQuestionCount(maps)
|
||||
response = &dto.StaticDto{
|
||||
Name: "问答题",
|
||||
Total: int(total),
|
||||
}
|
||||
|
||||
responses = append(responses, response)
|
||||
|
||||
// 获取每个题目类型题目数量
|
||||
maps = make(map[string]interface{})
|
||||
maps["question_type"] = 4
|
||||
maps["question_status"] = 1
|
||||
maps["question_source"] = 1
|
||||
total, err = questionDao.GetQuestionCount(maps)
|
||||
response = &dto.StaticDto{
|
||||
Name: "判断题",
|
||||
Total: int(total),
|
||||
}
|
||||
|
||||
responses = append(responses, response)
|
||||
|
||||
// 获取每个一级标签题目数量
|
||||
labelDao := dao.LabelDao{}
|
||||
maps = make(map[string]interface{})
|
||||
maps["parent_id"] = 0
|
||||
maps["label_level"] = 1
|
||||
labels, err := labelDao.GetLabelList(maps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, label := range labels {
|
||||
maps = make(map[string]interface{})
|
||||
maps["question_status"] = 1
|
||||
maps["question_source"] = 1
|
||||
maps["first_label_id"] = label.LabelId
|
||||
total, err = questionDao.GetQuestionCount(maps)
|
||||
response = &dto.StaticDto{
|
||||
Name: label.LabelName,
|
||||
Total: int(total),
|
||||
}
|
||||
|
||||
responses = append(responses, response)
|
||||
}
|
||||
|
||||
return responses, nil
|
||||
}
|
||||
Reference in New Issue
Block a user