<?php
namespace backend\controllers;

use components\Exception;
use components\service\AmqpConfig;
use components\service\Redis;
use Yii;

class QueryController extends BaseController
{
	/**
	 * behaviors
	 * @return array
	 */
	public function behaviors()
	{
		return [
			'verbs' => [
				'class' => \yii\filters\VerbFilter::className(),
				'actions' => [
					'message-status' => ['GET'],
				],
			],
		];
	}

	/**
	 * 查询消息状态
	 * @author: libingke
	 * @return mixed
	 * @throws Exception
	 */
	public function actionMessageStatus()
	{
		$params = Yii::$app->request->queryParams;
		if (!isset($params['queue']))
			throw new Exception(1100);

		if (!is_string($params['queue']) || !$params['queue'])
			throw new Exception(1101);

		if (!isset($params['mid']))
			throw new Exception(1203);

		if (!is_string($params['mid']) || !$params['mid'])
			throw new Exception(1204);

		$r = Redis::get($params['queue'], $params['mid'], 'status');
		if ($r == null)
			throw new Exception(1004);

		return [
			'code' => 200,
			'message' => Yii::t('error', 200),
			'data' => ['status' => $r, 'status_mark' => AmqpConfig::getMarkById($r)]
		];
	}

	/**
	 * 查询消息结果
	 * @author: libingke
	 * @return mixed
	 * @throws Exception
	 */
	public function actionMessageResult()
	{
		$params = Yii::$app->request->queryParams;
		if (!isset($params['queue']))
			throw new Exception(1100);

		if (!is_string($params['queue']) || !$params['queue'])
			throw new Exception(1101);

		if (!isset($params['mid']))
			throw new Exception(1203);

		if (!is_string($params['mid']) || !$params['mid'])
			throw new Exception(1204);

		$s = Redis::get($params['queue'], $params['mid'], 'status');
		if ($s != AmqpConfig::STATUS_HAND_OK)
			throw new Exception(1005);

		$r = Redis::get($params['queue'], $params['mid'], 'status');
		if ($r == null)
			throw new Exception(1004);

		return json_decode($r, true);
	}
}