package models

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

type CygxArticleDepartmentFollow struct {
	Id           int       `orm:"column(id);pk"`
	DepartmentId int       `description:"作者ID"`
	UserId       int       `description:"用户ID"`
	Mobile       string    `description:"手机号"`
	Email        string    `description:"邮箱"`
	CompanyId    int       `description:"公司id"`
	CompanyName  string    `description:"公司名称"`
	Type         int       `description:"操作方式,1报名,2取消报名"`
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time `description:"更新时间"`
}

type ArticleDepartmentIdRep struct {
	DepartmentId int `description:"作者ID"`
}

//添加
func AddArticleDepartmentFollow(item *CygxArticleDepartmentFollow) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

type CygxArticleDepartmentFollowResp struct {
	Status   int  `description:"1:关注,2:取消关注"`
	GoFollow bool `description:"是否去关注"`
}

func RemoveArticleDepartmentFollow(userId, industrialManagementId, doType int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_article_department_follow SET type = ? ,modify_time=? WHERE user_id=? AND department_id=? `
	_, err = o.Raw(sql, doType, time.Now(), userId, industrialManagementId).Exec()
	return
}

//获取数量
func GetArticleDepartmentFollow(userId, departmentId int, condition string) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM cygx_article_department_follow WHERE user_id=? AND department_id=? ` + condition
	err = orm.NewOrm().Raw(sql, userId, departmentId).QueryRow(&count)
	return
}

//获取数量
func GetArticleDepartmentFollowByUid(userId int) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM cygx_article_department_follow WHERE user_id=? `
	err = orm.NewOrm().Raw(sql, userId).QueryRow(&count)
	return
}