<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
use Home\Controller\PromoteCoinController;

/**
 * 前台公共库文件
 * 主要定义前台公共函数库
 */

function is_weixin_brower()
{
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
        return true;
    }
    return false;
}

function is_in_qq_brower()
{
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'QQ/') !== false) {
        return true;
    }
    return false;
}

function get_apply_launch_status($applyid = 0, $gameid = 0)
{

    if (is_numeric($applyid) && is_numeric($gameid) && $applyid > 0 and $gameid > 0) {

        $launchmodel = M('apply_launch', 'tab_');

        $launch = $launchmodel->field('launch_packge')->where(['apply_id' => $applyid, 'launch_game_id' => $gameid, 'launch_packge' => array('neq', 1)])->find();

        return !empty($launch) ? true : false;

    } else {
        return false;
    }

}

/**
 * 平台币来源
 * @author 鹿文学
 */
function get_promote_coin_source_name($id = 0)
{
    if (is_numeric($id) && $id > 0 && $id < 6) {
        $list = get_promote_coin_source_list();
        return $list[$id];
    } else {
        return '';
    }
}

/**
 * 平台币来源列表
 * @author 鹿文学
 */
function get_promote_coin_source_list()
{
    return [
        1 => '后台发放',
        2 => '自主充值',
        3 => '渠道充值',
        4 => '充值渠道',
        5 => '充值子渠道',
    ];
}

function get_user_by_game($promote_id, $game_id = '', $fields = 'user_id,user_account')
{

    $map = [];

    $game_id > 0 && $map['game_id'] = $game_id;

    $promote_id > 0 && $map['promote_id'] = $promote_id;

    $map['ppuid'] = 0;

    $data = M("UserPlay", "tab_")->field($fields)->where($map)->group('user_id')->select();

    return $data;

}

function get_pro_grade_name($id = 0, $flag = 0)
{
    $list = ['', '一级渠道', '二级渠道', '三级渠道'];
    if ($flag > 0)
        return $list[$id + 1];
    else
        return $list[$id];
}

function check_another_status($game_id = 0, $promote_id = 0)
{
    if (is_numeric($game_id) && $game_id > 0 && is_numeric($promote_id) && $promote_id > 0) {

        $result = M('apply', 'tab_')->field('id')->where(['game_id' => $game_id, 'promote_id' => $promote_id])->find();

        if (is_array($result) && $result['id'] > 0) {
            return true;
        } else {
            return false;
        }

    } else {
        return false;
    }
}

/* 截取指定两个字符之间字符串 */
function get_between($input, $start, $end)
{
    $substr = substr($input, strlen($start) + strpos($input, $start), (strlen($input) - strpos($input, $end)) * (-1));
    return $substr;
}

