package cygx

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

type CygxYanxuanSpecial struct {
	Id          int       `orm:"column(id);pk"`
	UserId      int       // 用户ID
	CreateTime  time.Time // 创建时间
	ModifyTime  time.Time // 修改时间
	PublishTime time.Time // 提审过审或驳回时间
	Content     string    // 内容
	Tags        string    // 标签
	Status      int       // 1:未发布,2:审核中 3:已发布 4:驳回
	ImgUrl      string    // 图片链接
	DocUrl      string    // 文档链接
	Reason      string    // 理由
	Title       string    // 标题
	Type        string    // 类型1:笔记,2:观点
}

type CygxYanxuanSpeciaResplItem struct {
	Id                int    `orm:"column(id);pk"`
	UserId            int    // 用户ID
	CreateTime        string // 创建时间
	ModifyTime        string // 修改时间
	PublishTime       string // 提审过审或驳回时间
	Content           string // 内容
	Tags              string // 标签
	Status            int    // 1:未发布,2:审核中 3:已发布 4:驳回
	ImgUrl            string // 图片链接
	DocUrl            string // 文档链接
	SpecialName       string // 专栏名称
	Introduction      string // 介绍
	Label             string // 标签
	NickName          string // 昵称
	RealName          string // 姓名
	Mobile            string // 手机号
	HeadImg           string // 头像
	BgImg             string // 背景图
	Reason            string // 理由
	Title             string // 标题
	CompanyTags       string
	IndustryTags      string
	Type              int // 类型1:笔记,2:观点
	ContentHasImg     int //正文是否包含图片 1包含 0不包含
	Docs              []Doc
	Pv                string `description:"Pv"`
	Uv                string `description:"Uv"`
	ArticleCollectNum int    // 文章收藏数量
	AdminName         string // 审核人员姓名
	SpecialAuthorId   int    //cygx_yanxuan_special_author 表主键ID 作者专栏ID
}

type Doc struct {
	DocName   string
	DocSuffix string
	DocUrl    string
	DocIcon   string
}

type GetCygxYanxuanSpeciaResplItemResp struct {
	Paging *paging.PagingItem `description:"分页数据"`
	List   []*CygxYanxuanSpeciaResplItem
}

// 获取数量
func GetGetYanxuanSpecialCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_yanxuan_special as a 
				JOIN cygx_yanxuan_special_author AS b ON a.user_id = b.user_id  WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

func GetYanxuanSpecialList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxYanxuanSpeciaResplItem, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ``
	sql = `SELECT a.*,b.bg_img,b.head_img,b.introduction,b.label,b.mobile,b.nick_name,b.real_name,b.special_name
FROM cygx_yanxuan_special AS a
JOIN cygx_yanxuan_special_author AS b ON a.user_id = b.user_id 
 WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetYanxuanSpecialListByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxYanxuanSpeciaResplItem, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ``
	sql = `SELECT a.*
FROM cygx_yanxuan_special AS a
 WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type EnableCygxYanxuanSpecialReq struct {
	Id     int    // 文章id
	Status int    // 1通过2驳回
	Reason string //理由
}

func EnableYanxuanSpecial(id, status int, reason, adminName string) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ``
	sql = `UPDATE cygx_yanxuan_special SET status=?,reason=?,admin_name = ? , publish_time=NOW() WHERE id = ? `
	_, err = o.Raw(sql, status, reason, adminName, id).Exec()
	return
}

type CygxYanxuanSpecialItem struct {
	Id            int    `orm:"column(id);pk"`
	UserId        int    // 用户ID
	CreateTime    string // 创建时间
	ModifyTime    string // 修改时间
	PublishTime   string // 提审过审或驳回时间
	Content       string // 内容
	Tags          string // 标签
	Status        int    // 1:未发布,2:审核中 3:已发布 4:驳回
	ImgUrl        string // 图片链接
	DocUrl        string // 文档链接
	SpecialName   string // 专栏名称
	Introduction  string // 介绍
	Label         string // 标签
	NickName      string // 昵称
	RealName      string // 姓名
	Mobile        string // 手机号
	HeadImg       string // 头像
	BgImg         string // 背景图
	Reason        string // 理由
	Title         string // 标题
	Type          int    // 类型1:笔记,2:观点
	CollectNum    int
	MyCollectNum  int
	IsCollect     int
	CompanyTags   string
	IndustryTags  string
	ContentHasImg int //正文是否包含图片 1包含 0不包含
	Docs          []Doc
}

func GetYanxuanSpecialFollowUserById(specialId int) (items []int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ``
	sql = `SELECT b.user_id
FROM cygx_yanxuan_special AS a
JOIN cygx_yanxuan_special_follow AS b ON a.user_id = b.follow_user_id
 WHERE a.id=? `
	_, err = o.Raw(sql, specialId).QueryRows(&items)
	return
}

func GetYanxuanSpecialItemById(specialId int) (item *CygxYanxuanSpecialItem, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ``
	sql = `SELECT a.*,b.bg_img,b.head_img,b.introduction,b.label,b.mobile,
b.nick_name,b.real_name,b.special_name
FROM cygx_yanxuan_special AS a
JOIN cygx_yanxuan_special_author AS b ON a.user_id = b.user_id 
 WHERE a.id=? `
	err = o.Raw(sql, specialId).QueryRow(&item)
	return
}

// 获取数量
func GetCygxYanxuanSpecialCount(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_yanxuan_special WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

type CygxYanxuanSpecialShowButton struct {
	IsShowYanXuanSpecial bool // 研选专栏文章管理按钮是否展示
	IsShowSpecialAuthor  bool // 作者管理的按钮是否展示
}

func GetYanxuanSpecialBySpecialId(specialId int) (item *CygxYanxuanSpecialItem, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ``
	sql = `SELECT a.* FROM cygx_yanxuan_special AS a WHERE a.id=? `
	err = o.Raw(sql, specialId).QueryRow(&item)
	return
}

func GetYanxuanSpecialListBycondition(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxYanxuanSpecial, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT a.* FROM cygx_yanxuan_special AS a WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	if startSize+pageSize > 0 {
		sql += ` LIMIT ?,? `
		_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	} else {
		_, err = o.Raw(sql, pars).QueryRows(&items)
	}
	return
}