package models

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

type CygxXzsChooseCategory struct {
	Id          int       `orm:"column(id);pk"`
	UserId      int       `description:"用户ID"`
	Mobile      string    `description:"手机号"`
	Email       string    `description:"邮箱"`
	CompanyId   int       `description:"公司id"`
	CompanyName string    `description:"公司名称"`
	RealName    string    `description:"用户实际名称"`
	CategoryId  int       `description:"权益文章对应分类,cygx_article"`
	CreateTime  time.Time `description:"创建时间"`
	ModifyTime  time.Time `description:"更新时间"`
	IdCygx      int       `description:"cygx_report_mapping_cygx 表主键ID"`
	FollowType  int       `description:"1,重点关注，3不感兴趣，0默认接受推送"`
}

// 根据手机号获取用户关注的产业
func GetCygxXzsChooseCategoryList(mobile string) (items []*CygxXzsChooseCategory, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_xzs_choose_category WHERE mobile = ?`
	_, err = o.Raw(sql, mobile).QueryRows(&items)
	return
}

// 列表
func GetCygxXzsChooseCategoryListByCon(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxXzsChooseCategory, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_xzs_choose_category as art WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	//sql += ` LIMIT ?,?  `
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

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

// 删除
func RemoveCygxCategoryFllow(mobile string, CategoryId int) (err error) {
	o := orm.NewOrm()
	sql := `DELETE FROM cygx_xzs_choose_category WHERE mobile=? AND category_id=? `
	_, err = o.Raw(sql, mobile, CategoryId).Exec()
	return
}

// 根据手机号获取用户关注的产业
func GetCygxXzsChooseCategoryWhithIdCygxList() (items []*CygxXzsChooseCategory, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_xzs_choose_category WHERE id_cygx = 0`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type XzsChooseMapResp struct {
	Id                  int    `description:"id"`
	CategoryId          int    `description:"权益文章对应分类,cygx_article"`
	CharPpermissionName string `description:"权限名称"`
	MatchTypeName       string `description:"分类名称"`
}

// 根据手机号获取用户关注的产业
func GetCygxXzsChooseCategoryMapList() (items []*XzsChooseMapResp, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			r.id,
			r.chart_permission_name,
			r.match_type_name,
			c.category_id 
		FROM
			cygx_report_mapping_cygx AS r
			INNER JOIN cygx_report_mapping_group AS p ON p.id_cygx = r.id
			INNER JOIN cygx_report_mapping_celue AS c ON c.category_id = p.category_id_celue 
		WHERE
			r.chart_permission_id IN (23,100000)`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 修改数据
func UpdateCygxXzsChooseCategory(idCygx, id int) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE cygx_xzs_choose_category SET id_cygx=?  WHERE id=?   `
	_, err = o.Raw(sql, idCygx, id).Exec()
	return
}

// 列表
func GetCygxXzsChooseCategoryListFollowType(categoryId, followType int) (items []*CygxXzsChooseCategory, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM cygx_xzs_choose_category as art WHERE category_id  = ? AND  follow_type = ?  `
	_, err = o.Raw(sql, categoryId, followType).QueryRows(&items)
	return
}

// 获取重点关注数量
func GetCountCygxXzsChooseCategoryFllowByType(userId, categoryId int) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM cygx_xzs_choose_category WHERE user_id=? AND category_id=?  AND  follow_type = 1  `
	err = orm.NewOrm().Raw(sql, userId, categoryId).QueryRow(&count)
	return
}
