package models

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

type Classify struct {
	Id                   int       `orm:"column(id);pk"`
	ClassifyName         string    `description:"分类名称"`
	Sort                 int       `json:"-"`
	ParentId             int       `description:"父级分类id"`
	CreateTime           time.Time `description:"创建时间"`
	ModifyTime           time.Time `description:"修改时间"`
	Abstract             string    `description:"栏目简介"`
	Descript             string    `description:"分享描述"`
	ReportAuthor         string    `description:"栏目作者"`
	AuthorDescript       string    `description:"作者简介"`
	ColumnImgUrl         string    `description:"栏目配图"`
	HeadImgUrl           string    `description:"头部banner"`
	AvatarImgUrl         string    `description:"头像"`
	ReportImgUrl         string    `description:"报告配图"`
	HomeImgUrl           string    `description:"首页配图"`
	ClassifyLabel        string    `description:"分类标签"`
	ShowType             int       `description:"展示类型：1-列表 2-专栏"`
	HasTeleconference    int       `description:"是否有电话会：0-否 1-是"`
	VipTitle             string    `description:"研究员头衔"`
	IsShow               int       `description:"是否在小程序显示：1-显示 0-隐藏"`
	YbFiccSort           int       `description:"小程序FICC页排序"`
	YbFiccIcon           string    `description:"小程序FICC页icon"`
	YbFiccPcIcon         string    `description:"小程序PC端FICC页背景图"`
	YbIconUrl            string    `description:"小程序已购页icon"`
	YbBgUrl              string    `description:"小程序已购详情背景图"`
	YbListImg            string    `description:"小程序研报列表封面图"`
	YbShareBgImg         string    `description:"小程序研报详情分享背景图"`
	YbRightBanner        string    `description:"Pc端详情页，右侧，报告合集背景图"`
	RelateTel            int       `description:"是否在电话会中可选: 0-否; 1-是"`
	RelateVideo          int       `description:"是否在路演视频中可选: 0-否; 1-是"`
	IsMassSend           int       `description:"1:群发,0:非群发"`
	Enabled              int       `description:"是否可用，1可用，0禁用"`
	Level                int       `description:"层级"`
	HasChild             int       `description:"是否有子级别，0：下面没有子分类，1：下面有子分类；默认：0"`
	ReportDetailShowType int       `description:"报告详情的展示类型：1-拼接；2:目录"`
}

func GetClassifyById(classifyId int) (item *Classify, err error) {
	sql := `SELECT * FROM classify WHERE id=? `
	o := orm.NewOrmUsingDB("rddp")
	err = o.Raw(sql, classifyId).QueryRow(&item)
	return
}

type ClassifyList struct {
	Id                int       `orm:"column(id);pk"`
	ClassifyName      string    `description:"分类名称"`
	Sort              int       `description:"排序"`
	ParentId          int       `description:"父级分类id"`
	CreateTime        time.Time `description:"创建时间"`
	ModifyTime        time.Time `description:"修改时间"`
	Abstract          string    `description:"简介"`
	Descript          string    `description:"描述"`
	ClassifyLabel     string    `description:"分类标签"`
	ShowType          int       `description:"展示类型：1-列表 2-专栏"`
	HasTeleconference int       `description:"是否有电话会：0-否 1-是"`
	IsShow            int       `description:"是否在小程序显示：1-显示 0-隐藏"`
	YbFiccSort        int       `description:"小程序FICC页排序"`
	YbFiccIcon        string    `description:"小程序FICC页icon"`
	YbFiccPcIcon      string    `description:"小程序PC端FICC页背景图"`
	YbIconUrl         string    `description:"小程序已购页icon"`
	YbBgUrl           string    `description:"小程序已购详情背景图"`
	YbListImg         string    `description:"小程序研报列表封面图"`
	YbShareBgImg      string    `description:"小程序研报详情分享背景图"`
	YbRightBanner     string    `description:"Pc端详情页，右侧，报告合集背景图"`
	RelateTel         int       `description:"是否在电话会中可选: 0-否; 1-是"`
	RelateVideo       int       `description:"是否在路演视频中可选: 0-否; 1-是"`
	Enabled           int       `description:"是否可用，1可用，0禁用"`
	Child             []*ClassifyItem
	ClassifyMenuList  []*ClassifyMenu
}

