You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
1.6 KiB
30 lines
1.6 KiB
<?php
|
|
namespace App\Console\Commands;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Models\Dialogue;
|
|
use App\Models\Message;
|
|
use App\Services\SupplyDemand\Conversations;
|
|
class SupplyDemandConversations extends Command {
|
|
protected $signature='supply-demand:normalize-conversations {--apply : 写入历史会话关联,否则仅预览}';
|
|
protected $description='按需求和双方身份整理已有消息,不猜测缺失的供需关联';
|
|
public function handle() {
|
|
$count=0;
|
|
Message::orderBy('id')->chunkById(200,function($rows) use(&$count) {
|
|
foreach($rows as $m) {
|
|
$key=Conversations::key($m->supply_demand_id,$m->user_id,$m->to_user_id);
|
|
$old=Dialogue::find($m->dialogue_id);
|
|
if($old && $old->conversation_key===$key) continue;
|
|
$count++;
|
|
if(!$this->option('apply')) continue;
|
|
DB::transaction(function() use($m,$key) {
|
|
$c=Dialogue::firstOrCreate(['conversation_key'=>$key],['user_id'=>$m->user_id,'to_user_id'=>$m->to_user_id,'supply_demand_id'=>$m->supply_demand_id?:null]);
|
|
$m->dialogue_id=$c->id; $m->save();
|
|
if(!$c->last_datetime || (string)$m->created_at >= $c->last_datetime) { $c->last_content=$m->content;$c->last_datetime=$m->created_at->format('Y-m-d H:i:s');$c->save(); }
|
|
});
|
|
}
|
|
});
|
|
$this->info(($this->option('apply')?'已整理':'待整理').'消息 '.$count.' 条。缺失供需ID的历史消息保持普通对话。'); return 0;
|
|
}
|
|
}
|