#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号
	string ITEM_REVISION_ID;//类型
}PRO_STRUCT1;
//物料停用逻辑：对选中的物料发起流程，检查其下所有的版本是否发布，如果有未发布的版本，就阻止进入流程，并且提示信息
//注意：可能对多个物料发起流程
int MaterialDiscontinuation(EPM_rule_message_t msg)
{
	cout<<"检查能否发起物料停用流程"<<endl;
	int ifail = ITK_ok;
	EPM_decision_t decision = EPM_go;
	tag_t task_tag = NULLTAG, rootTask_tag = NULLTAG, *attachments = NULLTAG,*revision_list=NULLTAG;
	int  arg_cnt = 0, i = 0, att_cnt = 0,revtion_nums=0;
	int release_nums=0;
	tag_t *release_list=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<<"数量dd------->"<<att_cnt<<endl;
	vector<PRO_STRUCT1>	item_vec;
	for (i = 0; i < att_cnt; i++)
	{
		//获得该物料下所有版本（版本revision_list）
		ITKCALL(ifail=AOM_ask_value_string(attachments[i],"object_type",&object_type));
		if(strcmp("JK8Material",object_type)==0)//LP2_Design_Part
		{
			ITKCALL(	ifail=AOM_ask_value_tags(attachments[i],"revision_list",&revtion_nums,&revision_list));
			for (int j=0;j<revtion_nums;j++)
			{
				PRO_STRUCT1 elements ;
				ITKCALL(	ifail=	AOM_ask_value_tags(revision_list[j],"release_status_list",&release_nums,&release_list));
				ITKCALL(ifail=AOM_ask_value_string(revision_list[j],"object_string",&object_type));
				cout<<"object_string--------------"<<object_type<<endl;
				cout<<"release_nums--------------"<<release_nums<<endl;
				if(release_nums==0)
				{
					ITKCALL(ifail=AOM_ask_value_string(attachments[i],"item_id",&object_type));
					cout<<"item id-----------------------"<<object_type<<endl;
					elements.ITEM_ID.assign(object_type);
					ITKCALL(ifail=AOM_ask_value_string(revision_list[j],"item_revision_id",&object_type));
					cout<<"item_revision_id--------------"<<object_type<<endl;
					elements.ITEM_REVISION_ID.assign(object_type);
					item_vec.push_back(elements);
				}
			}
		}


	}
	
	cout<<"item_vec.size()---------------"<<item_vec.size()<<endl;
	if(item_vec.size()>0)
	{
		cout<<"111111111111"<<endl;
		string error_string="\n";
		for (int i=0;i<item_vec.size();i++)
		{
//			cout<<"i----------"<<i<<endl;
//			cout<<"item_vec[i].ITEM_ID-------------"<<item_vec[i].ITEM_REVISION_ID.c_str()<<endl;
//			cout<<"item_vec[i].ITEM_REVISION_ID-------------"<<item_vec[i].ITEM_REVISION_ID.c_str()<<endl;
			error_string=error_string+item_vec[i].ITEM_ID.c_str()+"物料"+item_vec[i].ITEM_REVISION_ID.c_str()+"版本,";
		}
		error_string=error_string+"未发布，不允许做停用操作！";
		cout<<"error_string--------"<<error_string.c_str()<<endl;
		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 (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(item_vec.size()==0)
	{
		if (attachments != NULL)
	{
		MEM_free(attachments);
		attachments = NULL;
	}	
	if (revision_list != NULL)
	{
		MEM_free(revision_list);
		revision_list = 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;

	}
}