/**
 * 检测用户是否登录
 * @return integer 0-未登录,大于0-当前登录用户ID
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function is_login_promote()
{
    $user = session('promote_auth');
    if (empty($user)) {
        return 0;
    } else {
        return session('promote_auth_sign') == data_auth_sign($user) ? $user['pid'] : 0;
    }
}

function get_pay_sett($id)
{
    switch ($id) {
        case 0:
            return "未提现";
            break;

        case 1:
            return "已提现";
            break;

    }
}

/**
 * 检测验证码
 * @param integer $id 验证码ID
 * @return boolean     检测结果
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function check_verify($code, $id = 1)
{
    $verify = new \Think\Verify();
    return $verify->check($code, $id);
}

/**
 * 获取列表总行数
 * @param string $category 分类ID
 * @param integer $status 数据状态
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_list_count($category, $status = 1)
{
    static $count;
    if (!isset($count[$category])) {
        $count[$category] = D('Document')->listCount($category, $status);
    }
    return $count[$category];
}

/**
 * 获取段落总数
 * @param string $id 文档ID
 * @return integer    段落总数
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_part_count($id)
{
    static $count;
    if (!isset($count[$id])) {
        $count[$id] = D('Document')->partCount($id);
    }
    return $count[$id];
}

/**
 * 获取导航URL
 * @param string $url 导航URL
 * @return string      解析或的url
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_nav_url($url)
{
    switch ($url) {
        case 'http://' === substr($url, 0, 7):
        case '#' === substr($url, 0, 1):
            break;
        default:
            $url = U($url);
            break;
    }
    return $url;
}

function get_apply_dow_url($game_id = 0, $promote_id = 0)
{
    $model = M('Apply', 'tab_');
    $map['game_id'] = $game_id;
    $map['promote_id'] = $promote_id;
    $map['offline_status'] = 0;
    $data = $model->where($map)->find();
    if (empty($data['dow_url'])) {
        $game_address = M('game', 'tab_')->where('id=' . $game_id)->find();
        if ($game_address['sdk_version'] == 1) {
            if ($game_address['add_game_address'] != '') {
                $game_address_url = $game_address['add_game_address'];
                return $game_address_url;
            } else {
                $game_address_url = $game_address['and_dow_address'];
                return 'http://' . C('APPLY_DOWN_DOMAIN') . substr($game_address_url, 1);
            }
        } else {
            if ($game_address['ios_game_address'] != '') {
                $game_address_url = $game_address['ios_game_address'];
                return $game_address_url;
            } else {
                $game_address_url = $game_address['ios_dow_address'];
                return 'http://' . C('APPLY_DOWN_DOMAIN') . substr($game_address_url, 1);
            }
        }
    }
    return 'https://' . C('APPLY_DOWN_DOMAIN') . $data['dow_url'];
}

function get_promote_list_by_id()
{
    $map['parent_id'] = get_pid();
    $pro = M("promote", "tab_")->where($map)->select();
    return $pro;
}

function index_show($param = array())
{
    $paramcount = count($param);
    if ($paramcount > 0) {
        $paramm[0][] = $param[0];
        $paramm[0][] = $param[1];
        $paramm[0][] = $param[2];
    }
    if ($paramcount - 3 > 0) {
        $paramm[1][] = $param[3];
        $paramm[1][] = $param[4];
        $paramm[1][] = $param[5];
    }
    foreach ($paramm as $key => $value) {
        foreach ($value as $k => $v) {
            if ($v == '') {
                unset($paramm[$key][$k]);
            }
        }
    }
    return $paramm;
}


//获取礼包激活码数量
function get_site_gift_num($id)
{
    $data = M('site_gift', 'tab_')->find($id);
    $novice = explode(',', $data['novice']);
    $count = count($novice);
    if (empty(array_pop($novice)) && $count > 0) {
        $count--;
    }
    if (empty($novice[0])) {
        //$count--;
    }
    return $count;
}

//获取游戏类型
function get_game_type($id)
{
    $game_type = D('SiteGame')->field('game_type')->find($id);
    return $game_type['game_type'];
}

//获取游戏名称
function get_site_game_name($id)
{
    $game_type = D('SiteGame')->field('game_name')->find($id);
    return $game_type['game_name'];
}

//获取游戏图标
function get_site_game_icon($id)
{
    $data = D('SiteGame')->field('game_icon')->find($id);
    $icon_url = get_cover($data['game_icon'], 'path');
    return $icon_url;
}

//获取游戏ID
function get_game_id($game_name)
{
    $map['game_name'] = $game_name;
    $map['promote_id'] = PID;
    $game_type = M('Game', 'tab_')->field('id')->where($map)->find();
    return $game_type['id'];
}

function get_game_icon()
{
    $data = $game_type = M('Game', 'tab_')->field('icon')->find($id);
    return $data['icon'];

}

function get_site_game_id($game_name)
{
    $map['game_name'] = $game_name;
    $map['promote_id'] = PID;
    $game_type = D('SiteGame')->field('id')->where($map)->find();
    return $game_type['id'];
}

/**
 * 判断手机访问型号
 * @return string
 */
function get_device_type()
{
    //全部变成小写字母
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    $type = 'other';
    //分别进行判断
    if (strpos($agent, 'iphone') || strpos($agent, 'ipad')) {
        $type = 'ios';
    }

    if (strpos($agent, 'android')) {
        $type = 'android';
    }
    return $type;
}

//获取支付方式
function get_pay_way($id = null)
{
    if (!isset($id)) {
        return false;
    }
    switch ($id) {
        case -1:
            return "绑币";
            break;
        case 0:
            return "平台币";
            break;
        case 1:
            return "支付宝";
            break;
        case 2:
        case 3:
        case 4:
            return "微信";
            break;
        case 5:
            return "聚宝云";
            break;
        case 6:
            return "竣付通";
            break;
        case 7:
            return "苹果支付";
            break;
        case 8:
            return "金猪支付";
            break;
        case 9:
            return "双乾支付";
            break;
    }
}

