package cygx

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

// CygxMorningMeetingReviews [...]
type CygxMorningMeetingReviews struct {
	Id            int       `orm:"column(id);pk"`
	MeetingTime   time.Time `json:"meetingTime"` // 晨会日期
	PublishTime   time.Time `json:"publishTime"` // 发布日期
	CreateTime    time.Time `json:"createTime"`
	ModifyTime    time.Time `json:"modifyTime"`
	Status        int       `json:"status"`       // 0:未发布，1:已发布
	PartNums      int       `json:"partNums"`     // 段落数
	IndustryNames string    `json:"industryName"` // 产业名称
}

// 添加晨报点评
func AddCygxMorningMeetingReviews(item *CygxMorningMeetingReviews) (id int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	id, err = o.Insert(item)
	return
}

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

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

type CygxMorningMeetingReviewItem struct {
	Id            int    `orm:"column(id);pk"`
	MeetingTime   string `json:"meetingTime"` // 晨会日期
	PublishTime   string `json:"publishTime"` // 发布日期
	CreateTime    string `json:"createTime"`
	ModifyTime    string `json:"modifyTime"`
	Status        int    `json:"status"`       // 0:未发布，1:已发布
	PartNums      int    `json:"partNums"`     // 段落数
	IndustryNames string `json:"industryName"` // 产业名称
	Pv            int    `description:"PV"`
	Uv            int    `description:"UV"`
}

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

func GetMorningMeetingReviewById(reviewId int) (item *CygxMorningMeetingReviews, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_morning_meeting_reviews WHERE id=?`
	err = o.Raw(sql, reviewId).QueryRow(&item)
	return
}

// 发布报告
func PublishMorningMeetingReviewById(reviewId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_morning_meeting_reviews SET status=1,publish_time=now(),modify_time=NOW() WHERE id = ? `
	_, err = o.Raw(sql, reviewId).Exec()
	return
}

type MorningReviewPublishReq struct {
	ReviewIds string `description:"晨会id,多个用英文逗号隔开"`
}

type MorningReviewPublishCancelReq struct {
	ReviewId int `description:"晨会id"`
}

// 取消发布报告
func PublishCancelMorningMeetingReview(reviewId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := ` UPDATE cygx_morning_meeting_reviews SET status=0,publish_time=null WHERE id =?  `
	_, err = o.Raw(sql, reviewId).Exec()
	return
}

type MorningReviewDeleteReq struct {
	ReviewId int `description:"晨会id"`
}

// DeleteMorningReviewAndChapter 删除晨会点评及章节
func DeleteMorningReviewAndChapter(reviewId int) (err error) {
	reviewInfo, err := GetMorningMeetingReviewById(reviewId)
	if err != nil {
		err = errors.New("报告信息有误, Err: " + err.Error())
		return
	}
	if reviewInfo.Status == 1 {
		err = errors.New("报告已发布，不可删除")
		return
	}

	err = DeleteMorningMeeting(reviewId)
	if err != nil {
		err = errors.New("删除失败, Err: " + err.Error())
		return
	}

	err = DeleteMorningMeetingChapter(reviewId)
	if err != nil {
		err = errors.New("删除失败, Err: " + err.Error())
		return
	}

	return
}

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

// 更新晨报点评
func UpdateCygxMorningMeetingReviews(item *CygxMorningMeetingReviews) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_morning_meeting_reviews
			SET
			  meeting_time =?,
			  publish_time = ?,
			  modify_time = ?,
			  part_nums = ?,
			  industry_names = ?,
			  status = ? 
			WHERE id = ? `
	_, err = o.Raw(sql, item.MeetingTime, item.PublishTime, item.ModifyTime, item.PartNums, item.IndustryNames, item.Status, item.Id).Exec()

	return
}
