erp/app/Console/Commands/CheckPrice.php

68 lines
2.0 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Http\Service\MessageService;
use App\Models\GoodsSku;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class CheckPrice extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'check:price';
/**
* 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()
{
//定时任务每天只查询昨天当天数据 触发节点每日凌晨
$yesterdayStartTime = Carbon::yesterday()->startOfDay()->toDateTimeString();
$yesterdayEndTime = Carbon::yesterday()->endOfDay()->toDateTimeString();
//查询价格异常订单
$results = DB::table('business_order_items as a')
->select('c.title as cn_name', 'b.title as title', DB::raw('ROUND(a.goods_price / 100,2) AS goods_price')
, 'b.cost','a.created_at','a.business_order_id')
->leftJoin('goods_skus as b', 'a.external_sku_id', '=', 'b.external_sku_id')
->leftJoin('goods as c', 'b.goods_id', '=', 'c.id')
->whereBetween('a.created_at', [$yesterdayStartTime,$yesterdayEndTime])
->havingRaw('goods_price < cost')
->get();
if($results->isNotEmpty()){
Log::info($yesterdayStartTime.'异常订单',$results->toArray());
$messageService = new MessageService();
foreach ($results as $v){
$messageService->createPriceExceptionMessage($v->business_order_id,
$v->cn_name,$v->title,$v->goods_price,$v->cost);
}
}
Log::info('任务完成:check-CheckPrice');
}
}