...
|
...
|
@@ -2,24 +2,18 @@ |
|
|
|
|
|
namespace app\admin\controller\litestore;
|
|
|
|
|
|
use addons\fastexport\library\ExportLib;
|
|
|
use app\admin\model\litestore\Litestoregoodsspec;
|
|
|
use app\api\model\Goods;
|
|
|
use app\common\controller\Backend;
|
|
|
use app\admin\model\litestore\Litestorespec as SpecModel;
|
|
|
use app\admin\model\litestore\Litestorespecvalue as SpecValueModel;
|
|
|
use PHPExcel;
|
|
|
use PHPExcel_Style;
|
|
|
use PHPExcel_Style_Alignment;
|
|
|
use PHPExcel_Style_Border;
|
|
|
use PHPExcel_Style_Fill;
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xls;
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
|
|
use think\Db;
|
|
|
use think\Exception;
|
|
|
use think\exception\PDOException;
|
|
|
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
*
|
...
|
...
|
@@ -34,6 +28,7 @@ class Litestoregoods extends Backend |
|
|
* @var \app\admin\model\litestore\Litestoregoods
|
|
|
*/
|
|
|
protected $model = null;
|
|
|
protected $spec_model = null;
|
|
|
|
|
|
public function _initialize()
|
|
|
{
|
...
|
...
|
@@ -41,7 +36,8 @@ class Litestoregoods extends Backend |
|
|
$this->SpecModel = new SpecModel;
|
|
|
$this->SpecValueModel = new SpecValueModel;
|
|
|
|
|
|
$this->model = new \app\admin\model\litestore\Litestoregoods;
|
|
|
$this->model = new \app\admin\model\litestore\Litestoregoods;
|
|
|
$this->spec_model = new \app\admin\model\litestore\Litestoregoodsspec();
|
|
|
$this->view->assign("specTypeList", $this->model->getSpecTypeList());
|
|
|
$this->view->assign("deductStockTypeList", $this->model->getDeductStockTypeList());
|
|
|
$this->view->assign("goodsStatusList", $this->model->getGoodsStatusList());
|
...
|
...
|
@@ -292,6 +288,7 @@ class Litestoregoods extends Backend |
|
|
->select();
|
|
|
$list = collection($list)->toArray();
|
|
|
foreach ($list as &$item) {
|
|
|
$item['category_name'] = Db::name('litestore_category')->where('id', $item['category_id'])->value('name');
|
|
|
if (!empty($item['spec_sku_id'])) {
|
|
|
$sku_value = explode('_', $item['spec_sku_id']);
|
|
|
if (count($sku_value) > 1) {
|
...
|
...
|
@@ -320,35 +317,57 @@ class Litestoregoods extends Backend |
|
|
$objPHPExcel->setActiveSheetIndex(0)
|
|
|
->setCellValue('A1', '商品ID')
|
|
|
->setCellValue('B1', '商品名称')
|
|
|
->setCellValue('C1', '商品规格')
|
|
|
->setCellValue('D1', '商品原价')
|
|
|
->setCellValue('E1', '折扣')
|
|
|
->setCellValue('F1', '商品现价')
|
|
|
->setCellValue('G1', '已销数量')
|
|
|
->setCellValue('H1', '导出时间');
|
|
|
->setCellValue('C1', '搜索关键词')
|
|
|
->setCellValue('D1', '分类名称')
|
|
|
->setCellValue('E1', '活动名称(新人用户=1,限时秒杀=2,进口商品=4)')
|
|
|
->setCellValue('F1', '商品规格(10=单规格,20=多规格)')
|
|
|
->setCellValue('G1', '库存')
|
|
|
->setCellValue('H1', '商品规格')
|
|
|
->setCellValue('I1', '商品原价')
|
|
|
->setCellValue('J1', '折扣')
|
|
|
->setCellValue('K1', '商品现价')
|
|
|
->setCellValue('L1', '已销数量')
|
|
|
->setCellValue('M1', '商品状态(10=上架,20=下架)')
|
|
|
->setCellValue('N1', '首页展示(0=未展示,1=已展示)')
|
|
|
->setCellValue('O1', '导出时间');
|
|
|
//设置A列水平居中
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
|
|
|
->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
|
|
|
//设置单元格宽度
|
|
|
//6.循环刚取出来的数组,将数据逐一添加到excel表格。
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(10);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('L')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('M')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('N')->setWidth(20);
|
|
|
$objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('O')->setWidth(20);
|
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($list); $i++) {
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['goods_id']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['goods_name']);//商品名称
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['sku_value']);//商品规格
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['line_price']);//原价
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['discount']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['goods_price']);//标签码
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $list[$i]['goods_sales']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), datetime(time()));//标签码
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['keywords']);//商品规格
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['category_name']);//原价
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['activity_id']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['spec_type']);//标签码
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $list[$i]['stock_num']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $list[$i]['sku_value']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $list[$i]['line_price']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $list[$i]['discount']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('K' . ($i + 2), $list[$i]['goods_price']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('L' . ($i + 2), $list[$i]['goods_sales']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('M' . ($i + 2), $list[$i]['goods_status']);//ID
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('N' . ($i + 2), $list[$i]['is_index']);//标签码
|
|
|
$objPHPExcel->getActiveSheet()->setCellValue('O' . ($i + 2), datetime(time()));//标签码
|
|
|
}
|
|
|
ob_end_clean();
|
|
|
ob_start();
|
...
|
...
|
@@ -366,6 +385,102 @@ class Litestoregoods extends Backend |
|
|
exit();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*导入
|
|
|
*
|
|
|
* */
|
|
|
public function import()
|
|
|
{
|
|
|
$file = $this->request->request('file');
|
|
|
if (!$file) {
|
|
|
$this->error(__('Parameter %s can not be empty', 'file'));
|
|
|
}
|
|
|
$filePath = ROOT_PATH . DS . 'public' . DS . $file;
|
|
|
if (!is_file($filePath)) {
|
|
|
$this->error(__('No results were found'));
|
|
|
}
|
|
|
//实例化reader
|
|
|
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
|
|
|
if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
|
|
|
$this->error(__('Unknown data format'));
|
|
|
}
|
|
|
if ($ext === 'csv') {
|
|
|
$file = fopen($filePath, 'r');
|
|
|
$filePath = tempnam(sys_get_temp_dir(), 'import_csv');
|
|
|
$fp = fopen($filePath, "w");
|
|
|
$n = 0;
|
|
|
while ($line = fgets($file)) {
|
|
|
$line = rtrim($line, "\n\r\0");
|
|
|
$encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
|
|
|
if ($encoding != 'utf-8') {
|
|
|
$line = mb_convert_encoding($line, 'utf-8', $encoding);
|
|
|
}
|
|
|
if ($n == 0 || preg_match('/^".*"$/', $line)) {
|
|
|
fwrite($fp, $line . "\n");
|
|
|
} else {
|
|
|
fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
|
|
|
}
|
|
|
$n++;
|
|
|
}
|
|
|
fclose($file) || fclose($fp);
|
|
|
$reader = new Csv();
|
|
|
} elseif ($ext === 'xls') {
|
|
|
$reader = new Xls();
|
|
|
} else {
|
|
|
$reader = new Xlsx();
|
|
|
}
|
|
|
set_time_limit(0);
|
|
|
//加载文件
|
|
|
$insert = [];
|
|
|
try {
|
|
|
if (!$PHPExcel = $reader->load($filePath)) {
|
|
|
$this->error(__('Unknown data format'));
|
|
|
}
|
|
|
$currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
|
|
|
$allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
|
|
|
$allRow = $currentSheet->getHighestRow(); //取得一共有多少行
|
|
|
$maxColumnNumber = Coordinate::columnIndexFromString($allColumn); // 一共多少列
|
|
|
for ($i = 2; $i <= $allRow; $i++) {
|
|
|
if (!empty($PHPExcel->getActiveSheet()->getCell("A" . $i)->getValue())) {
|
|
|
$insert[$i]['goods_name'] = $PHPExcel->getActiveSheet()->getCell("B" . $i)->getValue();//商品名称
|
|
|
$insert[$i]['keywords'] = $PHPExcel->getActiveSheet()->getCell("C" . $i)->getValue();//关键词
|
|
|
$category = $PHPExcel->getActiveSheet()->getCell("D" . $i)->getValue() ?? '';//类别
|
|
|
$insert[$i]['category_id'] = Db::name('litestore_category')->where('name', $category)->value('id') ?? '';
|
|
|
$insert[$i]['activity_id'] = $PHPExcel->getActiveSheet()->getCell("E" . $i)->getValue() ?? '';//活动类型
|
|
|
$insert[$i]['spec_type'] = $PHPExcel->getActiveSheet()->getCell("F" . $i)->getValue() ?? '';//规格类型
|
|
|
$insert[$i]['is_index'] = $PHPExcel->getActiveSheet()->getCell("N" . $i)->getValue() ?? '';//是否首页
|
|
|
$insert[$i]['goods_status'] = $PHPExcel->getActiveSheet()->getCell("M" . $i)->getValue() ?? '';//商品状态
|
|
|
$spec_insert[$i]['stock_num'] = $PHPExcel->getActiveSheet()->getCell("G" . $i)->getValue() ?? '';//库存
|
|
|
$spec_insert[$i]['goods_price'] = $PHPExcel->getActiveSheet()->getCell("K" . $i)->getValue() ?? '';//现价
|
|
|
$spec_insert[$i]['line_price'] = $PHPExcel->getActiveSheet()->getCell("I" . $i)->getValue() ?? '';//原价
|
|
|
}
|
|
|
}
|
|
|
} catch (\Exception $exception) {
|
|
|
$this->error($exception->getMessage());
|
|
|
}
|
|
|
if (!$insert) {
|
|
|
$this->error(__('No rows were updated'));
|
|
|
}
|
|
|
try {
|
|
|
|
|
|
$res_goods = $this->model->saveAll($insert);
|
|
|
halt($res_goods);
|
|
|
foreach ($spec_insert as $key => &$v) {
|
|
|
$v['goods_id'] = $res_goods[$key]['id'];
|
|
|
}
|
|
|
unset($v);
|
|
|
$this->spec_model->saveAll($spec_insert);
|
|
|
} catch (PDOException $exception) {
|
|
|
$msg = $exception->getMessage();
|
|
|
if (preg_match("/.+Integrity constraint violation: 1062 Duplicate entry '(.+)' for key '(.+)'/is", $msg, $matches)) {
|
|
|
$msg = "导入失败,包含【{$matches[1]}】的记录已存在";
|
|
|
};
|
|
|
$this->error($msg);
|
|
|
} catch (\Exception $e) {
|
|
|
$this->error($e->getMessage());
|
|
|
}
|
|
|
$this->success();
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|