package dao

import (
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/business/model"
)

func GetKnowledgeDepot(db *DaoDB, keyword string, offset, pageSize int) (page *model.PagedInfo, err error) {
	var (
		knows []*model.KnowledgeDepot
	)
	sql := `
		SELECT SQL_CALC_FOUND_ROWS *
		  FROM knowledge_depot
         WHERE 1 = 1
	`
	sqlParams := []interface{}{}
	if keyword != "" {
		sql += " AND (content LIKE ? OR title LIKE ?)"
		sqlParams = append(sqlParams, "%"+keyword+"%", "%"+keyword+"%")
	}
	sql += " LIMIT ? OFFSET ?"
	pageSize = jxutils.FormalizePageSize(pageSize)
	offset = jxutils.FormalizePageOffset(offset)
	sqlParams = append(sqlParams, pageSize, offset)
	txDB, _ := Begin(db)
	defer Commit(db, txDB)
	if err = GetRowsTx(txDB, &knows, sql, sqlParams...); err == nil {
		page = &model.PagedInfo{
			TotalCount: GetLastTotalRowCount2(db, txDB),
			Data:       knows,
		}
	}
	return page, err
}

func GetKnowledgeDepotNoPage(db *DaoDB, ids []int) (knows []*model.KnowledgeDepot, err error) {
	sql := `
		SELECT *
		  FROM knowledge_depot
         WHERE 1 = 1
	`
	sqlParams := []interface{}{}
	if len(ids) > 0 {
		sql += " AND id IN (" + GenQuestionMarks(len(ids)) + ")"
		sqlParams = append(sqlParams, ids)
	}
	GetRows(db, &knows, sql, sqlParams...)
	return knows, err
}
