#include "epm_handler_common.h"
#include "jk_custom_erp.h"
#include <tc/tc.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 <map>
#include "epm/signoff.h"
#include <ctime>
#include <vector>
#include <locale>
#include <codecvt>
#include "ae/dataset.h"
#include <server_exits/user_server_exits.h>
#include <tccore/custom.h>
#include <epm/epm.h>
#include <epm/epm_toolkit_tc_utils.h>
#include <ict/ict_userservice.h>
#include <tccore/item.h>
#include <ae/ae.h>
#include <tc/folder.h>
#include <tccore/aom.h>
#include <sa/sa.h>
#include <tccore/aom_prop.h>
#include <property/prop_errors.h>
#include <tccore/workspaceobject.h>
#include <tc/preferences.h>
#include <tccore/imantype.h>
#include <tccore//grm.h>
#include <tccore/grmtype.h>
#include <sa/am.h>
#include <cfm/cfm.h>
#include <bom/bom.h>
#include <tccore/uom.h>
#include <ps/ps.h>
#include <epm/signoff.h>
#include <fclasses/tc_date.h>
#include <form/form.h>
#include <form/formtype.h>
#include <ics\ics.h>
#include <stdarg.h>
#include <iostream>
# include "common_itk_util.h"
#include "tx_erp_bom.h"
using namespace std;
extern "C" int POM_AM__set_application_bypass(logical bypass);
int  tx_addDisabled(EPM_action_message_t msg)
{
	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;
	tag_t release_stat = NULLTAG;
	vector<tag_t> attatcgments;
	string dateType="dis";
	ITKCALL(ifail=RELSTAT_create_release_status("tx2Disabled", &release_stat));//TCM Released  jk8Disabled
		//获取当前触发的任务
	task_tag = msg.task;
	//获取根流程节点
	ITKCALL(ifail = EPM_ask_root_task(task_tag, &rootTask_tag));
	//获取目标引用对象
	ITKCALL(ifail = EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &att_cnt, &attachments));
	//循环目标对象
	cout<<"数量------->"<<att_cnt<<endl;
	for (i = 0; i < att_cnt; i++)
	{
		//获得该物料下所有版本（版本revision_list）
		ITKCALL(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)
		{
			//TODO 给物料添加失效状态和失效时间
				
			ITKCALL(ifail=RELSTAT_add_release_status(release_stat, 1, &attachments[i], true));
			ITKCALL(ifail=AOM_ask_value_tags(attachments[i],"revision_list",&revtion_nums,&revision_list));
				
			date_t current_date;
			char create_date_str[32] = "\0";
			current_time(&current_date);
			sprintf(create_date_str,"%04d-%02d-%02d",current_date.year,1+ current_date.month,current_date.day);
			cout<<"create_date_str-------------------"<<create_date_str;
			string ddd=create_date_str;
			ITKCALL(ifail=AOM_ask_value_string(attachments[i],"object_string",&object_type));
			cout<<"attachments[i]--------------"<<object_type;
			POM_AM__set_application_bypass(true);
			AOM_lock(attachments[i]);
			ITKCALL(ifail=AOM_set_value_string(attachments[i],"tx2DisabledDate",ddd.c_str()));//jk8DisabledDate TCM Released
			AOM_save(attachments[i]);
			AOM_unlock(attachments[i]);
			POM_AM__set_application_bypass(false); 
			for (int j=0;j<revtion_nums;j++)
			{
				//给每一个版本添加失效状态
				POM_AM__set_application_bypass(true);
				ITKCALL(ifail=RELSTAT_add_release_status(release_stat, 1, &revision_list[j], true));
				POM_AM__set_application_bypass(false);
			if(j==revtion_nums-1)
			{
				attatcgments.push_back(revision_list[j]);//把最新版本加到版本集合中
			}
			}
		}
	

	}
		//TODO 把最新版本的属性信息发送到U8，附带失效时间
		erp(msg.task, attatcgments);
//				JK_send_material1(attatcgments,dateType);


	return ifail;
}