package cygx

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"hongze/hz_crm_api/utils"
)

type CygxArticleAndYanxuanRecordResp struct {
	Id               int    `orm:"column(id);pk"`
	SourceId         int    `description:"资源ID"`
	Source           string `description:"资源I类型  文章、研选专栏"`
	UserId           int    `description:"用户ID"`
	CreateTime       string `description:"创建时间"`
	Mobile           string `description:"手机号"`
	Email            string `description:"邮箱"`
	CompanyId        int    `description:"公司id"`
	CompanyName      string `description:"公司名称"`
	ModifyTime       string `description:"修改时间"`
	RealName         string `description:"用户实际名称"`
	SellerName       string `description:"所属销售"`
	StopTime         int    `description:"停留时间"`
	RegisterPlatform int    `description:"来源 1小程序，2：网页"`
}

// 获取数量
func GetCygxArticleAndYanxuanRecordCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_article_and_yanxuan_record as r
                  INNER JOIN cygx_article  as art  ON  art.article_id = r.source_id 
                  WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 获取数量(关联用户跟销售的绑定关系跨库查询)
func GetCygxArticleAndYanxuanRecordCountWeekly(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	databaseName := utils.GetWeeklyDatabase()
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_article_and_yanxuan_record as r 
                             INNER JOIN cygx_article  as art  ON  art.article_id = r.source_id
				  INNER JOIN %s.user_seller_relation AS us ON us.user_id = r.user_id   AND us.product_id = 2 
		WHERE 1= 1  `
	sqlCount = fmt.Sprintf(sqlCount, databaseName)
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 列表
func GetCygxArticleAndYanxuanRecordRespList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxArticleAndYanxuanRecordResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT r.* ,art.title  FROM cygx_article_and_yanxuan_record as r 
          INNER JOIN cygx_article  as art  ON  art.article_id = r.source_id  WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?  `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// 列表(关联用户跟销售的绑定关系跨库查询)
func GetCygxArticleAndYanxuanRecordRespListWeekly(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxArticleAndYanxuanRecordResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	databaseName := utils.GetWeeklyDatabase()
	sql := `SELECT r.* FROM cygx_article_and_yanxuan_record as r 
     		INNER JOIN cygx_article  as art  ON  art.article_id = r.source_id 
			INNER JOIN %s.user_seller_relation AS us ON us.user_id = r.user_id   AND us.product_id = 2 
		WHERE 1= 1   `
	sql = fmt.Sprintf(sql, databaseName)
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?  `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

// 获取研选专栏阅读数量
func GetCygxYanxuanSpecialRecordCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM 	cygx_yanxuan_special_record AS r
			INNER JOIN cygx_yanxuan_special AS art ON art.id = r.yanxuan_special_id
		WHERE 1 = 1  `
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 获取研选专栏阅读数量
func GetCygxYanxuanSpecialRecordCountWeekly(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM 	cygx_yanxuan_special_record AS r
			INNER JOIN cygx_yanxuan_special AS art ON art.id = r.yanxuan_special_id
			INNER JOIN %s.user_seller_relation AS us ON us.user_id = r.user_id   AND us.product_id = 2 
		WHERE 1 = 1  `
	databaseName := utils.GetWeeklyDatabase()
	sqlCount = fmt.Sprintf(sqlCount, databaseName)
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 收藏的专栏文章
func GetCygxYanxuanSpecialCollectCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM 	cygx_yanxuan_special_collect AS r
			INNER JOIN cygx_yanxuan_special AS art ON art.id = r.yanxuan_special_id
		WHERE 1 = 1  `
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 收藏的专栏文章
func GetCygxYanxuanSpecialCollectCountWeekly(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM 	cygx_yanxuan_special_collect AS r
			INNER JOIN cygx_yanxuan_special AS art ON art.id = r.yanxuan_special_id
			INNER JOIN %s.user_seller_relation AS us ON us.user_id = r.user_id   AND us.product_id = 2 
		WHERE 1 = 1  `
	databaseName := utils.GetWeeklyDatabase()
	sqlCount = fmt.Sprintf(sqlCount, databaseName)
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 关注的专栏作者
func GetCygxYanxuanSpecialFollowCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM 	cygx_yanxuan_special_follow AS r
			INNER JOIN cygx_yanxuan_special_author AS art ON art.user_id = r.follow_user_id
		WHERE 1 = 1  `
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 关注的专栏作者
func GetCygxYanxuanSpecialFollowCountWeekly(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM 	cygx_yanxuan_special_follow AS r
			INNER JOIN cygx_yanxuan_special_author AS art ON art.user_id = r.follow_user_id
			INNER JOIN %s.user_seller_relation AS us ON us.user_id = r.user_id   AND us.product_id = 2 
		WHERE 1 = 1  `
	databaseName := utils.GetWeeklyDatabase()
	sqlCount = fmt.Sprintf(sqlCount, databaseName)
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}
