package models

import (
	"strings"
	"time"

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

type UserReadRecord struct {
	UserReadRecordId int    `orm:"pk" description:"id"`
	UserId           int    `description:"用户id"`
	ReportId         int    `description:"报告id"`
	ReportTitle      string `description:"报告标题"`
	// ChartPermissionId1  string    `description:"一级品种id"`
	// ChartPermissionId2  string    `description:"二级品种id"`
	ChartPermissionName string    `description:"二级品种名称"`
	ClassifyId1         int       `description:"一级级分类id"`
	ClassifyName1       string    `description:"一级分类名称"`
	ClassifyId2         int       `description:"二级分类id"`
	ClassifyName2       string    `description:"二级分类名称"`
	Timestamp           int       `description:"阅读开始时间戳"`
	EndTimestamp        int       `description:"阅读结束时间戳"`
	CreateTime          time.Time `description:"创建时间"`
	CreateDate          string    `description:"创建日期"`
	StayTime            string    `description:"停留时间"`
	StayTimestamp       string    `description:"停留时间戳"`
}

func (u *UserReadRecord) Insert() (insertId int64, err error) {
	o := orm.NewOrm()
	insertId, err = o.Insert(u)
	return
}

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

func GetUserReadRecordListById(recordId int) (items *UserReadRecord, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM user_read_record WHERE 1=1 AND user_read_record_id = ?`
	err = o.Raw(sql, recordId).QueryRow(&items)
	return
}

func GetUserReadRecordListByRcordIds(recordIds []string) (items []*UserReadRecord, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM user_read_record WHERE 1=1  `
	var stringIds string
	if len(recordIds) > 0 {
		sql += ` AND user_read_record_id in (?) `
		stringIds = strings.Join(recordIds, ",")
	}
	_, err = o.Raw(sql, stringIds).QueryRows(&items)
	return
}

func UpdateUserReadRecordById(recordId, endTimeStamp, stayTime int, stayTimeStr string) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE user_read_record SET end_timestamp=?, stay_timestamp=?, stay_time=? WHERE 1=1 AND user_read_record_id = ? `
	_, err = o.Raw(sql, endTimeStamp, stayTime, stayTimeStr, recordId).Exec()
	return
}

func UpdateUserReadRecordByRecordIds(recordIds []string, endTimeStamp, stayTime int, stayTimeStr string) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE user_read_record SET end_timestamp=?, stay_timestamp=?, stay_time=? WHERE 1=1 `
	var stringIds string
	if len(recordIds) > 0 {
		sql += ` AND user_read_record_id in (?) `
		stringIds = strings.Join(recordIds, ",")
	}
	_, err = o.Raw(sql, endTimeStamp, stayTime, stayTimeStr, stringIds).Exec()
	return
}