/*==============================================================================
					Copyright (c) 2008-2018 Connor Corporation
					Unpublished - All Rights Reserved
==============================================================================*/
#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"

int ML_ElevateTask(EPM_action_message_t msg)
{
	printf("=========================提升流程节点 Start===================\n");
	auto startTime = std::chrono::high_resolution_clock::now();
	int ifail = ITK_ok, att_cnt = 0, i = 0,  arg_cnt = 0;
	tag_t task_tag = NULL_TAG, rootTask_tag = NULL_TAG;
	task_tag = msg.task;
	if (task_tag == NULLTAG)
	{
		return -1;
	}
	ITKCALL(EPM_ask_root_task(task_tag, &rootTask_tag));
	char * taskname=NULL,*type=NULL;
	ITKCALL(EPM_ask_name2(task_tag,&taskname));
	ITKCALL(AOM_ask_value_string(task_tag,"object_type",&type));
	printf("name=%s\n",taskname);
	printf("type=%s\n",type);
	if (strcmp(type,"EPMReviewTask")==0)
	{
		int num=0;
		tag_t *sub_tags=NULL;
		ITKCALL(EPM_ask_sub_tasks(task_tag,&num,&sub_tags));
		for (int j = 0; j < num; j++)
		{
			char * taskname2=NULL,*type2=NULL;
			ITKCALL(EPM_ask_name2(sub_tags[j],&taskname2));
			ITKCALL(AOM_ask_value_string(sub_tags[j],"object_type",&type2));
			if (strcmp(type2,"EPMPerformSignoffTask")==0)
			{
				printf("name2=%s,type2=%s\n",taskname2,type2);
				int valid_num=0;
				tag_t *valid_tags=NULL;
				ITKCALL(AOM_ask_value_tags(sub_tags[j],"valid_signoffs",&valid_num,&valid_tags));
				printf("valid_num=%d\n",valid_num);
				if (valid_num==0)
				{
					printf("未选人执行提升\n");
					POM_AM__set_application_bypass(true);
					ITKCALL(EPM_promote_task(task_tag,""));
					POM_AM__set_application_bypass(false);
				}
				DOFREE(valid_tags);

			}
			DOFREE(taskname2);
			DOFREE(type2);
			
		}
		DOFREE(sub_tags);

	}
	DOFREE(taskname);
	DOFREE(type);

	auto stopTime = std::chrono::high_resolution_clock::now();
	auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stopTime - startTime);
	//std::cout << "ML_ElevateTask用时:" << duration.count() / 1000 << std::endl;
	string usetime = "ML_ElevateTask用时:";
	usetime.append(std::to_string(duration.count() / 1000));
	WriteLog(true, usetime.c_str());
	printf("=========================提升流程节点 End===================\n");
	return ITK_ok;
}
