package material

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

type Material struct {
	MaterialId      int       `orm:"column(material_id);pk" description:"素材id"`
	MaterialName    string    `description:"素材名称"`
	MaterialNameEn  string    `description:"英文素材名称"`
	ImgUrl          string    `description:"素材图片地址"`
	SysUserId       int       `description:"作者id"`
	SysUserRealName string    `description:"作者名称"`
	ModifyTime      time.Time `description:"修改时间"`
	CreateTime      time.Time `description:"创建时间"`
	ClassifyId      int       `description:"分类id"`
	Sort            int       `description:"排序"`
}

// Update 素材字段变更
func (material *Material) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Update(material, cols...)
	return
}

// GetMaterialById 根据素材id获取素材详情
func GetMaterialById(MaterialId int) (materialInfo *Material, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `select * FROM material where material_id = ?`
	err = o.Raw(sql, MaterialId).QueryRow(&materialInfo)
	return
}
func DeleteByMaterialIds(ids []int) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `DELETE FROM material WHERE material_id in (` + utils.GetOrmInReplace(len(ids)) + `)`
	_, err = o.Raw(sql, ids).Exec()
	return
}

func UpdateClassifyByMaterialIds(ids []int, classifyId int, now time.Time) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `UPDATE material SET classify_id=?, modify_time=? WHERE material_id in (` + utils.GetOrmInReplace(len(ids)) + `) and classify_id != ?`
	_, err = o.Raw(sql, classifyId, now, ids, classifyId).Exec()
	return
}

// AddMultiMaterial 批量添加素材
func AddMultiMaterial(materialInfoList []*Material) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.InsertMulti(utils.MultiAddNum, materialInfoList)
	if err != nil {
		return
	}
	return
}

// MaterialListItem 素材推演列表数据
type MaterialListItem struct {
	MaterialId      int    `description:"素材id"`
	MaterialName    string `description:"素材名称"`
	MaterialNameEn  string `description:"英文素材名称"`
	ImgUrl          string `description:"素材图片地址"`
	ModifyTime      string `description:"修改时间"`
	CreateTime      string `description:"创建时间"`
	SysUserId       int    `description:"作者id"`
	SysUserRealName string `description:"作者名称"`
}

// MaterialSaveResp 保存素材响应体
type MaterialSaveResp struct {
	*Material
}

type MaterialListItems struct {
	Material
	ModifyTime string `description:"修改时间"`
	CreateTime string `description:"创建时间"`
	//ParentIds  string
}

func GetMaterialListPageByCondition(condition string, pars []interface{}, startSize, pageSize int) (item []*MaterialListItems, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT * FROM material WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += " ORDER BY create_time DESC, material_id DESC LIMIT ?,? "
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
	return
}

func GetMaterialListByCondition(condition string, pars []interface{}) (item []*MaterialListItems, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT * FROM material WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += " ORDER BY create_time DESC, material_id DESC"
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

func GetMaterialListCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT COUNT(1) AS count FROM material WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

type MaterialListResp struct {
	Paging *paging.PagingItem
	List   []*MaterialListItems
}

func AddMaterial(item *Material) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("rddp")
	lastId, err = o.Insert(item)
	return
}

// GetMaterialByClassifyIdAndName 根据分类id和素材名获取图表信息
func GetMaterialByClassifyIdAndName(classifyId int, name string) (item *Material, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT * FROM material WHERE classify_id = ? and material_name=? `
	err = o.Raw(sql, classifyId, name).QueryRow(&item)
	return
}

// GetMaterialByIds 根据素材id获取素材信息
func GetMaterialByIds(ids []int) (items []*MaterialListItems, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT * FROM material WHERE material_id in (` + utils.GetOrmInReplace(len(ids)) + `) `
	_, err = o.Raw(sql, ids).QueryRows(&items)
	return
}

// SaveAsMaterialReq 添加素材的请求数据
type SaveAsMaterialReq struct {
	MaterialName string `description:"素材名称"`
	ClassifyId   int    `description:"分类id"`
	ObjectId     int    `description:"对象id"`
	ObjectType   string `description:"对象类型:chart,excel,sandbox，sa_doc"`
}

// MyChartSaveAsMaterialReq 添加素材的
type MyChartSaveAsMaterialReq struct {
	MaterialList []*MyChartSaveAsMaterialItem
}

type MyChartSaveAsMaterialItem struct {
	MaterialName string `description:"素材名称"`
	ChartInfoId  int    `description:"图表id"`
	MyChartId    int    `description:"我的图表ID"`
	ClassifyId   int    `description:"分类id"`
}

// BatchAddMaterialReq 批量添加素材的请求数据
type BatchAddMaterialReq struct {
	MaterialList []BatchAddMaterialItem
	ClassifyId   int `description:"分类id"`
}

type BatchAddMaterialItem struct {
	MaterialName string `description:"素材名称"`
	ImgUrl       string `description:"素材图片地址"`
}

// DeleteMaterial 删除素材的请求数据
type DeleteMaterial struct {
	MaterialId int `description:"素材id"`
}

// BatchDeleteMaterialReq 删除素材的请求数据
type BatchDeleteMaterialReq struct {
	MaterialIds []int  `description:"素材id"`
	ClassifyId  int    `description:"分类id"`
	IsShowMe    bool   `description:"操作人id，支持多选，用英文,隔开"`
	Keyword     string `description:"关键字"`
	IsSelectAll bool   `description:"是否选择所有素材"`
}

type BatchChangeClassifyMaterialReq struct {
	BatchDeleteMaterialReq
	NewClassifyId int `description:"新分类ID"`
}

type ChangeClassifyMaterialReq struct {
	MaterialId    int `description:"素材id"`
	NewClassifyId int `description:"新分类ID"`
}

// RenameMaterialReq 添加/编辑素材的请求数据
type RenameMaterialReq struct {
	MaterialId   int    `description:"素材id"`
	MaterialName string `description:"素材名称"`
}

func GetMaterialInfoCountByClassifyIds(classifyIds []string) (count int, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` SELECT COUNT(1) AS count FROM material WHERE classify_id in(` + utils.GetOrmInReplace(len(classifyIds)) + `)`
	err = o.Raw(sql, classifyIds).QueryRow(&count)
	return
}

func GetMaterialByNames(materialNames []string) (items []*Material, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM material WHERE material_name in (` + utils.GetOrmInReplace(len(materialNames)) + `) limit 1`
	_, err = o.Raw(sql, materialNames).QueryRows(&items)
	return
}

func GetMaterialByNameEns(materialNames []string) (items []*Material, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM material WHERE material_name_en in (` + utils.GetOrmInReplace(len(materialNames)) + `) limit 1`
	_, err = o.Raw(sql, materialNames).QueryRows(&items)
	return
}

func GetMaterialByName(materialName string) (item *Material, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM material WHERE material_name = ?`
	err = o.Raw(sql, materialName).QueryRow(&item)
	return
}
func GetMaterialByNameEn(materialName string) (item *Material, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM material WHERE material_name_en = ?`
	err = o.Raw(sql, materialName).QueryRow(&item)
	return
}

// GetMaterialMaxSort 获取最大的排序数
func GetMaterialMaxSort() (sort int, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT MAX(sort) AS sort FROM material `
	err = o.Raw(sql).QueryRow(&sort)
	return
}
