#pragma warning (disable: 4996) 
#pragma warning (disable: 4819) 
#pragma warning (disable: 4995) 

#include <tc/tc.h>
#include "epm_handler_common.h"
#include <epm/epm_task_template_itk.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 "error_handling.h"
#include "Remove_Release_Status.h"
extern "C" int POM_AM__set_application_bypass(logical bypass);

int Remove_Release_Status(EPM_action_message_t msg)
{
	cout<<"删除发布状态"<<endl;

	int ifail = ITK_ok,  i = 0, att_cnt = 0;
	tag_t task_tag = NULLTAG, rootTask_tag = NULLTAG, *attachments = NULL;
	tag_t *release_status=NULL;int rs_num=0;
	char *value=NULL;
	tag_t *targets=NULLTAG;
	task_tag = msg.task;
	//获取根流程节点
	vector<tag_t>	target_vec;
	ITKCALL(	ifail = EPM_ask_root_task(task_tag, &rootTask_tag));
	//获取目标引用对象
	ITKCALL(	ifail = EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &att_cnt, &attachments));
	//循环目标对象	
	for (int i=0;i<att_cnt;i++)
	{
		//			ifail = AOM_ask_value_tags(attachments[i],"release_status_list",&rs_num,&release_status);
//		ITKCALL(ifail =AOM_ask_value_string(attachments[i],"object_string",&value));
	
//		target_vec.push_back(attachments[i]);
			int num1=0;
			ITKCALL(ifail=AOM_ask_value_tags(attachments[i],"revision_list",&num1,&targets));
				for (int s=0;s<num1;s++)
				{
						target_vec.push_back(targets[s]);
				}
//		cout<<value<<endl;
//		ITKCALL(ifail=CR_ask_release_statuses(attachments[i],&rs_num,&release_status));
//			ITKCALL(ifail=WSOM_ask_release_status_list(attachments[i],&rs_num,&release_status));
//			
//		cout<<"rs_num--------"<<rs_num<<endl;
//		WSOM_status_ask_effectivities()
//		for (int j=0;j<rs_num;j++)
//		{
//			ITKCALL(ifail =AOM_ask_value_string(release_status[j],"object_name",&value));
//			int num1=0;
//			ITKCALL(ifail=AOM_ask_value_tags(attachments[i],"revision_list",&num1,&targets));
//				for (int s=0;s<num1;s++)
//				{
//						target_vec.push_back(targets[s]);
//				}
//
//
//
////			cout<<"object_name"<<value<<endl;
////			if(strcmp(value,"TCM Released")==0)
////			{
////				cout<<""<<"bon"<<endl;
////				POM_AM__set_application_bypass(true);
//////				ifail=EPM_remove_status_from_targets(release_status[j],rootTask_tag);//只能删除根任务的状态
////				ITKCALL(ifail=EPM_remove_status_from_targets(release_status[j],attachments[i]));
////				ITKCALL(ifail=AOM_ask_value_tags(attachments[i],"revision_list",&num1,&targets));
////				for (int s=0;s<num1;s++)
////				{
////					ITKCALL(ifail=EPM_remove_status_from_targets(release_status[j],targets[s]));
////				}
////				POM_AM__set_application_bypass(false);
////			}
//			
//
//
//
////			cout<<"object_name"<<value<<endl;
////			if(strcmp(value,"TCM Released")==0)
////			{
////				//开旁路
////				POM_AM__set_application_bypass(true);
////				ITKCALL(ifail=EPM_remove_release_status(attachments[i],release_status[j]));
////				//					ifail = AOM_set_value_tags(attachments[i],"release_status_list",0,NULL);//删不掉，提示属性“发布状态”不可修改
////				POM_AM__set_application_bypass(false);
////				cout<<"删除完成"<<endl;
////				//					break;
////			}
//		}

	}
	POM_AM__set_application_bypass(true);
//	removeReleaseStatus("删除状态流程",target_vec);
	removeReleaseStatusByName("TCM Released",rootTask_tag,target_vec);
	removeRevFromTask(rootTask_tag,target_vec);
	POM_AM__set_application_bypass(false);
	if (value != NULL)
	{
		MEM_free(value);
		value = NULL;
	}
	if (attachments != NULL)
	{
		MEM_free(attachments);
		attachments = NULL;
	}
	if (release_status != NULL)
	{
		MEM_free(release_status);
		release_status = NULL;
	}
	if (targets != NULL)
	{
		MEM_free(targets);
		targets = NULL;
	}
	return ifail;
}



void removeReleaseStatus(string process_name,vector<tag_t> wf_it){
	if(wf_it.size()<1){
		return ;
	}
	
	tag_t template_tag =NULLTAG,new_process=NULLTAG;
	tag_t * attachments = NULL;
	int *att_types=NULL;
	att_types = (int*) MEM_alloc (sizeof(int) * ( wf_it.size() ));
	attachments = (tag_t*) MEM_alloc (sizeof(tag_t) * ( wf_it.size() ));
	for ( int indx = 0; indx < wf_it.size(); indx++ )
	{
		att_types[indx] = EPM_target_attachment;//EPM_reference_attachment;
		attachments[indx] = wf_it[indx];
	}
	
	ITKCALL( EPM_find_template( process_name.c_str(),PROCESS_TEMPLATE, &template_tag ) );
	if( template_tag != NULLTAG )
	{
		ITKCALL( EPM_create_process(process_name.c_str(), "", template_tag, wf_it.size(), attachments, att_types, &new_process));
	}
	if(att_types)										
	{											
		MEM_free(att_types);							
		att_types = NULL;								
	}
	if(attachments)										
	{											
		MEM_free(attachments);							
		attachments = NULL;								
	}
}
void removeReleaseStatusByName(string statusName,tag_t rootTask){
	tag_t release_status =NULLTAG;
	ITKCALL(CR_create_release_status(statusName.c_str(),&release_status));
	if(release_status!=NULLTAG){
		ITKCALL(EPM_remove_status_from_targets(release_status,rootTask));
	}
}

void removeReleaseStatusByName(string statusName,tag_t rootTask,vector<tag_t> revVec){
	tag_t release_status =NULLTAG;
	tag_t * attachments = NULL;
	int *att_types=NULL;
	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];
	}
	//把所有的版本都添加到流程中
	ITKCALL(EPM_add_attachments(rootTask,revVec.size(),attachments,att_types));
	ITKCALL(CR_create_release_status(statusName.c_str(),&release_status));
	if(release_status!=NULLTAG){
		ITKCALL(EPM_remove_status_from_targets(release_status,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;
	AOM_refresh(rootTask,0);
	ITKCALL(EPM_remove_attachments(rootTask,revVec.size(),attachments));
		cout<<"mark2"<<endl;
	if(attachments)										
	{											
		MEM_free(attachments);							
		attachments = NULL;								
	}
}