<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2016/10/6
 * Time: 下午10:35
 */


namespace search;

require_once (BASE_ROOT_PATH . '/helper/message/msgutil.php');
require_once (BASE_ROOT_PATH . '/helper/message/subscriber.php');

use StatesHelper;
use Log;

class processor implements IProcessor
{
    const GetRelatedWord = 1;
    const SearchReasult  = 2;
    const MatchPrice     = 3;
    const PromoteGoods   = 4;

    const ValidateArea   = 20;

    public function handle_input($body)
    {
        if(StatesHelper::fetch_state('init')) {
            Log::record("searcher::instance reload data.",Log::DEBUG);
            searcher::instance()->init();
        }

        $body = unserialize($body);
        $type = $body['type'];

        if($type == self::GetRelatedWord)
        {
            $words = $body['keyword'];
            $result = searcher::instance()->get_word($words);
            return serialize($result);
        }
        elseif($type == self::SearchReasult) {
            $params = $body['params'];
            $result = searcher::instance()->get_result($params);
            return serialize($result);
        }
        elseif($type == self::MatchPrice) {
            $params = $body['params'];
            $result = searcher::instance()->match_price($params);
            return serialize($result);
        }
        elseif($type == self::PromoteGoods) {
            $params = $body['params'];
            $result = searcher::instance()->promote_goods($params);
            return serialize($result);
        }
        elseif ($type == self::ValidateArea) {
            $params = $body['params'];
            $area_id = $params['area_id'];
            $result = area_library::instance()->country($area_id);
            if($result == false) {
                return serialize(array('result' => false));
            } else {
                $result['result'] = true;
                return serialize($result);
            }
        }
        else {

        }
        ob_clean();
    }
}