<?php
namespace App\Module\Goods;
use App\Http\Controllers\Wap\AjaxController;
use Barryvdh\Debugbar\Twig\Extension\Dump;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;
class GoodsData
{
public $dealZip;
public function import($request)
{
$isCover = $request->input('isCover');
$storeId = $request->input('storeId');
$basePath = dirname(dirname(app_path()));
$memberId = session('memberId');
$path = $basePath . '/Public/Upload/Business/Goods/' . $memberId . '/upload/';
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
$dealZip = new DealZip();
$this->dealZip = $dealZip;
$uploadFile = $dealZip->uploadFile($request, $path);
if ($uploadFile['status'] == 0) {
$this->dealZip->clearDirAndFiles($path);
return $uploadFile;
}
$filename = $path . $this->dealZip->uploadName . '/upload.zip';
$extract = $this->dealZip->extractZipToFile($filename, $path . $this->dealZip->uploadName);
if (!$extract) {
$this->dealZip->clearDirAndFiles($path);
return ['status' => 0, 'msg' => '解压失败'];
}
$archivePath = $path . $this->dealZip->uploadName . '/extract';
$table = 'woo_goods';
$verifyFile = $this->dealZip->verifyFile($archivePath, $table);
if ($verifyFile['status'] == 0) {
$this->dealZip->clearDirAndFiles($path);
return $verifyFile;
};
$data = $verifyFile['data'];
$data['isCover'] = $isCover;
$data['storeId'] = $storeId;
$data['oldPicPath'] = $basePath . "/Public/Upload/Goods/Goodsku/{$storeId}/";
$data['picPath'] = $path . $this->dealZip->uploadName . '/extract/pic/';
$importData = $this->importDataToDatabase($data);
if ($importData['status'] == 0) {
$this->dealZip->clearDirAndFiles($path);
return $importData;
}
$this->dealZip->clearDirAndFiles($path);
return ['status' => 1, 'msg' => '导入成功'];
}
public function export($request)
{
$postData = $request->input();
if (!empty($postData['ids'])) {
$postData['ids'] = explode(',', $postData['ids']);
} else {
$postData['ids'] = [];
}
$basePath = dirname(dirname(app_path()));
$memberId = session('memberId');
$path = $basePath . '/Public/Upload/Business/Goods/' . $memberId . '/export/';
$dealZip = new DealZip();
$this->dealZip = $dealZip;
$this->dealZip->clearDirAndFiles($path);
$this->dealZip->exportName = time() . rand(1, 10000);
$data = $this->getGoods($postData);
if ($data['status'] == 0) {
return errors($data['msg']);
}
$data = $data['data'];
$data['path'] = $path . $this->dealZip->exportName . '/build/';
$data['picPath'] = $basePath . "/Public/Upload/Goods/Goodsku/{$postData['storeId']}/";
$heads = ['商品条码', '商品名称', '商品分类', '库存单位', '参与优惠', '单规格', '建议零售价', '规格条码'];
$heads = array_pad($heads, $data['max'], '规格');
$data['heads'] = $heads;
$buildFiles = $this->dealZip->buildFiles($data);
if ($buildFiles['status'] == 0) {
return errors($buildFiles['msg']);
}
$this->dealZip->zipFiles($path . $this->dealZip->exportName);
$filename = '基础商品库压缩包.zip';
$file = $path . $this->dealZip->exportName . '/goods.zip';
if (file_exists($file)) {
header('Content-Description: File download');
header('Content-Disposition: attachment; filename=' . $filename);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
@readfile($file);
exit;
} else {
return errors('压缩包未知错误');
}
}
}