type ClassifyItem struct {
	Classify
	ClassifyMenuId   int `description:"二级分类-子目录ID"`
	ClassifyMenuList []*ClassifyMenu
}

type ClassifyListResp struct {
	List []*ClassifyListV2
}
type ClassifyListV2 struct {
	Id                    int               `orm:"column(id);pk"`
	ClassifyName          string            `description:"分类名称"`
	Sort                  int               `description:"排序"`
	ParentId              int               `description:"父级分类id"`
	CreateTime            time.Time         `description:"创建时间"`
	ModifyTime            time.Time         `description:"修改时间"`
	Abstract              string            `description:"简介"`
	Descript              string            `description:"描述"`
	ClassifyLabel         string            `description:"分类标签"`
	ShowType              int               `description:"展示类型：1-列表 2-专栏"`
	HasTeleconference     int               `description:"是否有电话会：0-否 1-是"`
	IsShow                int               `description:"是否在小程序显示：1-显示 0-隐藏"`
	YbFiccSort            int               `description:"小程序FICC页排序"`
	YbFiccIcon            string            `description:"小程序FICC页icon"`
	YbFiccPcIcon          string            `description:"小程序PC端FICC页背景图"`
	YbIconUrl             string            `description:"小程序已购页icon"`
	YbBgUrl               string            `description:"小程序已购详情背景图"`
	YbListImg             string            `description:"小程序研报列表封面图"`
	YbShareBgImg          string            `description:"小程序研报详情分享背景图"`
	YbRightBanner         string            `description:"Pc端详情页，右侧，报告合集背景图"`
	RelateTel             int               `description:"是否在电话会中可选: 0-否; 1-是"`
	RelateVideo           int               `description:"是否在路演视频中可选: 0-否; 1-是"`
	Enabled               int               `description:"是否可用，1可用，0禁用"`
	Child                 []*ClassifyListV2 `gorm:"-"`
	ClassifyMenuId        int               `description:"二级分类-子目录ID"`
	ClassifyMenuList      []*ClassifyMenu   `gorm:"-"`
	ChartPermissionIdList []int             `description:"绑定的权限ID" gorm:"-"`
	Level                 int               `description:"层级"`
	HasChild              int               `description:"是否有子级别，0：下面没有子分类，1：下面有子分类；默认：0"`
	ReportDetailShowType  int               `description:"报告详情的展示类型：1-拼接；2:目录"`
}

// 获取分类列表
func GetClassifyList(startSize, pageSize int, keyWord, companyType string, hideDayWeek int) (items []*ClassifyList, err error) {
	sql := ``
	companyTypeSqlStr := ``
	if companyType == "ficc" {
		companyTypeSqlStr = " AND id != 40 AND parent_id != 40 "
	} else if companyType == "权益" {
		companyTypeSqlStr = " AND (id = 40 or parent_id = 40)  "
	}
	if keyWord != "" {
		sql = `SELECT * FROM (
                   SELECT * FROM classify
                   WHERE parent_id=0 ` + companyTypeSqlStr + `  AND classify_name LIKE '%` + keyWord + `%'
                   UNION
                   SELECT * FROM classify
                   WHERE id IN(SELECT parent_id FROM classify
                   WHERE parent_id>0 ` + companyTypeSqlStr + `  AND classify_name LIKE '%` + keyWord + `%')
                   )AS t
                   ORDER BY sort ASC,create_time ASC
                   LIMIT ?,? `
	} else {
		sql = `SELECT * FROM classify WHERE parent_id=0 ` + companyTypeSqlStr
		if hideDayWeek == 1 {
			sql += ` AND classify_name <> '晨报' AND classify_name <> '周报' `
		}
		sql += ` ORDER BY sort ASC, create_time ASC LIMIT ?,? `
	}
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql, startSize, pageSize).QueryRows(&items)
	return
}