//获取支付方式
function getPayStatusName($status)
{
    switch ($status) {
        case -1:
            return "绑币";
            break;
        case 0:
            return "平台币";
            break;
        case 1:
            return "支付宝";
            break;
        case 2:
        case 3:
        case 4:
            return "微信";
            break;
        case 5:
            return "聚宝云";
            break;
        case 6:
            return "竣付通";
            break;
        case 7:
            return "苹果支付";
            break;
        case 8:
            return "金猪支付";
            break;
        case 9:
            return "双乾支付";
            break;
    }
}

/**
 * 获取打包状态
 * @param  [type] $promote_id [description]
 * @param  [type] $game_id    [description]
 * @return [type]             [description]
 */
function get_enable_status($promote_id = 0, $game_id = 0)
{
    if ($promote_id != 0 && $game_id != 0) {
        $map['promote_id'] = $promote_id;
        $map['game_id'] = $game_id;
        $data = M('apply', 'tab_')->field('enable_status')->where($map)->find();
        if ($data) {
            return $data['enable_status'];
        } else {
            return false;
        }
    } else {
        return false;
    }
}

/**
 * 获取打包id ,单价 分成比例
 * @param  [type] $promote_id [description] 0:id  1:money 2:ratio
 * @param  [type] $game_id    [description]
 * @return [type]             [description]
 */
function get_apply_id($promote_id = 0, $game_id = 0, $type = 0)
{
    if ($promote_id != 0 && $game_id != 0) {
        $map['promote_id'] = $promote_id;
        $map['game_id'] = $game_id;
        $data = M('apply', 'tab_')->field('id,promote_ratio,promote_money')->where($map)->find();
        if ($type == 1) {
            return $data['promote_money'];
            die;
        } elseif ($type == 2) {
            return $data['promote_ratio'];
            die;
        } else {
            return $data['id'];
            die;
        }

    }
    if (empty($data)) {
        $data = M('game', 'tab_')->field('id,ratio,money')->find($game_id);
        if ($type == 1) {
            return $data['money'];
            die;
        } elseif ($type == 2) {
            return $data['ratio'];
            die;
        } else {
            return $data['id'];
            die;
        }
    }

}

/**
 * 获取打包状态
 * @param  [type] $promote_id [description]
 * @param  [type] $game_id    [description]
 * @return [type]             [description]
 */
function get_apply_status($promote_id = 0, $game_id = 0)
{
    if ($promote_id != 0 && $game_id != 0) {
        $map['promote_id'] = $promote_id;
        $map['game_id'] = $game_id;
        $info = M('apply', 'tab_')->field('status')->where($map)->find();
        if ($info) {
            return $info['status'];
        } else {
            $game = M('Game', 'tab_');
            $gdata = $game->where(array('id' => $game_id))->find();
            $ratio = $gdata['ratio'];
            $money = $gdata['money'];
            $sdk_version = $gdata['sdk_version'];
            $model = M('Apply', 'tab_');
            $data['game_id'] = $game_id;
            $data['game_name'] = get_game_name($game_id);
            $data['promote_id'] = session("promote_auth.pid");
            $data['promote_account'] = session("promote_auth.account");
            $data['apply_time'] = NOW_TIME;
            $data['status'] = 1;
            $data['sdk_version'] = $sdk_version;
            $data['ratio'] = $ratio;
            $data['money'] = $money;

            $res = $model->add($data);
            return 1;
        }
    } else {
        return false;
    }
}

/**
 * [获取游戏版本]
 * @param  [type] $id [description]
 * @return [type]     [description]
 */
function game_version($id)
{
    $game = M('game', 'tab_');
    $map['id'] = $id;
    $data = $game->field('sdk_version')->where($map)->find();
    if ($data['id'] == null) {
        return false;
    }
    return $data['version'];
}

// 获取IOS游戏名称
function get_ios_game_name($game_id = null, $field = 'id')
{
    $map[$field] = $game_id;
    $map['game_version'] = 0;
    $data = M('Game', 'tab_')->where($map)->find();
    if (empty($data)) {
        return false;
    }
    $game_name = explode("(", $data['game_name']);
    return $game_name[0];
}

