package cygx

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

type CygxResourceData struct {
	Id                int       `orm:"column(id);pk"`
	SourceId          int       `description:"资源ID"`
	Source            string    `description:"资源类型 报告 ：article 、图表 ：newchart、微路演 ：roadshow、活动 ：activity、活动视频：activityvideo、活动音频：activityvoice、专项调研活动：activityspecial"`
	Title             string    `description:"标题"`
	Annotation        string    `description:"核心观点"`
	CreateTime        time.Time `description:"创建时间"`
	PublishDate       string    `description:"发布时间"`
	Abstract          string    `description:"摘要"`
	SearchTag         string    `description:"搜索标签"`
	ChartPermissionId int       `description:"行业id"`
	SearchTitle       string    `description:"搜索匹配用的标题"`
	SearchContent     string    `description:"搜索匹配用的内容"`
	SearchOrderTime   string    `description:"搜索排序时间"`
}

// 根据资源类型获取列表
func GetCygxResourceDataListBysource(source string) (items []*CygxResourceData, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_resource_data as art WHERE 1= 1  AND  source = ?  AND search_title = ''   `
	_, err = o.Raw(sql, source).QueryRows(&items)
	return
}

// 添加
func AddCygxResourceData(item *CygxResourceData) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	lastId, err = o.Insert(item)
	return
}

// 删除数据
func DeleteResourceData(sourceId int, source string) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` DELETE FROM cygx_resource_data WHERE source_id = ? AND source =?  `
	_, err = o.Raw(sql, sourceId, source).Exec()
	return
}

// 修改数据
func UpdateResourceData(sourceId int, source, publishDate string) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_resource_data SET publish_date=?  WHERE source_id=?  AND source =? `
	_, err = o.Raw(sql, publishDate, sourceId, source).Exec()
	return
}

// 修改
func UpdateResourceDataByItem(item *CygxResourceData) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	updateParams := make(map[string]interface{})
	updateParams["PublishDate"] = item.PublishDate
	updateParams["SearchTag"] = item.SearchTag
	updateParams["ChartPermissionId"] = item.ChartPermissionId
	updateParams["SearchTitle"] = item.SearchTitle
	updateParams["SearchContent"] = item.SearchContent
	updateParams["SearchOrderTime"] = item.SearchOrderTime
	ptrStructOrTableName := "cygx_resource_data"
	whereParam := map[string]interface{}{"source_id": item.SourceId, "source": item.Source}
	qs := o.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	if err != nil {
		return
	}
	return
}

// 修改
func UpdateChartPermissionIdResourceDataByItem(item *CygxResourceData) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	updateParams := make(map[string]interface{})
	updateParams["ChartPermissionId"] = item.ChartPermissionId
	ptrStructOrTableName := "cygx_resource_data"
	whereParam := map[string]interface{}{"source_id": item.SourceId, "source": item.Source}
	qs := o.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	if err != nil {
		return
	}
	return
}

// 获取数量
func GetCygxReportSelectionBySourceAndId(sourceId int, source string) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_resource_data  WHERE  source_id = ? AND source =?  `
	err = o.Raw(sqlCount, sourceId, source).QueryRow(&count)
	return
}

// 通过ID跟资源获取详情
func GetCygxResourceDataByIdAndSource(sourceId int, source string) (item *CygxResourceData, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_resource_data WHERE  source_id = ? AND source =?  `
	err = o.Raw(sql, sourceId, source).QueryRow(&item)
	return
}

// 获取数量
func GetCygxResourceDataBySourceAndIdCount(sourceId int, source string) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_resource_data  WHERE  source_id = ? AND source =?  `
	err = o.Raw(sqlCount, sourceId, source).QueryRow(&count)
	return
}
