<?php

namespace Modules\Mall\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Modules\Mall\Events\OrderPaid;
use Modules\Mall\Models\Goods;
use Modules\Mall\Models\Order;

class OrderPaidListener implements ShouldQueue
{

    public string $queue = 'LISTENER';

    /**
     * Notes: description
     *
     * @Author: 玄尘
     * @Date  : 2020/12/9 14:00
     * @param  OrderPaid  $event
     */
    public function handle(OrderPaid $event)
    {
        $order = $event->order->refresh();
        //增加销量
        foreach ($order->items as $item) {
            if ($item->source['deduct_stock_type'] == Goods::DEDUCT_STOCK_PAID) {
                $item->item->deductStock($item->qty, $order->user_id);
            }
            $item->item->addSold($item->qty);      //加销量
        }

        //扣除积分
        if ($order->type == Order::TYPE_SCORE) {
            $order->user->account->rule('score_buy', -$order->total, false, [
                'order_no' => $order->order_no,
            ]);
        }

    }

}
