package help_doc

import (
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
	"time"
)

type HelpDoc struct {
	Id            int       `orm:"column(id);pk"`
	ClassifyId    int       // 分类id
	Title         string    // 标题
	Author        string    // 作者
	CreateTime    time.Time // 创建时间
	ModifyTime    time.Time // 修改时间
	Status        int       // 1:未发布,2:已发布
	PublishTime   time.Time // 发布时间
	Content       string    // 内容
	AdminId       int       // 创建人
	AdminRealName string    // 创建人姓名
	Anchor        string    // 锚点
	Recommend     string    // 推荐
}

type HelpDocItem struct {
	Id            int    `orm:"column(id);pk"`
	ClassifyId    int    // 分类id
	Title         string // 标题
	Author        string // 作者
	CreateTime    string // 创建时间
	ModifyTime    string // 修改时间
	Status        int    // 1:未发布,2:已发布
	PublishTime   string // 发布时间
	Content       string // 内容
	AdminId       int    // 创建人
	AdminRealName string // 创建人姓名
	Anchor        string // 锚点
	Recommend     string // 推荐
}

func EditHelpDocClassifyId(classifyId int, classifyName string) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE help_doc SET classify_name=?,modify_time=NOW() WHERE classify_id=? `
	_, err = o.Raw(sql, classifyId, classifyName).Exec()
	return
}

type AddHelpDocReq struct {
	Id            int64  `description:"文章id"`
	ClassifyId    int    `description:"分类id"`
	Title         string `description:"标题"`
	Author        string `description:"作者"`
	Status        int    `description:"状态:1:未发布,2:已发布"`
	Content       string `description:"内容"`
	AnchorData    []AnchorList
	RecommendData []RecommendList
}

type AnchorList struct {
	AnchorId   string
	Anchor     string
	AnchorName string
	Child      []AnchorList
}

type RecommendList struct {
	Name string
	Url  string
}

func AddHelpDoc(item *HelpDoc) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

type EditHelpDocReq struct {
	Id            int64  `description:"文章id"`
	ClassifyId    int    `description:"分类id"`
	Title         string `description:"标题"`
	Author        string `description:"作者"`
	Status        int    `description:"状态:1:未发布,2:已发布"`
	Content       string `description:"内容"`
	AnchorData    []AnchorList
	RecommendData []RecommendList
}

func EditHelpDoc(item *HelpDoc, reportId int64) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE help_doc
			SET
			  classify_id =?,
			  title = ?,
			  author = ?,
			  content = ?,
			  modify_time = ?,
			  status = ?,
			  publish_time = ?,
			  anchor = ?,
			  recommend = ? 
			WHERE id = ? `
	_, err = o.Raw(sql, item.ClassifyId, item.Title, item.Author, item.Content, item.ModifyTime,
		item.Status, item.PublishTime, item.Anchor, item.Recommend, reportId).Exec()
	return
}

func GetHelpDocById(docId int) (item *HelpDocItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM help_doc WHERE id=?`
	err = o.Raw(sql, docId).QueryRow(&item)
	return
}

type HelpDocResp struct {
	Id            int             `orm:"column(id);pk"`
	ClassifyId    int             // 分类id
	ClassifyName  string          // 分类路径
	Title         string          // 标题
	Author        string          // 作者
	CreateTime    string          // 创建时间
	ModifyTime    string          // 修改时间
	Status        int             // 1:未发布,2:已发布
	PublishTime   string          // 发布时间
	Content       string          // 内容
	AdminId       int             // 创建人
	AdminRealName string          // 创建人姓名
	Anchor        []AnchorList    // 锚点
	Recommend     []RecommendList // 推荐
}

func GetHelpDocListCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM help_doc WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func GetHelpDocList(condition string, pars []interface{}, startSize, pageSize int) (items []*HelpDocItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM help_doc WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += `ORDER BY create_time DESC LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type HelpDocListResp struct {
	List         []*HelpDocResp
	ClassifyName string
	Paging       *paging.PagingItem `description:"分页数据"`
}

type PublishReq struct {
	DocId  int
	Status int `description:"状态:1:未发布,2:已发布"`
}

// 发布报告
func PublishHelpDocById(reportId, status int) (err error) {
	o := orm.NewOrm()
	sql := ``
	if status == 1 {
		sql = `UPDATE help_doc SET status=1,publish_time=NULL,modify_time=NOW() WHERE id = ? `
	} else {
		sql = `UPDATE help_doc SET status=2,publish_time=now(),modify_time=NOW() WHERE id = ? `
	}
	_, err = o.Raw(sql, reportId).Exec()
	return
}

type DeleteReq struct {
	DocId int `description:"id"`
}

func DeleteHelpDoc(reportIds int) (err error) {
	o := orm.NewOrm()
	sql := ` DELETE FROM help_doc WHERE id =? `
	_, err = o.Raw(sql, reportIds).Exec()
	return
}

func GetAllHelpDoc() (items []*HelpDocItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM help_doc `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}