package wx_user

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

type WxUser struct {
	UserId           int64 `orm:"column(user_id);pk"`
	Mobile           string
	Email            string
	CompanyId        int
	RealName         string `description:"姓名"`
	CreatedTime      time.Time
	MobileTwo        string `description:"备用手机号"`
	BusinessCardUrl  string `description:"名片"`
	IsMaker          int    `description:"是否决策人，1:是，0:否"`
	Position         string `description:"职位"`
	Sex              int    `description:"普通用户性别，1为男性，2为女性"`
	DepartmentName   string `description:"联系人部门"`
	RegisterTime     time.Time
	RegisterPlatform int
	Enabled          int8 `description:"用户状态"`
}

type OpenIdList struct {
	OpenId string
}

//获取所有的用户openid列表
func GetOpenIdList(openIdStr string) (items []*OpenIdList, err error) {
	sql := `SELECT DISTINCT ur.open_id FROM wx_user AS wu 
          INNER JOIN company AS c ON c.company_id = wu.company_id 
          INNER JOIN company_product AS d ON c.company_id=d.company_id
		INNER join user_record  as ur on wu.user_id=ur.user_id
          WHERE ur.open_id != "" and ur.create_platform=1 AND  d.status IN('正式','试用','永续') `
	if openIdStr != "" {
		sql += ` AND ur.open_id in (` + openIdStr + `) `
	}
	_, err = orm.NewOrm().Raw(sql).QueryRows(&items)
	return
}

//根据手机号获取用户的openid列表
func GetOpenIdListByMobile(mobile, openIdStr string) (items []*OpenIdList, err error) {
	sql := `SELECT DISTINCT ur.open_id FROM wx_user AS wu 
          INNER JOIN company AS c ON c.company_id = wu.company_id 
          INNER join user_record  as ur on wu.user_id=ur.user_id
          WHERE ur.open_id != "" and ur.create_platform=1 AND wu.mobile=? `
	if openIdStr != "" {
		sql += ` AND ur.open_id in (` + openIdStr + `) `
	}
	_, err = orm.NewOrm().Raw(sql, mobile).QueryRows(&items)
	return
}

type WxUserItem struct {
	UserId      int
	RealName    string
	Mobile      string
	Email       string
	CompanyName string
}

// GetWxUserByMobile 根据手机号获取用户信息
func GetWxUserByMobile(mobile string) (item *WxUserItem, err error) {
	sql := `SELECT a.user_id,a.real_name,a.mobile,b.company_name FROM wx_user AS a 
          INNER JOIN company AS b ON a.company_id = b.company_id 
          WHERE a.mobile=? `
	err = orm.NewOrm().Raw(sql, mobile).QueryRow(&item)
	return
}

// GetWxUserByEmail 根据邮箱获取用户信息
func GetWxUserByEmail(email string) (item *WxUserItem, err error) {
	sql := `SELECT a.user_id,a.real_name,a.mobile,b.company_name FROM wx_user AS a 
          INNER JOIN company AS b ON a.company_id = b.company_id 
          WHERE a.email=? `
	err = orm.NewOrm().Raw(sql, email).QueryRow(&item)
	return
}

// GetWxUserByMobile 根据手机号获取用户信息
func GetWxUserByMobileStr(mobile string) (item *WxUserItem, err error) {
	sql := `SELECT a.user_id,a.real_name,a.mobile,b.company_name FROM wx_user AS a 
          INNER JOIN company AS b ON a.company_id = b.company_id 
          WHERE a.mobile IN (` + mobile + `) LIMIT 1 `
	err = orm.NewOrm().Raw(sql).QueryRow(&item)
	return
}

// GetWxUserByMobileCountryCode 根据手机号和区号获取用户信息
func GetWxUserByMobileCountryCode(mobile, countryCode string) (item *WxUser, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM wx_user WHERE mobile = ? `
	sql += ` and country_code in ("","` + countryCode + `") `
	sql += ` LIMIT 1 `
	err = o.Raw(sql, mobile).QueryRow(&item)
	return
}

// GetByUserId 根据user_id获取用户信息
func GetByUserId(userId int) (wxUser *WxUser, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM wx_user WHERE user_id = ? LIMIT 1 `
	err = o.Raw(sql, userId).QueryRow(&wxUser)
	return
}
