<?php
namespace app\common\controller;
use think\Controller;
use think\Db;
class Csv extends Controller
{
public static function putCsv($condition, $action, $file_name)
{
set_time_limit(0);
switch ($action) {
case "web":
$field = [
'A' => [
"tid",
"Transaction Number"
],
'B' => [
"create_date",
"Time"
],
'C' => [
"full_name",
"Card Name"
],
'D' => [
"face_value",
"Face Value($)"
],
'E' => [
"price_sgd",
"Cost($)"
],
'F' => [
"price_rmb",
"Cost(¥)"
],
'G' => [
"mobile",
"Top-up Number"
],
'H' => [
"Status",
"Status"
],
'I' => [
"ip",
"IP"
],
'J' => [
"email",
"Email"
]
];
$file_name = "web" . $file_name . "order_list";
$builder = Db::table("Transactions")
->field("tid,create_date,menu_id as full_name,tag as face_value,price_sgd,price_rmb,mobile,status,user_ip,email")
->where($condition)
->select();
$list = collection($builder)->each(function ($item) {
//csv导出数字满15位之后自动省略---科学计算法,加上"\t"则可以完整导出
$item['create_date'] .= "\t";
$item['mobile'] .= "\t";
$product_info = getProdInfoByMenuId($item['full_name'], true);
$item['full_name'] = $product_info['full_name'];
$item['face_value'] = number_format($product_info['card_val'] / 100, 2);
if ($item['status'] == '1') $item['status'] = "Success";
if ($item['status'] == '0') $item['status'] = "Pending";
if ($item['status'] == '102') $item['status'] = "Fail";
unset($item['menu_id']);
return $item;
});
//dump($list);exit;
break;
case "wechat":
$file_name = "wechat" . $file_name . "order_list";
$field = [
'A' => [
"tid",
"Transaction Number"
],
'B' => [
"uid",
"Wechat uid"
],
'C' => [
"create_date",
"Time"
],
'D' => [
"mobile",
"Top-up Number"
],
'E' => [
"pid",
"Product pid"
],
'F' => [
"state",
"Status"
],
'G' => [
"upid_lst",
"Red Packet"
],
'H' => [
"admin_note",
"Remark"
]
];
$builder = Db::table("WXTrans")
->field("tid,uid,create_date,mobile,pid,state,upid_lst,admin_note")
->where("uid <> 13")
->where("uid <> 15")
->where("total_fee > 1")
->where("mode != 1")
->where($condition)
->select();
$list = collection($builder)->each(function ($item) {
$item['tid'] = "wx_" . $item['tid'];
$item['create_date'] .= "\t";
$item['mobile'] .= "\t";
$item['upid_lst'] = str_replace(",", " ", $item['upid_lst']);
if ($item['state'] == 3) $item['state'] = "Success";
if ($item['state'] == 2) $item['state'] = "Pending";
if ($item['state'] == 102) $item['state'] = "Fail";
if ($item['state'] == 200) $item['state'] = "Refund";
return $item;
});
break;
case "client":
$file_name = "API" . $file_name . "order_list";
$field = [
'A' => [
"ctime",
"Time"
],
'B' => [
"full_name",
"Card Name"
],
'C' => [
"face_value",
" Face Value($)"
],
'D' => [
"price_sgd",
"Cost($)"
],
'E' => [
"mobile",
"Top-up Number"
],
'F' => [
"err_code",
"Status"
],
'G' => [
"order_id",
"Transaction Number"
],
'H' => [
"client_id",
"API Client"
],
];
if (isset($condition['client_id'])) {
$file_name = $condition['client_id'] . $file_name . "order_list";
} else {
$file_name = "API" . $file_name . "order_list";
}
$builder = Db::table("ApiTrans")
//->fetchSql(true)
->field("ctime,pid as full_name,amount as face_value,price_sgd,mobile,err_code,tid as order_id,client_tag,client_id")
->where("category = 'topup' or category is NULL")
->whereNotLike("client_id", "%_test%")
->where($condition)
->select();
//echo $builder;exit;
$list = collection($builder)->each(function ($item) {
$item['order_id'] = "Our Transaction Number:" . $item['order_id'] . "\n" . "API Transaction Number:" . $item['client_tag'];
$item['price_sgd'] = number_format($item['price_sgd'] / 100, 2);
$item['ctime'] .= "\t";
$item['mobile'] .= "\t";
if ($item['err_code'] == 0) {
$item['err_code'] = "Success";
} elseif ($item['err_code'] == 311) {
$item['err_code'] = "Pending";
} else {
$item['err_code'] = "Fail";
}
$product_info = getProdInfo($item['full_name'], true);
$item['full_name'] = $product_info['full_name'];
$item['face_value'] = number_format($product_info['card_val'] / 100, 2);
unset($item['client_tag']);
//dump($item);exit;
return $item;
});
break;
case "alipay":
$file_name = "alipay" . $file_name . "order_list";
$field = [
'A' => [
"uid",
"Wechat uid"
],
'B' => [
"tid",
"Transaction Number"
],
'C' => [
"ctime",
"Time"
],
'D' => [
"mobile",
"Top-up Number"
],
'E' => [
"pid",
"Product pid"
],
'F' => [
"state",
"Status"
],
'G' => [
"upid_lst",
"Red Packet"
],
'H' => [
"admin_note",
"Remark"
]
];
$builder = Db::table("cz_alipay_trans")
->field("tid,source_uid as uid,ctime,mobile,pid,state,upid_lst,admin_note")
->where($condition)
->select();
$list = collection($builder)->each(function ($item) {
$item['tid'] = "ap_" . $item['tid'];
$item['ctime'] .= "\t";
$item['mobile'] .= "\t";
$item['upid_lst'] = str_replace(",", " ", $item['upid_lst']);
$item['uid'] = getUidByOpenid($item['uid'])['uid'];
if ($item['state'] == 3) $item['state'] = "Success";
if ($item['state'] == 2) $item['state'] = "Pending";
if ($item['state'] == 102) $item['state'] = "Fail";
if ($item['state'] == 200) $item['state'] = "Refund";
return $item;
});
break;
default:
break;
}
$file_name = $file_name . ".csv";
//$file_name = time() . ".csv";
$file_path= $_SERVER['DOCUMENT_ROOT'] . "/uploads/csv/" . $file_name;
if (file_exists($file_path)) {
unlink($file_path);
}
$file_path="/uploads/csv/" . $file_name;
$fp = fopen("." . $file_path, "a");
$limit = 10000;
$calc = 0;
foreach ($field as $k => $v) {
//$tit[] = iconv("UTF-8", "GB2312//IGNORE", $v[1]);
//外国不支持UTF-8,需要转换成utf-8 with bom
$tit[] = "\xEF\xBB\xBF".$v[1];
}
fputcsv($fp, $tit);
foreach ($list as $v) {
$calc ++;
if ($limit == $calc) {
ob_flush();
flush();
$calc = 0;
}
foreach ($v as $_v) {
//$tarr[] = iconv("UTF-8", "GB2312//IGNORE", $_v);
$tarr[] = $_v;
}
fputcsv($fp, $tarr);
unset($tarr);
}
unset($list);
fclose($fp);
return ["code" => 1, "msg" => "获取成功", "url" => $_SERVER['HTTP_HOST'] . $file_path];
}
}
请发表评论