#include "epm_handler_common.h"
#include "jk_custom_erp.h"
#include "jk_custom.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>
extern "C" int POM_AM__set_application_bypass(logical bypass);
using namespace std;


int  removeDisabled(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;
	string  dateType="ena";
	vector<tag_t> attatchments;
	vector<tag_t> target_vec;

//	ITKCALL(ifail=RELSTAT_create_release_status("jk8Disabled", &release_stat));
		//获取当前触发的任务
	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<<"数量--qq----->"<<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("JK8Material",object_type)==0)//LP2_Design_Part  JK8Material
		{
			//TODO 修改启用时间
		/*	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);
			
			AOM_load(attachments[i]);
			AOM_lock(attachments[i]);
			AOM_refresh(attachments[i],0);
			POM_AM__set_application_bypass(true);
			ITKCALL(ifail=AOM_set_value_string(attachments[i],"jk8EnableDate",create_date_str));//object_name  jk8EnableDate
			POM_AM__set_application_bypass(false); 
			AOM_save(attachments[i]);
			AOM_refresh(attachments[i],0);
			AOM_unlock(attachments[i]);
			*/


			ITKCALL(ifail=AOM_ask_value_tags(attachments[i],"revision_list",&revtion_nums,&revision_list));
			for (int j=0;j<revtion_nums;j++)
			{
//				ifail=RELSTAT_add_release_status(release_stat, 1, &revision_list[j], true);
				target_vec.push_back(revision_list[j]);
				if(j==revtion_nums-1)
				{
					attatchments.push_back(revision_list[j]);
				}
			}
		}
	

	}
	//删除物料和版本的状态
	POM_AM__set_application_bypass(true);
	//	removeReleaseStatus("删除状态流程",target_vec);
	cout<<"mark1"<<endl;
	removeReleaseStatusByName("jk8Disabled",rootTask_tag,target_vec);//jk8Disabled
	cout<<"mark2"<<endl;
	removeRevFromTask(rootTask_tag,target_vec);
	cout<<"mark3"<<endl;
	POM_AM__set_application_bypass(false);
	cout<<"mark4"<<endl;
	//发送最新版本的信息到U8

	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  JK8Material
		{
			//TODO 修改启用时间
			date_t current_date;
			tag_t prop_tag=NULLTAG;
			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);
			
			ITKCALL(AOM_load(attachments[i]));
			ITKCALL(AOM_lock(attachments[i]));
			ITKCALL(AOM_refresh(attachments[i],1));
			POM_AM__set_application_bypass(true);
			ITKCALL(PROP_ask_property_by_name(attachments[i],"jk8EnableDate",&prop_tag));
			ITKCALL(PROP_set_value_string(prop_tag,create_date_str));
			
			//POM_set_attr_string(1,)
			//ITKCALL(ifail(attachments[i],"jk8EnableDate",create_date_str));//object_name  jk8EnableDate
			POM_AM__set_application_bypass(false); 
			AOM_save(attachments[i]);
			AOM_refresh(attachments[i],0);
			AOM_unlock(attachments[i]);
		}
	}
	JK_send_material1(attatchments,dateType);

	//
	if (attachments != NULL)
	{
		MEM_free(attachments);
		attachments = NULL;
	}
	if (revision_list != NULL)
	{
		MEM_free(revision_list);
		revision_list = NULL;
	}
	
	if (object_type != NULL)
	{
		MEM_free(object_type);
		object_type = NULL;
	}
	cout<<"mark5"<<endl;
	printf("ifail=>%d\n",ifail);
	return 0;
}

void removeReleaseStatusByName(string statusName,tag_t rootTask,vector<tag_t> revVec){
	tag_t release_status =NULLTAG;
	tag_t * attachments = NULL;
	int *att_types=NULL;
	int ifail = 0;
	att_types = (int*) MEM_alloc (sizeof(int) * ( revVec.size() ));
	attachments = (tag_t*) MEM_alloc (sizeof(tag_t) * ( revVec.size() ));
	//制定目标，数组转换
	for ( int indx = 0; indx < revVec.size(); indx++ )
	{
		att_types[indx] = EPM_target_attachment;//EPM_reference_attachment;
		attachments[indx] = revVec[indx];
	}
	//把所有的版本都添加到流程中
	//POM_AM__set_application_bypass(true);
	//ITKCALL(ifail = AOM_refresh(rootTask,0));
	printf("1ifail=>%d\n",ifail);
	//AOM_load(rootTask);
	//AOM_lock(rootTask);
	ITKCALL(ifail=EPM_add_attachments(rootTask,revVec.size(),attachments,att_types));
	printf("2ifail=>%d\n",ifail);
	//AOM_unlock(rootTask);
	//ITKCALL(ifail =AOM_refresh(rootTask,0));
	//printf("3ifail=>%d\n",ifail);
		//POM_AM__set_application_bypass(false); 
	ITKCALL(ifail =CR_create_release_status(statusName.c_str(),&release_status));
	printf("4ifail=>%d\n",ifail);
	if(release_status!=NULLTAG){
		POM_AM__set_application_bypass(true);
		//ITKCALL(ifail =AOM_refresh(rootTask,0));
		//printf("5ifail=>%d\n",ifail);
		//AOM_lock(rootTask);
		ITKCALL(ifail =EPM_remove_status_from_targets(release_status,rootTask));
		printf("6ifail=>%d\n",ifail);
		//AOM_unlock(rootTask);
		//ITKCALL(ifail =AOM_refresh(rootTask,0));
		//printf("7ifail=>%d\n",ifail);
		POM_AM__set_application_bypass(false); 
	}
	//AOM_unlock(rootTask);
	if(att_types)										
	{											
		MEM_free(att_types);							
		att_types = NULL;								
	}
	if(attachments)										
	{											
		MEM_free(attachments);							
		attachments = NULL;								
	}

}

void removeRevFromTask(tag_t rootTask,vector<tag_t> revVec){
	cout<<"enter  removeRevFromTask "<<endl;
	tag_t * attachments = NULL;
	
	attachments = (tag_t*) MEM_alloc (sizeof(tag_t) * ( revVec.size() ));
	//制定目标，数组转换
	for ( int indx = 0; indx < revVec.size(); indx++ )
	{
		//att_types[indx] = EPM_target_attachment;//EPM_reference_attachment;
		attachments[indx] = revVec[indx];
	}
	cout<<"mark1--"<<endl;
	POM_AM__set_application_bypass(true);
	//ITKCALL(AOM_refresh(rootTask,0));
	//	ITKCALL(	AOM_lock(rootTask));
	ITKCALL(EPM_remove_attachments(rootTask,revVec.size(),attachments));
	//ITKCALL(	AOM_unlock(rootTask));
	//	ITKCALL(AOM_refresh(rootTask,0));
	POM_AM__set_application_bypass(false);
		cout<<"mark2--"<<endl;
	if(attachments)										
	{											
		MEM_free(attachments);							
		attachments = NULL;								
	}
}