package cygx

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

type CygxTacticsTimeLine struct {
	TimeLineId  int       `orm:"column(time_line_id);pk"`
	PublishTime time.Time `description:"发布日期"`
	CreateTime  time.Time `description:"创建时间"`
	ModifyTime  time.Time `description:"更新时间"`
	Status      int       `description:"0:未发布，1:已发布"`
	Content     string    `description:"内容"`
	ArticleId   int       `description:"文章ID"`
	ChartId     int       `description:"图表ID"`
	Link        string    `description:"文章或图表链接"`
	AdminId     int       `description:"管理员ID"`
}

type AddTacticsTimeLineReq struct {
	TimeLineId  int    `orm:"column(time_line_id);pk"`
	PublishTime string `description:"发布日期"`
	Content     string `description:"内容"`
	Link        string `description:"文章或图表链接"`
}
type TacticsTimeLineTimeLineIdReq struct {
	TimeLineId int `description:"ID"`
}

// 添加
func AddCygxTacticsTimeLine(item *CygxTacticsTimeLine) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Insert(item)
	return
}

// 修改
func UpdateCygxTacticsTimeLine(item *CygxTacticsTimeLine) (err error) {
	to := orm.NewOrmUsingDB("hz_cygx")
	updateParams := make(map[string]interface{})
	updateParams["PublishTime"] = item.PublishTime
	updateParams["ModifyTime"] = item.ModifyTime
	updateParams["Content"] = item.Content
	updateParams["ArticleId"] = item.ArticleId
	updateParams["ChartId"] = item.ChartId
	updateParams["Link"] = item.Link
	ptrStructOrTableName := "cygx_tactics_time_line"
	whereParam := map[string]interface{}{"time_line_id": item.TimeLineId}
	qs := to.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	return
}

type GetCygxTacticsTimeLineResp struct {
	Status int                `description:"0:内部可见，1:全部可见"`
	Paging *paging.PagingItem `description:"分页数据"`
	List   []*CygxTacticsTimeLineResp
}

type CygxTacticsTimeLineResp struct {
	TimeLineId  int    `description:"ID"`
	PublishTime string `description:"发布日期"`
	CreateTime  string `description:"创建时间"`
	ModifyTime  string `description:"更新时间"`
	Status      int    `description:"0:未发布，1:已发布"`
	Content     string `description:"内容"`
	ArticleId   int    `description:"文章ID"`
	ChartId     int    `description:"图表ID"`
	Link        string `description:"文章或图表链接"`
	Pv          int    `description:"PV"`
	Uv          int    `description:"UV"`
	Title       string `description:"标题"`
}

// 获取数量
func GetCygxTacticsTimeLineCount(condition string, pars []interface{}) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_tactics_time_line as art WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 列表
func GetCygxTacticsTimeLineList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxTacticsTimeLineResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_tactics_time_line as art WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?  `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type GetCygxTacticsTimeLineDetailResp struct {
	Detail *CygxTacticsTimeLineResp
}

// 通过ID获取详情
func GetCygxTacticsTimeLineDetail(timeLineId int) (item *CygxTacticsTimeLineResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_tactics_time_line  WHERE time_line_id=? `
	err = o.Raw(sql, timeLineId).QueryRow(&item)
	return
}

// 删除数据
func DeleteCygxTacticsTimeLine(timeLineId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` DELETE FROM cygx_tactics_time_line WHERE time_line_id = ?`
	_, err = o.Raw(sql, timeLineId).Exec()
	return
}

// 修改是否展示
func EditCygxTacticsTimeLineStatus(status, timeLineId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_tactics_time_line SET status=?, modify_time=NOW()   WHERE time_line_id=? `
	_, err = o.Raw(sql, status, timeLineId).Exec()
	return
}

// 一键修改所有的是否展示
func EditCygxTacticsTimeLineStatusAll(status int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	//sql := `UPDATE cygx_tactics_time_line SET status=?, modify_time=NOW()  `
	//_, err = to.Raw(sql, status).Exec()
	//if err != nil {
	//	return
	//}
	sql := ` UPDATE cygx_config SET  config_value=?   WHERE config_code= ?  `
	_, err = to.Raw(sql, status, utils.CYGX_TACTICS_TIME_LINE_STATUS).Exec()
	if err != nil {
		return
	}
	return
}
