#include <Poco/Format.h>
#include "Poco/Exception.h"
#include "query_analyzer.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/Query.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/JSON/Stringifier.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/JSON/PrintHandler.h"
#include "Poco/JSON/Template.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/Dynamic/Var.h"

using namespace std;
using namespace Poco::JSON;
using Poco::JSON::Parser;
using Poco::JSON::Object;
using namespace Poco::Dynamic;
using Poco::Dynamic::Var;


QueryAnalyzerImpl::QueryAnalyzerImpl()
:_logger(Logger::get("StrategyServer")){

	_pRedis = RedisManage::getInstance();
}

Status QueryAnalyzerImpl::QueryInferenceService(ServerContext* context,const QueryInferenceRequest *request,QueryInferenceReply *reply){

	std::map<std::string,int> query_flag_map = {
			{"unknown", 0},   //未知
			{"face", 1},	  //魔镜
			{"free_face", 2}, //免费整形
			{"hospital", 3},  //医院
			{"doctor", 4},    //医生
			{"project", 5},   //项目
	};
	try {
		std::string query_word = request->query();
		std::string version_type = request->version_type();
		bool return_face = request->return_face();

	    int iredisDuration = 0;
		redisContext *pContext = NULL;
		_pRedis->getRedisContext(&pContext);

		int iRetLabel = query_flag_map["unknown"];
		std::string stValue;
		std::string stCommand = "get " + query_word;
		if(_pRedis->get(stCommand, stValue,pContext,&iredisDuration)){
			Parser parser;
			Var result = parser.parse(stValue);
			Object::Ptr pObjPtr = result.extract<JSON::Object::Ptr>();
			iRetLabel = atoi(pObjPtr->get(std::string("value")).toString().c_str());
			int is_online = atoi((pObjPtr->get(std::string("is_online"))).toString().c_str());
		}

		reply->set_label(iRetLabel);
		return Status::OK;
	} catch (Poco::Exception e) {
		_logger.error(Poco::format("Exception occured in func %s,err_msg is:%s",string(__FUNCTION__),e.displayText()));
		return Status::OK;
	}
}


