package user_record

import "hongze/hongze_yb/global"

// GetByUserId 根据用户id和平台id获取用户关系
func GetByUserId(userId, platform int) (item *UserRecord, err error) {
	err = global.DEFAULT_MYSQL.Where("user_id = ? and create_platform = ?", userId, platform).First(&item).Error
	return
}

// GetByUnionID 根据用户UnionID和平台id获取用户关系
func GetByUnionID(unionID string, platform int) (item *UserRecord, err error) {
	err = global.DEFAULT_MYSQL.Where("union_id = ? and create_platform = ?", unionID, platform).First(&item).Error
	return
}

// GetByOpenID 根据用户OpenID获取用户关系
func GetByOpenID(openID string) (item *UserRecord, err error) {
	err = global.DEFAULT_MYSQL.Where("open_id = ? ", openID).First(&item).Error
	return
}

// GetUserThirdRecordByUserId 获取该用户第一个的 三方信息（微信头像信息）
func GetUserThirdRecordByUserId(userId int) (item *UserRecord, err error) {
	err = global.DEFAULT_MYSQL.Where("user_id = ? ", userId).Order("user_record_id asc").First(&item).Error
	return
}

// GetFirstByUnionID 根据用户UnionID获取最小平台的用户关系（已经绑定了user_id的）
func GetFirstByUnionID(unionID string) (item *UserRecord, err error) {
	err = global.DEFAULT_MYSQL.Where("union_id = ? and user_id>0", unionID).Order("create_platform asc").First(&item).Error
	return
}

// GetRepeatBindAccount 获取重复的绑定账号
func GetRepeatBindAccount(account, unionId string) (items []*UserRecord, err error) {
	err = global.DEFAULT_MYSQL.Model(UserRecord{}).Where("bind_account = ? AND union_id <> ? AND create_platform IN (3,6)", account, unionId).Find(&items).Error
	return
}

// ClearRepeatBindAccount 清除重复的绑定账号
func ClearRepeatBindAccount(account, unionId string) (err error) {
	sql := ` UPDATE user_record SET bind_account = '', user_id = 0 WHERE bind_account = ? AND union_id <> ? AND create_platform IN (3,6)`
	err = global.DEFAULT_MYSQL.Exec(sql, account, unionId).Error
	return
}
