Commit 0447cbcf by 杨树贤

fix

parent 001dbadf
<?php
namespace App\Console\Commands;
use App\Http\Services\SyncSupplierService;
use Illuminate\Console\Command;
use App\Model\SupplierChannelModel;
// 同步供应商到ERP
class SyncSupplierToErp extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync:supplier_to_erp {--num=0 : 处理数量限制(0=不限制)}';
/**
* The console command description.
*
* @var string
*/
protected $description = '同步供应商到ERP';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$num = (int)$this->option('num');
ini_set('memory_limit', -1);
set_time_limit(0);
ignore_user_abort(true);
ob_implicit_flush(true);
$this->info('========================================');
$this->info('开始同步供应商到ERP');
$this->info('处理数量: ' . ($num > 0 ? $num : '不限制'));
$this->info('========================================');
// 构建查询:yunxin_channel_uid不为空且不为0
$query = SupplierChannelModel::where('is_type', 0)
->where('yunxin_channel_uid', '!=', '');
if ($num > 0) {
$query->limit($num);
}
$suppliers = $query->get();
$total = count($suppliers);
$this->info("共找到 {$total} 个供应商需要同步");
$syncService = new SyncSupplierService();
$success = 0;
$failed = 0;
$count = 0;
foreach ($suppliers as $supplier) {
$count++;
$supplierId = $supplier->supplier_id;
// 每100条重连数据库
if ($count > 1 && $count % 100 == 0) {
\DB::reconnect('web');
}
echo "[{$count}/{$total}] ID:{$supplierId} {$supplier->supplier_name} ... ";
try {
$result = $syncService->syncSupplierToErp($supplierId);
if ($result) {
$success++;
echo "成功\n";
} else {
$failed++;
echo "失败\n";
}
} catch (\Exception $e) {
$failed++;
echo "异常: " . $e->getMessage() . "\n";
}
}
$this->info('');
$this->info('========================================');
$this->info('处理完成');
$this->info('总计: ' . $total);
$this->info('成功: ' . $success);
$this->info('失败: ' . $failed);
$this->info('========================================');
return 0;
}
}
......@@ -2,6 +2,7 @@
namespace App\Console;
use App\Console\Commands\SyncSupplierToErp;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
......@@ -20,6 +21,7 @@ class Kernel extends ConsoleKernel
Commands\SyncAllSupplierToErp::class,
Commands\SupplierRecallMailRemind::class,
Commands\RefreshHistoryPurchaseUser::class,
Commands\SyncSupplierToErp::class,
];
/**
......
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