package models

import (
	"github.com/beego/beego/v2/client/orm"
	"hongze/hongze_cygx/utils"
	"strings"
)

type SendTemplateResponse struct {
	Errcode int    `json:"errcode"`
	Errmsg  string `json:"errmsg"`
	MsgID   int    `json:"msgid"`
}

type OpenIdList struct {
	OpenId    string
	UserId    int
	Mobile    string `description:"手机号"`
	CompanyId int    `description:"手机号"`
}

func GetOpenIdList() (items []*OpenIdList, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	openIdstr := WxUsersGet()
	sql := `SELECT open_id FROM wx_user AS wu 
          INNER JOIN company AS c ON c.company_id = wu.company_id 
          WHERE wu.open_id IS NOT NULL AND  c.type IN (1,2) `
	if openIdstr != "" {
		sql += ` AND open_id in (` + openIdstr + `) `
	}
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func GetWxOpenIdByMobileList(mobile string) (items []*OpenIdList, err error) {
	sliceMobile := strings.Split(mobile, ",")
	var mobiles []string
	for _, v := range sliceMobile {
		mobiles = append(mobiles, v)
	}
	lenarr := len(mobiles)
	if lenarr == 0 {
		return
	}
	var condition string
	var pars []interface{}
	condition = ` AND u.cygx_bind_account IN (` + utils.GetOrmInReplace(lenarr) + `)`
	pars = append(pars, mobiles)
	sql := ` SELECT
				union_id,open_id,
				cygx_user_id AS user_id
				FROM cygx_user_record as u	WHERE	1 = 1	 ` + condition
	_, err = orm.NewOrm().Raw(sql, pars).QueryRows(&items)
	return
}

type AdminOpenIdList struct {
	OpenId    string `description:"OpenId"`
	UserId    int    `description:"UserId"`
	Mobile    string `description:"手机号"`
	CompanyId int    `description:"手机号"`
}

// GetAdminOpendidByCompany 通过用户公司ID获取对应销售的openid
func GetAdminOpendidByCompany(condition string, pars []interface{}) (list []*AdminOpenIdList, err error) {
	o := orm.NewOrmUsingDB("weekly_report")
	sql := `SELECT
			a.mobile,
			p.company_id 
		FROM
			company_product AS p
			INNER JOIN admin AS a ON a.admin_id = p.seller_id 
		WHERE
			1 = 1 
			AND p.product_id = 2 ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&list)
	return
}

//func GetWxOpenIdByMobileSliceList(mobiles []string) (items []*OpenIdList, err error) {
//	itemsLen := len(mobiles)
//	if itemsLen == 0 {
//		return
//	}
//	o := orm.NewOrm()
//	sql := `SELECT cr.*,user_id FROM user_record  as c
//			INNER JOIN cygx_user_record AS cr ON cr.union_id = c.union_id
//			WHERE bind_account IN (` + utils.GetOrmInReplace(itemsLen) + `) AND create_platform = 4`
//	_, err = o.Raw(sql, mobiles).QueryRows(&items)
//	return
//}

// 根据手机号获取用户的openid
func GetWxOpenIdByMobileSliceList(mobiles []string) (items []*OpenIdList, err error) {
	o := orm.NewOrm()
	lenarr := len(mobiles)
	if lenarr == 0 {
		return
	}
	var condition string
	var pars []interface{}
	condition = ` AND u.cygx_bind_account IN (` + utils.GetOrmInReplace(lenarr) + `)`
	pars = append(pars, mobiles)
	sql := `SELECT
			u.open_id,
			u.cygx_user_id AS user_id 
		FROM
			cygx_user_record AS u 
		WHERE
			1 = 1  ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// 根据手机号获取用户的openid
func GetMfyxWxOpenIdByMobileSliceList(mobiles []string) (items []*OpenIdList, err error) {
	o := orm.NewOrm()
	lenarr := len(mobiles)
	if lenarr == 0 {
		return
	}
	var condition string
	var pars []interface{}
	condition = ` AND u.cygx_bind_account IN (` + utils.GetOrmInReplace(lenarr) + `)`
	pars = append(pars, mobiles)
	sql := `SELECT
			u.open_id,
			u.cygx_user_id AS user_id 
		FROM
			cygx_mfyx_gzh_user_record AS u 
		WHERE
			1 = 1  ` + condition
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
