package models

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

type CygxTimelineLog struct {
	Id                     int       `orm:"column(id);pk"`
	IndustrialManagementId int       `description:"产业D"`
	UserId                 int       `description:"用户ID"`
	Mobile                 string    `description:"手机号"`
	Email                  string    `description:"邮箱"`
	CompanyId              int       `description:"公司id"`
	CompanyName            string    `description:"公司名称"`
	CreateTime             time.Time `description:"创建时间"`
	ModifyTime             time.Time `description:"更新时间"`
	RealName               string    `description:"用户实际名称"`
}

// 判断用户是否阅读过该产业下的时间线
func GetCygxTimelineLogCountByUser(userId, industrialManagementId int) (count int, err error) {
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_timeline_log as a  WHERE user_id= ?    AND industrial_management_id  = ? `
	o := orm.NewOrm()
	err = o.Raw(sqlCount, userId, industrialManagementId).QueryRow(&count)
	return
}

// 新增
func AddCygxTimelineLog(item *CygxTimelineLog) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

// UpdateCygxTimelineLogModifyTime 更新用户阅读过该产业下的时间线的时间
func UpdateCygxTimelineLogModifyTime(userId, industrialManagementId int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_timeline_log SET modify_time = ?  WHERE user_id= ?    AND industrial_management_id  = ? `
	_, err = o.Raw(sql, time.Now(), userId, industrialManagementId).Exec()
	return
}

//func GetCygxTimelineLogCount(userId int, date string) (items []*CygxPageHistoryRecord, err error) {
//	o := orm.NewOrm()
//	sql := `SELECT * FROM cygx_timeline_log WHERE user_id=?  `
//	_, err = o.Raw(sql, userId, date).QueryRows(&items)
//	return
//}

func GetCygxTimelineLogCount(userId int, industrialIdArr []int) (items []*CygxTimelineLog, err error) {
	if len(industrialIdArr) == 0 {
		return
	}
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_timeline_log WHERE user_id=? AND 	 industrial_management_id IN (` + utils.GetOrmInReplace(len(industrialIdArr)) + `)`
	_, err = o.Raw(sql, userId, industrialIdArr).QueryRows(&items)
	return
}
