package models

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

type CygxYanxuanSpecialAuthor struct {
	Id           int       `orm:"column(id);pk"`
	UserId       int       // 用户ID
	SpecialName  string    // 专栏名称
	Introduction string    // 介绍
	Label        string    // 标签
	NickName     string    // 昵称
	RealName     string    // 姓名
	Mobile       string    // 手机号
	CreateTime   time.Time // 创建时间
	ModifyTime   time.Time // 修改时间
	HeadImg      string    // 头像
	BgImg        string    // 背景图上部分
	BgImgDown    string    // 背景图下部分
	Status       int       // 1启用2禁用
}

type CygxYanxuanSpecialAuthorItem struct {
	Id                   int                                 `orm:"column(id);pk"`
	UserId               int                                 // 用户ID
	SpecialName          string                              // 专栏名称
	Introduction         string                              // 介绍
	Label                string                              // 标签
	NickName             string                              // 昵称
	RealName             string                              // 姓名
	CompanyName          string                              // 公司名
	Mobile               string                              // 手机号
	CreateTime           string                              // 创建时间
	ModifyTime           time.Time                           // 修改时间
	HeadImg              string                              // 头像
	BgImg                string                              // 背景图
	BgImgDown            string                              // 背景图下半部分
	Status               int                                 // 1启用2禁用
	CollectNum           int                                 // 被收藏数
	FollowNum            int                                 // 被关注数
	SpecialArticleNum    int                                 // 文章数
	LatestPublishTime    time.Time                           // 最近更新时间
	LatestPublishDate    string                              // 最近更新时间
	IsFollow             int                                 // 是否已关注 1已关注 0 未关注
	HasChangeHeadImg     int                                 // 是否更换过默认头像 1是，0否
	MomentsImg           string                              `description:"分享到朋友圈的封面图片"`
	YanxuanSpecialCenter *CygxYanxuanSpecialCenterAuthorResp // 研选专栏文章内容
}

type CygxYanxuanSpecialCenterAuthorResp struct {
	Id          int    //研选专栏ID
	UserId      int    // 用户ID
	PublishTime string // 提审过审或驳回时间
	Title       string // 标题
}

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

type EnableCygxYanxuanSpecialAuthorReq struct {
	UserId int // 用户ID
	Status int // 1启用2禁用
}

// 启用禁用作者
func EnableYanxuanSpecialAuthor(userId, status int) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special_author SET status=?,modify_time=NOW() WHERE user_id = ? `
	_, err = o.Raw(sql, status, userId).Exec()
	return
}

func GetYanxuanSpecialAuthor(reqUserId, sysUserId int, cond string) (item *CygxYanxuanSpecialAuthorItem, err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `SELECT
	a.*,
	( SELECT count( 1 ) FROM cygx_yanxuan_special_collect AS ac  INNER JOIN cygx_yanxuan_special as cs ON  ac.yanxuan_special_id = cs.id  WHERE cs.user_id = a.user_id  ) AS collect_num,
	( SELECT count( 1 ) FROM cygx_yanxuan_special_follow AS cf  WHERE cf.follow_user_id = a.user_id  ) AS follow_num,
	( SELECT count( 1 ) FROM cygx_yanxuan_special AS ca  WHERE ca.user_id = a.user_id AND ca.status = 3 ) AS special_article_num,
	( SELECT count( 1 ) FROM cygx_yanxuan_special_follow AS cf  WHERE cf.follow_user_id =? AND cf.user_id = ?  ) AS is_follow 
FROM
	cygx_yanxuan_special_author as a WHERE a.user_id=? `
	if cond != "" {
		sql += cond
	}
	err = o.Raw(sql, reqUserId, sysUserId, reqUserId).QueryRow(&item)
	return
}

// 根据用户ID获取专栏详情
func GetCygxYanxuanSpecialAuthorByUserId(userId int) (item *CygxYanxuanSpecialAuthorItem, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_yanxuan_special_author  WHERE user_id = ? `
	err = o.Raw(sql, userId).QueryRow(&item)
	return
}

type SaveCygxYanxuanSpecialAuthorReq struct {
	UserId       int    // 用户ID
	SpecialName  string // 专栏名称
	Introduction string // 介绍
	Label        string // 标签
	NickName     string // 昵称
	BgImg        string // 背景图
}

