package models

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         int       // 类型1:笔记，2:观点
	CompanyTags  string    // 公司标签
	IndustryTags string    // 行业标签
	AdminName    string    // 审核人员姓名
}

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不包含
	ContentHasStyle bool //正文是否包含格式
	Docs            []Doc
	Pv              int `description:"Pv"`
	Uv              int `description:"Uv"`
	HzPv            int `description:"HzPv"`
}

type CygxYanxuanSpecialResp struct {
	CygxYanxuanSpecialItem
	Docs           []Doc
	CompanyTags    []string
	IndustryTags   []string
	HasPermission  int  `description:"1:正常展示，2：不展示"`
	ExamineStatus  int  `description:"1:未发布，2:审核中 3:已发布 4:驳回"`
	IsShowExamine  bool `description:"是否展示审核状态按钮"`
	IsFollowAuthor bool // 是否已关注专栏
}

type CygxYanxuanSpecialCenterResp 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不包含
	ContentHasStyle  bool //正文是否包含格式
	Docs             []Doc
	Annotation       string   `description:"核心观点"`
	Source           string   `description:"来源"`
	Pv               int      `description:"Pv"`
	Uv               int      `description:"Uv"`
	HzPv             int      `description:"HzPv"`
	PublishDate      string   // 提审过审或驳回时间      string `description:"核心观点"`
	BodyHighlight    []string `description:"搜索高亮展示结果"`
	LikeCount        int      `description:"点赞数量"`
	LikeCountHistory int      `description:"一个小时前的点赞数量"`
}

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

func GetYanxuanSpecialList(userId int, condition string, pars []interface{}, startSize, pageSize int) (items []*CygxYanxuanSpecialCenterResp, err error) {
	o := orm.NewOrm()
	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,
( SELECT count( 1 ) FROM cygx_yanxuan_special_collect AS ac WHERE ac.yanxuan_special_id = a.id  ) AS collect_num,
( SELECT count( 1 ) FROM cygx_yanxuan_special_collect AS ac WHERE ac.yanxuan_special_id = a.id  AND user_id = ? ) AS my_collect_num 
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 += `ORDER BY a.publish_time DESC `
	if startSize+pageSize > 0 {
		sql += ` LIMIT ?,? `
		_, err = o.Raw(sql, userId, pars, startSize, pageSize).QueryRows(&items)
	} else {
		_, err = o.Raw(sql, userId, pars).QueryRows(&items)
	}
	return
}

func GetYanxuanSpecialListBycondition(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxYanxuanSpecialCenterResp, err error) {
	o := orm.NewOrm()
	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
}

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

func EnableYanxuanSpecial(id, status int, reason, adminName string) (err error) {
	o := orm.NewOrm()
	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 SpecialListResp struct {
	IsAuthor             bool               `description:"是否开通了研选专栏"`
	IsImproveInformation bool               `description:"作者信息是否完善"`
	Paging               *paging.PagingItem `description:"分页数据"`
	List                 []*CygxYanxuanSpecialCenterResp
}

func GetYanxuanSpecialById(specialId, userId int) (item *CygxYanxuanSpecialItem, err error) {
	o := orm.NewOrm()
	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,
( SELECT count( 1 ) FROM cygx_yanxuan_special_collect AS ac WHERE ac.yanxuan_special_id = a.id  ) AS collect_num,
( SELECT count( 1 ) FROM cygx_yanxuan_special_collect AS ac WHERE ac.yanxuan_special_id = a.id  AND user_id = ? ) AS my_collect_num
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, userId, specialId).QueryRow(&item)
	return
}

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

type CygxYanxuanSpecialReq struct {
	Id                  int    `orm:"column(id);pk"`
	Content             string // 内容
	Tags                string // 标签
	DoType              int    // 1保存 2发布
	ImgUrl              string // 图片链接
	DocUrl              string // 文档链接
	Title               string // 标题
	Type                int    // 类型1:笔记，2:观点
	IndustryTags        string // 行业标签
	CompanyTags         string // 公司标签
	IsApprovalPersonnel bool   // 是否是审批人员操作
}

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

