package models

import (
	"time"

	"github.com/beego/beego/v2/client/orm"
)

type ReportStatus string
type ReportSource string

const (
	SourceETA     ReportSource = "ETA"
	SourceHT      ReportSource = "HT"
	StatusInit    ReportStatus = "INIT"
	StatusPending ReportStatus = "PENDING"
	StatusDone    ReportStatus = "DONE"
	MaxBatchNum                = 1000
	CommonColumns              = "id,org_id,author,abstract,title,source,cover_src,published_time"
	taskColumns                = "id,author,published_time"
)

type Report struct {
	Id            int          `description:"id"`
	OrgId         int          `description:"组织id"`
	Source        ReportSource `description:"来源"`
	Title         string       `description:"标题"`
	Abstract      string       `description:"摘要"`
	Author        string       `description:"作者"`
	CoverSrc      int          `description:"封面"`
	Status        ReportStatus `description:"状态"`
	SendStatus    SendStatus   `description:"发送状态"`
	PublishedTime string       `description:"发布时间"`
	CreatedTime   time.Time    `description:"创建时间"`
	UpdatedTime   time.Time    `description:"更新时间"`
}

type ReportView struct {
	Id              int
	OrgId           int
	Source          ReportSource
	Title           string
	Abstract        string
	Author          string
	CoverSrc        string
	PermissionNames string
	SendStatus      SendStatus
	PublishedTime   string
}

func (r *Report) ToView() (item *ReportView) {
	item = &ReportView{
		Id:            r.Id,
		OrgId:         r.OrgId,
		Source:        r.Source,
		Title:         r.Title,
		Abstract:      r.Abstract,
		Author:        r.Author,
		SendStatus:    r.SendStatus,
		PublishedTime: r.PublishedTime,
	}
	return

}

func (r *Report) TableName() string {
	return "reports"
}
func (r *Report) UpdateSendStatus(status SendStatus) (err error) {
	o := orm.NewOrm()
	r.SendStatus = status
	_, err = o.Update(r, "send_status")
	return
}
func GetReportCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(*) AS count FROM reports WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func GetReportById(id int) (items *Report, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM reports WHERE id =?`
	err = o.Raw(sql, id).QueryRow(&items)
	return
}

func GetReportByCondition(condition, sortCondition string, pars []interface{}, startPage, pageSize int) (items []*Report, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM reports WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	if sortCondition != "" {
		sql += sortCondition // 排序
	}
	sql += ` LIMIT ?,?`
	_, err = o.Raw(sql, pars, startPage, pageSize).QueryRows(&items)
	return
}

func GetETAReportIdsByCondition(condition string, pars []interface{}) (ids []int, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT distinct  id FROM report WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&ids)
	return
}
func GetETAReportById(id int) (classifyId int, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT COALESCE(NULLIF(classify_id_third,0),NULLIF(classify_id_second,0),classify_id_first) classify_id  FROM report WHERE id =?`
	err = o.Raw(sql, id).QueryRow(&classifyId)
	return
}

func GetETAReportIdByClassifyId(id int) (classifyId int, err error) {
	o := orm.NewOrmUsingDB("rddp")
	sql := `SELECT COALESCE(NULLIF(classify_id_third,0),NULLIF(classify_id_second,0),classify_id_first) classify_id FROM report WHERE id=? `
	err = o.Raw(sql, id).QueryRow(&classifyId)
	return
}
