<?php

/**
 * 红包相关
 * @author solu
 */
class RedpackController extends BaseController {

    protected $ajaxLoginActions = [
        'grab',
    ];

    public function __construct() {
        parent::__construct(true);
    }

    /**
     * 抢红包
     * @author solu
     * @param $args
     * @return array
     */
    public function actionGrab($args) {
        $rules = [
            'trx_id' => ['string', 'desc' => '红包交易id'],
        ];
        Param::checkParam2($rules, $args);

        $userId = User::getUserId();
        $trxId = $args['trx_id'];

        $objLock = new Lock('grab_redpack', 10);
        $objRedpack = new Redpack();

        $objLock->lock($userId);
        $quantity = 0;
        $type = 'eos';
        try {
            list($quantity, $type) = $objRedpack->grab($trxId, $userId);
        } catch (Exception $e) {
            $objLock->unlock($userId);
            Response::error($e->getCode(), $e->getMessage());
        }

        $objLock->unlock($userId);
        return compact('type', 'quantity');
    }

}