func UpdateYanxuanSpecialAuthor(item *CygxYanxuanSpecialAuthor) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special_author SET special_name=?,introduction=?,label=?,nick_name=?
	,modify_time=NOW() WHERE user_id = ? `
	_, err = o.Raw(sql, item.SpecialName, item.Introduction, item.Label, item.NickName, item.UserId).Exec()
	return
}

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

func GetYanxuanSpecialAuthorList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxYanxuanSpecialAuthorItem, err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `SELECT
	a.*,
	IFNULL(( SELECT publish_time FROM cygx_yanxuan_special WHERE user_id = a.user_id AND STATUS = 3 ORDER BY publish_time DESC LIMIT 1 ), a.modify_time) AS latest_publish_time
FROM
	cygx_yanxuan_special_author AS a
WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?  `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type SpecialAuthorListResp struct {
	Paging   *paging.PagingItem `description:"分页数据"`
	List     []*CygxYanxuanSpecialAuthorItem
	IsAuthor bool
}

type SaveCygxYanxuanSpecialAuthoHeadImgrReq struct {
	UserId  int    // 用户ID
	HeadImg string // 头像
}

func UpdateYanxuanSpecialAuthorHeadImg(item *CygxYanxuanSpecialAuthor) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special_author SET head_img=?,modify_time=NOW(),has_change_head_img = 1  WHERE user_id = ? `
	_, err = o.Raw(sql, item.HeadImg, item.UserId).Exec()
	return
}

// UpdateCygxYanxuanSpecialAuthorPv 修改研选专栏作者的阅读Pv
func UpdateCygxYanxuanSpecialAuthorPv(userId int) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special_author SET pv=pv+1 WHERE user_id = ? `
	_, err = o.Raw(sql, userId).Exec()
	return
}

// UpdateCygxYanxuanSpecialAuthorUv 修改研选专栏作者的阅读Uv
func UpdateCygxYanxuanSpecialAuthorUv(userId int) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special_author SET uv=uv+1 WHERE user_id = ? `
	_, err = o.Raw(sql, userId).Exec()
	return
}

// 增加收藏数量
func UpdateYanxuanSpecialAuthorArticleCollectNumIncrease(userId int) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special_author SET article_collect_num = article_collect_num +1  WHERE id = ? `
	_, err = o.Raw(sql, userId).Exec()
	return
}

// 减少收藏数量
func UpdateYanxuanSpecialAuthorArticleCollectNumReduce(userId int) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special_author SET article_collect_num = article_collect_num  - 1  WHERE id = ? `
	_, err = o.Raw(sql, userId).Exec()
	return
}

// 更新收藏数量
func UpdateYanxuanSpecialAuthorArticleCollectNum(collectNum, userId int) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special_author SET article_collect_num = ?  WHERE user_id = ? `
	_, err = o.Raw(sql, collectNum, userId).Exec()
	return
}

// 增加粉丝数量
func UpdateYanxuanSpecialAuthorFansNumIncrease(userId int) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special_author SET fans_num = fans_num +1  WHERE id = ? `
	_, err = o.Raw(sql, userId).Exec()
	return
}

// 减少粉丝数量
func UpdateYanxuanSpecialAuthorFansNumReduce(userId int) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special_author SET fans_num = fans_num  - 1  WHERE id = ? `
	_, err = o.Raw(sql, userId).Exec()
	return
}

// 更新粉丝数量
func UpdateYanxuanSpecialAuthorFansNum(fansNum, userId int) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special_author SET fans_num = ?  WHERE user_id = ? `
	_, err = o.Raw(sql, fansNum, userId).Exec()
	return
}

// 更新作者发布文章的数量
func UdpateYanxuanSpecialauthorArticleNum(articleNum, userId int) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special_author SET article_num = ? ,article_publish_time = NOW(), modify_time = NOW()   WHERE user_id = ? `
	_, err = o.Raw(sql, articleNum, userId).Exec()
	return
}

func UpdateYanxuanSpecialauthorPvNUm(pv, userId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_yanxuan_special_author SET pv = ? WHERE user_id = ? `
	_, err = o.Raw(sql, pv, userId).Exec()
	return
}

func UpdateYanxuanSpecialauthorUvUm(pv, userId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_yanxuan_special_author SET uv = ? WHERE user_id = ? `
	_, err = o.Raw(sql, pv, userId).Exec()
	return
}

// 更新作者分享到朋友圈的封面图片
func UpdateYanxuanSpecialauthorMomentsImg(momentsImg string, userId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_yanxuan_special_author SET moments_img = ?  WHERE user_id = ? `
	_, err = o.Raw(sql, momentsImg, userId).Exec()
	return
}
