package yb

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

// YbCommunityQuestionComment 研报问答社区 用户留言表
type YbCommunityQuestionComment struct {
	CommunityQuestionCommentId uint64    `orm:"column(community_question_comment_id);pk;" description:"留言ID"`
	CommunityQuestionId        int       `orm:"column(community_question_id)" description:"社区问答id"`
	UserId                     uint64    `orm:"column(user_id)" description:"用户id"`
	RealName                   string    `orm:"column(real_name)" description:"用户当时昵称"`
	Content                    string    `orm:"column(content)" description:"留言内容"`
	ReplyCommentId             uint64    `orm:"column(reply_comment_id)" description:"回复的留言ID"`
	IsTop                      int8      `orm:"column(is_top)" description:"是否置顶(0-未置顶,1-置顶)"`
	IsHot                      int8      `orm:"column(is_hot)" description:"是否设置精选(0-未设置,1-已设置)"`
	HotTopTime                 time.Time `orm:"column(hot_top_time)" description:"设置精选或者设置置顶的时间"`
	Type                       int8      `orm:"column(type)" description:"留言类型 1-评论 2-回复"`
	Enabled                    int8      `orm:"column(enabled)" description:"是否有效, 0-无效留言 1-有效留言"`
	IsShowName                 int8      `orm:"column(is_show_name)" description:"是否匿名 0-匿名,1-不匿名"`
	SourceAgent                int       `orm:"column(source_agent)" description:"留言入口来源,1:小程序,2:小程序pc, 4:web pc"`
	TopTime                    time.Time `orm:"column(top_time)" description:"设置置顶的时间"`
	HotTime                    time.Time `orm:"column(hot_time)" description:"设置精选的时间"`
	ModifyTime                 time.Time `orm:"column(modify_time)" description:"修改时间"`
	CreateTime                 time.Time `orm:"column(create_time)" description:"创建时间"`
	Source                     int       `orm:"column(source)" description:"来源:1-问答社区; 2-视频社区"`
}

// GetQuestionCommentById 主键获取提问评论
func GetQuestionCommentById(questionId int) (item *YbCommunityQuestionComment, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM yb_community_question_comment WHERE community_question_comment_id = ? AND enabled = 1 LIMIT 1`
	err = o.Raw(sql, questionId).QueryRow(&item)
	return
}

//Update 更新评论信息
func (item *YbCommunityQuestionComment) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(item, cols...)
	return
}

//删除数据
func (item *YbCommunityQuestionComment) Delete() (err error) {
	o := orm.NewOrm()
	sql := ` DELETE FROM yb_community_question_comment WHERE community_question_comment_id = ?`
	_, err = o.Raw(sql, item.CommunityQuestionCommentId).Exec()
	return
}

// YbCommunityQuestionCommentAndQuestion 研报问答社区和信息
type YbCommunityQuestionCommentAndQuestion struct {
	CommunityQuestionCommentId uint64    `orm:"column(community_question_comment_id);pk;" description:"留言ID"`
	CommunityQuestionId        int       `orm:"column(community_question_id)" description:"社区问答id"`
	UserId                     uint64    `orm:"column(user_id)" description:"用户id"`
	NickName                   string    `description:"用户名称"`
	RealName                   string    `description:"用户真实姓名"`
	CompanyId                  int       `description:"客户id"`
	CompanyName                string    `description:"客户名称"`
	CompanyFiccStatus          string    `description:"客户ficc状态"`
	Content                    string    `orm:"column(content)" description:"留言内容"`
	ReplyCommentId             uint64    `orm:"column(reply_comment_id)" description:"回复的留言ID" json:"-"`
	IsTop                      int8      `orm:"column(is_top)" description:"是否置顶(0-未置顶,1-置顶)" json:"-"`
	IsHot                      int8      `orm:"column(is_hot)" description:"是否设置精选(0-未设置,1-已设置)"`
	HotTopTime                 time.Time `orm:"column(hot_top_time)" description:"设置精选或者设置置顶的时间"`
	Type                       int8      `orm:"column(type)" description:"留言类型 1-评论 2-回复" json:"-"`
	Enabled                    int8      `orm:"column(enabled)" description:"是否有效, 0-无效留言 1-有效留言" json:"-"`
	IsShowName                 int8      `orm:"column(is_show_name)" description:"是否匿名 0-匿名,1-不匿名"`
	SourceAgent                int       `orm:"column(source_agent)" description:"留言入口来源,1:小程序,2:小程序pc, 4:web pc"`
	TopTime                    time.Time `orm:"column(top_time)" description:"设置置顶的时间"`
	HotTime                    time.Time `orm:"column(hot_time)" description:"设置精选的时间"`
	ModifyTime                 time.Time `orm:"column(modify_time)" description:"修改时间"`
	CreateTime                 string    `orm:"column(create_time)" description:"创建时间"`
	QuestionContent            string    `description:"问题内容"`
}

// GetCommunityQuestionCommentList 获取问答列表
func GetCommunityQuestionCommentList(condition string, pars []interface{}, startSize, pageSize int) (total int, list []*YbCommunityQuestionCommentAndQuestion, err error) {
	o := orm.NewOrm()

	//汇总数据
	totalSQl := `SELECT COUNT(1) total FROM yb_community_question_comment a join yb_community_question b on a.community_question_id = b.community_question_id WHERE a.enabled = 1 AND a.type = 1 AND b.is_deleted=0 `
	totalSQl += condition
	if err = o.Raw(totalSQl, pars).QueryRow(&total); err != nil {
		return
	}

	// 列表数据
	sql := `SELECT a.*,b.question_content FROM yb_community_question_comment a join yb_community_question b on a.community_question_id = b.community_question_id WHERE a.enabled = 1 AND a.type = 1 AND b.is_deleted=0 `
	sql += condition
	sql += ` ORDER BY a.create_time DESC LIMIT ?,?`
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
	return
}

// GetCommunityQuestionCommentListByIds 根据问答评论id获取列表数据
func GetCommunityQuestionCommentListByIds(CommunityQuestionCommentIds string) (list []*YbCommunityQuestionComment, err error) {
	o := orm.NewOrm()
	// 列表数据
	sql := `SELECT q.* FROM yb_community_question_comment q WHERE q.enabled = 1 AND community_question_comment_id in (` + CommunityQuestionCommentIds + `)`
	_, err = o.Raw(sql).QueryRows(&list)
	return
}