package models

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

type WxUserCode struct {
	Id             int `orm:"column(id);pk"`
	WxCode         string
	UserId         int
	Code           int
	FirstLogin     int
	Authorization  string
	UserPermission int
	CreateTime     time.Time
}

// 添加联系人日志信息
func AddWxUserCode(item *WxUserCode) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	lastId, err = o.Insert(item)
	return
}

func GetWxUserCode(wxCode string) (item *WxUserCode, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT * FROM wx_user_code WHERE wx_code=? `
	err = o.Raw(sql, wxCode).QueryRow(&item)
	return
}

// 获取所有有权限的用户的手机号
func GetCygxUserRecordPower(condition string, pars []interface{}) (items []*OpenIdList, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT
			u.user_id,
			u.company_id,
			u.real_name,
			u.mobile 
		FROM
			company_report_permission AS p
			INNER JOIN wx_user AS u ON u.company_id = p.company_id
			INNER JOIN user_record AS r ON r.user_id = u.user_id
		WHERE
			r.create_platform = 4 ` + condition + `
			AND p.STATUS IN ( '正式', '试用', '永续' ) 
			GROUP BY u.mobile `
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// 获取关注这个文章对应产业的用户的 openid
func GetCygxUserFllowOpenid(articleId int) (items []*OpenIdList, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			f.mobile,
			f.user_id 
		FROM
			cygx_xzs_choose_send AS s
			INNER JOIN cygx_industry_fllow AS f ON f.user_id = s.user_id
			INNER JOIN cygx_industrial_article_group_management AS mg ON mg.industrial_management_id = f.industrial_management_id 
		WHERE
			1 = 1 
			AND mg.article_id = ? 
			AND f.follow_type = 1
			GROUP BY f.user_id `
	_, err = o.Raw(sql, articleId).QueryRows(&items)
	return
}

// 获取拒绝接收推送的的用户的 openid
func GetCygxUserRefusetOpenid() (items []*OpenIdList, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			cr.open_id,
			cr.cygx_user_id AS user_id 
		FROM
			cygx_xzs_choose_send AS s
			INNER JOIN cygx_user_record AS cr ON cr.cygx_user_id = s.user_id 
		WHERE
			1 = 1 
			AND s.is_refuse = 1`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// 获取选择策略推送的用户的openid openid
func GetCygxUserFllowCeLueOpenid(categoryId int) (items []*OpenIdList, err error) {
	o := orm.NewOrm()
	sql := `SELECT
			cr.open_id,
			cr.cygx_user_id AS user_id 
		FROM
			cygx_xzs_choose_send AS s
			INNER JOIN cygx_xzs_choose_category AS f ON f.user_id = s.user_id
			INNER JOIN cygx_user_record AS cr ON cr.cygx_user_id = s.user_id 
		WHERE
			1 = 1 
			AND s.is_refuse = 0 
			AND ( s.is_subjective = 1 OR s.is_objective = 1 ) 
			AND f.category_id = ?
		GROUP BY
			s.user_id `
	_, err = o.Raw(sql, categoryId).QueryRows(&items)
	return
}

// 获取提交过推送规则用户的userId
//func GetCygxXzsChooseSendOpenIdByUserIds(idStr string) (items []*OpenIdList, err error) {
//	o := orm.NewOrm()
//	sql := `SELECT ur.user_id, cr.open_id FROM user_record AS ur
//INNER JOIN cygx_user_record AS cr
//WHERE ur.user_id IN (` + idStr + `) AND create_platform=4 AND ur.union_id=cr.union_id `
//	_, err = o.Raw(sql).QueryRows(&items)
//	return
//}
