thinkphp 命令行执行导入

<?php
declare (strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;

class Hello extends Command
{
protected function configure()
{
// 指令配置
$this->setName('hello')
->setDescription('测试');
}

protected function execute(Input $input, Output $output)
{
// 指令输出
$output->writeln('执行中');
$output->writeln(sprintf("已处理 %d 条数据", 10));
exit;
// 设置永不退出
set_time_limit(0);

$cursor = Db::connect('kuaidian')
->table('wx_member')
->where('mobile', '>', 0)
->field("id,mobile,nickname,status,addtime,last_time,balance")
->cursor();
$count = 0;
$maxInserNum = 0;
$insertAll = [];
foreach ($cursor as $v) {
$count++;
$userTab = userTab($v['mobile']);
$user = Db::name($userTab)
->where("mobile", $v['mobile'])
->field("uid")
->find();

if (!isset($user['uid'])) {
$maxInserNum++;
//执行插入
$userData = [];
$userData["uid"] = creatId($v['mobile'] . rand(1, 9999999));
$userData["name"] = $v['nickname'];
$userData["mobile"] = $v['mobile'];
$userData["reg_time"] = date("Y-m-d H:i:s", $v['addtime']);
$userData["last_time"] = date("Y-m-d H:i:s", $v['last_time']);
$userData["avatar"] = "dinggedan-avatar/avatar.png";
$userData["status"] = $v['status'];
$userData["sex"] = 1;
$userData["zxtime"] = "";
$userData["birthday"] = "";
$userData["balance_xiche"] = $v['balance'];
if (!key_exists($userTab, $insertAll)) {
$insertAll[$userTab] = [];
}
array_push($insertAll[$userTab], $userData);
} else {
//执行更新
Db::name($userTab)->where("uid", $user['uid'])->update(['balance_xiche' => $v['balance']]);
}

// 每次100数据插入一次
if ($maxInserNum >= 100) {
foreach ($insertAll as $tab => $datas) {
Db::name($tab)->insertAll($datas);
}
$insertAll = [];
$maxInserNum = 0;
}
$output->writeln(sprintf("已处理 %d 条数据", $count));
}
$output->writeln('执行完毕!');
}
}
posted @ 2023-02-22 15:36  zlf2000  阅读(25)  评论(0编辑  收藏  举报