#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"
#define ITK_err 919012
int recursionBOM(tag_t rev, char *checkResult);
int ML_CheckMBOM(EPM_action_message_t msg)
{
	printf("=========================MBOM校验 Start===================\n");
	auto startTime = std::chrono::high_resolution_clock::now();
	//EPM_decision_t decision = EPM_go;
	int ifail = ITK_ok;
	int  attachments_num = 0;
	tag_t  rootTask = NULLTAG, *attachments = NULLTAG;
	string type;
	vector<string> types;
	char checkResult[1024] = "";
	//获取任务对象
	EPM_ask_root_task(msg.task, &rootTask);
	//获取任务目标对象
	EPM_ask_attachments(rootTask, EPM_target_attachment, &attachments_num, &attachments);

	char *argflag = NULL, *argvalue = NULL, *arg = NULL;
	//获取参数
	int arg_cnt = TC_number_of_arguments(msg.arguments);
	printf("参数个数为：%d\n", arg_cnt);
	if (arg_cnt > 0)
	{
		for (int i = 0; i < arg_cnt; i++)
		{
			//获取下一个参数（从0开始）
			arg = TC_next_argument(msg.arguments);
			//获取参数的名称和值
			ITKCALL(ifail = ITK_ask_argument_named_value((const char*)arg, &argflag, &argvalue)) ;
			if (stricmp(argflag, "type") == 0)
			{
				if (argvalue != NULL)
				{
					//strcpy(arg1value, argvalue);
					type.assign(argvalue);
				}
			}
			DOFREE(argflag);
			DOFREE(argvalue);
		}
	}

	//解析handler参数
	{
		int strnum = 0;
		types = split2(type, ",");
		if (types.size() == 0)
		{
			EMH_store_error_s1(EMH_severity_error, ITK_err, "未配置流程参数：-type");
			ifail = 1;
			goto end;
		}

	}

	for (int i = 0; i < attachments_num; i++)
	{
		char *itemType = NULL,
			*factory = NULL,
			*line = NULL,
			*item_id = NULL,
			*name = NULL;
		
		AOM_ask_value_string(attachments[i], "object_type", &itemType);
		printf("type_class : %s,-itemType：%s \r\n", itemType, type.c_str());
		//过滤掉非目标版本的对象
		if (!isVecHasStr(types, itemType))
		{
			DOFREE(itemType);
			continue;
		}
		ITKCALL(ifail = AOM_ask_value_string(attachments[i],"ml8_Factory",&factory));
		ITKCALL(ifail = AOM_ask_value_string(attachments[i],"ml8_Line",&line));
		ITKCALL(ifail = AOM_ask_value_string(attachments[i],"item_id",&item_id));
		ITKCALL(ifail = AOM_ask_value_string(attachments[i],"object_name",&name));
		if (factory == NULL || strcmp(factory, "") == 0 || line == NULL || strcmp(line, "") == 0)
		{
			strcat(checkResult,item_id);
			strcat(checkResult,name);
			strcat(checkResult,"不为MBOM！\n");
			DOFREE(item_id);
			DOFREE(name);
			DOFREE(factory);
			DOFREE(line);
			DOFREE(itemType);
			continue;
		}
		recursionBOM(attachments[i], checkResult);
	}
		if (strcmp(checkResult, "") != 0)
		{
			//decision = EPM_nogo;
			//EMH_store_error_s2(EMH_severity_error, ITK_err, G2U("MBOM校验失败"), G2U(checkResult) );
			ITKCALL(ifail = EMH_store_error_s2(EMH_severity_error, ITK_err, "MBOM校验失败", checkResult ));
		}

		DOFREE(item_id);
		DOFREE(name);
		DOFREE(factory);
		DOFREE(line);
		DOFREE(itemType);
	end:

		auto stopTime = std::chrono::high_resolution_clock::now();
		auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stopTime - startTime);
		//std::cout << "ML_CheckMBOM用时:" << duration.count() / 1000 << std::endl;
		string usetime = "ML_CheckMBOM用时:";
		usetime.append(std::to_string(duration.count() / 1000));
		WriteLog(true, usetime.c_str());
	printf("=========================MBOM校验 End===================\n");
	return ifail;
}

int recursionBOM(tag_t rev,char *checkResult)
{
		tag_t *child_revs = NULL,
			*child_revs2 = NULL;
		int child_num = 0;
		int child_num2 = 0,
			ifail = ITK_ok;
		char *factory = NULL,
			*line = NULL,
			*processing_mode = NULL,
			*add_trade = NULL,
			*item_id = NULL,
			*name = NULL;
		ITKCALL(AOM_ask_value_tags(rev,"ps_children",&child_num,&child_revs));
		if (child_num > 0)
		{
			for (int i = 0; i < child_num; i++)
			{
				ITKCALL(ifail = AOM_ask_value_string(child_revs[i],"ml8_ProcessingMode", &processing_mode));
				if (ifail != ITK_ok) {
					printf("清理不存在属性ml8_ProcessingMode错误信息\n");
					EMH_clear_last_error(ifail);
					processing_mode = (char*)malloc(1 + sizeof(char) * strlen(""));
					strcpy(processing_mode,"");
				}
				ITKCALL(ifail = AOM_ask_value_string(child_revs[i],"ml8_AddTrade", &add_trade));
				if (ifail != ITK_ok) {
					printf("清理不存在属性ml8_ProcessingMode错误信息\n");
					EMH_clear_last_error(ifail);
					add_trade = (char*)malloc(1 + sizeof(char) * strlen(""));
					strcpy(add_trade,"");
				}
				if (strcmp(processing_mode, "自制") == 0 || strcmp(processing_mode, "外协外购") == 0 && strcmp(add_trade, "是") == 0)
				{
					ITKCALL(AOM_ask_value_string(child_revs[i],"ml8_Factory", &factory));
					ITKCALL(AOM_ask_value_string(child_revs[i],"ml8_Line", &line));
					ITKCALL(AOM_ask_value_string(child_revs[i],"item_id", &item_id));
					ITKCALL(AOM_ask_value_string(child_revs[i],"object_name", &name));
					if (factory == NULL || strcmp(factory, "") == 0 || line == NULL || strcmp(line, "") == 0)
					{
						strcat(checkResult, item_id);
						strcat(checkResult, name);
						strcat(checkResult, "产线、工厂为空！\n");
					}
					if (factory)DOFREE(factory);
					if (line)DOFREE(line);
					if (item_id)DOFREE(item_id);
					if (name)DOFREE(name);

				}
				if(processing_mode)DOFREE(processing_mode);
				if (add_trade)DOFREE(add_trade);
				//递归
				ITKCALL(AOM_ask_value_tags(child_revs[i],"ps_children",&child_num2,&child_revs2));
				if (child_num2 > 0)
				{
					recursionBOM(child_revs[i], checkResult);
				}
			}
		}
		DOFREE(child_revs);
		return 0;
}