function get_version($game_id = '')
{
    if ($game_id) {
        $res = M('game_source', 'tab_')->field('version')->where(['game_id' => $game_id])->find();
        return $res['version'];
    } else {
        return false;
    }
}

//根据id获取游戏原包更新时间
function get_source_time($game_id)
{
    $model = M('game_source', 'tab_');
    $map['game_id'] = $game_id;
    $res = $model->where($map)->find();
    return $res['create_time'];
}

/**
 *推广员搜索游戏下拉列表
 */
function get_promote_serach_game()
{
    $game = M("game", "tab_");
    if (!empty($type)) {
        $map['developers'] = $type == 1 ? array('EQ', 0) : array('GT', 0);
        $map['game_status'] = $type == 1 ? 1 : array('in', '0,1');

    } else {
        $map['apply_status'] = 1;
        $map['online_status'] = 1;

    }
    $map['down_port'] = 1;
    $lists = $game->field('id,game_name,sdk_version,relation_game_name,icon,relation_game_id')->where($map)->group('relation_game_id')->select();
    if (empty($lists)) {
        return false;
    }
    return $lists;
}

function getPromoteSearchGame()
{
    $game = M("game", "tab_");
    if (!empty($type)) {
        $map['developers'] = $type == 1 ? array('EQ', 0) : array('GT', 0);
        $map['game_status'] = $type == 1 ? 1 : array('in', '0,1');

    } else {
        $map['apply_status'] = 1;
        $map['online_status'] = 1;

    }
    $map['down_port'] = 1;
    $lists = $game->field('id,game_name,sdk_version,relation_game_name,icon,relation_game_id')->where($map)->select();
    if (empty($lists)) {
        return false;
    }
    return $lists;
}

//二级推广员id
function get_promote_child_ids($id)
{
    $map1['parent_id'] = $id;
    $arr1 = M('promote', 'tab_')->where($map1)->field('id')->select();
    if ($arr1) {
        return $arr1;
    } else {
        return false;
    }

}

/**
 * 获取原包信息
 * @return [type] [description]
 */
function get_game_source_info($id)
{
    $map["game_id"] = $id;
    $data = M('GameSource', 'tab_')->where($map)->find();
    if (empty($data)) {
        $result = '';
    } else {
        $result = $data;
    }
    if (empty($result['file_size'])) {
        $result['file_size'] = 0;
    }
    return $result;

}

/**
 * 获取游戏信息
 * @return [type] [description]
 */
function get_game_info($id = 0)
{
    $map["id"] = $id;
    $data = M('Game', 'tab_')->where($map)->find();


    return $data;

}

/**
 * 获取游戏区服
 * @return [type] [description]
 */
function getServer_name($game_name = "")
{
    $data = M('server', 'tab_')->where(['game_name' => $game_name])->select();
    return $data;
}

/**
 * 获取游戏已开服的区服
 * @return [type] [description]
 */
function getServer_name_on($game_name = "", $status = 1)
{
    $map['game_name'] = $game_name;
    $map['start_time'] = ['lt', time()];
    if ($status == 2) {
        $map['show_status'] = 1;
    }
    $data = M('server', 'tab_')->where($map)->select();
    return $data;
}


/**
 * 统计推广员总流水
 * @param  [type] $id [description]
 * @return [type]     [description]
 */
function sum_promote_total_money($id)
{
    $map['promote_id'] = $id;
    $map['pay_status'] = 1;
    $res = M('Spend', 'tab_')->field("SUM(pay_amount) as pay_amount")->where($map)->select();
    return $res[0]['pay_amount'] == null ? '0.00' : $res[0]['pay_amount'];
}


function get_promote_levels($id = 0)
{
    $data = M('promote', 'tab_')->field('parent_id,grand_id')->where(array('id' => $id))->find();
    if ($data['parent_id'] == 0 && $data['grand_id'] == 0) {
        return '一级推广员';
    } elseif ($data['parent_id'] > 0 && $data['grand_id'] == 0) {
        return '二级推广员';
    } elseif ($data['parent_id'] > 0 && $data['grand_id'] > 0) {
        return '三级推广员';
    }
}

/**
 * [上线渠道]
 * @param  [type] $id  [description]
 * @param  [type] $pid [description]
 * @return [type]      [description]
 */
