shop-php/crmeb/jobs/ImportProductXlsJob.php
2024-01-26 09:39:36 +08:00

65 lines
2.1 KiB
PHP
Executable File

<?php
namespace crmeb\jobs;
use crmeb\interfaces\JobInterface;
use think\facade\Log;
use app\common\repositories\store\order\StoreOrderRepository;
use think\facade\Db;
use crmeb\services\SpreadsheetExcelService;
use think\facade\Queue;
class ImportProductXlsJob implements JobInterface
{
public function fire($job, $data)
{
Log::info('开始读取excel商品:' . $job->attempts());
if ($job->attempts() > 1) {
$job->delete();
$this->failed($data);
}
$arrary = $data['data'];
//读取excel
$excel_data = SpreadsheetExcelService::instance()->_import($arrary['path'], $arrary['sql'], $arrary['where'], 1);
if (!$excel_data) return false;
unset($excel_data[0]);
$product_type = 0;
if ($data['type_id'] == 12) {
$product_type = 98; //供应链
}
try {
foreach ($excel_data as $datum) {
Queue::push(ImportProductJob::class,['data'=>$datum,'mer_id'=>$data['mer_id'],'product_type'=>$product_type]);
// $StoreOrderRepository->setProduct();
}
} catch (\Exception $e) {
$job->delete();
$datas['product_id'] = $product_id ?? 0;
$datas['mer_id'] = $data['mer_id'];
$datas['store_name'] = $datum['value']['store_name'];
$datas['content'] = $e->getMessage() . 'line:' . $e->getLine();
$this->create_product_import_log($datas, 0);
}
}
public function failed($data)
{
Log::error('读取excel失败:' . json_encode($data));
// TODO: Implement failed() method.
}
public function create_product_import_log($data, $status = 1)
{
$data = [
'product_id' => $data['product_id'] ?? 0,
'mer_id' => $data['mer_id'],
'name' => $data['store_name'],
'content' => $data['content'] ?? '',
'status' => $status,
'create_time' => date('Y-m-d H:i:s'),
];
Db::name('store_product_import')->insert($data);
}
}