1
This commit is contained in:
@@ -3,6 +3,7 @@ package dao
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"knowledge/api/model"
|
||||
"knowledge/api/requests"
|
||||
"knowledge/global"
|
||||
)
|
||||
|
||||
@@ -63,6 +64,17 @@ func (r *LabelDao) GetLabelList(maps interface{}) (m []*model.Label, err error)
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetLabelCount 获取数量
|
||||
func (r *LabelDao) GetLabelCount(maps interface{}) (total int64, err error) {
|
||||
var totalRecords int64
|
||||
err = global.Db.Model(&model.Label{}).Where(maps).Count(&totalRecords).Error
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return totalRecords, nil
|
||||
}
|
||||
|
||||
// AddLabel 新增
|
||||
func (r *LabelDao) AddLabel(tx *gorm.DB, model *model.Label) (*model.Label, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
@@ -79,3 +91,38 @@ func (r *LabelDao) GetLabel(maps interface{}) (m *model.Label, err error) {
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetLabelListSearch 获取列表
|
||||
func (r *LabelDao) GetLabelListSearch(req requests.GetLabelList) (m []*model.Label, err error) {
|
||||
// 构建查询条件
|
||||
query := global.Db.Model(&model.Label{})
|
||||
|
||||
// 主键id
|
||||
if req.LabelId != "" {
|
||||
query = query.Where("label_id = ?", req.LabelId)
|
||||
}
|
||||
|
||||
// 标签名称
|
||||
if req.LabelName != "" {
|
||||
query = query.Where("label_name LIKE ?", "%"+req.LabelName+"%")
|
||||
}
|
||||
|
||||
// 父级ID
|
||||
if req.ParentId != "" {
|
||||
query = query.Where("parent_id = ?", req.ParentId)
|
||||
}
|
||||
|
||||
// 级别
|
||||
if req.LabelLevel != nil {
|
||||
query = query.Where("label_level = ?", req.LabelLevel)
|
||||
}
|
||||
|
||||
// 排序
|
||||
query = query.Order("created_at desc")
|
||||
|
||||
err = query.Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
+57
-1
@@ -3,6 +3,7 @@ package dao
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"knowledge/api/model"
|
||||
"knowledge/api/requests"
|
||||
"knowledge/global"
|
||||
@@ -22,6 +23,15 @@ func (r *QuestionDao) GetQuestionById(questionId int64) (m *model.Question, err
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetQuestionPreloadById 获取数据-加载全部关联-id
|
||||
func (r *QuestionDao) GetQuestionPreloadById(questionId int64) (m *model.Question, err error) {
|
||||
err = global.Db.Preload(clause.Associations).First(&m, questionId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// DeleteQuestion 删除
|
||||
func (r *QuestionDao) DeleteQuestion(tx *gorm.DB, maps interface{}) error {
|
||||
err := tx.Where(maps).Delete(&model.Question{}).Error
|
||||
@@ -77,7 +87,7 @@ func (r *QuestionDao) GetQuestionCount(maps interface{}) (total int64, err error
|
||||
|
||||
// GetQuestionListRand 获取列表-随机
|
||||
func (r *QuestionDao) GetQuestionListRand(maps interface{}, limit int) (m []*model.Question, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Order(gorm.Expr("RAND()")).Limit(limit).Error
|
||||
err = global.Db.Where(maps).Order("rand()").Limit(limit).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -205,3 +215,49 @@ func (r *QuestionDao) GetQuestionPageSearch(req requests.GetQuestionPage, page,
|
||||
}
|
||||
return m, totalRecords, nil
|
||||
}
|
||||
|
||||
// GetQuestionCountSearch 获取题目数量
|
||||
func (r *QuestionDao) GetQuestionCountSearch(req requests.GetQuestionCount) (total int64, err error) {
|
||||
var totalRecords int64
|
||||
|
||||
// 构建查询条件
|
||||
query := global.Db.Model(&model.Question{})
|
||||
|
||||
// 题目类型
|
||||
query = query.Where("question_type = ?", req.QuestionType)
|
||||
|
||||
// 状态
|
||||
if req.QuestionStatus != nil {
|
||||
query = query.Where("question_status = ?", req.QuestionStatus)
|
||||
}
|
||||
|
||||
// 难度
|
||||
if req.Difficulty != nil {
|
||||
query = query.Where("difficulty = ?", req.Difficulty)
|
||||
}
|
||||
|
||||
// 题目来源
|
||||
if req.QuestionSource != nil {
|
||||
query = query.Where("question_source = ?", req.QuestionSource)
|
||||
}
|
||||
|
||||
// 一级标签id
|
||||
if req.FirstLabelId != nil {
|
||||
query = query.Where("first_label_id = ?", req.FirstLabelId)
|
||||
}
|
||||
|
||||
// 二级标签id
|
||||
if req.SecondLabelId != nil {
|
||||
query = query.Where("second_label_id = ?", req.SecondLabelId)
|
||||
}
|
||||
|
||||
// 排序
|
||||
query = query.Order("created_at desc")
|
||||
|
||||
// 查询总数量
|
||||
if err := query.Count(&totalRecords).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return totalRecords, nil
|
||||
}
|
||||
|
||||
+154
-2
@@ -1,8 +1,10 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
"knowledge/api/model"
|
||||
"knowledge/api/requests"
|
||||
"knowledge/global"
|
||||
)
|
||||
|
||||
@@ -10,8 +12,17 @@ type QuestionQaItemDao struct {
|
||||
}
|
||||
|
||||
// GetQuestionQaItemById 获取数据-id
|
||||
func (r *QuestionQaItemDao) GetQuestionQaItemById(qaId int64) (m *model.QuestionQaItem, err error) {
|
||||
err = global.Db.First(&m, qaId).Error
|
||||
func (r *QuestionQaItemDao) GetQuestionQaItemById(itemId int64) (m *model.QuestionQaItem, err error) {
|
||||
err = global.Db.First(&m, itemId).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetQuestionQaItemListByQaId 获取数据-QaId
|
||||
func (r *QuestionQaItemDao) GetQuestionQaItemListByQaId(qaId int64) (m []*model.QuestionQaItem, err error) {
|
||||
err = global.Db.Where("qa_id = ?", qaId).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -27,6 +38,14 @@ func (r *QuestionQaItemDao) DeleteQuestionQaItem(tx *gorm.DB, maps interface{})
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteQuestionQaItemById 删除-Id
|
||||
func (r *QuestionQaItemDao) DeleteQuestionQaItemById(tx *gorm.DB, itemId int64) error {
|
||||
if err := tx.Delete(&model.QuestionQaItem{}, itemId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteQuestionQaItemByQaId 删除-QaId
|
||||
func (r *QuestionQaItemDao) DeleteQuestionQaItemByQaId(tx *gorm.DB, qaId int64) error {
|
||||
if err := tx.Where("qa_id = ?", qaId).Delete(&model.QuestionQaItem{}).Error; err != nil {
|
||||
@@ -44,6 +63,15 @@ func (r *QuestionQaItemDao) EditQuestionQaItem(tx *gorm.DB, maps interface{}, da
|
||||
return nil
|
||||
}
|
||||
|
||||
// EditQuestionQaItemById 修改-id
|
||||
func (r *QuestionQaItemDao) EditQuestionQaItemById(tx *gorm.DB, itemId int64, data interface{}) error {
|
||||
err := tx.Model(&model.QuestionQaItem{}).Where("item_id = ?", itemId).Updates(data).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetQuestionQaItemList 获取列表
|
||||
func (r *QuestionQaItemDao) GetQuestionQaItemList(maps interface{}) (m []*model.QuestionQaItem, err error) {
|
||||
err = global.Db.Where(maps).Find(&m).Error
|
||||
@@ -53,6 +81,15 @@ func (r *QuestionQaItemDao) GetQuestionQaItemList(maps interface{}) (m []*model.
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetQuestionQaItemListRand 获取列表-随机
|
||||
func (r *QuestionQaItemDao) GetQuestionQaItemListRand(maps interface{}, limit int) (m []*model.QuestionQaItem, err error) {
|
||||
err = global.Db.Where(maps).Limit(limit).Order("rand()").Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AddQuestionQaItem 新增
|
||||
func (r *QuestionQaItemDao) AddQuestionQaItem(tx *gorm.DB, model *model.QuestionQaItem) (*model.QuestionQaItem, error) {
|
||||
if err := tx.Create(model).Error; err != nil {
|
||||
@@ -69,3 +106,118 @@ func (r *QuestionQaItemDao) GetQuestionQaItem(maps interface{}) (m *model.Questi
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetQuestionQaItemPageSearch 获取列表-分页
|
||||
func (r *QuestionQaItemDao) GetQuestionQaItemPageSearch(req requests.GetQuestionQaItemPage, page, pageSize int) (m []*model.QuestionQaItem, total int64, err error) {
|
||||
var totalRecords int64
|
||||
|
||||
// 构建查询条件
|
||||
query := global.Db.Model(&model.QuestionQaItem{})
|
||||
|
||||
// 题目表
|
||||
query = query.Preload("Question", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Select("question_id", "question_name", "question_type", "question_source", "difficulty", "first_label_id", "second_label_id")
|
||||
})
|
||||
|
||||
// 一级标签
|
||||
query = query.Preload("Question.FirstLabel", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Select("label_id", "label_name")
|
||||
})
|
||||
|
||||
// 二级标签
|
||||
query = query.Preload("Question.SecondLabel", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Select("label_id", "label_name")
|
||||
})
|
||||
|
||||
// 题库id
|
||||
query = query.Where("qa_id = ?", req.QaId)
|
||||
|
||||
// 题目id
|
||||
if req.QuestionId != "" {
|
||||
query = query.Where("question_id = ?", req.QuestionId)
|
||||
}
|
||||
|
||||
// 是否必被选中
|
||||
if req.IsMustSelect != nil {
|
||||
query = query.Where("is_must_select = ?", req.IsMustSelect)
|
||||
}
|
||||
|
||||
// 题目名称
|
||||
if req.QuestionName != "" {
|
||||
subQuery := global.Db.Model(&model.Question{}).
|
||||
Select("question_id").
|
||||
Where("question_name LIKE ?", "%"+req.QuestionName+"%")
|
||||
|
||||
query = query.Where(gorm.Expr("question_id IN (?)", subQuery))
|
||||
}
|
||||
|
||||
// 题目类型
|
||||
if req.QuestionType != nil {
|
||||
subQuery := global.Db.Model(&model.Question{}).
|
||||
Select("question_id").
|
||||
Where("question_type = ?", req.QuestionType)
|
||||
|
||||
query = query.Where(gorm.Expr("question_id IN (?)", subQuery))
|
||||
}
|
||||
|
||||
// 题目来源
|
||||
if req.QuestionSource != nil {
|
||||
subQuery := global.Db.Model(&model.Question{}).
|
||||
Select("question_id").
|
||||
Where("question_source = ?", req.QuestionSource)
|
||||
|
||||
query = query.Where(gorm.Expr("question_id IN (?)", subQuery))
|
||||
}
|
||||
|
||||
// 难度
|
||||
if req.Difficulty != nil {
|
||||
subQuery := global.Db.Model(&model.Question{}).
|
||||
Select("question_id").
|
||||
Where("difficulty = ?", req.Difficulty)
|
||||
|
||||
query = query.Where(gorm.Expr("question_id IN (?)", subQuery))
|
||||
}
|
||||
|
||||
// 一级标签id
|
||||
if req.FirstLabelId != nil {
|
||||
subQuery := global.Db.Model(&model.Question{}).
|
||||
Select("question_id").
|
||||
Where("first_label_id = ?", req.FirstLabelId)
|
||||
|
||||
query = query.Where(gorm.Expr("question_id IN (?)", subQuery))
|
||||
}
|
||||
|
||||
// 二级标签id
|
||||
if req.SecondLabelId != nil {
|
||||
subQuery := global.Db.Model(&model.Question{}).
|
||||
Select("question_id").
|
||||
Where("second_label_id = ?", req.SecondLabelId)
|
||||
|
||||
query = query.Where(gorm.Expr("question_id IN (?)", subQuery))
|
||||
}
|
||||
|
||||
// 排序
|
||||
if req.Order != nil {
|
||||
if req.Order.UpdatedAt != "" {
|
||||
if req.Order.UpdatedAt != "desc" && req.Order.UpdatedAt != "asc" {
|
||||
return nil, 0, errors.New("排序字段错误")
|
||||
}
|
||||
|
||||
query = query.Order("updated_at " + req.Order.UpdatedAt)
|
||||
}
|
||||
}
|
||||
|
||||
// 排序
|
||||
query = query.Order("created_at desc")
|
||||
|
||||
// 查询总数量
|
||||
if err := query.Count(&totalRecords).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
err = query.Scopes(model.Paginate(page, pageSize)).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return m, totalRecords, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user