package cygx

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

type CygxArticleDepartment struct {
	DepartmentId int       `orm:"column(department_id);pk"`
	CreateTime   time.Time `description:"创建时间"`
	NickName     string    `description:"昵称"`
	Remark       string    `description:"备注"`
	Remarks      string    `description:"备注辅助字段"`
	Content      string    `description:"初始内容"`
	ImgUrl       string    `description:"作者头像"`
}

type CygxArticleDepartmentRep struct {
	DepartmentId int    `description:"主键ID"`
	CreateTime   string `description:"创建时间"`
	NickName     string `description:"昵称"`
	Remark       string `description:"备注"`
	Remarks      string `description:"备注辅助字段"`
	Content      string `description:"初始内容"`
	ImgUrl       string `description:"作者头像"`
}

type CygxArticleDepartmentList struct {
	List []*CygxArticleDepartmentRep
}

// 新增
func AddCygxArticleDepartment(item *CygxArticleDepartment) (newId int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	newId, err = o.Insert(item)
	return
}

// 详情
func GetArticleDepartmentDateil(nickName, remarks string) (item *CygxArticleDepartmentRep, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_article_department WHERE nick_name = ? AND  remarks = ? LIMIT 1 `
	err = o.Raw(sql, nickName, remarks).QueryRow(&item)
	return
}

// 详情
func GetArticleDepartmentDateilById(departmentId int) (item *CygxArticleDepartmentRep, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_article_department WHERE department_id = ? `
	err = o.Raw(sql, departmentId).QueryRow(&item)
	return
}

// 数量
func GetArticleDepartmentCount(nickName, remarks string) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT COUNT(1)  FROM cygx_article_department WHERE nick_name = ? AND  remarks = ? `
	err = o.Raw(sql, nickName, remarks).QueryRow(&count)
	return
}

// 数量
func GetArticleDepartmentCountAll() (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT COUNT(1)  FROM cygx_article_department`
	err = o.Raw(sql).QueryRow(&count)
	return
}

// 列表
func GetCygxArticleDepartmentList(condition string) (items []*CygxArticleDepartmentRep, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_article_department `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type CygxDepartmentFllowList struct {
	RealName    string `description:"姓名"`
	CompanyName string `description:"公司名称"`
	CompanyId   int    `description:"公司Id"`
	SellerName  string `description:"公司名称"`
	CreateTime  string `description:"创建时间"`
}

type CygxDepartmentFllowRep struct {
	NickName string `description:"作者昵称"`
	FllowNum int    `description:"关注数量"`
	List     []*CygxDepartmentFllowList
}

// 列表
func GetCygxDepartmentFllowList(departmentId int) (items []*CygxDepartmentFllowList, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT
			f.*
		FROM
			cygx_article_department_follow AS f
		WHERE
			f.department_id = ?
			AND f.type = 1 `
	_, err = o.Raw(sql, departmentId).QueryRows(&items)
	return
}

// 详情
func GetArticleDepartmentDateilByDepartmentId(departmentId int) (item *CygxArticleDepartment, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_article_department WHERE department_id = ? `
	err = o.Raw(sql, departmentId).QueryRow(&item)
	return
}
