12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- namespace App\Console\Commands;
- use App\Models\Bike;
- use App\Models\Order;
- use App\Models\OrderRent;
- use App\Utils\RedisKeys;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class UpdateBikesProfitCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'update:bikes_profit';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '更新车辆总收益 截止到昨天24:00';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $today = Carbon::today();
- $yesterday = Carbon::yesterday();
- $lock = $this->makeUpdateBikesProfitLock($yesterday);
- if($lock){
- //
- $order = Order::query()->where('status', Order::STATUS_COMPLETE_ORDER)
- ->with(['bikes'])
- ->where('pay_time', '>', $yesterday)
- ->where('pay_time', '<', $today)
- ->groupBy(['bike_id'])
- ->select('bike_id', DB::raw('sum(pay_money) as total_sum'))
- ->orderByDesc('total_sum')
- ->get();
- if(!empty($order)){
- foreach ($order as $v){
- Bike::query()->where('id',$v->bike_id)->increment('total_money',$v->total_sum);
- }
- }
- $order_rent = OrderRent::query()->where('status', OrderRent::STATUS_COMPLETE_ORDER)
- ->with(['bikes'])
- ->where('pay_time', '>', $yesterday)
- ->where('pay_time', '<', $today)
- ->groupBy(['bike_id'])
- ->select('bike_id', DB::raw('sum(order_total_money) as total_sum'))
- ->orderByDesc('total_sum')
- ->get();
- if(!empty($order_rent)){
- foreach ($order_rent as $v){
- Bike::query()->where('id',$v->bike_id)->increment('total_money',$v->total_sum);
- }
- }
- }else{
- Log::error('已执行过更新昨日车辆总收益');
- }
- }
- public function makeUpdateBikesProfitLock($yesterday)
- {
- $key = sprintf(RedisKeys::LOCK_EVERYDAY_UPDATE_BIKES_PROFIT_DAY, $yesterday);
- $res = app()->redis->incr($key);
- app()->redis->Expire($key, RedisKeys::LOCK_EVERYDAY_UPDATE_BIKES_PROFIT_DAY_EXPRIRE);
- return $res > 1 ? false : true;
- }
- }
|