function get_top_promote($id, $pid)
{
    if ($pid == 0) {
        $pro = M("promote", "tab_")->field('account')->where(array('id' => $id))->find();
    } else {
        $map['id'] = $pid;
        $pro = M("promote", "tab_")->field('account')->where($map)->find();
    }
    if ($pro == '') {
        return false;
    }
    return $pro['account'];
}

function get_promote_list($select = '')
{
    $list = M("Promote", "tab_")->field('id,account,balance_coin')->select();//where("status=1")->
    if (empty($list)) {
        return '';
    }
    if ($select == 111) {
        $new['id'] = -1;
        $new['account'] = "全站用户";
        array_unshift($list, $new);
    }
    return $list;
}

/**
 * [获取所有一级推广员]
 * @return [type] [description]
 */
function get_all_toppromote()
{
    $map['status'] = 1;
    $map['parent_id'] = 0;
    $list = M("Promote", "tab_")->where($map)->select();
    if (empty($list)) {
        return '';
    }
    return $list;
}

//获取sdk类型名称
function getSDKTypeName($sdkType, $chinese = false)
{
    $android = 'Android';

    if ($chinese) {
        $android = '安卓';
    }

    switch ($sdkType) {
        case 0:
            $sdkName = $android . '+ios';
            break;
        case 1:
            $sdkName = $android;
            break;
        case 2:
            $sdkName = 'ios';
            break;
    }

    return $sdkName;
}

//获取sdk类型
function getSDKType()
{
    $sdkTypeData = array(
        1 => '安卓',
        2 => 'ios',
    );

    return $sdkTypeData;
}

//判断是否有推广员权限 $parentId-父推广员ID $promoterId-推广员ID $promoterParentId-推广员父ID
function hasPromotePermission($parentId, $promoteId)
{
    if ($promoteId == $parentId) {
        return true;
    }

    return isParentPromote($parentId, $promoteId);
}

function isParentPromote($parentId, $promoteId)
{
    $promoteParentId = D('Promote')->where(array('id' => $promoteId))->getField('parent_id');

    if ($promoteParentId == $parentId) {
        return true;
    } elseif ($promoteParentId == 0) {
        return false;
    } else {
        return isParentPromote($parentId, $promoteParentId);
    }
}

//获取所有该类型的渠道    $promoteId-渠道ID  $promoteType-1:本账号 2:组长 3:推广员
function getAllPromoteListByType($promoteType = 1, $retOne = false)
{
    $childPromoteData = array();

    switch ($promoteType) {
        case 1:
            $map['id'] = PID;
            break;
        case 2:
        case 3:
            $promoteData = D('Promote')->where(array('id' => PID))->find();

            if ($promoteData['grand_id'] > 0) {
                return [];
            } elseif ($promoteData['parent_id'] > 0) {
                $promoteType--;
            }
            $childPromoteIds = getAllPromoteIdsByType((string)PID, $promoteType);
            break;
    }

    if (!empty($childPromoteIds)) {
        $map['id'] = ['in', $childPromoteIds];
    }

    if (!empty($map)) {
        if ($retOne) {
            $childPromoteData = D('Promote')->where($map)->getField('id', true);
        } else {
            $childPromoteData = D('Promote')->where($map)->field('id,account,nickname,real_name')->select();
        }
    }

    return $childPromoteData;
}

//获取所有子渠道ID
function getAllPromoteIdsByType($promoteIds, $level = 3, $nowLevel = 1)
{
    if (!empty($promoteIds)) {
        $map['parent_id'] = ['in', $promoteIds];
        $nowLevel++;

        $promoteData = D('Promote')
            ->where($map)
            ->getField('id', true);

        if (count($promoteData) > 0) {
            $promoteIdsStr = implode(',', $promoteData);
            $thisMap['parent_id'] = ['in', $promoteIdsStr];
            $thisPromoteDataCount = D('Promote')->where($thisMap)->count('id');

            if ($thisPromoteDataCount > 0 && $level > $nowLevel) {
                return getAllPromoteIdsByType($promoteIdsStr, $level, $nowLevel);
            } else {
                if ($level == $nowLevel) {
                    return $promoteIdsStr;
                } else {
                    return '';
                }
            }
        } else {
            return '';
        }
    }

    return '';
}

