#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"

int ML_ChangOwning(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] = "";
	//获取参数
	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, "referenced") == 0)
			{
				if (argvalue != NULL)
				{
					strcpy(arg1value, argvalue);
				}
			}
		}
		DOFREE(argflag);
		DOFREE(argvalue);
		DOFREE(arg);
	}

	vector<char *> referenceValues;

	if (strstr(arg1value, ",") != NULL)
	{
		int vectorValueCount = 0;
		char ** vectorValueChar = new char *[64];
		split(arg1value, ",", vectorValueChar, &vectorValueCount);
		for (int i = 0; i < vectorValueCount; i++)
		{
			referenceValues.push_back(vectorValueChar[i]);
		}
	}
	else
	{
		referenceValues.push_back(arg1value);
	}
	char resultString[1024] = "";

	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 ((strstr(itemType, "Revision") == NULL) || (strstr(itemType, "Master") != NULL)
			|| (strstr(itemType, "master") != NULL) || (strstr(itemType, "BOM") != NULL) || (strstr(itemType, "bom") != NULL) || (strstr(itemType, "Bom") != NULL))
		{
			DOFREE(itemType);
			continue;
		}

		//获取当前节点的人
		int valid_signoffs_count = 0;
		tag_t * valid_signoffs_tags = NULL;
		ITKCALL(AOM_ask_value_tags(msg.task, "valid_signoffs", &valid_signoffs_count, &valid_signoffs_tags));
		if (valid_signoffs_count != 0) {

			tag_t assign_user = NULL_TAG;
			tag_t assign_goup_mem = NULL_TAG;
			tag_t o_group_tag = NULL_TAG;
			//通知人
			AOM_ask_value_tag(valid_signoffs_tags[0], "fnd0Assignee", &assign_user);

			AOM_ask_value_tag(valid_signoffs_tags[0], "group_member", &assign_goup_mem);
			if (assign_goup_mem != NULL_TAG) {
				
				AOM_ask_value_tag(assign_goup_mem, "group", &o_group_tag);

			}


			tag_t item = NULLTAG;
			//获取对象
			ITEM_ask_item_of_rev(attachments[i], &item);
			//更改对象所有者
			POM_AM__set_application_bypass(true);
			//先lock
			AOM_lock(item);
			//更改流程所有者
			AOM_set_ownership(item, assign_user, o_group_tag);
			//再save
			AOM_save(item);
			//最后unlock
			AOM_unlock(item);
			AOM_refresh(item, FALSE);

			//先lock
			AOM_lock(attachments[i]);
			//更改流程所有者
			AOM_set_ownership(attachments[i], assign_user, o_group_tag);
			//再save
			AOM_save(attachments[i]);
			//最后unlock
			AOM_unlock(attachments[i]);
			AOM_refresh(attachments[i], FALSE);

			//更改数据集的所有者
			for (auto referencedValue : referenceValues) {
				tag_t relation_tag = NULLTAG;
				//获取关系
				GRM_find_relation_type(referencedValue, &relation_tag);
				if (relation_tag != NULLTAG) {
					int rfCount = 0;
					tag_t * rfTags = NULLTAG;
					GRM_list_secondary_objects_only(attachments[i], relation_tag, &rfCount, &rfTags);
					for (int j = 0; j < rfCount; j++)
					{
						//先lock
						AOM_lock(rfTags[j]);
						//更改流程所有者
						AOM_set_ownership(rfTags[j], assign_user, o_group_tag);
						//再save
						AOM_save(rfTags[j]);
						//最后unlock
						AOM_unlock(rfTags[j]);
						AOM_refresh(rfTags[j], FALSE);
					}
					DOFREE(itemType);
				}
			}

			tag_t home_folder = NULLTAG;
			//将对象添加到home下
			SA_ask_user_home_folder(assign_user, &home_folder);//获取用户HOME文件夹
			FL_insert(home_folder, item, 999);
			AOM_save_with_extensions(home_folder);
			AOM_refresh(home_folder, TRUE);

			POM_AM__set_application_bypass(false);

		}
		DOFREE(valid_signoffs_tags);
		DOFREE(itemType);
	}
	referenceValues.clear();
	vector<char *>().swap(referenceValues);
	DOFREE(attachments);

	auto stopTime = std::chrono::high_resolution_clock::now();
	auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stopTime - startTime);
	//std::cout << "ML_ChangOwning用时:" << duration.count() / 1000 << std::endl;
	string usetime = "ML_ChangOwning用时:";
	usetime.append(std::to_string(duration.count() / 1000));
	WriteLog(true, usetime.c_str());
	printf("=========================更改对象所有者 Start===================\n");
	return ifail;
}