|
...
|
...
|
@@ -2,21 +2,17 @@ |
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Http\Logic\Aside\Project\ProjectLogic;
|
|
|
|
use App\Models\Com\NoticeLog;
|
|
|
|
use App\Models\Product\Keyword;
|
|
|
|
use App\Models\Project\Project;
|
|
|
|
use App\Models\RouteMap\RouteMap;
|
|
|
|
use App\Services\ProjectServer;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 初始化项目
|
|
|
|
* Class InitProject
|
|
|
|
* Class InitKeyword
|
|
|
|
* @package App\Console\Commands
|
|
|
|
* @author zbj
|
|
|
|
* @date 2023/10/8
|
|
|
|
*/
|
|
|
|
class InitKeyword extends Command
|
|
|
|
{
|
|
...
|
...
|
@@ -50,29 +46,82 @@ class InitKeyword extends Command |
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
while (true){
|
|
|
|
$list = NoticeLog::where('type', NoticeLog::TYPE_INIT_KEYWORD)->where('status', NoticeLog::STATUS_PENDING)->get();
|
|
|
|
if(!empty($list)){
|
|
|
|
foreach ($list as $item){
|
|
|
|
echo 'start:' . $item['data']['project_id'] . PHP_EOL;
|
|
|
|
ProjectServer::useProject($item['data']['project_id']);
|
|
|
|
$keywordModel = new Keyword();
|
|
|
|
$list = $keywordModel->list(['route'=>'']);
|
|
|
|
echo 'start:' . json_encode($list) . PHP_EOL;
|
|
|
|
foreach ($list as $v){
|
|
|
|
$route = RouteMap::setRoute($v['title'],RouteMap::SOURCE_PRODUCT_KEYWORD,$v['id'],$item['data']['project_id']);
|
|
|
|
if(empty($route)){
|
|
|
|
$keywordModel->del(['id'=>$v['id']]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$keywordModel->edit(['route'=>$route],['id'=>$v['id']]);
|
|
|
|
}
|
|
|
|
$item->status = NoticeLog::STATUS_SUCCESS;
|
|
|
|
$item->save();
|
|
|
|
DB::disconnect('custom_mysql');
|
|
|
|
}
|
|
|
|
$notice_id = $this->getTask();
|
|
|
|
if (empty($notice_id)) {
|
|
|
|
sleep(30);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$this->output(' taskID: ' . $notice_id . ' start');
|
|
|
|
$this->bind($notice_id);
|
|
|
|
$this->output(' taskID: ' . $notice_id . ' end');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->output(' taskID: ' . $notice_id . ', error: ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
sleep(2);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 处理子任务
|
|
|
|
* @param $notice_id
|
|
|
|
* @return bool
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function bind($notice_id)
|
|
|
|
{
|
|
|
|
$notice = NoticeLog::where(['id' => $notice_id])->first();
|
|
|
|
if (empty($notice) || $notice->type != NoticeLog::TYPE_INIT_KEYWORD || $notice->status != NoticeLog::STATUS_PENDING)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
ProjectServer::useProject($notice['data']['project_id']);
|
|
|
|
|
|
|
|
$keyword = Keyword::whereNull('route')->get();
|
|
|
|
foreach ($keyword as $val) {
|
|
|
|
$this->output(' keywordID: ' . $val->id . ', title: ' . $val->title);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$route = RouteMap::setRoute($val['title'],RouteMap::SOURCE_PRODUCT_KEYWORD, $val->id, $notice['data']['project_id']);
|
|
|
|
$val->route = $route;
|
|
|
|
$val->save();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->output(' keywordID: ' . $val->id . ', title: ' . $val->title . ', error: ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$notice->status = NoticeLog::STATUS_SUCCESS;
|
|
|
|
$notice->save();
|
|
|
|
|
|
|
|
DB::disconnect('custom_mysql');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取需要处理的任务
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getTask()
|
|
|
|
{
|
|
|
|
$key = 'notice_log_type_keyword';
|
|
|
|
$notice_id = Redis::rpop($key);
|
|
|
|
if ($notice_id)
|
|
|
|
return $notice_id;
|
|
|
|
$ids = NoticeLog::where('type', NoticeLog::TYPE_INIT_KEYWORD)->where('status', NoticeLog::STATUS_PENDING)->limit(100)->pluck('id');
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
Redis::lpush($key, $id);
|
|
|
|
}
|
|
|
|
$notice_id = Redis::rpop($key);
|
|
|
|
return $notice_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 输出message
|
|
|
|
* @param $message
|
|
|
|
*/
|
|
|
|
public function output($message)
|
|
|
|
{
|
|
|
|
echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|