<?php

namespace app\model;

use app\exception\RepositoryException;
use app\service\Math;

/**
 * 用户佣金提现记录
 *
 * Class AccountWithdrawalCommission
 * @package app\controller\manager
 */
class AccountWithdrawalCommission extends Base
{

    static $status_default = 0;//待审核
    static $status_success = 1;//审核通过
    static $status_fail = 2;//审核失败


    /**
     *  客户
     */
    public function account()
    {
        return $this->hasOne(Account::class, 'id', 'account_id');
    }

    /**
     * 转换佣金和金额
     * @param $num int 金额或者佣金 单位为分
     * @param $type string commission/money  需要获得的金额或者佣金 单位分
     *
     * @throws RepositoryException
     */
    public static function convertCommissionOrMoney(int $num, string $type)
    {
        \think\facade\Config::load('extra/commission_withdrawal', 'commission_withdrawal');
        $config = config('commission_withdrawal')['withdrawal_proportion'] ?? [];

        if (!$config) {
            throw new RepositoryException("提现比例未配置,请先联系平台");
        }

        if ($config["commission"] <= 0 || $config["money"] <= 0) {
            throw new RepositoryException("提现失败,系统错误");
        }

        //如果想得到佣金 或者 金额 单位分
        if ($type == AccountDataLog::TYPE_COMMISSION) {
            $total = Math::fen2Yuan($num) * ($config["commission"]) / ($config["money"]);
        } else {
            $total = $num * ($config["money"]) / ($config["commission"]);
        }

        return ceil($total);
    }
}