用户信息大数据量导出错误

master
zhengyongxing 5 years ago
parent ff77638001
commit cc4a071194

@ -197,40 +197,53 @@ class ExportController extends Controller
break;
case 3:
if($_REQUEST['type']==2){
$xlsCell = array(
array('id', L('Account_number_ID')),
array('register_type', L('Third_party_source'), "get_registertype", '*'),
array('account', "玩家账号"),
array('promote_account', L('Subordinate_channel')),
array('balance', "账户平台币"),
array('recharge_total', "累计充值"),
array('gold_coin', "金币"),
array('alipay', "绑定支付宝"),
array('small_count', "小号"),
array('vip_level', "VIP等级"),
array('register_time', L('Registration_time'), 'time_format', '*'),
array('login_time', "最后登录时间", 'time_format', '*'),
array('register_ip', L('Register_iP')),
array('lock_status', L('Account_status'), 'get_info_status', '*', '4'),
);
// $xlsCell = array(
// array('id', L('Account_number_ID')),
// array('register_type', L('Third_party_source'), "get_registertype", '*'),
// array('account', "玩家账号"),
// array('promote_account', L('Subordinate_channel')),
// array('balance', "账户平台币"),
// array('recharge_total', "累计充值"),
// array('gold_coin', "金币"),
// array('alipay', "绑定支付宝"),
// array('small_count', "小号"),
// array('vip_level', "VIP等级"),
// array('register_time', L('Registration_time'), 'time_format', '*'),
// array('login_time', "最后登录时间", 'time_format', '*'),
// array('register_ip', L('Register_iP')),
// array('lock_status', L('Account_status'), 'get_info_status', '*', '4'),
// );
$field = 'tab_user.id,tab_user.register_type,tab_user.account,tab_user.promote_account,tab_user.balance
,IFNULL(sum(ss.pay_amount),0) AS recharge_total,tab_user.gold_coin,tab_user.alipay,tab_user.id as small_count,
tab_user.id as vip_level,register_time,tab_user.login_time,tab_user.register_ip,tab_user.lock_status,
tab_user.age_status,tab_user.register_way';
$xlsCell = [L('Account_number_ID'),L('Third_party_source'),"玩家账号",L('Subordinate_channel'), "账户平台币"
, "累计充值", "金币", "绑定支付宝", "小号", "VIP等级", L('Registration_time'), "最后登录时间", L('Register_iP'), L('Account_status')];
$map['tab_user.register_type']= ['in','3,4,5,6'];
}else{
$xlsCell = array(
array('id', L('Account_number_ID')),
array('account', "玩家账号"),
array('promote_account', L('Subordinate_channel')),
array('balance', "账户平台币"),
array('recharge_total', "累计充值"),
array('gold_coin', "金币"),
array('alipay', "绑定支付宝"),
array('small_count', "小号"),
array('vip_level', "VIP等级"),
array('register_type', "注册方式"),
array('register_time', L('Registration_time'), 'time_format', '*'),
array('login_time', "最后登录时间", 'time_format', '*'),
array('register_ip', L('Register_iP')),
array('lock_status', L('Account_status'), 'get_info_status', '*', '4'),
);
// $xlsCell = array(
// array('id', L('Account_number_ID')),
// array('account', "玩家账号"),
// array('promote_account', L('Subordinate_channel')),
// array('balance', "账户平台币"),
// array('recharge_total', "累计充值"),
// array('gold_coin', "金币"),
// array('alipay', "绑定支付宝"),
// array('small_count', "小号"),
// array('vip_level', "VIP等级"),
// array('register_type', "注册方式"),
// array('register_time', L('Registration_time'), 'time_format', '*'),
// array('login_time', "最后登录时间", 'time_format', '*'),
// array('register_ip', L('Register_iP')),
// array('lock_status', L('Account_status'), 'get_info_status', '*', '4'),
// );
$field = 'tab_user.id,tab_user.account,tab_user.promote_account,tab_user.balance,
IFNULL(sum(ss.pay_amount),0) AS recharge_total,tab_user.gold_coin,tab_user.alipay
,tab_user.id as small_count,tab_user.id as vip_level,tab_user.register_type,register_time,tab_user.login_time
,tab_user.register_ip,tab_user.lock_status,tab_user.age_status,tab_user.register_way';
$xlsCell = [L('Account_number_ID'),"玩家账号",L('Subordinate_channel'), "账户平台币"
, "累计充值", "金币", "绑定支付宝", "小号", "VIP等级", "注册方式", L('Registration_time'),
"最后登录时间", L('Register_iP'), L('Account_status')];
$map['tab_user.register_type']=['in','1,2'];
$map['tab_user.register_type']=['in','0,1,2,7'];
@ -286,6 +299,8 @@ class ExportController extends Controller
$maps['tab_user.register_type'] = 2;
}
} else {
$map['register_way'] = I('register_way');
unset($_REQUEST['register_way']);
}
@ -320,62 +335,141 @@ class ExportController extends Controller
$map['puid'] = array('eq',0);
$map['is_platform'] = 0;
set_time_limit(0);
// $columns = [
// '序号ID', '姓名', '电话',
// ];
$fileName = '导出.xlsx';
$csvFileName = $xlsName.'.csv';
//设置好告诉浏览器要下载excel文件的headers
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'. $csvFileName .'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$fp = fopen('php://output', 'a');//打开output流
mb_convert_variables('GBK', 'UTF-8', $xlsCell);
fputcsv($fp, $xlsCell);//将数据格式化为CSV格式并写入到output流中
$accessNum = '100000';//从数据库获取总量,假设是十万
if($_REQUEST['promote_id'] && $_REQUEST['promote_id'] > -1){
$xlsData = M('user', 'tab_')->field('tab_user.id,tab_user.age_status,tab_user.account,tab_user.gold_coin,tab_user.alipay,tab_user.balance,tab_user.promote_account,register_time,tab_user.lock_status,tab_user.register_way,tab_user.register_type,tab_user.register_ip,tab_user.login_time,IFNULL(sum(ss.pay_amount),0) AS recharge_total')
->join('left join tab_spend as ss on ss.user_id=tab_user.id AND ss.pay_status = 1'.$spendprom)
->join($game_map ? "tab_user_play on tab_user_play.user_id = tab_user.id $game_map" : false)
->join($game_play_map ? "tab_user_play_info on tab_user_play_info.user_id = tab_user.id $game_play_map" : false)
->where($map)
->group('tab_user.id')
->order($order)
->having($havs)
->select();
$accessNum = M('user', 'tab_')
->field('IFNULL(sum(ss.pay_amount),0) AS recharge_total,count(tab_user.id) as count')
// ->field($field)
->join('left join tab_spend as ss on ss.user_id=tab_user.id AND ss.pay_status = 1'.$spendprom)
->join($game_map ? "tab_user_play on tab_user_play.user_id = tab_user.id $game_map" : false)
->join($game_play_map ? "tab_user_play_info on tab_user_play_info.user_id = tab_user.id $game_play_map" : false)
->where($map)
->group('tab_user.id')
->having($havs)
->select(false);
$sql = "select count(t.count) as count from ({$accessNum}) as t ";
$accessNum = M('user', 'tab_')->query($sql);
}else{
$xlsData = M('user', 'tab_')->field('tab_user.id,tab_user.age_status,tab_user.account,tab_user.gold_coin,tab_user.alipay,tab_user.balance,tab_user.promote_account,register_time,tab_user.lock_status,tab_user.register_way,tab_user.register_type,tab_user.register_ip,tab_user.login_time,IFNULL(sum(ss.pay_amount),0) AS recharge_total')
->join('left join tab_spend as ss on ss.user_id=tab_user.id AND ss.pay_status = 1')
->join($game_map ? "tab_user_play on tab_user_play.user_id = tab_user.id $game_map" : false)
->join($game_play_map ? "tab_user_play_info on tab_user_play_info.user_id = tab_user.id $game_play_map" : false)
->where($map)
->group('tab_user.id')
->order($order)
->having($havs)
->select();
$accessNum = M('user', 'tab_')
// ->field('tab_user.id,tab_user.age_status,tab_user.account,tab_user.gold_coin,tab_user.alipay,tab_user.balance,tab_user.promote_account,register_time,tab_user.lock_status,tab_user.register_way,tab_user.register_type,tab_user.register_ip,tab_user.login_time,IFNULL(sum(ss.pay_amount),0) AS recharge_total')
->field('IFNULL(sum(ss.pay_amount),0) AS recharge_total,count(tab_user.id) as count')
->join('left join tab_spend as ss on ss.user_id=tab_user.id AND ss.pay_status = 1')
->join($game_map ? "tab_user_play on tab_user_play.user_id = tab_user.id $game_map" : false)
->join($game_play_map ? "tab_user_play_info on tab_user_play_info.user_id = tab_user.id $game_play_map" : false)
->where($map)
->group('tab_user.id')
->having($havs)
->select(false);
$sql = "select count(t.count) as count from ({$accessNum}) as t ";
$accessNum = M('user', 'tab_')->query($sql);
}
//数据
if($xlsData) {
$ids = array_column($xlsData,'id');
/* 在手里 */
$small = M('user','tab_')->field('count(id) as count,puid')->where(['puid'=>array('in',$ids)])->group('puid')->select();
/* 出售中 */
$small2 = M('user','tab_')->field('count(id) as count,source_puid')->where(['source_puid'=>array('in',$ids),'source_time'=>0])->group('source_puid')->select();
foreach($xlsData as $k => $v) {
$xlsData[$k]['vip_level'] = get_vip_level($v['recharge_total']);
if(empty($v['alipay'])){
$xlsData[$k]['alipay'] = '--';
}
$xlsData[$k]['register_type'] = $v['register_type'] == 2?'手机号':($v['register_type']==7?'邮箱':'账号');
$accessNum = $accessNum[0]['count'];
$perSize = 5000;//每次查询的条数
$pages = ceil($accessNum / $perSize);
$lastId = 0;
for($i = 1; $i <= $pages; $i++) {
if($_REQUEST['promote_id'] && $_REQUEST['promote_id'] > -1){
$xlsData = M('user', 'tab_')
// ->field('tab_user.id,tab_user.age_status,tab_user.account,tab_user.gold_coin,tab_user.alipay,tab_user.balance,tab_user.promote_account,register_time,tab_user.lock_status,tab_user.register_way,tab_user.register_type,tab_user.register_ip,tab_user.login_time,IFNULL(sum(ss.pay_amount),0) AS recharge_total')
->field($field)
->join('left join tab_spend as ss on ss.user_id=tab_user.id AND ss.pay_status = 1'.$spendprom)
->join($game_map ? "tab_user_play on tab_user_play.user_id = tab_user.id $game_map" : false)
->join($game_play_map ? "tab_user_play_info on tab_user_play_info.user_id = tab_user.id $game_play_map" : false)
->where($map)
->limit(($i-1)*$perSize ,$perSize)
->group('tab_user.id')
->order($order)
->having($havs)
->select();
}else{
$xlsData = M('user', 'tab_')
// ->field('tab_user.id,tab_user.age_status,tab_user.account,tab_user.gold_coin,tab_user.alipay,tab_user.balance,tab_user.promote_account,register_time,tab_user.lock_status,tab_user.register_way,tab_user.register_type,tab_user.register_ip,tab_user.login_time,IFNULL(sum(ss.pay_amount),0) AS recharge_total')
->field($field)
->join('left join tab_spend as ss on ss.user_id=tab_user.id AND ss.pay_status = 1')
->join($game_map ? "tab_user_play on tab_user_play.user_id = tab_user.id $game_map" : false)
->join($game_play_map ? "tab_user_play_info on tab_user_play_info.user_id = tab_user.id $game_play_map" : false)
->where($map)
->limit(($i-1)*$perSize ,$perSize)
->group('tab_user.id')
->order($order)
->having($havs)
->select();
}
//数据
if($xlsData) {
$ids = array_column($xlsData,'id');
/* 在手里 */
$small = M('user','tab_')->field('count(id) as count,puid')->where(['puid'=>array('in',$ids)])->group('puid')->select();
/* 出售中 */
$small2 = M('user','tab_')->field('count(id) as count,source_puid')->where(['source_puid'=>array('in',$ids),'source_time'=>0])->group('source_puid')->select();
$xlsData[$k]['small_count'] = 0;
foreach($small as $s) {
if($s['puid'] == $v['id']) {
$xlsData[$k]['small_count'] += $s['count'];break;
foreach($xlsData as $k => $v) {
$xlsData[$k]['vip_level'] = get_vip_level($v['recharge_total']);
if(empty($v['alipay'])){
$xlsData[$k]['alipay'] = '--';
}
}
foreach($small2 as $s) {
if($s['source_puid'] == $v['id']) {
$xlsData[$k]['small_count'] += $s['count'];break;
$xlsData[$k]['register_time'] = date('Y-m-d H:i:s',$xlsData[$k]['register_time']);
$xlsData[$k]['login_time'] = date('Y-m-d H:i:s',$xlsData[$k]['login_time']);
$xlsData[$k]['lock_status'] = $xlsData[$k]['lock_status']==1?'正常':'锁定';
$xlsData[$k]['register_type'] = $v['register_type'] == 2?'手机号':($v['register_type']==7?'邮箱':'账号');
$xlsData[$k]['small_count'] = 0;
foreach($small as $s) {
if($s['puid'] == $v['id']) {
$xlsData[$k]['small_count'] += $s['count'];break;
}
}
foreach($small2 as $s) {
if($s['source_puid'] == $v['id']) {
$xlsData[$k]['small_count'] += $s['count'];break;
}
}
unset($xlsData[$k]['age_status']);
unset($xlsData[$k]['register_way']);
}
}
foreach($xlsData as $value) {
mb_convert_variables('GBK', 'UTF-8', $value);
fputcsv($fp, $value);
$lastId = $value['id'];
}
unset($xlsData);//释放变量的内存
//刷新输出缓冲到浏览器
ob_flush();
flush();//必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。
}
fclose($fp);
exit();
break;
case 4:

Loading…
Cancel
Save