#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号
}TX_PRO_STRUCT;
int tx_checkMaterialDisabled(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<<"数量------->"<<att_cnt<<endl;
	vector<TX_PRO_STRUCT>	item_vec;
	for (i = 0; i < att_cnt; i++)
	{
		//获得该物料下所有版本（版本revision_list）不是判断版本有没有失效状态，而是判断物料有没有失效状态
		ifail=AOM_ask_value_string(attachments[i],"object_type",&object_type);
		if(strcmp("TX2RawMaterial",object_type)==0||strcmp("TX2SemiProduct",object_type)==0||strcmp("TX2Component",object_type)==0||strcmp("TX2FinalProduct",object_type)==0)
		{
			tag_t rev=attachments[i];


			ifail=AOM_ask_value_tags(rev,"release_status_list",&release_nums,&release_list);	
			if(release_nums==0)
			{
				TX_PRO_STRUCT elements ;
					ifail=AOM_ask_value_string(attachments[i],"item_id",&object_type);
					elements.ITEM_ID.assign(object_type);
					item_vec.push_back(elements);
			}
			for (int k=0;k<release_nums;k++)
			{
				ifail=AOM_ask_value_string(release_list[k],"object_name",&object_type);
				if(strcmp(object_type,"tx2Disabled")==0)
				{
					break;
				}else
				{
					TX_PRO_STRUCT elements ;
					ifail=AOM_ask_value_string(attachments[i],"item_id",&object_type);
					elements.ITEM_ID.assign(object_type);
					item_vec.push_back(elements);
				}
				
			}

			

		}
		if (revision_list != NULL)
			{
				MEM_free(revision_list);
				revision_list = NULL;
			}

	}
	
	if(item_vec.size()>0)
	{
		string error_string="\n";
		for (int i=0;i<item_vec.size();i++)
		{
			error_string=error_string+item_vec[i].ITEM_ID.c_str()+";";
		}
		error_string=error_string+"物料不是失效物料，不允许做启用操作！";

		EMH_store_error_s1(EMH_severity_warning, EMH_AE_error_base, error_string.c_str());
		if (attachments != NULL)
			{
				MEM_free(attachments);
				attachments = 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;
	}


	return EPM_go;
}
