100 lines
2.8 KiB
Go
100 lines
2.8 KiB
Go
package dao
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/civet148/log"
|
|
"github.com/civet148/sqlca/v2"
|
|
"intent-system/pkg/dal/models"
|
|
)
|
|
|
|
type QaDraftCondition struct {
|
|
PageNo int
|
|
PageSize int
|
|
Asc bool
|
|
Id int64
|
|
Search string
|
|
}
|
|
|
|
type QuestionDraftDAO struct {
|
|
db *sqlca.Engine
|
|
}
|
|
|
|
func NewQuestionDraftDAO(db *sqlca.Engine) *QuestionDraftDAO {
|
|
return &QuestionDraftDAO{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// insert into table by data model
|
|
func (dao *QuestionDraftDAO) Insert(do *models.QuestionDraftDO) (lastInsertId int64, err error) {
|
|
return dao.db.Model(&do).Table(models.TableNameQuestionDraft).Insert()
|
|
}
|
|
|
|
// insert if not exist or update columns on duplicate key...
|
|
func (dao *QuestionDraftDAO) Upsert(do *models.QuestionDraftDO, columns ...string) (lastInsertId int64, err error) {
|
|
_, err = dao.db.Model(&do).
|
|
Table(models.TableNameQuestionDraft).
|
|
Select(models.QUESTION_DRAFT_COLUMN_ID).
|
|
Eq(models.QUESTION_DRAFT_COLUMN_QA_ID, do.QaId).
|
|
Query()
|
|
if err != nil {
|
|
return 0, log.Errorf(err.Error())
|
|
}
|
|
if do.Id == 0 {
|
|
return dao.db.Model(&do).Table(models.TableNameQuestionDraft).Insert()
|
|
}
|
|
return dao.db.Model(&do).Table(models.TableNameQuestionDraft).Select(columns...).Upsert()
|
|
}
|
|
|
|
// update table set columns where id=xxx
|
|
func (dao *QuestionDraftDAO) Update(do *models.QuestionDraftDO, columns ...string) (rows int64, err error) {
|
|
return dao.db.Model(&do).Table(models.TableNameQuestionDraft).Select(columns...).Update()
|
|
}
|
|
|
|
// query records by id
|
|
func (dao *QuestionDraftDAO) QueryById(id interface{}, columns ...string) (do *models.QuestionDraftDO, err error) {
|
|
if _, err = dao.db.Model(&do).Table(models.TableNameQuestionDraft).Id(id).Select(columns...).Query(); err != nil {
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
|
|
// query records by conditions
|
|
func (dao *QuestionDraftDAO) QueryByCondition(conditions map[string]interface{}, columns ...string) (dos []*models.QuestionDraftDO, err error) {
|
|
if len(conditions) == 0 {
|
|
return nil, fmt.Errorf("condition must not be empty")
|
|
}
|
|
e := dao.db.Model(&dos).Table(models.TableNameQuestionDraft).Select(columns...)
|
|
for k, v := range conditions {
|
|
e.Eq(k, v)
|
|
}
|
|
if _, err = e.Query(); err != nil {
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
|
|
func (dao *QuestionDraftDAO) QueryList(cond *QaDraftCondition) (dos []*models.QuestionDraftDO, total int64, err error) {
|
|
e := dao.db.Model(&dos).
|
|
Table(models.TableNameQuestionDraft).
|
|
Eq(models.QUESTION_DRAFT_COLUMN_IS_DELETED, 0).
|
|
Page(cond.PageNo, cond.PageSize)
|
|
|
|
if cond.Id != 0 {
|
|
e.Eq(models.QUESTION_DRAFT_COLUMN_ID, cond.Id)
|
|
}
|
|
if cond.Search != "" {
|
|
e.Like(models.QUESTION_DRAFT_COLUMN_QUESTION, cond.Search)
|
|
}
|
|
if cond.Asc {
|
|
e.Asc(models.QUESTION_DRAFT_COLUMN_UPDATED_TIME, models.QUESTION_DRAFT_COLUMN_ID)
|
|
} else {
|
|
e.Desc(models.QUESTION_DRAFT_COLUMN_UPDATED_TIME, models.QUESTION_DRAFT_COLUMN_ID)
|
|
}
|
|
_, total, err = e.QueryEx()
|
|
if err != nil {
|
|
return nil, 0, log.Errorf(err.Error())
|
|
}
|
|
return
|
|
}
|