package models

import (
	"fmt"
	"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"` // 产业名称
	OpUserId      int       `description:"操作人编号"`
	OpUserName    string    `description:"操作人名称"`
}

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

// 列表
func GetCygxMorningMeetingReviewsList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxMorningMeetingReviews, err error) {
	o := orm.NewOrm()
	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.NewOrm()
	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"` // 产业名称
}

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

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

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

// 发布报告
func UpdateMorningMeetingReviewById(status, reviewId int, content string) (err error) {
	o, err := orm.NewOrm().Begin()
	if err != nil {
		return
	}
	defer func() {
		fmt.Println(err)
		if err == nil {
			o.Commit()
		} else {
			o.Rollback()
		}
	}()
	sql := `UPDATE cygx_morning_meeting_reviews SET status=?,publish_time=now(),modify_time=NOW() WHERE id = ? `
	_, err = o.Raw(sql, status, reviewId).Exec()
	if err != nil {
		return
	}

	sql = `UPDATE cygx_morning_meeting_review_chapter SET content = ?  WHERE meeting_id = ? `
	_, err = o.Raw(sql, content, 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.NewOrm()
	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"`
}

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

// 更新晨报点评
func UpdateCygxMorningMeetingReviews(item *CygxMorningMeetingReviews) (err error) {
	o := orm.NewOrm()
	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
}
