package models

import (
	"time"

	"github.com/beego/beego/v2/client/orm"
)

type SysMessageReport struct {
	SysMessageReportId int       `orm:"pk" description:"消息id"`
	UserId             int       `description:"到期用户id"`
	ReceiveSysUserId   int       `description:"接收系统用户id"`
	MessageType        int       `description:"消息类型"`
	Content            string    `description:"内容"`
	Remark             string    `description:"备注"`
	IsRead             bool      `description:"是否已读"`
	CreateTime         time.Time `description:"创建时间"`
	ModifyTime         time.Time `description:"修改时间"`
}

type SysMessageReportView struct {
	SysMessageReportId int    `orm:"pk" description:"消息id"`
	UserId             int    `description:"到期用户id"`
	ReceiveSysUserId   int    `description:"接收系统用户id"`
	MessageType        int    `description:"消息类型"`
	Content            string `description:"内容"`
	Remark             string `description:"备注"`
	IsRead             bool   `description:"是否已读"`
	CreateTime         string `description:"创建时间"`
	ModifyTime         string `description:"修改时间"`
}

func (s *SysMessageReport) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(s, cols...)
	return
}

func UpdateReadSysMessageReportByUserId(userId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE sys_message_report SET is_read=1 WHERE receive_sys_user_id=? AND is_read=0`
	_, err = o.Raw(sql, userId).Exec()
	return
}

func GetSysMessageReportCountBySysUserId(sysUserId int) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(*) AS count FROM sys_message_report WHERE receive_sys_user_id=?`
	err = o.Raw(sql, sysUserId).QueryRow(&count)
	return
}

func GetSysMessageReportBySysUserId(sysUserId, startSize, pageSize int) (item []*SysMessageReport, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_message_report WHERE receive_sys_user_id=? ORDER BY is_read ASC, create_time DESC LIMIT ?,?`
	_, err = o.Raw(sql, sysUserId, startSize, pageSize).QueryRows(&item)
	return
}

func InsertMultiSysMessageReport(sysMessageReportList []*SysMessageReport) (err error) {
	o := orm.NewOrm()
	_, err = o.InsertMulti(500, sysMessageReportList)
	return
}

func GetSysMessageReportListById(sysMessageReportIds []int) (items []*SysMessageReport, err error) {
	o := orm.NewOrm()
	_, err = o.QueryTable(&SysMessageReport{}).
		Filter("sys_message_report_id__in", sysMessageReportIds).
		All(&items)
	return
}

func GetSysMessageReportCount(sysUserId, userId, messageType int) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(*) AS count FROM sys_message_report WHERE user_id=? AND message_type=? `
	err = o.Raw(sql, userId, messageType).QueryRow(&count)
	return
}

func GetSysMessageReportByCondition(condition string, pars []interface{}) (items []*SysMessageReport, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_message_report WHERE 1=1`
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars...).QueryRows(&items)
	return
}

func GetSysMessageReportCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(*) AS count FROM sys_message_report `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars...).QueryRow(&count)
	return
}
