add type 1 3 4 5 logic

This commit is contained in:
xing 2024-05-26 22:06:21 +08:00
parent 657663c9d0
commit 1ba4a79ee3
14 changed files with 181 additions and 159 deletions

View File

@ -3,6 +3,7 @@ APP_ENV=local
APP_KEY= APP_KEY=
APP_DEBUG=true APP_DEBUG=true
APP_URL=http://localhost APP_URL=http://localhost
API_TOKEN=
NOTICE_URL= NOTICE_URL=
GOODS_URL= GOODS_URL=
LOG_CHANNEL=stack LOG_CHANNEL=stack

View File

@ -15,7 +15,7 @@ class HandleNotice extends Command implements Isolatable
* *
* @var string * @var string
*/ */
protected $signature = 'handle:notice'; protected $signature = 'handle:notice {chunk=1000 : 分片}';
/** /**
* The console command description. * The console command description.
@ -42,15 +42,19 @@ public function __construct()
*/ */
public function handle(): int public function handle(): int
{ {
$chunk = $this->argument('chunk');
$this->line('开始消息队列'); $this->line('开始消息队列');
$url = env('NOTICE_URL'); $url = env('NOTICE_URL');
$res = apiRequest::request($url); $res = apiRequest::request($url, [
'token' => env('API_TOKEN'),
]);
if (!$res['result'] || $res['errCode'] != '0000') { if (!$res['result'] || $res['errCode'] != '0000') {
$this->error('request err: ' . $res['desc'] ?? ''); $this->error('request err: ' . $res['desc'] ?? '');
return 1; return 1;
} }
$arr = $res['data']['notice_list'] ?? []; $arr = $res['data']['notice_list'] ?? [];
while ($items = array_splice($arr, 0, 500)) { $typeIds = array_flip(range(1, 5));
while ($items = array_splice($arr, 0, $chunk)) {
$ids = array_column($items, 'notice_id'); $ids = array_column($items, 'notice_id');
if (!$ids) { if (!$ids) {
continue; continue;
@ -74,8 +78,11 @@ public function handle(): int
'raw_content' => json_encode($data[$id]), 'raw_content' => json_encode($data[$id]),
'created_at' => $time, 'created_at' => $time,
]; ];
if (isset($typeIds[$data[$id]['type']])) {
$queue[$data[$id]['type']][] = $data[$id]; $queue[$data[$id]['type']][] = $data[$id];
} }
}
try { try {
Notice::insert($notices); Notice::insert($notices);
} catch (\Exception $exception) { } catch (\Exception $exception) {
@ -85,8 +92,6 @@ public function handle(): int
foreach ($queue as $type => $item) { foreach ($queue as $type => $item) {
\App\Jobs\notice::dispatch($type, $item); \App\Jobs\notice::dispatch($type, $item);
} }
} }
return 0; return 0;
} }

View File