//获取所有子渠道列表    $type 1-返回数组 2-返回id数组 3-返回id字符串
function getAllChildPromoteList($type = 1)
{
    $childPromoteData = array();
    $childPromoteIds = getAllChildPromoteIds((string)PID);

    if (!empty($childPromoteIds)) {
        $map['id'] = ['in', $childPromoteIds];

        switch ($type) {
            case 1:
                $childPromoteData = D('Promote')->where($map)->field('id,account,nickname')->select();
                break;
            case 2:
                $childPromoteData = D('Promote')->where($map)->getField('id');
                break;
            case 3:
                $childPromoteData = $childPromoteIds;
                break;
        }
    }

    return $childPromoteData;
}

//获取所有子渠道ID
function getAllChildPromoteIds($promoteIds, $isStart = true)
{
    if (!empty($promoteIds)) {
        $map['parent_id'] = ['in', $promoteIds];

        $promoteData = D('Promote')
            ->where($map)
            ->getField('id', true);

        if (count($promoteData) > 0) {
            $promoteIdsStr = implode(',', $promoteData);
            $thisMap['parent_id'] = ['in', $promoteIdsStr];
            $thisPromoteDataCount = D('Promote')->where($thisMap)->count('id');

            if ($thisPromoteDataCount > 0) {
                return getAllChildPromoteIds($promoteIdsStr, false);
            } else {
                if ($isStart) {
                    return $promoteIdsStr;
                } else {
                    return $promoteIds . ',' . $promoteIdsStr;
                }
            }
        } else {
            return '';
        }
    }

    return '';
}

//获上级渠道ID
function getParentPromoteId($promoteId)
{
    $map['id'] = $promoteId;

    return D('Promote')->where($map)->getField('parent_id');
}

function getGamePermission($gameId)
{
    $gameData = M('Game', 'tab_')
        ->where(array('id' => $gameId))
        ->select();

    if (count($gameData) == 2) {
        $applyIdAnd = M('Apply', 'tab_')->where(array('game_id' => $gameData[0]['id']))->getField('id');
        $applyIdIos = M('Apply', 'tab_')->where(array('game_id' => $gameData[1]['id']))->getField('id');
    }

    if (empty($applyIdAnd) || empty($applyIdIos)) {
        return 1;
    } else {
        return 0;
    }
}

function isMinPromote($promoteId = PID)
{
    return D('Promote')->where(array('id' => $promoteId))->getField('grand_id');
}

//判断渠道游戏是否添加--安卓+ios
function addedAboutDoubleSdk($promoteId, $gameId)
{
    $promoteData = D('Promote')->where(array('id' => $promoteId))->find();
    $gameData = M('Game', 'tab_')->where(array('id' => $gameId))->find();

    $map['tab_game.relation_game_id'] = $gameData['relation_game_id'];
    if ($promoteData['parent_id'] == 0) {
        $count = M('Game', 'tab_')
            ->join('tab_apply on tab_game.id = tab_apply.game_id and tab_apply.offline_status = 0 and tab_apply.promote_id = ' . $promoteId)
            ->where($map)
            ->count();

        $added = ($count == 2) ? 1 : 0;
    } else {
        $applyData = M('Game', 'tab_')
            ->field('tab_game.id,ta_2.id as apply_id')
            ->join('tab_apply as ta_1 on tab_game.id = ta_1.game_id and ta_1.offline_status = 0 and ta_1.promote_id = ' . $promoteData['parent_id'])
            ->join('left join tab_apply as ta_2 on tab_game.id = ta_2.game_id and ta_2.offline_status = 0 and ta_2.promote_id = ' . $promoteId)
            ->where($map)
            ->select();

        $added = 0;
        $count = 0;
        foreach ($applyData as $list) {
            if ($list['apply_id'] > 0) {
                $count++;
            }
        }

        if ($count == count($applyData)) {
            $added = 1;
        }
    }

    return $added;
}

function hideStar($string, $start = 0, $length = 0, $re = '*')
{
    if (empty($string)) return false;
    $strarr = array();
    $mb_strlen = mb_strlen($string);
    while ($mb_strlen) {
        $strarr[] = mb_substr($string, 0, 1, 'utf8');
        $string = mb_substr($string, 1, $mb_strlen, 'utf8');
        $mb_strlen = mb_strlen($string);
    }
    $strlen = count($strarr);
    $begin = $start >= 0 ? $start : ($strlen - abs($start));
    $end = $last = $strlen - 1;
    if ($length > 0) {
        $end = $begin + $length - 1;
    } elseif ($length < 0) {
        $end -= abs($length);
    }
    for ($i = $begin; $i <= $end; $i++) {
        $strarr[$i] = $re;
    }
    if ($begin >= $end || $begin >= $last || $end > $last) return false;
    return implode('', $strarr);
}

