package cygx

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

// CygxMorningMeetingReviewChapter [...]
type CygxMorningMeetingReviewChapter struct {
	Id                   int       `orm:"column(id);pk"`
	MeetingTime          time.Time `json:"meetingTime"` // 晨会日期
	CreateTime           time.Time `json:"createTime"`
	ModifyTime           time.Time `json:"modifyTime"`
	IndustryId           int       `json:"industryId"`          // 产业id
	IndustryName         string    `json:"industryName"`        // 产业名称
	ChartPermissionName  string    `json:"chartPermissionName"` // 行业名称
	ChartPermissionId    int       `json:"chartPermissionId"`   // 行业id
	IndustrialSubjectIds string    `json:"industrialSubjectId"` // 标的id
	MeetingId            int64     `json:"meetingId"`           // 主表id
	Content              string    `json:"content"`             // 内容
	Title                string    `json:"title"`               // 标题
	ReportLink           string    `json:"reportLink"`          // 报告链接
	LinkArticleId        int       `json:"linkArticleId"`       // 报告ID链接
}

type AddMorningMeetingReviewItem struct {
	ChapterId            int
	Content              string
	ChartPermissionId    int
	ChartPermissionName  string
	IndustryId           int
	IndustryName         string
	IndustrialSubjectIds string
	ReportLink           string `description:"报告链接"`
	Title                string `description:"标题"`
}

type AddMorningMeetingReviewsReq struct {
	List        []*AddMorningMeetingReviewItem
	MeetingId   int
	MeetingTime string
	PublishTime string
	DoType      int `description:"操作类型 0，保存 、1，发布"`
}

// 添加晨报点评章节
func AddCygxMorningMeetingReviewChapter(item *CygxMorningMeetingReviewChapter) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Insert(item)
	return
}

// 列表
func GetCygxMorningMeetingReviewsListById(meetingId int) (items []*CygxMorningMeetingReviewChapter, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_morning_meeting_review_chapter WHERE meeting_id = ? `
	_, err = o.Raw(sql, meetingId).QueryRows(&items)
	return
}

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

type IndustrialSubjectItem struct {
	IndustrialSubjectId int    `orm:"column(industrial_subject_id);pk" description:"标的id"`
	SubjectName         string `description:"标的名称"`
}

type CygxMorningMeetingReviewChapterRespItem struct {
	Id                    int                      `orm:"column(id);pk"`
	MeetingTime           time.Time                `json:"meetingTime"` // 晨会日期
	CreateTime            time.Time                `json:"createTime"`
	ModifyTime            time.Time                `json:"modifyTime"`
	IndustryId            int                      `json:"industryId"`            // 产业id
	IndustryName          string                   `json:"industryName"`          // 产业名称
	ChartPermissionName   string                   `json:"chartPermissionName"`   // 行业名称
	ChartPermissionId     int                      `json:"chartPermissionId"`     // 行业id
	IndustrialSubjectList []*IndustrialSubjectItem `json:"industrialSubjectList"` // 标的list
	MeetingId             int64                    `json:"meetingId"`             // 主表id
	Content               string                   `json:"content"`               // 内容
	Title                 string                   `json:"title"`                 // 标题
	ReportLink            string                   `json:"reportLink"`            // 报告链接
}

type CygxMorningMeetingReviewChapterResp struct {
	MeetingTime string
	Status      int
	List        []*CygxMorningMeetingReviewChapterRespItem
}

// 删除晨会点评章节
func DeleteMorningMeetingChapter(reviewId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` DELETE FROM cygx_morning_meeting_review_chapter WHERE meeting_id =? `
	_, err = o.Raw(sql, reviewId).Exec()
	return
}

// 更新晨报点评章节
func UpdateCygxMorningMeetingReviewChapter(item *CygxMorningMeetingReviewChapter) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_morning_meeting_review_chapter
			SET
			  meeting_time =?,
			  modify_time = ?,
			  industry_id = ?,
			  industry_name = ?,
			  chart_permission_id = ?,
			  chart_permission_name = ?,
			  industrial_subject_ids = ?,
			  content = ?,
			  report_link = ?,
			  link_article_id = ?,
			  title = ?
			WHERE id = ? `
	_, err = o.Raw(sql, item.MeetingTime, item.ModifyTime, item.IndustryId, item.IndustryName,
		item.ChartPermissionId, item.ChartPermissionName, item.IndustrialSubjectIds, item.Content, item.ReportLink, item.LinkArticleId, item.Title, item.Id).Exec()

	return
}

func GetCygxMorningMeetingReviewsListByIdAndIndustryId(meetingId, industryId int) (item *CygxMorningMeetingReviewChapter, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_morning_meeting_review_chapter WHERE meeting_id = ? AND industry_id = ? `
	err = o.Raw(sql, meetingId, industryId).QueryRow(&item)
	return
}

// 删除晨会点评章节
func DeleteMorningMeetingChapterById(chapterId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` DELETE FROM cygx_morning_meeting_review_chapter WHERE id =? `
	_, err = o.Raw(sql, chapterId).Exec()
	return
}

type CygxMorningMeetingGather struct {
	Id          int       `orm:"column(id);pk"`
	MeetingIds  string    `description:"主表ID多个用 , 隔开"`
	PublishTime string    `description:"发布日期"`
	CreateTime  time.Time `description:"创建时间"`
	ModifyTime  time.Time `description:"更新时间"`
	Title       string    `description:"标题"`
	Status      int       `description:"0:未发布，1:已发布"`
}

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

// 更改
func UpdateCygxMorningMeetingGather(meetingIds string, meetingGatherId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` UPDATE cygx_morning_meeting_gather SET meeting_ids=? WHERE id =?  `
	_, err = o.Raw(sql, meetingIds, meetingGatherId).Exec()
	return
}

// 详情
func GetCygxMorningMeetingReviewChapterDetail(meetingGatherId int) (item *CygxMorningMeetingReviewChapter, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT *  FROM cygx_morning_meeting_review_chapter  WHERE id= ? `
	err = o.Raw(sql, meetingGatherId).QueryRow(&item)
	return
}

// 获取数量
func GetCygxMorningMeetingReviewChapterCount(cid int) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT
				COUNT( 1 ) AS count 
			FROM
				cygx_morning_meeting_review_chapter AS c
				INNER JOIN cygx_morning_meeting_reviews AS a ON a.id = c.meeting_id 
			WHERE
				1 = 1 
				AND c.id = ?
				AND a.STATUS = 1`
	err = o.Raw(sqlCount, cid).QueryRow(&count)
	return
}