@ -5,6 +5,8 @@
use App\helpers\apiRequest; use App\helpers\apiRequest;
use App\helpers\FormType; use App\helpers\FormType;
use App\Models\Good; use App\Models\Good;
use App\Models\GoodsItem;
use Exception;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
@ -12,12 +14,15 @@
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Throwable;
class notice implements ShouldQueue class notice implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public Client $client; public Client $client;
/** /**
* Create a new job instance. * Create a new job instance.
*/ */
@ -31,6 +36,7 @@ public function __construct(public int $type,public mixed $data)
/** /**
* Execute the job. * Execute the job.
* @throws GuzzleException * @throws GuzzleException
* @throws Throwable
*/ */
public function handle(): void public function handle(): void
{ {
@ -39,34 +45,111 @@ public function handle(): void
$this->addGoods(); $this->addGoods();
break; break;
case 2: case 2:
case 3: case 3:
$this->changeProductState();
break;
case 4: case 4:
case 5: case 5:
$this->changePrice();
break;
} }
} }
/** /**
* @throws GuzzleException * @throws GuzzleException
* @throws \Exception * @throws Exception
* @throws Throwable
*/ */
public function addGoods(): void public function addGoods(): void
{ {
$goods = []; $goods = $goodsItems = $noticeIds = [];
$this->client = new Client(); $this->client = new Client();
$apiToken = env('API_TOKEN');
foreach ($this->data as $item) { foreach ($this->data as $item) {
$product = apiRequest::requests($this->client, $this->goodsUrl, [ $product = apiRequest::requests($this->client, $this->goodsUrl, [
'token' => $apiToken,
'itemId' => $item['result']['itemId'], 'itemId' => $item['result']['itemId'],
], FormType::json); ], FormType::json);
if ($product['errCode'] != '0000') { if ($product['errCode'] != '0000') {
throw new \Exception('request item '. $item['item_id'].' err:' .$product['desc'] ?? ''); throw new Exception('request item ' . $item['item_id'] . ' err:' . $product['desc'] ?? '');
} }
$noticeIds[] = $item['notice_id'];
$product = $product['data']['product']; $product = $product['data']['product'];
$product['itemid'] = $product['itemId']; $product['itemid'] = $product['itemId'];
$good = new Good(); $good = new Good();
$good->fill($product); $good->fill($product);
$goods[] = $good->toArray(); $goods[] = $good->toArray();
$goodsItem = new GoodsItem();
$product['item_id'] = $product['itemid'];
$product['specifications'] = json_encode($product['specifications']);
$goodsItem->fill($product);
$goodsItems[] = $goodsItem->toArray();
} }
Good::insert($goods); try {
DB::transaction(function () use ($goods, $goodsItems, $noticeIds) {
if (!Good::insert($goods) || !GoodsItem::insert($goodsItems) || !\App\Models\Notice::query()->whereIn('notice_id', $noticeIds)->update(['state' => 3])) {
throw new Exception('insert failed');
};
});
} catch (Throwable $throwable) {
\App\Models\Notice::query()->whereIn('notice_id', $noticeIds)
->update(['err_message' => $throwable->getMessage()]);
throw new $throwable;
}
}
/**
* @throws Throwable
*/
public function changeProductState(): void
{
$ids = $idss = [];
foreach ($this->data as $item) {
$idss[] = $item['notice_id'];
$ids[$item['result']['state']][] = $item['result']['itemId'];
}
try {
DB::transaction(function () use ($ids, $idss) {
foreach ($ids as $state => $item) {
Good::query()->whereIn('itemid', $item)->update(['state' => $state]);
}
\App\Models\Notice::query()->whereIn('notice_id', $idss)
->update(['state' => 3]);
});
} catch (Throwable $throwable) {
\App\Models\Notice::query()->whereIn('notice_id', $idss)
->update(['err_message' => $throwable->getMessage()]);
throw $throwable;
}
}
/**
* @throws Throwable
*/
public function changePrice(): void
{
$updateData = array_column($this->data, 'result');
$priceType = [4 => 'sell_price', 5 => 'settle_price'];
$tablePriceType = [4 => 'market_price', 5 => 'settlement'];
$updateData = array_map(function ($item) use ($priceType, $tablePriceType) {
$item[$tablePriceType[$this->type]] = $item[$priceType[$this->type]];
unset($item[$priceType[$this->type]]);
return $item;
}, $updateData);
$ids = array_column($this->data, 'notice_id');
try {
DB::transaction(function () use ($updateData, $ids) {
Good::updateBatch($updateData);
\App\Models\Notice::query()->whereIn('notice_id', $ids)
->update(['state' => 3]);
});
} catch (Throwable $throwable) {
\App\Models\Notice::query()->whereIn('notice_id', $updateData)
->update(['err_message' => $throwable->getMessage()]);
throw $throwable;
}
} }
} }

View File

@ -3,8 +3,49 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
/**
* @method static int update(array $values)
* @method static bool updateOrInsert(array $attributes, array $values = []) Insert or update a record matching the attributes, and fill it with values.
* @method static bool upsert(array $values, $uniqueBy, $update = null) insert new records or update the existing ones.
* @method static bool insert(array $values)
* @method static int insertOrIgnore(array $values)
* @method static int insertGetId(array $values, null|string $sequence)
* @method static int delete(mixed $id = null)
*/
class Base extends Model class Base extends Model
{ {
public $timestamps = true; public $timestamps = true;
public static function updateBatch($multipleData = []): int
{
$tableName = (new static())->getTable(); // 表名
$firstRow = current($multipleData);
$updateColumn = array_keys($firstRow);
// 默认以id为条件更新如果没有ID则以第一个字段为条件
$referenceColumn = isset($firstRow['id']) ? 'id' : current($updateColumn);
unset($updateColumn[0]);
// 拼接sql语句
$updateSql = "UPDATE " . $tableName . " SET ";
$sets = [];
$bindings = [];
foreach ($updateColumn as $uColumn) {
$setSql = "`" . $uColumn . "` = CASE ";
foreach ($multipleData as $data) {
$setSql .= "WHEN `" . $referenceColumn . "` = ? THEN ? ";
$bindings[] = $data[$referenceColumn];
$bindings[] = $data[$uColumn];
}
$setSql .= "ELSE `" . $uColumn . "` END ";
$sets[] = $setSql;
}
$updateSql .= implode(', ', $sets);
$whereIn = array_column($multipleData, $referenceColumn);
$bindings = array_merge($bindings, $whereIn);
$whereIn = rtrim(str_repeat('?,', count($whereIn)), ',');
$updateSql = rtrim($updateSql, ", ") . " WHERE `" . $referenceColumn . "` IN (" . $whereIn . ")";
// 传入预处理sql语句和对应绑定数据
return DB::update($updateSql, $bindings);
}
} }

View File

@ -2,12 +2,10 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOne;
/** /**
* @property GoodItem $goodItem * @property GoodsItem $good_item
*/ */
class Good extends Base class Good extends Base
{ {
@ -15,7 +13,9 @@ class Good extends Base
'itemid','typeid','product_name','product_code','product_group', 'itemid','typeid','product_name','product_code','product_group',
'market_price','settlement','category_id', 'category_name' 'market_price','settlement','category_id', 'category_name'
]; ];
public function GoodItem() :HasOne {
return $this->hasOne(GoodItem::class,'item_id'); public function goodItem(): HasOne
{
return $this->hasOne(GoodsItem::class, 'item_id', 'itemid');
} }
} }

