<?php
/**
 * Author：晴天 QQ：1186258278
 * Creation：2020/3/23 20:21
 * Filename：UserConf.php
 * 用户配置载入
 */

namespace extend;

class UserConf
{
    public static function judge($dome = null)
    {
        global $DB, $conf;
        if (isset($_COOKIE['league']) && $conf['userdomaintype'] == 2) {
            $domain = xiaochu_de($_COOKIE['league']);
        } else $domain = href();
        if ($dome <> null) $domain = $dome;
        $UserData = $DB->get_row("SELECT id,configuration,grade FROM `sky_user` WHERE `domain` = '$domain' AND `state` = 1 AND `grade` >= '{$conf['userleaguegrade']}' LIMIT 1");
        if (!$UserData || $UserData['configuration'] == '') return false;
        return $UserData;
    }

    public static function Conf($data)
    {
        global $protect_admin;
        if ($data['userleague'] <> 1 || $protect_admin == true) return $data;
        global $DB, $_QET;
        if (isset($_COOKIE['league']) && $data['userdomaintype'] == 2) {
            $domain = xiaochu_de($_COOKIE['league']);
            if ($domain == '') setcookie("league", null, time() - 3600 * 12 * 30, '/');
        } else $domain = ($data['userdomaintype'] == 1 ? href() : $_GET['t']);

        if ($data['userdomaintype'] == 2 && isset($_QET['t'])) {
            setcookie("league", xiaochu_en($domain), time() + 3600 * 12 * 30, '/');
        }

        $UserData = $DB->get_row("SELECT id,configuration,grade FROM `sky_user` WHERE `domain` = '$domain' AND `state` = 1 AND `grade` >= '{$data['userleaguegrade']}' LIMIT 1");
        if (!$UserData || $UserData['configuration'] == '') return $data;
        $data = array_merge($data, \config::common_unserialize($UserData['configuration']));
        return $data;
    }

    public static function PushMoney($order, $gid, $price, $type, $num, $input)
    {
        global $DB, $conf;
        $domain_user = self::judge();
        if ($domain_user == false && $conf['userdomaintype'] == 2 && file_exists(ROOT . 'includes/extend/log/Order/' . $order . '.log')) {
            $domain_user = self::judge(file_get_contents(ROOT . 'includes/extend/log/Order/' . $order . '.log'));
        }

        if ($domain_user <> false) {
            $gid_data = $DB->get_row("SELECT profits,money,freight,name FROM `sky_goods` WHERE `gid` = $gid LIMIT 1");
            if (!$gid_data) return false;
            $pricer = \price::price_uid(\login_data::user_grade($domain_user['id']), $gid_data['profits'], $gid_data['money']);
            $gid_data['price'] = $pricer['price'];
            $gid_data['points'] = $pricer['points'];
            $price_domain = $gid_data['price'] * $num;
            $points_domain = $gid_data['points'] * $num;
            if ($type == 1) {
                if ($gid_data['freight'] <> -1) {
                    $freight = $DB->get_row("SELECT * FROM `sky_freight` WHERE `id` = '{$gid_data['freight']}' LIMIT 1");
                    if ($freight) {
                        $price_domain = \price::freight($freight, $input, $price_domain, $num);
                    }
                }
                $Money = $price - $price_domain;
                if ($price <= $price_domain) $Money = 0;
                self::PaidCommission($domain_user['id'], $Money, 1, '余额提成', '有用户在您的站点购买了商品' . $gid_data['name'] . '，根据Ta购买的价格和您(' . $pricer['level'] . ')的购买价来计算,特奖励您' . $Money . '元提成,继续加油推广哦！');
            } else {
                $Money = $price - $points_domain;
                if ($price <= $points_domain) $Money = 0;
                self::PaidCommission($domain_user['id'], $Money, 2, '货币提成', '有用户在您的站点购买了商品' . $gid_data['name'] . '，根据Ta兑换商品的' . $conf['currency'] . '数和您(' . $pricer['level'] . ')的兑换价来计算,特奖励您' . $Money . '' . $conf['currency'] . ',继续加油推广哦！');
            }
        } else return false;
    }

    public static function PaidCommission($Uid, $Price, $type, $name, $msg)
    {
        global $DB;
        userlog($name, $msg, $Uid, $Price);
        $field = ($type == 1 ? 'money' : 'currency');
        $Sql = "UPDATE `sky_user` SET " . $field . " = " . $field . " + $Price WHERE (`id`='$Uid')";
        $re = $DB->query($Sql);
        if (!$re) {
            userlog('奖励失败', '提成奖励失败,无法写入奖励！' . $DB->error() . ',奖励' . ($type == 1 ? '金额' : '积分') . $Price . '应得用户ID:' . $Uid, $Uid);
        }
    }


    public static function Commission($dowid, $money, $uid = 0, $grade)
    {
        global $DB, $conf;

        $domain_user = self::judge();
        if (($uid == 0 AND $domain_user == false) || $conf['usergradecost'] == -1) return false;

        if ($domain_user <> $uid && $domain_user <> false && $uid <> 0) {
            $User1 = $DB->get_row("SELECT id,configuration,grade FROM `sky_user` WHERE `id` = '$uid' AND `state` = 1 AND `grade` >= '{$conf['userleaguegrade']}' LIMIT 1");
            if ($User1) {
                $money = $money / 2;
                if ($User1['grade'] >= $grade) {
                    userlog('升级提成', '您的下级(' . $dowid . ')将用户等级提升到了' . $grade . '级,特奖励您' . $money . '元下级升级奖励！', $uid, $money);
                    @$DB->query("UPDATE `sky_user` SET `money`= money + $money WHERE (`id`='$uid')");
                }
                if ($domain_user['grade'] >= $grade) {
                    userlog('升级提成', '用户(' . $dowid . ')在您的站点将等级提升到了' . $grade . '级,特奖励您' . $money . '元下级升级奖励！', $domain_user['id'], $money);
                    @$DB->query("UPDATE `sky_user` SET `money`= money + $money WHERE (`id`='{$domain_user['id']}')");
                }
            } else {
                if ($User1['grade'] >= $grade) {
                    userlog('升级提成', '您的下级(' . $dowid . ')将用户等级提升到了' . $grade . '级,特奖励您' . $money . '元下级升级奖励！', $uid, $money);
                    @$DB->query("UPDATE `sky_user` SET `money`= money + $money WHERE (`id`='$uid')");
                }
            }
        } else {
            if ($domain_user <> false) {
                if ($domain_user['grade'] >= $grade) {
                    userlog('升级提成', '您的下级(' . $dowid . ')将用户等级提升到了' . $grade . '级,特奖励您' . $money . '元下级升级奖励！', $domain_user['id'], $money);
                    @$DB->query("UPDATE `sky_user` SET `money`= money + $money WHERE (`id`= '{$domain_user['id']}' )");
                }
            } else {
                $User1 = $DB->get_row("SELECT id,configuration,grade FROM `sky_user` WHERE `id` = '$uid' AND `state` = 1 AND `grade` >= '{$conf['userleaguegrade']}' LIMIT 1");
                if (!$User1) return false;
                if ($User1['grade'] >= $grade) {
                    userlog('升级提成', '您的下级(' . $dowid . ')将用户等级提升到了' . $grade . '级,特奖励您' . $money . '元下级升级奖励！', $uid, $money);
                    @$DB->query("UPDATE `sky_user` SET `money`= money + $money WHERE (`id`='$uid')");
                }
            }
        }
    }
}