package cygx

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

type CygxTag struct {
	TagId         int64     `orm:"column(tag_id);pk"`
	TagName       string    `orm:"column(tag_name);NOT NULL"`       // 标签名
	ArticleTypes  string    `orm:"column(article_types);NOT NULL"`  // 报告系列
	ActivityTypes string    `orm:"column(activity_types);NOT NULL"` // 活动类型
	Industries    string    `orm:"column(industries);NOT NULL"`     // 产业
	SubjectNames  string    `orm:"column(subject_names);NOT NULL"`  // 标的
	Sort          int       `orm:"column(sort);"`                   // 优先级
	ModifyTime    time.Time `orm:"column(modify_time)"`             // 修改时间
	CreateTime    time.Time `orm:"column(create_time)"`             // 创建时间
	OnlineTime    time.Time `orm:"column(online_time)"`             // 上线时间
	OfflineTime   time.Time `orm:"column(offline_time)"`            // 下线时间
	Status        int       `orm:"column(status);NOT NULL"`         // 状态：0-禁用 1-启用
}

// 添加标签
func AddCygxTag(item *CygxTag) (id int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	id, err = o.Insert(item)
	return
}

// 通过ID获取分类详情
func GetCygxTagInfoById(tagId int) (item *CygxTagList, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_tag WHERE tag_id=?`
	err = o.Raw(sql, tagId).QueryRow(&item)
	return
}

func (m *CygxTag) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Update(m, cols...)
	return
}

type CygxTagList struct {
	TagId         int64  `orm:"column(tag_id);pk"`
	TagName       string `orm:"column(tag_name);"`       // 标签名
	ArticleTypes  string `orm:"column(article_types);"`  // 报告系列
	ActivityTypes string `orm:"column(activity_types);"` // 活动类型
	Industries    string `orm:"column(industries);"`     // 产业
	SubjectNames  string `orm:"column(subject_names);"`  // 标的
	Sort          int    `orm:"column(sort);"`           // 优先级
	ModifyTime    string `orm:"column(modify_time)"`     // 修改时间
	CreateTime    string `orm:"column(create_time)"`     // 创建时间
	OnlineTime    string `orm:"column(online_time)"`     // 上线时间
	OfflineTime   string `orm:"column(offline_time)"`    // 下线时间
	Status        int    `orm:"column(status);"`         // 状态：0-禁用 1-启用
	Pv            int    `description:"PV"`
	Uv            int    `description:"UV"`
	TagType       int    `description:"1：热门活动、2：海外研究、3：路演回放、4：语音问答"`
}

// 获取tag列表
func GetCygxTagList(cond string) (items []*CygxTagList, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_tag  WHERE 1=1 AND status = 1 `
	if cond != "" {
		sql += cond
	} else {
		sql += `  ORDER BY sort ASC `
	}
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 获取tag列表-总数
func GetCygxTagListCount(cond string) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT COUNT(1) AS count FROM cygx_tag  WHERE 1=1  `
	if cond != "" {
		sql += cond
	} else {
		sql += `  ORDER BY sort ASC `
	}
	err = o.Raw(sql).QueryRow(&count)
	return
}

// 获取tag列表-总数
func GetCygxTagListCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT COUNT(1) AS count FROM cygx_tag  WHERE 1=1  ` + condition

	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

// 获取tag列表-分页
func GetCygxTagListPage(cond string, startSize, pageSize int) (items []*CygxTagList, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT *,
		(SELECT COUNT(1) FROM cygx_tag_history AS avr WHERE avr.tag_id=cygx_tag.tag_id AND avr.company_id != 16  ) AS pv,
        (SELECT COUNT(DISTINCT user_id) FROM cygx_tag_history AS avr WHERE avr.tag_id=cygx_tag.tag_id AND avr.company_id != 16 ) AS uv
		FROM cygx_tag  WHERE 1=1 `
	if cond != "" {
		sql += cond
	} else {
		sql += `  ORDER BY sort ASC `
	}
	sql += ` LIMIT ?,? `
	_, err = o.Raw(sql, startSize, pageSize).QueryRows(&items)
	return
}

func UpdateCygxTagStatus(id, status int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ``
	if status == 1 {
		sql = ` UPDATE  cygx_tag
			SET
			  status =1,
			  modify_time = NOW() 
			WHERE tag_id = ?`
	} else {
		sql = ` UPDATE  cygx_tag
			SET
			  status =0,
			  offline_time = NOW(),
			  modify_time = NOW() 
			WHERE tag_id = ?`
	}

	_, err = o.Raw(sql, id).Exec()
	return
}

// GetCygxTagByTagId 根据指标id获取指标信息
func GetCygxTagByTagId(tagId int) (item *CygxTag, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_tag WHERE tag_id=? `
	err = o.Raw(sql, tagId).QueryRow(&item)
	return
}

// GetCygxTagMinSort 获取最小不等于0的排序
func GetCygxTagMinSort() (sort int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT min(sort) FROM cygx_tag WHERE  sort <> 0 `
	err = o.Raw(sql).QueryRow(&sort)
	return
}

// MoveDownCygxTagBySort 往下移动
func MoveDownCygxTagBySort(prevSort, currentSort int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `update cygx_tag set sort = sort - 1 where sort <= ? and sort> ? `
	_, err = o.Raw(sql, prevSort, currentSort).Exec()
	return
}

// MoveUpCygxTagBySort 往下移动
func MoveUpCygxTagBySort(prevSort, currentSort int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `update cygx_tag set sort = sort + 1 where sort >= ? and sort< ? `
	_, err = o.Raw(sql, prevSort, currentSort).Exec()
	return
}

// GetCygxTagMaxSort 获取最大不等于0的排序
func GetCygxTagMaxSort() (sort int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT max(sort) FROM cygx_tag WHERE  sort <> 0 `
	err = o.Raw(sql).QueryRow(&sort)
	return
}
