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

#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 <stdlib.h>
#include <tccore/aom.h>
#include <tc/emh.h>
#include <ict/ict_userservice.h>
#include <tc/tc.h>
#include <tccore/tctype.h>
#include <sa/tcfile.h>
#include <ss/ss_errors.h>


int tx_checkMaterialReleased(EPM_rule_message_t msg)
{
	EPM_decision_t decision = EPM_go;
	int ifail = ITK_ok, arg_cnt = 0, i = 0, att_cnt = 0;
	tag_t task_tag = NULLTAG, rootTask_tag = NULLTAG, *attachments = NULL;
	char * object_TYPE_1 = NULL;
	string value_type = "";
	string value_property="";
	char * ref_type=NULL;
	int actualNum=0;
	string errorMessage="以下物料版本未发布，不允许做停用操作!\n";
	//获取当前触发的任务
	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);
	int reference_count=0;tag_t * reference_attachment=NULLTAG;
	ifail = EPM_ask_attachments(rootTask_tag, EPM_reference_attachment, &reference_count, &reference_attachment);
	boolean warn=false;
	//循环目标对象
	for (i = 0; i < att_cnt; i++) {
		ifail = AOM_ask_value_string(attachments[i], "object_type", &object_TYPE_1);//对象类型
		cout << object_TYPE_1;
		cout << endl;
		if(strcmp("TX2RawMaterial",object_TYPE_1)==0||strcmp("TX2SemiProduct",object_TYPE_1)==0||strcmp("TX2Component",object_TYPE_1)==0||strcmp("TX2FinalProduct",object_TYPE_1)==0)//如果是虚拟件版本类型，就检查其下的实体物料的关系下有没有实体物料对象，并且对象下有发布的版本
		{
			int tagNum=0;
			tag_t *revTags=NULLTAG;
			AOM_ask_value_tags(attachments[i],"revision_list",&tagNum,&revTags);
			for (int j=0;j<tagNum;j++)
			{
				tag_t rev=revTags[j];
				int is_released=0;

				EPM_ask_if_released(rev,&is_released);
				if(is_released==0)
				{
					warn=true;
					char *value=NULL;
					AOM_ask_value_string(rev,"object_string",&value);
					errorMessage=errorMessage+value+"\n";
					if(value!=NULL)
					{
						MEM_free(value);
						value=NULL;
					}
				}

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

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

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


	if(warn==true)
	{
		EMH_store_error_s1(EMH_severity_warning, EMH_AE_error_base, errorMessage.c_str());

		return EPM_nogo;
	}else
	{
		return EPM_go;
	}
}