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

#include <tc/tc.h>
#include "epm_handler_common.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 <stdlib.h>
#include <tccore/aom.h>
#include <tc/emh.h>
#include <ict/ict_userservice.h>
#include <tc/tc.h>
#include <tccore/tctype.h>
#include <sa/tcfile.h>
#include <ss/ss_errors.h>
#include <string.h>
#include <time.h>
#include <tccore/project.h>

#include "tchar.h"
#include <ics/ics.h>
#include "common_itk_util.h"
using namespace libxl;
extern "C" int POM_AM__set_application_bypass(logical bypass);
int loop(tag_t top_bom_line_tag,map<tag_t,string> &rev_map)
{
	int ifail=0;
	int child_count = 0;
	tag_t * child_tags = NULL;
	tag_t  child_rev_tag=NULL;
	char * child_item_id = NULL;
	//		date_t p;//创建日期

	ITKCALL(ifail =  BOM_line_ask_all_child_lines(top_bom_line_tag,&child_count,&child_tags));
	for(int x=0;x<child_count;x++){
		tag_t child_tag=child_tags[x];
		ITKCALL(ifail = AOM_ask_value_tag(child_tag,"bl_line_object",&child_rev_tag));
		AOM_ask_value_string(child_rev_tag,"item_id",&child_item_id);
		string child_item_id_string=child_item_id;
		if(child_item_id_string.length()==12)
		{
			//并且未发布
			int is_released=0;
			EPM_ask_if_released(child_rev_tag,&is_released);
			if(is_released==0)
			{
				rev_map.insert(pair<tag_t,string>(child_rev_tag,""));
				//把BOM视图也加进来
				int num=0;
				tag_t *bomTags=NULLTAG;
				AOM_ask_value_tags(child_rev_tag,"structure_revisions",&num,&bomTags);
				if(num>0)
				{
					rev_map.insert(pair<tag_t,string>(bomTags[0],""));
					MEM_free(bomTags);
					bomTags=NULL;
				
				}
				
			}
			
		}
		loop(child_tag,rev_map);
	}
	if(child_tags!=NULL)
	{
		MEM_free(child_tags);
		child_tags=NULL;
	}
	if(child_item_id!=NULL)
	{
		MEM_free(child_item_id);
		child_item_id=NULL;
	}
	return 0;
}
int jk_add_attatchments(EPM_action_message_t msg)
{
	cout<<"把成品下面新生成的基板组件和控制器组件及其BOM视图带入流程中"<<endl;
	int ifail = ITK_ok,   att_cnt = 0;
	tag_t task_tag = NULLTAG, rootTask_tag = NULLTAG, *attachments = NULL,type_tag=NULLTAG;
	char type_class[TCTYPE_class_name_size_c+1]="";
	string value_type = "";
	string value_property="";
	char *item_id=NULL;
	map<tag_t,string> rev_map;
	map<tag_t,string>   bom_map;
	string errorMessage="";
	//获取当前触发的任务
	task_tag = msg.task;
	//获取根流程节点
	ifail = EPM_ask_root_task(task_tag, &rootTask_tag);
	//获取目标引用对象
	ifail = EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &att_cnt, &attachments);
	for(int i = 0; i < att_cnt; i++ )
	{
		//获得对象类型的系统方法	
		ITKCALL(TCTYPE_ask_object_type(attachments[i], &type_tag));
			ITKCALL(ifail = TCTYPE_ask_class_name(type_tag, type_class));
			ECHO("type_class : %s\n",type_class);	
			
			//必须为版本或者是bom，为什么把版本加到集合中？
			if(((strstr(type_class,"Revision") == NULL) || (strstr(type_class,"revision") == NULL))
				&&(strstr(type_class,"Master") == NULL) &&(strstr(type_class,"master") == NULL)
				&& ((strstr(type_class,"BOM") !=NULL) || (strstr(type_class,"bom") !=NULL) || (strstr(type_class,"Bom") != NULL))){
				bom_map.insert(pair<tag_t,string>(attachments[i],""));
		}
	}

	//遍历BOM结构
	map<tag_t,string>::iterator bom_it;
	for( bom_it = bom_map.begin(); bom_it != bom_map.end(); bom_it++ )
	{
		
		tag_t bom_window_tag = NULLTAG,top_bom_line_tag =NULLTAG,rev_tag = NULLTAG;
		ITKCALL(ifail = BOM_create_window(&bom_window_tag));
		ITKCALL(ifail = BOM_set_window_top_line_bvr(bom_window_tag,bom_it->first,&top_bom_line_tag));
		
		if(top_bom_line_tag != NULLTAG){
			/*ITKCALL(ifail = AOM_ask_value_tag(top_bom_line_tag,"bl_line_object",&rev_tag));
			//判断一下顶层有没有发布，没发布放到map集合中
			AOM_ask_value_string(rev_tag,"item_id",&item_id);
			string item_id_string=item_id;
			if(item_id_string.length()==12)
			{
				rev_map.insert(pair<tag_t,string>(rev_tag,""));//把顶层放到集合中
			}*/
			loop(top_bom_line_tag,rev_map);
		}
		ITKCALL(ifail = BOM_save_window(bom_window_tag));//要保存，否则修改属性后bom视图不会保存
		ITKCALL(ifail = BOM_close_window(bom_window_tag));//有开必有关


	}
	//将map集合中的所有版本带入到流程目标下
	vector<tag_t> target_vec;
		map<tag_t,string>::iterator rev_it;
	for( rev_it = rev_map.begin(); rev_it != rev_map.end(); rev_it++ )
	{
		target_vec.push_back(rev_it->first);
	}
	int *att_types=NULL;
	att_types = (int*) MEM_alloc (sizeof(int) * ( target_vec.size() ));
	attachments = (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;
		attachments[indx] = target_vec[indx];
	}
	//把所有的版本都添加到流程中
	printf("1ifail=>%d\n",ifail);
	ITKCALL(ifail=EPM_add_attachments(rootTask_tag,target_vec.size(),attachments,att_types));
	if(att_types)										
	{											
		MEM_free(att_types);							
		att_types = NULL;								
	}
	if(item_id!=NULL)
	{
		MEM_free(item_id);
		item_id=NULL;
	}
	if(attachments!=NULL)
	{
		MEM_free(attachments);
		attachments=NULL;
	}
	return 0;
}