jingcai-php/app/Console/Commands/SumCustomerPrize.php

98 lines
2.6 KiB
PHP
Executable File

<?php
namespace App\Console\Commands;
use App\Enums\BoolEnum;
use App\Enums\LottState;
use App\Enums\LottType;
use App\Enums\OrderType;
use App\Events\OrderWinedEvent;
use App\Jobs\HandleOrderWin;
use App\Jobs\RefreshOrderCtzqBqcResult;
use App\Jobs\RefreshOrderCtzqJqcResult;
use App\Jobs\RefreshOrderJclqResult;
use App\Jobs\RefreshOrderJczqResult;
use App\Model\Config;
use App\Model\Customer\Customer;
use App\Model\Lq\JclqResult;
use App\Model\Order;
use App\Model\OrderBjdcResult;
use App\Model\OrderBjdcZuhe;
use App\Model\OrderJclqResult;
use App\Model\OrderLqZuhe;
use App\Model\Zq\BjdcResult;
use App\Service\External\AlStatService;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class SumCustomerPrize extends Command
{
/**
* 这个就是命令名称
*/
protected $signature = 'customer:sum_prize';
/**
* 命令的说明描述
* @var string
*/
protected $description = '';
/**
* 创建命令的构造方法。
* @param string $words 传入的字符参数
* @return void
*/
public function __construct()
{
parent::__construct();
}
public function handle()
{
Order::whereIn('lottery_state', [LottState::WIN, LottState::SEND])
->chunkById(500, function($orders) {
foreach ($orders as $order) {
if ($order->type == OrderType::UNION) {
$this->handleUnionOrder($order);
} else {
$this->handleUnUnionOrder($order);
}
}
});
}
public function handleUnionOrder(Order $order) {
$perPrize = $order->lottery_prize / $order->union_piece_total;
// 合买参与人的数据
$unionOrders = Order::where('type', OrderType::UNION)
->where('pid', $order->id)
->get();
foreach ($unionOrders as $unionOrder) {
$buyPiece = $unionOrder->union_piece_self;
if ($unionOrder->id == $unionOrder->pid) {
$buyPiece = $unionOrder->union_piece_self + $unionOrder->union_piece_keep;
}
$winPrize = $perPrize * $buyPiece;
Customer::where('id', $unionOrder->customer_id)->update([
'win_prize_total' => DB::raw("`win_prize_total` + {$winPrize}")
]);
}
}
public function handleUnUnionOrder(Order $order) {
Customer::where('id', $order->customer_id)->update([
'win_prize_total' => DB::raw("`win_prize_total` + {$order->lottery_prize}")
]);
}
}