#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"

int ML_AddFormToAttachments(EPM_action_message_t msg)
{
	printf("========================添加对象流程目标下 Start===================\n");
	auto startTime = std::chrono::high_resolution_clock::now();
	int ifail = ITK_ok;
	int  attachments_num = 0;
	tag_t  rootTask = NULLTAG, *attachments = NULLTAG;
	//获取任务对象
	EPM_ask_root_task(msg.task, &rootTask);
	//获取任务目标对象
	EPM_ask_attachments(rootTask, EPM_target_attachment, &attachments_num, &attachments);

	char *argflag = NULL, *argvalue = NULL, *arg = NULL;
	char arg1value[1024] = "", arg2value[1024] = "", arg3value[1024] = "", arg4value[1024] = "";
	//获取参数
	int arg_cnt = TC_number_of_arguments(msg.arguments);
	printf("参数个数为：%d\n", arg_cnt);
	if (arg_cnt > 0)
	{
		for (int i = 0; i<arg_cnt; i++)
		{
			//获取下一个参数（从0开始）
			arg = TC_next_argument(msg.arguments);
			//获取参数的名称和值
			ifail = ITK_ask_argument_named_value((const char*)arg, &argflag, &argvalue);
			if (stricmp(argflag, "itemType") == 0)
			{
				//strcmp("","");
				if (argvalue != NULL)
				{
					strcpy(arg1value, argvalue);
				}
			}
			else if (stricmp(argflag, "referenced") == 0)
			{
				if (argvalue != NULL)
				{
					strcpy(arg2value, argvalue);
				}
			}
			else if (stricmp(argflag, "targetItemType") == 0)
			{
				if (argvalue != NULL)
				{
					strcpy(arg3value, argvalue);
				}
			}
			else if (stricmp(argflag, "attachmentType") == 0)
			{
				if (argvalue != NULL)
				{
					strcpy(arg4value, argvalue);
				}
			}
		}
	}

	for (int i = 0; i < attachments_num; i++)
	{
		char *itemType = NULL;

		AOM_ask_value_string(attachments[i], "object_type", &itemType);
		printf("type_class : %s \r\n", itemType);
		//过滤掉非版本的对象
		if ((strcmp(itemType, arg1value) != 0))
		{
			DOFREE(itemType);
			continue;
		}
		tag_t processTag = attachments[i];
		//判断是否是版本
		if (strstr(itemType, "Revision") != NULL)
		{
			ITEM_ask_item_of_rev(processTag, &processTag);
		}

		int attachmentType = EPM_target_attachment;
		if (strcmp("EPM_reference_attachment", arg4value) == 0)
		{
			attachmentType = EPM_reference_attachment;
		}
		int size = 0;
		int *tagType = (int *)MEM_alloc(1024 * sizeof(int));//EPM_reference_attachment
		tag_t * itemTag = (tag_t *)MEM_alloc(1024 * sizeof(tag_t));
		//判断是否为子查父
		if (strcmp("whereReferenced", arg2value) == 0)
		{
			int n_references = 0;
			int *levels = 0;
			tag_t * references_tag = NULLTAG;
			char ** relation_type_name = NULL;

			//通过引用关系查找到文件夹
			WSOM_where_referenced(processTag, 1, &n_references, &levels, &references_tag, &relation_type_name);
			for (int j = 0; j < n_references; j++)
			{
				char *objectType = NULL;

				AOM_ask_value_string(references_tag[j], "object_type", &objectType);
				if ((strcmp(objectType, arg3value) != 0))
				{
					DOFREE(objectType);
					continue;
				}
				tagType[size] = attachmentType;
				itemTag[size] = references_tag[j];
				size++;
				DOFREE(objectType);
			}
			DOFREE(references_tag);
		}
		else
		{
			int num = 0;
			tag_t * value = NULLTAG;
			AOM_ask_value_tags(attachments[i], arg2value, &num, &value);
			for (int j = 0; j < num; j++)
			{
				char *objectType = NULL;

				AOM_ask_value_string(value[j], "object_type", &objectType);
				if ((strcmp(objectType, arg3value) != 0))
				{
					DOFREE(objectType);
					continue;
				}
				tagType[size] = attachmentType;
				itemTag[size] = value[j];
				size++;
				DOFREE(objectType);
			}
			DOFREE(value);
		}
		if (size > 0)
		{
			POM_AM__set_application_bypass(true);
			//添加到流程目标下
			EPM_add_attachments(rootTask, size, itemTag, tagType);
			POM_AM__set_application_bypass(false);
		}

		DOFREE(itemTag);
		DOFREE(itemType);
	}
	DOFREE(attachments);

	auto stopTime = std::chrono::high_resolution_clock::now();
	auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stopTime - startTime);
	//std::cout << "ML_AddFormToAttachments用时:" << duration.count() / 1000 << std::endl;
	string usetime = "ML_AddFormToAttachments用时:";
	usetime.append(std::to_string(duration.count() / 1000));
	WriteLog(true, usetime.c_str());
	printf("=========================添加对象流程目标下 End===================\n");
	return ifail;
}