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}") ]); } }