#pragma warning (disable: 4996) 
#pragma warning (disable: 4819) 
#pragma warning (disable: 4995) 

#include <tc/tc.h>
#include "epm_handler_common.h"
#include <epm/epm_task_template_itk.h>
#include <ict/ict_userservice.h>
#include <tccore/custom.h>
#include <epm/epm_toolkit_tc_utils.h>
#include <tccore/aom.h>
#include <tccore/aom_prop.h>
#include <tccore/item.h>
#include <bom/bom.h>
#include "ps/ps.h";
#include "ps/vrule.h"
#include "sstream"
#include <tccore/grm.h>
#include "epm/epm.h"
#include "sa/sa.h"
#include "libxl.h"
#include <map>
#include "epm/signoff.h"
#include <ctime>
#include <vector>
#include <locale>
#include <codecvt>
#include "ae/dataset.h"
#include <iostream>
#include "error_handling.h"

extern "C" int POM_AM__set_application_bypass(logical bypass);
int AutomaticRelease(EPM_action_message_t msg)
{
	cout<<"关联零组件自动发布"<<endl;
	int ifail = ITK_ok,  i = 0, att_cnt = 0;
	tag_t task_tag = NULLTAG, rootTask_tag = NULLTAG, *attachments = NULL,*attachments2 = NULL;;
//	tag_t *release_status=NULL;
	int rs_num=0;
//	char *value=NULL;
	char * object_type=NULL;
	char * object_type2=NULL;
//	tag_t *targets=NULLTAG;
	task_tag = msg.task;
	int num1=0;
	int num2=0;
	tag_t * primary=NULLTAG;
	tag_t * second=NULLTAG;
	tag_t * only=NULLTAG; int num3=0;
	tag_t * final=NULLTAG;
	//获取根流程节点
	vector<tag_t>	target_vec;
	vector<tag_t>	target_vec2;//存放 OA变更附件下的资料
	ITKCALL(	ifail = EPM_ask_root_task(task_tag, &rootTask_tag));
	//获取目标引用对象
	ITKCALL(	ifail = EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &att_cnt, &attachments));
	//循环目标对象	
	for (int i=0;i<att_cnt;i++)
	{
		//DL2_Part
		ITKCALL(ifail=AOM_ask_value_string(attachments[i],"object_type",&object_type));

		if(strcmp(object_type,"DL2_PartRevision")==0)//TODO  限定发起流程的版本类型
		{
//			target_vec.push_back(attachments[i]);
			//TODO 将对象版本下伪文件夹关联更改零部件（DL2_Achangeparts）下所有零部件版本及版本下资料带入流程
			ITKCALL(ifail=AOM_ask_value_tags(attachments[i],"DL2_Achangeparts",&num1,&primary));
			for (int j=0;j<num1;j++)
			{
				//TODO 如果是版本就允许带入流程,过滤掉非版本对象
				ITKCALL(ifail = AOM_ask_value_string(primary[j], "object_type",&object_type2));
				if (((strstr(object_type2,"Revision") != NULL) || (strstr(object_type2,"revision") != NULL))
					&&(strstr(object_type2,"Master") == NULL) &&(strstr(object_type2,"master") == NULL)
					&& (strstr(object_type2,"BOM") ==NULL) && (strstr(object_type2,"bom") ==NULL) && (strstr(object_type2,"Bom") == NULL))	
				{
					target_vec.push_back(primary[j]);
					ITKCALL(ifail=AOM_ask_value_tags(primary[j],"IMAN_specification",&num2,&second));
					for (int k=0;k<num2;k++)
					{
						target_vec.push_back(second[k]);
					}
					ITKCALL(ifail=AOM_ask_value_tags(primary[j],"TC_Attaches",&num2,&second));
					for (int k=0;k<num2;k++)
					{
						target_vec.push_back(second[k]);
					}
				}

			}
//			//TODO 获得伪文件夹OA变更附件（DL2_CMAttachment）下的资料
//			ITKCALL(ifail=AOM_ask_value_tags(attachments[i],"DL2_CMAttachment",&num1,&primary));
//			for (int j=0;j<num1;j++)
//			{
//				target_vec2.push_back(primary[j]);
//
//			}
		}
	}

	//TODO 把版本伪文件夹下的版本以及版本下的所有资料带入流程
	int *att_types=NULL;
	att_types = (int*) MEM_alloc (sizeof(int) * ( target_vec.size() ));
	attachments2 = (tag_t*) MEM_alloc (sizeof(tag_t) * ( target_vec.size() ));
	//制定目标，数组转换
	for ( int indx = 0; indx < target_vec.size(); indx++ )
	{
		att_types[indx] = EPM_target_attachment;//EPM_reference_attachment;
		attachments2[indx] = target_vec[indx];
	}
	//把所有的版本都添加到流程中
	ITKCALL(EPM_add_attachments(rootTask_tag,target_vec.size(),attachments2,att_types));

	//TODO 把版本伪文件夹OA变更附件（DL2_CMAttachment）下的资料的版本复制到另一对象的OA变更附件（DL2_CMAttachment）伪文件夹下
	cout<<"target_vec.size()-------------"<<target_vec.size()<<endl;
	for (int i=0;i<att_cnt;i++)
	{
		ITKCALL(ifail=AOM_ask_value_string(attachments[i],"object_string",&object_type));
		cout<<"object_string------------"<<object_type<<endl;
		ITKCALL(ifail=AOM_ask_value_string(attachments[i],"object_type",&object_type));
		if(strcmp(object_type,"DL2_PartRevision")==0)//TODO 限定文件类型
		{
			//TODO 获得伪文件夹OA变更附件（DL2_CMAttachment）下的资料
			target_vec2.clear();//清空
			ITKCALL(ifail=AOM_ask_value_tags(attachments[i],"DL2_CMAttachment",&num1,&primary));
			for (int j=0;j<num1;j++)
			{
				target_vec2.push_back(primary[j]);

			}

			//TODO 获得版本下伪文件夹关联更改零部件（DL2_Achangeparts）下所有零部件版本，再获得零部件的伪文件夹OA变更附件（DL2_CMAttachment）

			ITKCALL(ifail=AOM_ask_value_tags(attachments[i],"DL2_Achangeparts",&num2,&second));
			cout<<"--------关联更改零部件数量---------"<<num2<<endl;
			for (int j=0;j<num2;j++)
			{
				ITKCALL(ifail=AOM_ask_value_tags(second[j],"DL2_CMAttachment",&num3,&only));
				cout<<"--------OA变更附件数量---------"<<num3<<endl;

				//把两个数组加起来target_vec2和only
				final= (tag_t*) MEM_alloc (sizeof(tag_t) * ( target_vec2.size()+num3 ));
				for (int n=0;n<num3;n++)
				{
					final[n]=only[n];
						ITKCALL(ifail = AOM_ask_value_string(final[n], "object_string",&object_type2));
					cout<<"变更附件对象------------"<<object_type2<<endl;
				}
				for (int n=num3;n< target_vec2.size()+num3 ;n++)
				{
					final[n]=target_vec2[n-num3];
				ITKCALL(ifail = AOM_ask_value_string(final[n], "object_string",&object_type2));
					cout<<"变更附件对象------------"<<object_type2<<endl;
				}
				//设置值
				cout<<"设置值----------"<<endl;
				ITKCALL(ifail=AOM_lock(second[j]));
				POM_AM__set_application_bypass(true);
				AOM_set_value_tags(second[j],"DL2_CMAttachment", target_vec2.size()+num3,final);
				AOM_save(second[j]);
				POM_AM__set_application_bypass(false);
				ITKCALL(ifail=AOM_unlock(second[j]));
			}

			
			
		}
		if (final != NULL)
		{
			MEM_free(final);
			final = NULL;
		}
		if (attachments2 != NULL)
	{
		MEM_free(attachments2);
		attachments2 = NULL;
	}
	}
	if (attachments != NULL)
	{
		MEM_free(attachments);
		attachments = NULL;
	}
	
	if (object_type != NULL)
	{
		MEM_free(object_type);
		object_type = NULL;
	}
	if (object_type2 != NULL)
	{
		MEM_free(object_type2);
		object_type2 = NULL;
	}
	if (primary != NULL)
	{
		MEM_free(primary);
		primary = NULL;
	}
	if (second != NULL)
	{
		MEM_free(second);
		second = NULL;
	}
	if (only != NULL)
	{
		MEM_free(only);
		only = NULL;
	}
	

	return 0;
}