/*检查EBOM派生出来的所有MBOM的最新版本是否已经发布，或者说最新版本是否在流程中或工作中的状态，如果未发布，就提示*/

#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_workflow(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="";
	string errorMessage="以下EBOM对应的MBOM最新版未发布，禁止升版:\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);
	vector<string> errorVec;//存放的是未发布的MBOMID和EBOM的id加版本
	//循环目标对象
	for (i = 0; i < att_cnt; i++) {
		ITKCALL(ifail = AOM_ask_value_string(attachments[i], "object_type", &object_TYPE_1));//对象类型
		cout << object_TYPE_1;
		cout << endl;
		if(strcmp("JK8MaterialRevision",object_TYPE_1)==0)
		{
			int MBOMNums=0;
			tag_t *MBOMTags=NULLTAG;
			ITKCALL(ifail=AOM_ask_value_tags(attachments[i],"JK8MBOMRelation",&MBOMNums,&MBOMTags));
			for (int j=0;j<MBOMNums;j++)
			{
				ITKCALL(ifail = AOM_ask_value_string(MBOMTags[j], "object_type", &object_TYPE_1));//对象类型
				if(strcmp(object_TYPE_1,"JK8Material")==0)
				{
					tag_t latest_rev;
					ITEM_ask_latest_rev(MBOMTags[j],&latest_rev);//获得最新版本
					int is_released=0;
					EPM_ask_if_released(latest_rev,&is_released);//0未发布，1发布
//					ITEM_rev_vi_revise_and_update()
					if(is_released==0)
					{
						char *item_id=NULL;
						char *item_revision_id=NULL;
						char *item_id2=NULL;
						string hint="";
						AOM_ask_value_string(attachments[i],"item_id",&item_id);
						AOM_ask_value_string(attachments[i],"item_revision_id",&item_revision_id);
						AOM_ask_value_string(MBOMTags[j],"item_id",&item_id2);
						hint=hint.append(item_id).append("/").append(item_revision_id).append("-").append(item_id2).append("   ");
						
						errorVec.push_back(hint);
						if(item_id!=NULL)
						{
							MEM_free(item_id);
							item_id=NULL;
						}
						if(item_revision_id!=NULL)
						{
							MEM_free(item_revision_id);
							item_revision_id=NULL;
						}
						if(item_id2!=NULL)
						{
							MEM_free(item_id2);
							item_id2=NULL;
						}
					}
				}
				
			}



			if(MBOMTags!=NULL)
			{
				MEM_free(MBOMTags);
				MBOMTags=NULL;
			}
		}
	}
	if(errorVec.size()>0)
	{
		for (int i=0;i<errorVec.size();i++)
		{
			errorMessage=errorMessage+errorVec[i];
		}
		
		EMH_store_error_s1(EMH_severity_warning, EMH_AE_error_base, errorMessage.c_str());

		return EPM_nogo;
	}
	return EPM_go;
}