func GetClassifyListCount(keyWord, companyType string, hideDayWeek int) (count int, err error) {
	sqlCount := ``

	companyTypeSqlStr := ``
	if companyType == "ficc" {
		companyTypeSqlStr = " AND id != 40 AND parent_id != 40 "
	} else if companyType == "权益" {
		companyTypeSqlStr = " AND (id = 40 or parent_id = 40)  "
	}
	if keyWord != "" {
		sqlCount = `SELECT  COUNT(1) AS count FROM (
               SELECT * FROM classify
               WHERE parent_id=0 ` + companyTypeSqlStr + `  AND classify_name LIKE '%` + keyWord + `%'
               UNION
               SELECT * FROM classify
               WHERE id IN(SELECT parent_id FROM classify
               WHERE parent_id>0 ` + companyTypeSqlStr + `  AND classify_name LIKE '%` + keyWord + `%')
               )AS t `

	} else {
		sqlCount = `SELECT COUNT(1) AS count FROM classify WHERE parent_id=0 ` + companyTypeSqlStr
		if hideDayWeek == 1 {
			sqlCount += ` AND classify_name <> '晨报' AND classify_name <> '周报' `
		}
	}
	o := orm.NewOrmUsingDB("rddp")
	err = o.Raw(sqlCount).QueryRow(&count)
	return
}

type FindByIdClassifyReq struct {
	ClassifyId int `description:"分类ID"`
}

func GetClassifyChildByParentIds(parentId []int, keyWord string) (items []*Classify, err error) {
	parentIdLen := len(parentId)
	if parentIdLen == 0 {
		return
	}
	o := orm.NewOrmUsingDB("rddp")
	sql := ``
	if keyWord != "" {
		sql = `SELECT * FROM classify WHERE parent_id IN (` + utils.GetOrmInReplace(parentIdLen) + `) AND classify_name LIKE '%` + keyWord + `%' ORDER BY create_time ASC `
	} else {
		sql = `SELECT * FROM classify WHERE parent_id IN (` + utils.GetOrmInReplace(parentIdLen) + `) ORDER BY create_time ASC `
	}
	_, err = o.Raw(sql, parentId).QueryRows(&items)
	return
}

// GetAllClassify 获取所有分类
func GetAllClassify() (list []*Classify, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT * FROM classify `
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// SimpleClassifyList 简版分类列表
type SimpleClassifyList struct {
	Id           int       `description:"分类ID"`
	ClassifyName string    `description:"分类名称"`
	ParentId     int       `description:"父级ID"`
	Sort         int       `description:"排序"`
	CreateTime   time.Time `description:"创建时间"`
	Level        int       `description:"层级"`
	Child        []*SimpleClassifyList
}

// GetClassifyByCondition 获取分类列表
func GetClassifyByCondition(condition, orderRule string, pars []interface{}) (items []*SimpleClassifyList, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM classify WHERE 1 = 1 `
	if condition != `` {
		sql += condition
	}
	order := `sort ASC, create_time ASC`
	if orderRule != `` {
		order = orderRule
	}
	sql += ` ORDER BY ` + order
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// GetAllClassifyByRelateVideo
// @Description: 获取关联视频的所有分类
// @author: Roc
// @datetime 2024-07-12 17:19:06
// @return list []*Classify
// @return err error
func GetAllClassifyByRelateVideo() (list []*Classify, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT * FROM classify where relate_video = 1`
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetClassifyListByParentIdList
// @Description: 获取分类列表
// @author: Roc
// @datetime 2024-06-19 09:49:33
// @param keyWord string
// @param enabled int
// @return items []*ClassifyList
// @return err error
func GetClassifyListByParentIdList(parentClassifyIdList []int) (items []*Classify, err error) {
	num := len(parentClassifyIdList)
	if num <= 0 {
		return
	}
	sql := `SELECT * FROM classify WHERE id in (` + utils.GetOrmInReplace(num) + `) ORDER BY sort ASC, create_time ASC`

	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Raw(sql, parentClassifyIdList).QueryRows(&items)
	return
}
