Merge pull request !213 from 赵世界/feat/2024
This commit is contained in:
赵世界 2024-02-04 09:21:02 +00:00 committed by Gitee
commit 016f78f7f9
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 189 additions and 0 deletions

View File

@ -0,0 +1,127 @@
<?php
namespace App\Console\Commands;
use App\Models\BusinessOrderItem;
use App\Models\DailyReport;
use App\Models\GoodsSku;
use Carbon\Carbon;
use Illuminate\Console\Command;
class GoodsSkuDailyReport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'daily:report:goods_sku {date?}';
/**
* The console command description.
*
* @var string
*/
protected $description = '每日商品数据记录';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$date = $this->argument('date');
if (is_null($date)) {
$date = Carbon::yesterday()->format('Y-m-d');
}
$startDateTime = Carbon::parse($date)->startOfDay()->toDateTimeString();
$endDateTime = Carbon::parse($date)->endOfDay()->toDateTimeString();
$orderItems = BusinessOrderItem::query()
->select(['shop_id', 'already_cancel_number', 'external_sku_id', 'goods_amount', 'goods_cost_price', 'goods_number', 'goods_price'])
->where('external_sku_id', '<>', '')
->where('created_at', '>=', $startDateTime)
->where('created_at', '<=', $endDateTime)
->get();
if ($orderItems->isEmpty()) {
return;
}
$externalSkuIds = array_unique(array_column($orderItems->toArray(), 'external_sku_id'));
$goodsSkus = GoodsSku::query()
->select(['id', 'goods_id', 'cost', 'external_sku_id'])
->with([
'goods:id,type_id,brand_id'
])
->where('is_combination', 0)
->whereIn('external_sku_id', $externalSkuIds)
->get()
->pluck(null, 'external_sku_id')
->toArray();
if (empty($goodsSkus)) {
return;
}
$data = [];
foreach ($orderItems as $orderItem) {
if (!isset($goodsSkus[$orderItem->external_sku_id])) {
continue;
}
if (!isset($data[$orderItem->external_sku_id])) {
$data[$orderItem->external_sku_id] = [
'goods_id' => $goodsSkus[$orderItem->external_sku_id]['goods']['id'],
'type_id' => $goodsSkus[$orderItem->external_sku_id]['goods']['type_id'],
'brand_id' => $goodsSkus[$orderItem->external_sku_id]['goods']['brand_id'],
'goods_sku_id' => $goodsSkus[$orderItem->external_sku_id]['id'],
'cost' => $goodsSkus[$orderItem->external_sku_id]['cost'],
'total_goods_price' => 0,
'total_goods_cost_price' => 0,
'total_goods_amount' => 0,
'total_goods_number' => 0,
'total_cancel_number' => 0,
'shop_data' => [],
];
}
$data[$orderItem->external_sku_id]['total_goods_price'] += $orderItem->goods_price;
$data[$orderItem->external_sku_id]['total_goods_cost_price'] += $orderItem->goods_cost_price;
$data[$orderItem->external_sku_id]['total_goods_amount'] += $orderItem->goods_amount;
$data[$orderItem->external_sku_id]['total_goods_number'] += $orderItem->goods_number;
$data[$orderItem->external_sku_id]['total_cancel_number'] += $orderItem->already_cancel_number;
if (isset($data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id])) {
$data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_goods_price'] += $orderItem->goods_price;
$data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_goods_cost_price'] += $orderItem->goods_cost_price;
$data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_goods_amount'] += $orderItem->goods_amount;
$data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_goods_number'] += $orderItem->goods_number;
$data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id]['total_cancel_number'] += $orderItem->already_cancel_number;
} else {
$data[$orderItem->external_sku_id]['shop_data'][$orderItem->shop_id] = [
'total_goods_price' => 0,
'total_goods_cost_price' => 0,
'total_goods_amount' => 0,
'total_goods_number' => 0,
'total_cancel_number' => 0,
];
}
}
foreach ($data as $externalSkuId => $datum) {
DailyReport::query()->firstOrCreate([
'date' => $date,
'external_sku_id' => $externalSkuId
], $datum);
}
}
}

View File

@ -2,6 +2,7 @@
namespace App\Console; namespace App\Console;
use App\Console\Commands\GoodsSkuDailyReport;
use App\Console\Commands\Inventory; use App\Console\Commands\Inventory;
use Illuminate\Console\Scheduling\Schedule; use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@ -29,6 +30,7 @@ class Kernel extends ConsoleKernel
{ {
// 服务器/etc/crontab添加cron入口 // 服务器/etc/crontab添加cron入口
// * * * * * cd /home/wwwroot/erp.chutang66.com && php artisan schedule:run >> /dev/null 2>&1 // * * * * * cd /home/wwwroot/erp.chutang66.com && php artisan schedule:run >> /dev/null 2>&1
$schedule->command(GoodsSkuDailyReport::class)->dailyAt('06:00');
$schedule->command(Inventory::class)->dailyAt('07:00'); $schedule->command(Inventory::class)->dailyAt('07:00');
$schedule->command(KttOrderQuery::class)->everyMinute(); $schedule->command(KttOrderQuery::class)->everyMinute();
$schedule->command(DeleteKttQuery::class)->daily(); $schedule->command(DeleteKttQuery::class)->daily();

View File

@ -0,0 +1,14 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class DailyReport extends Model
{
protected $guarded = [];
protected $casts = [
'shop_data' => 'array',
];
}

View File

@ -0,0 +1,46 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDailyReportsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('daily_reports', function (Blueprint $table) {
$table->bigIncrements('id');
$table->date('date');
$table->unsignedInteger('goods_id');
$table->unsignedInteger('type_id');
$table->unsignedInteger('brand_id');
$table->unsignedInteger('goods_sku_id');
$table->string('external_sku_id', 64);
$table->unsignedInteger('total_goods_price')->default(0);
$table->unsignedInteger('total_goods_cost_price')->default(0);
$table->unsignedInteger('total_goods_amount')->default(0);
$table->unsignedInteger('total_goods_number')->default(0);
$table->unsignedInteger('total_cancel_number')->default(0);
$table->unsignedInteger('cost')->default(0);
$table->text('shop_data');
$table->timestamps();
$table->index(['date', 'external_sku_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('week_reports');
}
}