#include <server_exits/user_server_exits.h>
#include <epm/epm.h>


#include <time.h>
#include <bom/bom_msg.h>
#include <tccore/item_msg.h>
#include <vector>
#include <string.h>
/**
* @headerfile			user's header files
*/
#include "epm_register_handler.h"
#include "epm_handler_common.h"
#include "RB_AutoSignServer.h"

using namespace std;

// Method and Workflow Handler
extern DLLAPI int CUST_init_module(int *decision, va_list args)
{
	int ifail = ITK_ok;


	//检查交付和物料关系
	ifail = EPM_register_action_handler("RB_CheckForCompletion", "RB_CheckForCompletion",
		(EPM_action_handler_t)RB_CheckForCompletion);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering action handler RB_CheckForCompletion completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering action handler RB_CheckForCompletion failed %d!\n", ifail);
	}


	//对象下发到SAP
	ifail = EPM_register_action_handler("RB_SendSapItem", "RB_SendSapItem",
		(EPM_action_handler_t)RB_SendErpItem);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering action handler RB_SendSapItem completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering action handler RB_SendSapItem failed %d!\n", ifail);
	}
	//对象下发到SAP-新版本
	ifail = EPM_register_action_handler("RB_SendErpItem_New", "RB_SendErpItem_New",
		(EPM_action_handler_t)RB_SendErpItem_New);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering action handler RB_SendErpItem_New completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering action handler RB_SendErpItem_New failed %d!\n", ifail);
	}

	//对象下发到SAP-新版本 Rule
	ifail = EPM_register_rule_handler("RB_SendErpItem_New_Rule", "RB_SendErpItem_New_Rule",
		(EPM_rule_handler_t)RB_SendErpItem_New_Rule);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering rule handler RB_SendErpItem_New_Rule completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering rule handler RB_SendErpItem_New_Rule failed %d!\n", ifail);
	}



	//对象下发到SAP-新版本 Rule Release
	ifail = EPM_register_rule_handler("RB_SendErpItem_New_Rule_Release", "RB_SendErpItem_New_Rule_Release",
		(EPM_rule_handler_t)RB_SendErpItem_New_Rule_Release);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering rule handler RB_SendErpItem_New_Rule_Release completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering rule handler RB_SendErpItem_New_Rule_Release failed %d!\n", ifail);
	}




	//BOM下发到SAP
	ifail = EPM_register_action_handler("RB_SendSapBom", "RB_SendSapBom",
		(EPM_action_handler_t)RB_SendErpBom);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering action handler RB_SendSapBom completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering action handler RB_SendSapBom failed %d!\n", ifail);
	}
	//BOM下发到SAP--新版本
	ifail = EPM_register_action_handler("RB_SendErpBom_New", "RB_SendErpBom_New",
		(EPM_action_handler_t)RB_SendErpBom_New);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering action handler RB_SendErpBom_New completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering action handler RB_SendErpBom_New failed %d!\n", ifail);
	}

	//BOM下发到SAP--新版本rule
	ifail = EPM_register_rule_handler("RB_SendErpBom_New_Rule", "RB_SendErpBom_New_Rule",
		(EPM_rule_handler_t)RB_SendErpBom_New_Rule);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering rule handler RB_SendErpBom_New_Rule completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering rule handler RB_SendErpBom_New_Rule failed %d!\n", ifail);
	}


	//BOM下发到SAP--新版本rule Release
	ifail = EPM_register_rule_handler("RB_SendErpBom_New_Rule_Release", "RB_SendErpBom_New_Rule_Release",
		(EPM_rule_handler_t)RB_SendErpBom_New_Rule_Release);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering rule handler RB_SendErpBom_New_Rule_Release completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering rule handler RB_SendErpBom_New_Rule_Release failed %d!\n", ifail);
	}

	//工序下发到SAP--新版本
	ifail = EPM_register_action_handler("RB_SendGX_New", "RB_SendGX_New",
		(EPM_action_handler_t)RB_SendGX_New);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering action handler RB_SendGX_New completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering action handler RB_SendGX_New failed %d!\n", ifail);
	}
	//工艺路线下发到SAP--新版本
	ifail = EPM_register_action_handler("RB_Send_MDM_GYLX_New", "RB_Send_MDM_GYLX_New",
		(EPM_action_handler_t)RB_Send_MDM_GYLX_New);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering action handler RB_Send_MDM_GYLX_New completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering action handler RB_Send_MDM_GYLX_New failed %d!\n", ifail);
	}
	//传递TC检测分析委托单信息至LIMS
	ifail = EPM_register_action_handler("RB_PLM2LIMS_JCSY", "RB_PLM2LIMS_JCSY",
		(EPM_action_handler_t)RB_PLM2LIMS_JCSY);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering action handler RB_PLM2LIMS_JCSY completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering action handler RB_PLM2LIMS_JCSY failed %d!\n", ifail);
	}

	//传递TC检测分析委托单信息至LIMS
	ifail = EPM_register_action_handler("RB_testquery_ID", "RB_testquery_ID",
		(EPM_action_handler_t)RB_testquery_ID);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering action handler RB_testquery_ID completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering action handler RB_testquery_ID failed %d!\n", ifail);
	}
	
	ifail = EPM_register_action_handler("RB_BOM_TO_ERP", "RB_BOM_TO_ERP",
		(EPM_action_handler_t)RB_BOM_TO_ERP);
	if (ifail == ITK_ok)
	{
		fprintf(stdout, "Registering action handler RB_BOM_TO_ERP completed!\n");
	}
	else
	{
		fprintf(stdout, "Registering action handler RB_BOM_TO_ERP failed %d!\n", ifail);
	}
	
	return ifail;
}
//
////register service method
extern DLLAPI int USERSERVICE_custom_register_methods()
{
	int
		status = ITK_ok,
		numberOfArguments = 0,
		returnValueType = USERARG_STRING_TYPE,
		*argumentList = NULL;
	USER_function_t functionPtr;

	METHOD_id_t mth_tag;

	//===========================================

	/*METHOD_find_method("ScheduleTask", ITEM_create_msg, &mth_tag);
	if (mth_tag.id)
	{
		status = METHOD_add_action(mth_tag, METHOD_post_action_type, (METHOD_function_t)ML_Test, NULL);
		if (status == ITK_ok)
		{
			printf("register  wx3_tzcpxh_check successfully\n ");
		}
		else
		{
			printf("register wx3_tzcpxh_check failed\n ");
		}

	}*/

	//===========================================
	//提取流程节点自动跳过
	numberOfArguments = 2;
	functionPtr = RB_AutoSignServer;
	argumentList = (int*)MEM_alloc(numberOfArguments * sizeof(int));
	argumentList[0] = USERARG_STRING_TYPE;      //需要执行的signoff的puid
	argumentList[1] = USERARG_STRING_TYPE;      //跳过的节点的PUID
	returnValueType = USERARG_STRING_TYPE;   //返回值
	ITKCALL(status = USERSERVICE_register_method("RB_AutoSignServer", functionPtr, numberOfArguments, argumentList, returnValueType));
	MEM_free(argumentList);
	if (status == ITK_ok)
	{
		fprintf(stdout, "Registering userservice RB_AutoSignServer successful !\n");
	}
	else {
		fprintf(stdout, "Registering userservice RB_AutoSignServer failed %d\n", status);
	}
	return(status);
}