func UpdateYanxuanSpecial(item *CygxYanxuanSpecial) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special SET title=?,content=?,company_tags=?,industry_tags=?,img_url=?,doc_url=?,type=?,status=?,
	modify_time=NOW(),publish_time=NOW(),admin_name = ? WHERE id = ? `
	_, err = o.Raw(sql, item.Title, item.Content, item.CompanyTags, item.IndustryTags, item.ImgUrl, item.DocUrl, item.Type, item.Status, item.AdminName, item.Id).Exec()
	return
}

func GetYanxuanSpecialIndustry(keyword string) (IndustryNames []string, err error) {
	o := orm.NewOrm()
	sql := ``
	if keyword == "" {
		sql = `SELECT industry_name FROM cygx_yanxuan_special_industry `
	} else {
		sql = `SELECT industry_name FROM cygx_yanxuan_special_industry WHERE industry_name LIKE '%` + keyword + `%' `
	}
	_, err = o.Raw(sql).QueryRows(&IndustryNames)
	return
}

type CancelPublishCygxYanxuanSpecialReq struct {
	Id int // 文章id
}

func CancelPublishYanxuanSpecial(id int) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special SET status=1,publish_time=NOW(),modify_time=NOW() WHERE id = ? `
	_, err = o.Raw(sql, id).Exec()
	return
}

type DelCygxYanxuanSpecialReq struct {
	Id int // 文章id
}

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

type CygxYanxuanSpecialCheckReq struct {
	Content string   // 内容
	ImgUrl  []string // 图片
}

func GetYanxuanSpecialFollowUserById(specialId int) (items []int, err error) {
	o := orm.NewOrm()
	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.NewOrm()
	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 as a  WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrm()
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

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

// UpdateYanxuanSpecialHzPv 修改研选专栏的阅读弘则Pv
func UpdateYanxuanSpecialHzPv(id int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_yanxuan_special SET hz_pv=hz_pv+1 WHERE id = ? `
	_, err = o.Raw(sql, id).Exec()
	return
}

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

// UpdateYanxuanSpecialHzUv 修改研选专栏的阅读弘则Uv
func UpdateYanxuanSpecialHzUv(id int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_yanxuan_special SET hz_uv=hz_uv+1 WHERE id = ? `
	_, err = o.Raw(sql, id).Exec()
	return
}

func UpdateYanxuanSpecialPvNUm(pv, id int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_yanxuan_special SET pv = ? WHERE id = ? `
	_, err = o.Raw(sql, pv, id).Exec()
	return
}

func UpdateYanxuanSpecialUvUm(pv, id int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_yanxuan_special SET uv = ? WHERE id = ? `
	_, err = o.Raw(sql, pv, id).Exec()
	return
}

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

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

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

// 添加朋友圈分享封面图片
func UpdateYanxuanSpecialMomentsImg(momentsImg string, id int) (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special SET moments_img = ?  WHERE id = ? `
	_, err = o.Raw(sql, momentsImg, id).Exec()
	return
}

// 更新历史点赞数量
func UpdateYanxuanSpecialLikeCountHistory() (err error) {
	o := orm.NewOrm()
	sql := ``
	sql = `UPDATE cygx_yanxuan_special SET like_count_history=like_count WHERE like_count > like_count_history `
	_, err = o.Raw(sql).Exec()
	return
}

// UpdateYanxuanSpecialHzPv 修改研选专栏的阅读弘则Pv
func Need_1006INIT(hz_pv, hz_uv, id int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_yanxuan_special SET hz_pv= ? , hz_uv = ?  WHERE id = ? `
	_, err = o.Raw(sql, hz_pv, hz_uv, id).Exec()
	return
}
