<?php
namespace hook\queue\redis;

use app\admin\model\Orders;
use Webman\RedisQueue\Consumer;

/**
 * 订单超时自动取消
 * Class CancelOrder
 * @package hook\queue\redis
 */
class CancelOrder implements Consumer
{
    public $queue = 'order-cancel';//消费队列名称

    public $connection = 'default';//连接名称

    /**
     * Notes:业务逻辑处理
     * @param $data
     * User: yym
     * Date: 2022/8/1
     */
    public function consume($data)
    {
        try {
            if(!isset($data['order_id']) || empty($data['order_id']))
            {
                throw new \Exception('缺少订单编号！');
            }
            $order = Orders::getOrderData($data['order_id']);
            if(empty($order))
            {
                throw new \Exception('查无此订单信息');
            }
            if($order['order_pay_status'] == Orders::SUCCESS)
            {
                //订单已支付成功
                return true;
            }
            $result = Orders::updateOrder($data['order_id'], ['order_pay_status' => Orders::CANCEL]);
            if($result)
            {
                return true;
            }
            throw new \Exception('取消失败~');
        }catch (\Exception $exception){
            throw new \Exception($exception->getMessage(), $exception->getCode());
        }
    }
}
