package services

import (
	"context"
	"errors"
	"fmt"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/models/order"
	"hongze/hongze_cygx/utils"
)

// 判断用户是否开通了个人研选权限
func GetMfyxUserPermissionTotal(userId int) (toatal int) {
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg(fmt.Sprint("判断用户是否开通了个人研选权限失败 GetMfyxUserPermissionTotal userId", userId, "，err：", err.Error()), 2)
		}
	}()
	//判断用户是否开通了个人研选权限，如果有权限后缀拼接权限名称
	toatal, e := models.GetCygxUserYanxuanPermissionCountByUserId(userId)
	if e != nil {
		err = errors.New("GetCygxUserYanxuanPermissionCountByUserId, Err: " + e.Error())
		return
	}
	return
}

// UpdateCygxUserYanxuanPermissionToClose 关闭到期个人用户研选权限
func UpdateCygxUserYanxuanPermissionToClose(cont context.Context) (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg(fmt.Sprint("关闭个人用户研选权限失败 UpdateCygxUserYanxuanPermissionToClose ，err：", err.Error()), 2)
		}
	}()
	e := models.UpdateCygxUserYanxuanPermissionToClose()
	if e != nil {
		err = errors.New("UpdateCygxUserYanxuanPermissionToClose, Err: " + e.Error())
		return
	}
	return
}

// 判断用户是购买了单场付费活动
func GetCygxOrderVirtualAssetdCountTotal(userId, activityId int) (total int) {
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go utils.SendAlarmMsg(fmt.Sprint("判断用户是购买了单场付费活动失败 GetCygxOrderUserCardTotal userId：", userId, "activityId:", activityId, "，err：", err.Error()), 2)
		}
	}()
	var condition string
	var pars []interface{}
	condition += ` AND user_id  =   ?  AND source  = 'activity'  AND  source_id =  ? `
	pars = append(pars, userId, activityId)

	total, e := order.GetCygxOrderVirtualAssetdCount(condition, pars)
	if e != nil {
		err = errors.New("GetCygxOrderVirtualAssetdCount, Err: " + e.Error())
		return
	}
	return
}
