diff --git a/app/Console/Commands/HandleNotice.php b/app/Console/Commands/HandleNotice.php index 76b37b6..a992d3d 100644 --- a/app/Console/Commands/HandleNotice.php +++ b/app/Console/Commands/HandleNotice.php @@ -2,6 +2,8 @@ namespace App\Console\Commands; +use App\Models\Notice; +use GuzzleHttp\Client; use Illuminate\Console\Command; use Illuminate\Contracts\Console\Isolatable; @@ -26,9 +28,9 @@ class HandleNotice extends Command implements Isolatable * * @return void */ - public function __construct() + public function __construct(public Client $client) { - parent::__construct(); + parent::__construct();; } /** @@ -39,6 +41,44 @@ public function __construct() public function handle() { $this->line('开始消息队列'); + $url = env('NOTICE_URL'); + $res = json_decode($this->client->post($url)->getBody()->getContents(), true); + if (!$res['result'] || $res['errCode'] != '0000') { + $this->error('request err: ' . $res['desc'] ?? ''); + return 1; + } + $arr = $res['data']['notice_list'] ?? []; + while ($items = array_splice($arr, 0, 1)) { + $ids = array_column($items, 'notice_id'); + $data = array_column($items, null, 'notice_id'); + if (!$ids) { + continue; + } + $hadIds = Notice::query()->select('id')->whereIn('id', $ids)->get()->toArray(); + + $hadIds = array_column($hadIds, 'id'); + $newIds = array_diff($ids, $hadIds); + $notices = []; + $time = date('Y-m-d H:i:s'); + foreach ($newIds as $id) { + $notices[] = [ + 'notice_id' => $id, + 'raw_content' => json_encode($data[$id]), + 'created_at' => $time, + ]; + } + try{ + Notice::insert($notices); + }catch (\Exception $exception){ + $this->error('insert err:'. $exception->getMessage()); + } + //todo 入队 + foreach ($newIds as $id){ + \App\Jobs\notice::dispatch($data[$id]); + } + + + } return 0; } } diff --git a/app/Models/Good.php b/app/Models/Good.php index b022ff4..a0a6e20 100644 --- a/app/Models/Good.php +++ b/app/Models/Good.php @@ -4,8 +4,16 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasOne; +/** + * @property GoodItem $goodItem + */ class Good extends Model { use HasFactory; + + public function GoodItem() :HasOne { + return $this->hasOne(GoodItem::class,'item_id'); + } } diff --git a/app/Models/GoodItem.php b/app/Models/GoodItem.php index 27451e5..a5418a8 100644 --- a/app/Models/GoodItem.php +++ b/app/Models/GoodItem.php @@ -4,8 +4,17 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; + +/** + * @property Good $good + */ class GoodItem extends Model { use HasFactory; + + public function Good () : BelongsTo{ + return $this->belongsTo(Good::class,'item_id'); + } } diff --git a/app/Models/Notice.php b/app/Models/Notice.php index 501cb3d..e3ff934 100644 --- a/app/Models/Notice.php +++ b/app/Models/Notice.php @@ -8,4 +8,6 @@ class Notice extends Model { use HasFactory; + + public $timestamps = true; }