View File

@ -1,18 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @property Good $good
*/
class GoodItem extends Base
{
public function Good () : BelongsTo{
return $this->belongsTo(Good::class,'item_id');
}
}

20
app/Models/GoodsItem.php Normal file
View File

@ -0,0 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @property Good $good
*/
class GoodsItem extends Base
{
public $fillable = [
'item_id', 'brand', 'color_name', 'sell_point', 'specifications', 'shop_url', 'product_images', 'images',
'product_infos', 'app_introduce', 'applet_introduce'
];
public function Good () : BelongsTo{
return $this->belongsTo(Good::class, 'item_id', 'itemid');
}
}

View File

@ -15,6 +15,8 @@ public function up()
{ {
Schema::create('notices', function (Blueprint $table) { Schema::create('notices', function (Blueprint $table) {
$table->id(); $table->id();
$table->integer('notice_id')->unique()->comment('通知id');
$table->integer('notice_type')->comment('通知type');
$table->text('raw_content')->comment('原始消息内容'); $table->text('raw_content')->comment('原始消息内容');
$table->string('err_message')->default('')->comment('处理时错误信息'); $table->string('err_message')->default('')->comment('处理时错误信息');
$table->tinyInteger('state')->default(1)->comment('消息状态1待消费2消费进行中,3处理完成'); $table->tinyInteger('state')->default(1)->comment('消息状态1待消费2消费进行中,3处理完成');

View File

@ -14,7 +14,8 @@ class CreateGoodsTable extends Migration
public function up() public function up()
{ {
Schema::create('goods', function (Blueprint $table) { Schema::create('goods', function (Blueprint $table) {
$table->id('itemid')->comment('商品id'); $table->id();
$table->integer('itemid')->comment('商品id');
$table->integer('typeid')->comment(''); $table->integer('typeid')->comment('');
$table->string('product_name')->comment('商品名'); $table->string('product_name')->comment('商品名');
$table->string('product_code')->index()->comment('商品code'); $table->string('product_code')->index()->comment('商品code');
@ -23,7 +24,7 @@ public function up()
$table->decimal('settlement')->comment('结算价'); $table->decimal('settlement')->comment('结算价');
$table->integer('category_id')->index()->comment('分类id'); $table->integer('category_id')->index()->comment('分类id');
$table->string('category_name')->comment('分类名'); $table->string('category_name')->comment('分类名');
$table->tinyInteger('state')->default(10)->comment('状态 1正常 2 下架'); $table->tinyInteger('state')->default(1)->comment('状态 1上架 0 下架');
$table->timestamps(); $table->timestamps();
}); });
} }

View File

@ -18,13 +18,13 @@ public function up()
$table->integer('item_id')->index(); $table->integer('item_id')->index();
$table->string('brand')->default('')->comment('品牌'); $table->string('brand')->default('')->comment('品牌');
$table->string('color_name')->default('')->comment('颜色'); $table->string('color_name')->default('')->comment('颜色');
$table->string('sell_point')->default('')->comment('卖点'); $table->text('sell_point')->comment('卖点');
$table->string('specifications')->comment('规格'); $table->string('specifications')->comment('规格');
$table->string('shop_url')->comment('链接'); $table->string('shop_url')->comment('链接');
$table->string('product_images')->default('')->comment('商品主图'); $table->string('product_images')->default('')->comment('商品主图');
$table->string('images')->default('')->comment('主图列表'); $table->string('images')->default('')->comment('主图列表');
$table->string('product_infos')->default('')->comment('图文信息'); $table->text('product_infos')->comment('图文信息');
$table->string('app_introduce')->default('')->comment('app信息'); $table->text('app_introduce')->comment('app图文信息');
$table->string('applet_introduce')->default('详情图片地址'); $table->string('applet_introduce')->default('详情图片地址');
$table->timestamps(); $table->timestamps();
}); });

View File

@ -1,29 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('notices', function (Blueprint $table) {
$table->integer('notice_id')->unique()->comment('通知id')
->after('id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('notices', function (Blueprint $table) {
$table->dropColumn('notice_id');
});
}
};

View File

@ -1,28 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('notices', function (Blueprint $table) {
$table->integer('notice_type')->comment('通知type')->after('notice_id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('notices', function (Blueprint $table) {
$table->dropColumn('notice_type');
});
}
};

View File

@ -1,28 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('goods', function (Blueprint $table) {
$table->renameColumn('itemid','id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('goods', function (Blueprint $table) {
$table->renameColumn('id','itemid');
});
}
};

View File

@ -1,28 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('goods', function (Blueprint $table) {
$table->integer('itemId')->after('id')->index()->comment('商品id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('goods', function (Blueprint $table) {
$table->dropColumn('itemId');
});
}
};