#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 <regex>
#include "string_utils.h"
#include <list>   
#include "tc_log.h"
#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 <tccore/grm.h>
#include "epm/epm.h"

//#include "libxl.h"
#include <iostream>
#include <regex>
using namespace std;
typedef struct
{
	string ITEM_ID;//id号
}PRO_STRUCT;


int BOMDiscontinuation(EPM_rule_message_t msg)
{
	cout<<"检查BOM能否发起物料停用流程"<<endl;
	int ifail = ITK_ok;
	EPM_decision_t decision = EPM_go;
	tag_t task_tag = NULLTAG, rootTask_tag = NULLTAG, *attachments = NULLTAG,*revision_list=NULLTAG,bom_window_tag=NULLTAG,top_line_tag=NULLTAG,*c_line_tags=NULLTAG;
	int  arg_cnt = 0, i = 0, att_cnt = 0,c_line_count=0,item_num=0;
	int release_nums=0,revtion_nums=0;
	tag_t *release_list=NULLTAG;
	tag_t *item=NULLTAG;
	char* object_type;
	//获取当前触发的任务
	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);
	//循环目标对象
	cout<<"数量------->"<<att_cnt<<endl;
	vector<PRO_STRUCT>	item_vec;
	for (i = 0; i < att_cnt; i++)
	{
		//获得该bom下所有子集
		ITKCALL(ifail=AOM_ask_value_string(attachments[i],"object_type",&object_type));
		if(strcmp(object_type,"BOMView Revision")==0)
		{
			ITKCALL(	ifail = BOM_create_window(&bom_window_tag));//创建window视图
			ITKCALL(ifail = BOM_set_window_top_line_bvr(bom_window_tag,attachments[i],&top_line_tag));//获取顶层bomline
			ITKCALL(ifail =BOM_line_ask_all_child_lines(top_line_tag,&c_line_count,&c_line_tags));
			for (int j=0;j<c_line_count;j++)
			{
				//获得所在版本的物料
				ITKCALL(ifail=AOM_ask_value_tags(c_line_tags[j],"bl_child_item",&item_num,&item));
				ITKCALL(ifail=AOM_ask_value_string(item[0],"object_type",&object_type));
				if(strcmp(object_type,"JK8Material")==0)//LP2_Design_Part  JK8Material
				{
					ITKCALL(	ifail=AOM_ask_value_tags(item[0],"revision_list",&revtion_nums,&revision_list));
					int mark=0;
					for (int k=0;k<revtion_nums;k++)
					{
						PRO_STRUCT elements ;
						ITKCALL(	ifail=AOM_ask_value_tags(revision_list[k],"release_status_list",&release_nums,&release_list));
						//TODO 如果存在object_name 为jk8Disabled即为失效状态的物料，就提示
						for (int f=0;f<release_nums;f++)
						{
							AOM_ask_value_string(release_list[f],"object_name",&object_type);
							if(strcmp(object_type,"jk8Disabled")==0)
							{
								ITKCALL(	ifail=AOM_ask_value_string(item[0],"item_id",&object_type));
								elements.ITEM_ID.assign(object_type);
								item_vec.push_back(elements);
								mark=1;
								break;
								
							}
						}
						if(mark==1)
						{
							mark=0;
							break;
						}
//						if(release_nums>0)
//						{
//							ITKCALL(	ifail=AOM_ask_value_string(item[0],"item_id",&object_type));
//							elements.ITEM_ID.assign(object_type);
//							item_vec.push_back(elements);
//							break;
//						}
					}
				}
			}
				ITKCALL(ifail=BOM_close_window(bom_window_tag));
		}


	}
	cout<<"item_vec.size()--------------"<<item_vec.size()<<endl;
	if(item_vec.size()>0)
	{
		for (int i=0;i<item_vec.size();i++)
		{
			string error_string="\nBOM子阶存在失效物料";
			for (int i=0;i<item_vec.size();i++)
			{
				error_string=error_string+item_vec[i].ITEM_ID.c_str()+";";
			}
			EMH_store_error_s1(EMH_severity_warning, EMH_AE_error_base, error_string.c_str());
			if (attachments != NULL)
			{
				MEM_free(attachments);
				attachments = NULL;
			}
			if (revision_list != NULL)
			{
				MEM_free(revision_list);
				revision_list = NULL;
			}
			if (c_line_tags != NULL)
			{
				MEM_free(c_line_tags);
				c_line_tags = NULL;
			}
			if (c_line_tags != NULL)
			{
				MEM_free(c_line_tags);
				c_line_tags = NULL;
			}	
			if (release_list != NULL)
			{
				MEM_free(release_list);
				release_list = NULL;
			}	
			if (object_type != NULL)
			{
				MEM_free(object_type);
				object_type = NULL;
			}	
			return EPM_nogo;
		}
	}else
	{
		if (attachments != NULL)
		{
			MEM_free(attachments);
			attachments = NULL;
		}
		if (revision_list != NULL)
		{
			MEM_free(revision_list);
			revision_list = NULL;
		}
		if (c_line_tags != NULL)
		{
			MEM_free(c_line_tags);
			c_line_tags = NULL;
		}
		if (c_line_tags != NULL)
		{
			MEM_free(c_line_tags);
			c_line_tags = NULL;
		}	
		if (release_list != NULL)
		{
			MEM_free(release_list);
			release_list = NULL;
		}	
		if (object_type != NULL)
		{
			MEM_free(object_type);
			object_type = NULL;
		}	
		return EPM_go;	
	}


	return EPM_go;	


	
}