<?php

namespace Vip\Lib;

use Dever;

class Promote extends Core
{
    # 获取当前用户的升级信息
    public function getUpdateInfo($uid, $force = false)
    {
        $result = array();

        $level_info = $this->getInfo($uid);

        $user = Dever::db('vip/user')->one(array('uid' => $uid));

        # 是否可以升级
        $update = false;
        # 这个值待定
        $user_bonuse_balance = $user['user_bonuse_balance'];
        if ($level_info && $level_info['next']['level'] > 0) {
            if ($level_info['next']['level'] >= 6) {
                if ($user_bonuse_balance >= 100) {
                    # 等级大于6 并且 user_bonuse_balance >= 1000 这个值待定
                    $update = true;
                } else {
                    $update = false;
                }
            } else {
                $update = false;
            }

            # 验证当前用户是否可以升级
            if ($update && ($force || $level_info['status'] == 1)) {
                # 获取上级用户
                $parent_user = $this->getParent($uid, $level_info['current']['level']);

                # 插入当前升级的日志
                $log_update['uid'] = $uid;
                $log_update['invite_uid'] = $parent_user['uid'];
                $log_update['level_id'] = $level_info['current']['id'];
                $log_update['next_level_id'] = $level_info['next']['id'];
                $log = Dever::db('vip/log')->one($log_update);
                if (!$log) {
                    $log_update['status'] = 1;
                    Dever::db('vip/log')->insert($log_update);
                }

                if ($parent_user && $parent_user['super_vip_flag'] == 1) {
                    $parent_user_update['can_upgrade_order_count'] = $parent_user['can_upgrade_order_count'] + 1;
                    $parent_user_update['where_id'] = $parent_user['id'];
                    Dever::db('vip/user')->update($parent_user_update);
                }
            }
        }

        return $result;
    }

    # 获取上级用户 这里直接使用旧版的逻辑...return $this->getParent() 有点头晕
    private function getParent($uid, $level, $order = false)
    {
        $parent_uid = Dever::load('invite/api')->getParent($uid);

        $parent_user = Dever::db('vip/user')->one(array('uid' => $parent_uid));

        if ($parent_user) {
            if ($parent_user['is_super'] == 1) {
                $rand_parent = $this->getOneRandUpgradableVipUser($level, $order);
                if ($rand_parent) {
                    return $rand_parent;
                } else {
                    $rand_user = $this->getOneRandSuperVipUser();
                    if ($rand_user) {
                        return $rand_user;
                    }
                }
                return $parent_user;
            } elseif ($parent_user['super_vip_flag'] == 1) {
                return $parent_user;
            } else {
                $parent_level = $this->getInfo($uid);
                if ($parent_level['current']['level'] > $level) {
                    if ($level == 0) {
                        return $parent_user;
                    } else {
                        if ($parent_user['can_upgrade_order_count'] > $parent_user['has_operated_order_count']) {
                            return $parent_user;
                        } else {
                            return $this->getParent($uid, $level, $order);
                        }
                    }
                } else {
                    return $this->getParent($uid, $level, $order);
                }
            }
        } else {
            return $this->getParent($uid, $level, $order);
            /*
            $parent_user_data['uid'] = $invite_uid;
            $id = Dever::db('vip/user')->insert($parent_user_data);
            $parent_user = Dever::db('vip/user')->one($id);
            */
        }

        return $parent_user;
    }

    /**
     * 当用户升级的上级数量或者级别都不满足，随机分配一个符合条件的上级 真诡异的需求...
     */
    private function getOneRandUpgradableVipUser($level, $order_id)
    {
    	$where['level'] = $level;
    	$user = Dever::db('vip/user')->getSupperVip($where);

    	if ($user) {
            $index = array_rand($user);
            return $user[$index];
        }

        return false;
    }

    # 随机获取一个VIP用户
    private function getOneRandSuperVipUser()
    {
        $user = Dever::db('vip/user')->getSupper();

        if ($user) {
            $index = array_rand($user);
            return $user[$index];
        }

        return false;
    }

    # 手动升级
    public function update($uid)
    {
        return Dever::load('score/lib/level')->update($uid, 1);
    }
}