// connor_erp_mes.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "connor_erp_mes.h"

#include <tc/envelope.h>
#include <tc/folder.h>
#include <stdlib.h>
#include <ict/ict_userservice.h>
#ifdef __cplusplus
extern "C" {
#endif
	/**
	* @fn					extern "C" DLLAPI int liborigin_register_callbacks
	* @return				usually return ITK_ok
	* @brief				liborigin customization entry
	* 此函数必须有规范的写法，必须以dll的名称加上"_"开头
	*/
	DLLAPI int connor_erp_mes_register_callbacks()
	{
		int ifail = ITK_ok;
		//ITKCALL(ifail = CUSTOM_register_exit("connor_erp_mes", "USER_invoke_pdm_server", (CUSTOM_EXIT_ftn_t)K_invoke_pdm_server));
		//fprintf(stdout, "connor_erp_mes register USER_invoke_pdm_server complete\n");
		ITKCALL(ifail = CUSTOM_register_exit("connor_erp_mes", "USERSERVICE_register_methods",
			(CUSTOM_EXIT_ftn_t)USERSERVICE_custom_register_methods));
		fprintf(stdout, "connor_erp_mes register USERSERVICE_custom_register_methods complete\n");
		ITKCALL(ifail = CUSTOM_register_exit("connor_erp_mes", "USER_gs_shell_init_module",
			(CUSTOM_EXIT_ftn_t)USERSERVICE_custom_register_handlers));
		fprintf(stdout, "connor_erp_mes register USER_gs_shell_init_module complete\n");
		return ifail;
	}


	DLLAPI int USERSERVICE_custom_register_handlers(int* decision, va_list args)
	{
		int ifail = ITK_ok;
		//QF_ERP_wl
		ITKCALL(ifail = EPM_register_action_handler("QF_ERP_wl", "QF_ERP_wl", (EPM_action_handler_t)QF_ERP_wl));
		if (ifail == 0) {
			printf("注册操作处理程序成功：QF_ERP_wl\n");
		}
		else {
			printf("注册操作处理程序失败[%d]：QF_ERP_wl\n", ifail);
		}
		//QF_ERP_BOM
		ITKCALL(ifail = EPM_register_action_handler("QF_ERP_BOM", "QF_ERP_BOM", (EPM_action_handler_t)QF_ERP_BOM));
		if (ifail == 0) {
			printf("注册操作处理程序成功：QF_ERP_BOM\n");
		}
		else {
			printf("注册操作处理程序失败[%d]：QF_ERP_BOM\n", ifail);
		}
		//QF_ERP_BOP
		ITKCALL(ifail = EPM_register_action_handler("QF_ERP_BOP", "QF_ERP_BOP", (EPM_action_handler_t)QF_ERP_BOP));
		if (ifail == 0) {
			printf("注册操作处理程序成功：QF_ERP_BOP\n");
		}
		else {
			printf("注册操作处理程序失败[%d]：QF_ERP_BOP\n", ifail);
		}
		// QF_Deleta_TCM
		ITKCALL(ifail = EPM_register_action_handler("QF_Delete_TCM", "QF_Delete_TCM", (EPM_action_handler_t)QF_Delete_TCM));
		if (ifail == 0) {
			printf("注册操作处理程序成功：QF_Delete_TCM\n");
		}
		else {
			printf("注册操作处理程序失败[%d]：QF_Delete_TCM\n", ifail);
		}

		/*ITKCALL(ifail = EPM_register_action_handler("QF_signoff_rev", "QF_signoff_rev", (EPM_action_handler_t)CONNOR_SIGN_MASTER));
		if (ifail == 0) {
			printf("注册操作处理程序成功：QF_signoff_rev\n");
		}
		else {
			printf("注册操作处理程序失败[%d]：QF_signoff_rev\n", ifail);
		}
		ITKCALL(ifail = EPM_register_action_handler("QF_signoff_prt", "QF_signoff_prt", (EPM_action_handler_t)CONNOR_SIGN_DATASET));
		if (ifail == 0) {
			printf("注册操作处理程序成功：QF_signoff_prt\n");
		}
		else {
			printf("注册操作处理程序失败[%d]：QF_signoff_prt\n", ifail);
		}*/
		return ifail;
	}

	//mail_name为邮件的名字，user_tag为要发送到的用户，tags为邮件内包含的对象
	void sendMail(char *mail_name, tag_t user_tag, tag_t *tags)
	{
		tag_t envelope_tag;
		MAIL_create_envelope(mail_name, "", &envelope_tag);
		MAIL_add_envelope_receiver(envelope_tag, user_tag);
		AOM_set_value_tags(envelope_tag, "contents", 1, tags);
		MAIL_send_envelope(envelope_tag);
	}
	int connor_send_envelop(void * returnValue)
	{
		printf("222set prop \n");
		int ifail = ITK_ok,
			count = 0,
			itemCount = 0,
			form_count = 0;
		tag_t tags = NULLTAG;
		char *mail_name = NULL;
		tag_t user_tag = NULLTAG;
		USERARG_get_tag_argument(&tags);//获得对象参:产线对象
		USERARG_get_tag_argument(&user_tag);//获得对象参数：收件人
		USERARG_get_string_argument(&mail_name);//邮件内容


												//获取属性对象

												//printf("222set prop %s\n", mail_name);
												//tag_t *contentTags=NULL ;
												//contentTags = (tag_t *)malloc(1 * sizeof(tag_t));
												//contentTags[0] = tags;
												//sendMail(mail_name, user_tag, contentTags);
		if (mail_name != NULL) {
			MEM_free(mail_name);
			mail_name = NULL;
		}
		//if (contentTags != NULL) {
		//	MEM_free(contentTags);
		//	contentTags = NULL;
		//}
		return  ifail;
	}
	DLLAPI int USERSERVICE_custom_register_methods()
	{
		int	numberOfArguments = 1,
			returnValueType = USERARG_STRING_TYPE,
			*argumentList = (int*)MEM_alloc(numberOfArguments * sizeof(int));
		argumentList[0] = USERARG_LOGICAL_TYPE;;
		USER_function_t functionPtr;
		int status = 0;
		//自动发起流程
		numberOfArguments = 3;
		functionPtr = connor_send_envelop;//注册方法
		argumentList = (int*)MEM_alloc(numberOfArguments * sizeof(int));
		argumentList[0] = USERARG_TAG_TYPE;
		argumentList[1] = USERARG_TAG_TYPE;
		argumentList[2] = USERARG_STRING_TYPE;
		returnValueType = USERARG_VOID_TYPE;
		status = USERSERVICE_register_method("connor_send_envelop", functionPtr, numberOfArguments,
			argumentList, returnValueType);
		MEM_free(argumentList);
		if (status == ITK_ok)
		{
			fprintf(stdout, "\n Registering connor_send_envelop finished\n");
		}
		else
		{
			fprintf(stdout, "\n Registering connor_send_envelop failed %d\n", status);
		}
		functionPtr = WX3_send_mail;
		argumentList = (int*)MEM_alloc(5 * sizeof(int));
		argumentList[0] = USERARG_STRING_TYPE;
		argumentList[1] = USERARG_STRING_TYPE;
		argumentList[2] = USERARG_TAG_TYPE;
		argumentList[3] = USERARG_TAG_TYPE + 100;
		argumentList[4] = USERARG_TAG_TYPE;
		returnValueType = USERARG_VOID_TYPE;
		status = USERSERVICE_register_method("WX3_send_mail", functionPtr, 5,
			argumentList, returnValueType);
		if (status == ITK_ok)
		{
			fprintf(stdout, "\n Registering WX_send_mail finished\n");
		}
		else
		{
			fprintf(stdout, "\n Registering WX_send_mail failed %d\n", status);
		}

		return status;
	}


#ifdef __cplusplus
}
#endif