Commit a80e8524 by mushishixian

导出数据

parent c483e6a1
......@@ -4,8 +4,10 @@
namespace App\Http\Services;
//后台用户相关信息服务
use App\Http\Transformers\SupplierTransformer;
use App\Model\BrandModel;
use App\Model\IntracodeModel;
use App\Model\RedisModel;
use App\Model\SupplierChannelModel;
use App\Model\SupplierContactModel;
use App\Model\UserInfoModel;
......@@ -185,9 +187,91 @@ class DataService
// return iconv('utf-8', 'GBK', $data);
// }
public function test()
//供应商导出给业务整理,还要带有是否有上架sku数量的标识
public function exportSuppliers()
{
$model = new BrandModel();
dd(DB::connection('spu')->table('brand')->count());
$model = new SupplierChannelModel();
$suppliers = $model->where('is_type', 0)->get();
if (empty($suppliers)) {
return '供应商空';
}
$redis = new RedisModel();
foreach ($suppliers as $supplier) {
if ($redis->hget('supplier_data_service', $supplier['supplier_id'])) {
continue;
}
var_dump($supplier['supplier_id']);
//先判断是否有sku
$url = env('ES_SKU_URL', '');
$map['canal/condition'] = $supplier['supplier_code'];
$return = curl($url, $map, 1);
$data = json_decode($return, true);
if (!empty($data['data']['total'])) {
$total = $data['data']['total'];
} else {
$redis->hset('supplier_data_service', $supplier['supplier_id'], '1');
continue;
}
$service = new AdminUserService();
$intracodeModel = new IntracodeModel();
$userCodes = $intracodeModel->getSampleEncode();
$channelUids = explode(',', $supplier['channel_uid']);
$channelUids = array_filter($channelUids, function ($codeId) {
return !empty($codeId);
});
$channelNames = array_map(function ($codeId) use ($userCodes) {
return array_get($userCodes, $codeId, '');
}, $channelUids);
$channelNames = implode('|', $channelNames);
$userInfo = $service->getAdminUserInfoByCodeId($supplier['purchase_uid']);
$purchaseName = $userInfo['name'] ?: '';
$redis->hset('supplier_data_service', $supplier['supplier_id'], json_encode([
$supplier['supplier_code'],
'"' . $supplier['supplier_name'] . '"',
$channelNames,
$purchaseName,
!empty($supplier['create_time']) ? '"' . date('Y-m-d H:i:s', $supplier['create_time']) . '"' : '',
$total
]));
}
}
function export()
{
$redis = new RedisModel();
$exportData = $redis->hgetall('supplier_data_service');
$exportData = array_filter($exportData, function ($data) {
return $data != 1;
});
$exportData = array_map(function ($data) {
return json_decode($data);
}, $exportData);
// 文件名,这里都要将utf-8编码转为gbk,要不可能出现乱码现象
$filename = $this->utfToGbk('导出csv文件.csv');
// 拼接文件信息,这里注意两点
// 1、字段与字段之间用逗号分隔开
// 2、行与行之间需要换行符
$fileData = $this->utfToGbk('供应商编码,供应商名称,采购员名称,渠道开发员,创建时间,sku数量') . "\n";
foreach ($exportData as $value) {
$temp = $value[0] . ',' . $value[1] . ',' . $value[2] . ',' . $value[3] . ',' . $value[4] . ',' . $value[5];
$fileData .= $this->utfToGbk($temp) . "\n";
}
// 头信息设置
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=" . $filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $fileData;
exit;
}
function utfToGbk($data)
{
return iconv('utf-8', 'GBK', $data);
}
}
\ No newline at end of file
......@@ -44,7 +44,8 @@ Route::group(['middleware' => ['web'], 'namespace' => 'Api'], function () {
});
Route::match(['get', 'post'], '/test', function () {
$service = new \App\Http\Services\DataService();
$service->test();
$service->exportSuppliers();
// $service->export();
// $service->repairData(10);
// $service->completeContact(10);
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment