#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 jk_check_virtual(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;
	char * object_string1=NULL;
	string value_type = "";
	string value_property="";
	char * ref_type=NULL;
	int actualNum=0;
	tag_t *actualtags=NULL;
	string errorMessage="";
	//获取当前触发的任务
	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;
	boolean ctn=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;
		ctn=false;
		if(strcmp("JK8VirtlMaterialRevision",object_TYPE_1)==0)//如果是虚拟件版本类型，就检查其下的实体物料的关系下有没有实体物料对象，并且对象下有发布的版本
		{
			ifail=AOM_ask_value_tags(attachments[i],"JK8InstanceMaterialRelation",&actualNum,&actualtags);
			for (int j=0;j<actualNum;j++)
			{
				if(ctn==true)
				{
					break;
				}
				char * object_TYPE_2 = NULL;
				AOM_ask_value_string(actualtags[j],"object_type",&object_TYPE_2);
				//如果关系下的对象类型符合，查看下面有没有最新发布的版本
				if(strcmp(object_TYPE_2,"JK8Material")==0)
				{
					//revision_list获得所有版本
					int revisionNums=0;
					tag_t *revisionTags=NULL;
					AOM_ask_value_tags(actualtags[j],"revision_list",&revisionNums,&revisionTags);
					for (int k=0;k<revisionNums;k++)
					{
						int releaseNums=0;
						tag_t * releaseTags=NULL;
						AOM_ask_value_tags(revisionTags[k],"release_status_list",&releaseNums,&releaseTags);
						
						if(releaseTags!=NULL)
						{
							MEM_free(releaseTags);
							releaseTags=NULL;
						}
						if(releaseNums>0)
						{
							
							ctn=true;
							break;
						}
					}
					if(revisionTags!=NULL)
					{
						MEM_free(revisionTags);
						revisionTags=NULL;
					}

				}
				if(object_TYPE_2!=NULL)
				{
					MEM_free(object_TYPE_2);
					object_TYPE_2=NULL;
				}	
			}
			
			if(actualtags!=NULL)
			{
				MEM_free(actualtags);
				actualtags=NULL;
			}
			if(ctn==false)
			{
				warn=true;
				ifail = AOM_ask_value_string(attachments[i], "object_string", &object_string1);//对象类型
				errorMessage=errorMessage+object_string1;
					if(object_string1!=NULL)
					{
						MEM_free(object_string1);
						object_string1=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(errorMessage.length()>0)
	{
		errorMessage="以下虚拟件没有关联实体物料或者实体物料没有发布的版本：\n"+errorMessage;
	}
	if(warn==true)
	{
		EMH_store_error_s1(EMH_severity_warning, EMH_AE_error_base, errorMessage.c_str());

		return EPM_nogo;
	}else
	{
		return EPM_go;
	}
}