\#include "stdafx.h"
\#include "pch.h"
\#include "${pyName}.h"
\#include <string>
\#include "pyutils.h"
\#include "jni.h"
\#include "Python.h"
\#include "PyGILThreadLock.h"
\#include "convertutils.h"

using namespace std;

#{foreach} ($entity in $modelMethods)
jobject ${pyName}_${entity.methodName}(JNIEnv* env, #{foreach} ($column in [1..$entity.dataLength])jobjectArray data${column}, #{end}#{if}($entity.model==1)jobject model_path_, #{end}jobject settings)
{
	try
	{

		PyGILThreadLock lock;

		jclass hashmapClass = env->FindClass("java/util/HashMap");
		jmethodID getMID = env->GetMethodID(hashmapClass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
		jstring keyJString = env->NewStringUTF("pyFile");
		jobject javaValueObj = env->CallObjectMethod(settings, getMID, keyJString);
		jstring javaStringValue = (jstring)javaValueObj;
		const char* strValue = env->GetStringUTFChars(javaStringValue, NULL);
		//*************导入模型******************
		PyObject* pModule = create_py_module(strValue);
		// 释放java占用内存
		env->ReleaseStringUTFChars(javaStringValue, strValue);
		env->DeleteLocalRef(javaStringValue);
		env->DeleteLocalRef(javaValueObj);
		env->DeleteLocalRef(keyJString);
		env->DeleteLocalRef(hashmapClass);
		if (pModule == NULL)
		{
			cout << "model error" << endl;
			throw "模块调用失败!";
		}

		//*************导入函数******************
		PyObject* pFunc = PyObject_GetAttrString(pModule, "${entity.methodName}");
		if (pFunc == NULL)
		{
			cout << "func error" << endl;
			throw "函数调用失败!";
		}
#{foreach} ($column in [1..$entity.dataLength])
		PyObject* data_${column} = ConvertJdarrToPydarr(env, data${column});
#{end}
#{if}($entity.model==1)
		PyObject* model_path = ConvertJhmapToPydict(env, model_path_);
#{end}
		PyObject* settings_1 = ConvertJhmapToPydict(env, settings);


		//************构造参数数据 ********************
#{if}($entity.model==1)
#set($a=$entity.dataLength + 2)
		PyObject* pArg = PyTuple_New(${a});
#{else}
#set($a=$entity.dataLength + 1)
		PyObject* pArg = PyTuple_New(${a});
#{end}
#{foreach} ($column in [1..$entity.dataLength])
#set($a=$column - 1)
		PyTuple_SetItem(pArg, ${a}, data_${column});
#{end}
#{if}($entity.model==1)
		PyTuple_SetItem(pArg, ${entity.dataLength}, model_path);
#set($a=$entity.dataLength + 1)
		PyTuple_SetItem(pArg, ${a}, settings_1);
#{else}
		PyTuple_SetItem(pArg, ${entity.dataLength}, settings_1);
#{end}

		/*PyTuple_SetItem(pArg, 1, parseCppData(data_in->feature_range));*/


		//********************调用python方法计算*****************
		PyObject* pReturn = PyEval_CallObject(pFunc, pArg);
		if (pReturn == NULL)
		{
			cout << "return error" << endl;
			throw "函数返回值异常!";
		}
		//*******************返回数据封装************************
	   // 开辟返回数据内存空间,转换数据,拆分数据

		/*jobject result = jhmappydict_(env, pReturn);*/
		jobject result = ConvertPydictToJhmap(env, pReturn);
		if (result == NULL)
		{
			cout << "ConvertPydictToJhmap error" << endl;
			throw "函数返回值异常!";
		}

		return result;
	}
	catch (const char* msg)
	{
		cout << "${entity.methodName} error" << endl;
		return NULL;
	}
}

#{end}