package models

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

// 英文策略报告
type EnglishPolicyReport struct {
	Id                 int       `orm:"column(id);pk;auto" description:"策略报告Id"`
	ClassifyIdFirst    int       `description:"一级分类id"`
	ClassifyNameFirst  string    `description:"一级分类名称"`
	ClassifyIdSecond   int       `description:"二级分类id"`
	ClassifyNameSecond string    `description:"二级分类名称"`
	Title              string    `description:"标题"`
	Abstract           string    `description:"摘要"`
	Author             string    `description:"作者"`
	Frequency          string    `description:"频度"`
	CreateTime         time.Time `description:"创建时间"`
	ModifyTime         time.Time `description:"修改时间"`
	State              int       `description:"1:未同步，2:已同步"`
	SyncTime           time.Time `description:"同步时间"`
	PublishStatus      int       `description:"1:未发布，2:已发布"`
	PublishTime        time.Time `description:"发布时间"`
	Content            string    `description:"内容"`
	KeyTakeaways       string    `description:"关键点"`
	AuthorMobile       string    `description:"创建者手机号"`
	SyncReportId       int       `description:"同步后的report_id为english_report的ID"`
	SourceReportId     int       `description:"原始报告ID"`
	ReportCoverUrl     string    `description:"原始报告封面图"`
}

// AddEnglishPolicyReportMulti 批量新增report
func AddEnglishPolicyReportMulti(list []*EnglishPolicyReport) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.InsertMulti(len(list), list)
	return
}

type EnglishPolicyReportDetail struct {
	Id                 int    `description:"策略报告Id"`
	ClassifyIdFirst    int    `description:"一级分类id"`
	ClassifyNameFirst  string `description:"一级分类名称"`
	ClassifyIdSecond   int    `description:"二级分类id"`
	ClassifyNameSecond string `description:"二级分类名称"`
	Title              string `description:"标题"`
	Abstract           string `description:"摘要"`
	Author             string `description:"作者"`
	Frequency          string `description:"频度"`
	//CreateTime         string `description:"创建时间"`
	PublishTime  string `description:"发布时间"`
	ModifyTime   string `description:"修改时间"`
	State        int    `description:"同步状态：1:未同步，2:已同步"`
	SyncTime     string `description:"同步时间"`
	Content      string `description:"内容"`
	KeyTakeaways string `description:"关键点"`
	AuthorMobile string `description:"创建者手机号"`
	SyncReportId int    `description:"同步后的report_id为english_report的ID"`
	//SourceReportId     int    `description:"原始报告ID"`
	ReportCoverUrl string `description:"原始报告封面图"`
}

func GetEnglishPolicyReportById(reportId int) (item *EnglishPolicyReportDetail, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM english_policy_report WHERE id=?`
	err = o.Raw(sql, reportId).QueryRow(&item)
	return
}

func GetEnglishPolicyReportBySourceReportId(sourceReportId int) (item *EnglishPolicyReport, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * FROM english_policy_report WHERE source_report_id = ? LIMIT 1`
	err = o.Raw(sql, sourceReportId).QueryRow(&item)
	return
}

type EnglishPolicyReportList struct {
	Id                 int    `orm:"column(id);pk;auto" description:"策略报告Id"`
	ClassifyIdFirst    int    `description:"一级分类id"`
	ClassifyNameFirst  string `description:"一级分类名称"`
	ClassifyIdSecond   int    `description:"二级分类id"`
	ClassifyNameSecond string `description:"二级分类名称"`
	Title              string `description:"标题"`
	Abstract           string `description:"摘要"`
	Author             string `description:"作者"`
	Frequency          string `description:"频度"`
	//CreateTime         string `description:"创建时间"`
	PublishTime string `description:"发布时间"`
	ModifyTime  string `description:"修改时间"`
	State       int    `description:"1:未同步，2:已同步"`
	SyncTime    string `description:"同步时间"`
	//Content            string `description:"内容"`
	KeyTakeaways string `description:"关键点"`
	AuthorMobile string `description:"创建者手机号"`
	SyncReportId int    `description:"同步后的report_id为english_report的ID"`
	//SourceReportId     int       `description:"原始报告ID"`
	ReportCoverUrl string `description:"原始报告封面图"`
}

type EnglishPolicyReportListResp struct {
	List   []*EnglishPolicyReportList
	Paging *paging.PagingItem `description:"分页数据"`
}

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

func GetEnglishPolicyReportList(condition string, pars []interface{}, startSize, pageSize int) (items []*EnglishPolicyReportList, err error) {
	o := orm.NewOrmUsingDB("rddp")
	//产品权限
	sql := `SELECT * 
        FROM english_policy_report WHERE 1=1  `
	if condition != "" {
		sql += condition
	}
	sql += `ORDER BY  publish_time DESC LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func GetEnglishPolicyReportByCondition(condition string, pars []interface{}) (items []*EnglishPolicyReport, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT * 
        FROM english_policy_report WHERE 1=1  `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

type SyncEnglishPolicyReq struct {
	Id int `description:"策略报告id"`
}

type PullEnglishPolicyDataResp struct {
	Num int `description:"成功获取的记录数"`
}

// 同步报告
func SyncEnglishPolicyReportById(id, newReportId int) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `UPDATE english_policy_report SET state=2, sync_time=now(),modify_time=NOW(), sync_report_id=? WHERE id = ? `
	_, err = o.Raw(sql, newReportId, id).Exec()
	return
}

// 设置策略报告内容
func SetEnglishPolicyReportContentBySourceReportId(sourceReportId int, content string) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `UPDATE english_policy_report SET content=?, modify_time=NOW() WHERE source_report_id = ? `
	_, err = o.Raw(sql, content, sourceReportId).Exec()
	return
}

// 取消发布报告
func SyncCancelEnglishPolicyReport(reportId int) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` UPDATE english_policy_report SET state=1,sync_time=null,modify_time=NOW(), sync_report_id=0 WHERE id =?  `
	_, err = o.Raw(sql, reportId).Exec()
	return
}

// 删除报告
func DeleteEnglishPolicyReport(reportIds int) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := ` DELETE FROM english_policy_report WHERE id =? `
	_, err = o.Raw(sql, reportIds).Exec()
	return
}

// Update 更新
func (item *EnglishPolicyReport) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("rddp")
	_, err = o.Update(item, cols...)
	return
}

// GetEnglishPolicyReportByIds 根据IDs获取英文报告列表
func GetEnglishPolicyReportByIds(reportIds []int, fieldArr []string) (list []*EnglishPolicyReport, err error) {
	listLen := len(reportIds)
	if listLen == 0 {
		return
	}
	fields := ` * `
	if len(fieldArr) > 0 {
		fields = strings.Join(fieldArr, ",")
	}
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT ` + fields + ` FROM english_policy_report WHERE id IN (` + utils.GetOrmInReplace(listLen) + `)`
	_, err = o.Raw(sql, reportIds).QueryRows(&list)
	return
}
