package models

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"`             // 内容
}

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

// 通过ID获取详情
func GetCygxMorningMeetingReviewChapterDetail(id int) (item *CygxMorningMeetingReviewChapter, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_morning_meeting_review_chapter  WHERE id =? `
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

// 通过ID获取详情
func GetCygxMorningMeetingReviewChapterDetailByMeetingId(meetingId int) (item *CygxMorningMeetingReviewChapter, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_morning_meeting_review_chapter  WHERE meeting_id =? LIMIT  1 `
	err = o.Raw(sql, meetingId).QueryRow(&item)
	return
}

// 列表
func GetCygxMorningMeetingReviewChapterList(condition string, pars []interface{}) (items []*CygxMorningMeetingGatherDetailListResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT c.*,r.publish_time
			FROM cygx_morning_meeting_review_chapter  as  c 
			INNER JOIN cygx_morning_meeting_reviews as r ON  r.id = c.meeting_id   WHERE 1 = 1  AND r.status = 1 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// 列表
func GetCygxMorningMeetingReviewChapterList2(condition string, pars []interface{}) (items []*CygxMorningMeetingReviewLogResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT c.*,r.publish_time
			FROM cygx_morning_meeting_review_chapter  as  c 
			INNER JOIN cygx_morning_meeting_reviews as r ON  r.id = c.meeting_id   WHERE 1 = 1  `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).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"`               // 内容
}

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

func GetCygxMorningMeetingReviewsListByIndustrialIds(industrialIds string) (items []*CygxMorningMeetingReviewChapter, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_morning_meeting_review_chapter WHERE industry_id IN (` + industrialIds + `)  `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

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

// 获取数量
func GetCygxMorningMeetingReviewChapterCount(cid int) (count int, err error) {
	o := orm.NewOrm()
	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
}
