package models

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 (m *CygxTag) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(m, cols...)
	return
}

type CygxTagList 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    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);NOT NULL"`         // 状态：0-禁用 1-启用
	CheckList     []string // ABCD勾选了哪几列
	TagType       int      `description:"1：热门活动、2：海外研究、3：路演回放、4：语音问答"`
}

type CygxTagListResp struct {
	List           []*CygxTagList
	ListPermission []*ChartPermission
}

// 列表
func GetCygxTagListCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxTagList, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_tag 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
}

// 获取tag列表
func GetCygxTagList(cond string) (items []*CygxTagList, err error) {
	o := orm.NewOrm()
	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
}

func UpdateCygxTagStatus(id, status int) (err error) {
	o := orm.NewOrm()
	sql := ``
	if status == 1 {
		sql = ` UPDATE  cygx_tag
			SET
			  status =1,
			  online_time = NOW(),
			  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.NewOrm()
	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.NewOrm()
	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.NewOrm()
	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.NewOrm()
	sql := `update cygx_tag set sort = sort + 1 where sort >= ? and sort< ? `
	_, err = o.Raw(sql, prevSort, currentSort).Exec()
	return
}

type CygxTagIdReq struct {
	TagId int `description:"TagId"`
}
