package dao

import (
	"fmt"
	"github.com/civet148/log"
	"github.com/civet148/sqlca/v2"
	"intent-system/pkg/dal/models"
)

type QuestionAnswerCondition struct {
	PageNo       int
	PageSize     int
	Id           int64
	IsDeleted    bool
	ContainExtra bool
	Asc          bool
	Search       string
	Language     string
}

type QuestionAnswerDAO struct {
	db *sqlca.Engine
}

func NewQuestionAnswerDAO(db *sqlca.Engine) *QuestionAnswerDAO {
	return &QuestionAnswerDAO{
		db: db,
	}
}

// insert into table by data model
func (dao *QuestionAnswerDAO) Insert(do *models.QuestionAnswerDO) (lastInsertId int64, err error) {
	return dao.db.Model(&do).Table(models.TableNameQuestionAnswer).Insert()
}

// insert if not exist or update columns on duplicate key...
func (dao *QuestionAnswerDAO) Upsert(do *models.QuestionAnswerDO, columns ...string) (lastInsertId int64, err error) {
	return dao.db.Model(&do).Table(models.TableNameQuestionAnswer).Select(columns...).Upsert()
}

// update table set columns where id=xxx
func (dao *QuestionAnswerDAO) Update(do *models.QuestionAnswerDO, columns ...string) (rows int64, err error) {
	return dao.db.Model(&do).Table(models.TableNameQuestionAnswer).Select(columns...).Update()
}

// query records by id
func (dao *QuestionAnswerDAO) QueryById(id interface{}, columns ...string) (do *models.QuestionAnswerDO, err error) {
	if _, err = dao.db.Model(&do).Table(models.TableNameQuestionAnswer).Id(id).Select(columns...).Query(); err != nil {
		return nil, err
	}
	return
}

// query records by conditions
func (dao *QuestionAnswerDAO) QueryByCondition(conditions map[string]interface{}, columns ...string) (dos []*models.QuestionAnswerDO, err error) {
	if len(conditions) == 0 {
		return nil, fmt.Errorf("condition must not be empty")
	}
	e := dao.db.Model(&dos).Table(models.TableNameQuestionAnswer).Select(columns...)
	for k, v := range conditions {
		e.Eq(k, v)
	}
	if _, err = e.Query(); err != nil {
		return nil, err
	}
	return
}

func (dao *QuestionAnswerDAO) QueryList(cond *QuestionAnswerCondition) (dos []*models.QuestionAnswerDO, total int64, err error) {
	e := dao.db.Model(&dos).
		Table(models.TableNameQuestionAnswer).
		Select(
			models.QUESTION_ANSWER_COLUMN_ID,
			models.QUESTION_ANSWER_COLUMN_ORG_ID,
			models.QUESTION_ANSWER_COLUMN_QUESTION,
			models.QUESTION_ANSWER_COLUMN_ANSWER,
			models.QUESTION_ANSWER_COLUMN_STATE,
			models.QUESTION_ANSWER_COLUMN_IS_OVERWRITTEN,
			models.QUESTION_ANSWER_COLUMN_IS_REPLICATE,
			models.QUESTION_ANSWER_COLUMN_IS_DELETED,
			models.QUESTION_ANSWER_COLUMN_CREATED_TIME,
			models.QUESTION_ANSWER_COLUMN_UPDATED_TIME,
		).
		Page(cond.PageNo, cond.PageSize)

	if cond.Id != 0 {
		e.Eq(models.QUESTION_ANSWER_COLUMN_ID, cond.Id)
	}
	if cond.IsDeleted {
		e.Eq(models.QUESTION_ANSWER_COLUMN_IS_DELETED, 1)
	} else {
		e.Eq(models.QUESTION_ANSWER_COLUMN_IS_DELETED, 0)
	}
	if cond.ContainExtra {
		e.Select(models.QUESTION_ANSWER_COLUMN_EXTRA_DATA)
	}
	if cond.Language != "" {
		e.Eq(models.QUESTION_ANSWER_COLUMN_LANGUAGE, cond.Language)
	}
	if cond.Search != "" {
		e.Like(models.QUESTION_ANSWER_COLUMN_QUESTION, cond.Search)
	}
	if cond.Asc {
		e.Asc(models.QUESTION_ANSWER_COLUMN_UPDATED_TIME, models.QUESTION_ANSWER_COLUMN_ID)
	} else {
		e.Desc(models.QUESTION_ANSWER_COLUMN_UPDATED_TIME, models.QUESTION_ANSWER_COLUMN_ID)
	}
	_, total, err = e.QueryEx()
	if err != nil {
		return nil, 0, log.Errorf(err.Error())
	}
	return
}

func (dao *QuestionAnswerDAO) UpdateByOrgId(do *models.QuestionAnswerDO, columns ...string) (rows int64, err error) {
	return dao.db.Model(&do).Table(models.TableNameQuestionAnswer).Eq(models.QUESTION_ANSWER_COLUMN_ORG_ID, do.OrgId).Select(columns...).Update()
}

// query max news id
func (dao *QuestionAnswerDAO) QueryMaxSyncId(lang models.LanguageType) (lastId int64, err error) {
	if _, err = dao.db.Model(&lastId).
		Table(models.TableNameQuestionAnswer).
		Max(models.QUESTION_ANSWER_COLUMN_ORG_ID).
		Eq(models.QUESTION_ANSWER_COLUMN_LANGUAGE, lang).
		Query(); err != nil {
		return 0, err
	}
	return
}