//ios下载链接处理
function getIosDownloadLink($gameId, $promoteId)
{
    $downloadLink = get_apply_dow_url($gameId, $promoteId);
    $gameData = M('Game', 'tab_')->where(array('id' => $gameId))->find();

    if ($downloadLink == ('https://' . $_SERVER['HTTP_HOST']) || $gameData['dow_status'] == 0) {
        return ('https://' . $_SERVER['HTTP_HOST']);
    }

    $gameSourceData = M('GameSource', 'tab_')->where(array('game_id' => $gameId))->find();

    $data['game_id'] = $gameId;
    $data['promote_id'] = $promoteId;
    $data['download_link'] = $downloadLink;
    $data['game_icon'] = 'https://' . $_SERVER['HTTP_HOST'] . get_cover($gameData['icon'], 'path');
    $data['pack_name'] = $gameSourceData['pack_name'];
    $data['version'] = $gameSourceData['version'];
    $data['source_version'] = $gameSourceData['source_version'];
    $data['relation_game_name'] = $gameData['relation_game_name'];

    return getIosPlistFile($data);
}

function getIosPlistFile($data)
{
    $sourceVersion = empty($data['source_version']) ? 0 : $data['source_version'];
    $fileName = __DIR__ . '/../../../Plist/manifest_' . $data['game_id'] . '_' . $data['promote_id'] . '_' . $sourceVersion . '.plist';
    $fileUrl = 'https://' . $_SERVER['HTTP_HOST'] . '/Plist/manifest_' . $data['game_id'] . '_' . $data['promote_id'] . '_' . $sourceVersion . '.plist';
//    if (!file_exists($fileName)) {
//
//    }
    $obj['items'][0]['assets'][0] = [
        'kind' => 'software-package',
        'url' => $data['download_link'],
    ];
    $obj['items'][0]['assets'][1] = [
        'kind' => 'display-image',
        'url' => $data['game_icon'],
    ];
    $obj['items'][0]['assets'][2] = [
        'kind' => 'full-size-image',
        'url' => $data['game_icon'],
    ];
    $obj['items'][0]['metadata']['bundle-identifier'] = $data['bao_name'];
    $obj['items'][0]['metadata']['bundle-version'] = empty($data['version']) ? '1.0' : $data['version'];
    $obj['items'][0]['metadata']['kind'] = 'software';
    $obj['items'][0]['metadata']['title'] = $data['relation_game_name'];
    Vendor('XmlOperate.XmlOperate');
    $plist = new PropertyList($obj);
    $xml = $plist->xml();

    file_put_contents($fileName, $xml, LOCK_EX);

    return 'itms-services://?action=download-manifest&url=' . $fileUrl;
}

function getCoinTypeName($coinType)
{
    $coinTypeName = '';
    switch ($coinType) {
        case 1:
            $coinTypeName = '通用币';
            break;
        case 2:
            $coinTypeName = '绑定币';
            break;
    }

    return $coinTypeName;
}

function getCoinStatusName($status)
{
    $statusName = '';
    switch ($status) {
        case 1:
            $statusName = '正常';
            break;
        case 2:
            $statusName = '锁定';
            break;
        case 3:
            $statusName = '验证失败';
            break;
    }

    return $statusName;
}

function getCoinRecordTypeName($type = 0)
{
    if ($type == 0) {
        return PromoteCoinController::$type;
    } else {
        return PromoteCoinController::$type[$type];
    }
}

function getCoinRecordSubTypeName($type = 0)
{
    if ($type == 0) {
        return PromoteCoinController::$subType;
    } else {
        return PromoteCoinController::$subType[$type];
    }
}

function getCoinRecordHandleTypeName($type = 0)
{
    if ($type == 0) {
        return PromoteCoinController::$handleType;
    }
}

function promoteCan($promoteId, \Closure $callback)
{
    $level = get_promote_level(session('promote_auth.pid'));
    return $callback($level);
}