erp/app/Console/Commands/ChkReplenish.php

84 lines
2.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\GoodsSku;
use App\Models\MessageStock;
use App\Models\StockNotice;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Pool\Core\Redis;
class ChkReplenish extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'check:replenish';
/**
* 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()
{
// Log::info('任务完成:check-replenish');
$stock=StockNotice::where('id',1)->first();
$stocksQuery = DB::table('goods_skus AS a')
->select(
'a.id',
'a.title',
'a.num',
'a.stock','a.updated_at',
DB::raw('b.title AS good_name'),
DB::raw('SUM(a.num - a.stock) AS stocks')
)
->leftJoin('goods AS b', 'a.goods_id', '=', 'b.id')
->where('a.num', '!=', 0)
->where('a.stock', '!=', 0)
->groupBy('id')
->havingRaw("stocks < $stock->active_piece");
$stocks = $stocksQuery->get();
$currentDateTime = Carbon::now();
$endOfDay = Carbon::today()->endOfDay();
$secondsRemaining = $currentDateTime->diffInSeconds($endOfDay);
$redis=new Redis();
$arr=[];
foreach ($stocks as $k=>$v){
if (!$redis->exists($stock->cn_name.'::'.$v->title)){
$redis->setex($stock->cn_name.'::'.$v->title,$secondsRemaining,$v->stock);
$arr[$k]['title']=$stock->cn_name;
$arr[$k]['user_id']=$stock->user_id;
$arr[$k]['stock_notice_id']=$stock->id;
$arr[$k]['content']=Carbon::now()->format('Y-m-d')." 商品{$v->good_name}规格{$v->title}需要补货,当前库存{$v->num}/支,可售库存{$v->stock}/支";
$arr[$k]['created_at']=Carbon::now();
MessageStock::insert($arr);
}
}
Log::info('任务完成:check-replenish');
}
}