package dao

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

type NewsSubscribeCondition struct {
	PageNo   int
	PageSize int
	Asc      bool
	Id       int64
	Search   string
}

type NewsSubscribeDAO struct {
	db *sqlca.Engine
}

func NewNewsSubscribeDAO(db *sqlca.Engine) *NewsSubscribeDAO {
	return &NewsSubscribeDAO{
		db: db,
	}
}

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

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

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

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

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

func (dao *NewsSubscribeDAO) QueryByNewsId(newsId interface{}, columns ...string) (do *models.NewsSubscribeDO, err error) {
	if _, err = dao.db.Model(&do).Table(models.TableNameNewsSubscribe).Eq(models.NEWS_SUBSCRIBE_COLUMN_NEWS_ID, newsId).Select(columns...).Query(); err != nil {
		return nil, err
	}
	return
}

func (dao *NewsSubscribeDAO) QueryAllNews(cond *NewsSubscribeCondition) (dos []*models.NewsDO, total int64, err error) {

	e := dao.db.Model(&dos).
		Table(models.TableNameNews).
		Eq(models.NEWS_COLUMN_IS_DELETED, 0).
		Desc(models.NEWS_COLUMN_UPDATED_TIME).
		Page(cond.PageNo, cond.PageSize)
	if cond.Id != 0 {
		e.Eq(models.NEWS_SUBSCRIBE_COLUMN_ID, 0)
	}
	if cond.Search != "" {
		e.Like(models.NEWS_SUBSCRIBE_COLUMN_NEWS_SUBJECT, cond.Search)
	}
	if cond.Asc {
		e.Asc(models.NEWS_COLUMN_UPDATED_TIME)
	} else {
		e.Desc(models.NEWS_COLUMN_UPDATED_TIME)
	}
	_, total, err = e.QueryEx()
	if err != nil {
		return nil, 0, log.Errorf(err.Error())
	}
	return
}

func (dao *NewsSubscribeDAO) QueryTodayNewsList(pageNo, pageSize int) (dos []*models.NewsDO, total int64, err error) {
	strTables := fmt.Sprintf("%s a, %s b", models.TableNameNews, models.TableNameNewsSubscribe)
	_, total, err = dao.db.Model(&dos).
		Table(strTables).
		Select("a.*").
		And("a.id = b.news_id").
		And("b.is_deleted=0").
		And("b.is_pushed='0'").
		Desc("b.updated_time").
		Page(pageNo, pageSize).
		QueryEx()
	if err != nil {
		return nil, 0, log.Errorf(err.Error())
	}
	return
}

func (dao *NewsSubscribeDAO) QueryPushedNews(cond *NewsSubscribeCondition) (dos []*models.NewsDO, total int64, err error) {
	strTables := fmt.Sprintf("%s a, %s b", models.TableNameNews, models.TableNameNewsSubscribe)
	e := dao.db.Model(&dos).
		Table(strTables).
		Select("a.*").
		And("a.id = b.news_id").
		And("b.is_deleted=0").
		And("b.is_pushed=1").
		Page(cond.PageNo, cond.PageSize)

	if cond.Id != 0 {
		e.Eq("a.id", cond.Id)
	}
	if cond.Search != "" {
		e.Like("a.main_title", cond.Search)
	}
	if cond.Asc {
		e.Asc("b.updated_time")
	} else {
		e.Desc("b.updated_time")
	}
	_, total, err = e.QueryEx()
	if err != nil {
		return nil, 0, log.Errorf(err.Error())
	}
	return
}

func (dao *NewsSubscribeDAO) IsSubNewsExist(newsId int64) (ok bool, err error) {
	var do *models.NewsSubscribeDO
	_, err = dao.db.Model(&do).
		Table(models.TableNameNewsSubscribe).
		Eq(models.NEWS_SUBSCRIBE_COLUMN_NEWS_ID, newsId).
		Eq(models.NEWS_SUBSCRIBE_COLUMN_IS_PUSHED, 0).
		Eq(models.NEWS_SUBSCRIBE_COLUMN_IS_DELETED, 0).
		Query()
	if err != nil {
		return false, log.Errorf(err.Error())
	}
	return do.Id != 0, nil
}

func (dao *NewsSubscribeDAO) DeleteNotPushed() (err error) {
	_, err = dao.db.Model(1).
		Table(models.TableNameNewsSubscribe).
		Select(models.NEWS_SUBSCRIBE_COLUMN_IS_DELETED).
		Eq(models.NEWS_SUBSCRIBE_COLUMN_IS_PUSHED, 0).
		Update()
	if err != nil {
		return log.Errorf(err.Error())
	}
	return nil
}
