您现在的位置是:首页 >技术杂谈 >lavarel console command批处理文件网站首页技术杂谈

lavarel console command批处理文件

weixin_41865469 2024-06-17 10:19:17
简介lavarel console command批处理文件

一 面板执行命令
php artisan XX
比如 php artisan command:batch_register_shop

<?php

namespace AppConsoleCommands;

use AppLibrariesError;
use AppLibrariesMysql;
use AppLibrariesShop;
use AppModelsWholesaleCmSupplierTemp;
use AppServicesGoodsCopyGoodsService;
use IlluminateConsoleCommand;
use MonologHandlerRotatingFileHandler;
use MonologLogger;
use AppModelsMerchantsCategory;
use AppModelsGoods;
use AppRepositoriesCommonBaseRepository;
class BatchRegisterShop extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:batch_register_shop';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '批量( 复制商品信息等)';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        
        echo '开始:' . date('Y-m-d H:i:s');
       
       
        $cm_srv = new CopyGoodsService();
        Goods::where('user_id', 179)->whereNotIn('goods_id',$arlist)->chunkById(10, function ($goods) use ($cm_srv) {
            foreach ($goods as $good) {
                $good = $good->toArray();
                $res = $cm_srv->importData($good['goods_id'],60);
            }
        });
       
        echo '结束:' . date('Y-m-d H:i:s');
        return ;
    }
  
}

二 分块处理数据

Goods::where( w h e r e ) − > c h u n k B y I d ( 10 , f u n c t i o n ( where)->chunkById(10, function ( where)>chunkById(10,function(goods) use (KaTeX parse error: Expected '}', got 'EOF' at end of input: … foreach (goods as $good) {
$good = $good->toArray();
$res = c m s r v − > i m p o r t D a t a ( cm_srv->importData( cmsrv>importData(good[‘goods_id’],60);
}
});
分块每次处理10条 整体处理完两千多商品的数据导入 大概用时 18分钟

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。