<?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
{
    /**
     * 压缩和解压类对象
     * @var
     */
    public $dealZip;

    /**
     * 商品导入
     * @param $request
     * @return array
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     */
    public function import($request)
    {
        $isCover = $request->input('isCover'); //是否覆盖
        $storeId = $request->input('storeId'); //门店ID
        $basePath = dirname(dirname(app_path()));
        $memberId = session('memberId');
        $path = $basePath . '/Public/Upload/Business/Goods/' . $memberId . '/upload/';
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }
        /*******上传zip文件********/
        $dealZip = new DealZip();
        $this->dealZip = $dealZip;
        $uploadFile = $dealZip->uploadFile($request, $path);
        if ($uploadFile['status'] == 0) {
            $this->dealZip->clearDirAndFiles($path);
            return $uploadFile;
        }
        /*******解压zip文件********/
        $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'];
        /******创建excel表和图片文件夹并复制图片********/
        $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('压缩包未知错误');
        }
    }

}

results matching ""

    No results matching ""