#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"

int ML_HaveBomLine(EPM_action_message_t msg) {
	printf("=========================是否包含子BOM Start===================\n");
	auto startTime = std::chrono::high_resolution_clock::now();
	int ifail = ITK_ok;
	int  attachments_num = 0;
	tag_t  rootTask = NULLTAG, *attachments = NULLTAG;
	//获取任务对象
	EPM_ask_root_task(msg.task, &rootTask);
	//获取任务目标对象
	EPM_ask_attachments(rootTask, EPM_target_attachment, &attachments_num, &attachments);

	for (int i = 0; i < attachments_num; i++)
	{
		char *itemType = NULL;

		AOM_ask_value_string(attachments[i], "object_type", &itemType);
		printf("type_class : %s \r\n", itemType);
		//过滤掉非版本的对象
		if ((strstr(itemType, "Revision") == NULL) || (strstr(itemType, "Master") != NULL)
			|| (strstr(itemType, "master") != NULL) || (strstr(itemType, "BOM") != NULL) || (strstr(itemType, "bom") != NULL) || (strstr(itemType, "Bom") != NULL))
		{
			DOFREE(itemType);
			continue;
		}
		//创建BOMWindow
		tag_t top_line = NULLTAG, window = NULLTAG;
		BOM_create_window(&window);
		//设置顶层BOM
		BOM_set_window_top_line(window, NULLTAG, attachments[i], NULLTAG, &top_line);

		int  count;
		tag_t *children_line = NULLTAG;

		BOM_line_ask_all_child_lines(top_line, &count, &children_line);

		if (count <= 0)
		{
			ifail = 1;
		}

		BOM_close_window(window);

		DOFREE(itemType);
		DOFREE(children_line);

	}
	DOFREE(attachments);
	if (ifail == 1)
	{
		EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, "目标对象下未包含子BOM！");
	}

	auto stopTime = std::chrono::high_resolution_clock::now();
	auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stopTime - startTime);
	//std::cout << "ML_HaveBomLine用时:" << duration.count() / 1000 << std::endl;
	string usetime = "ML_HaveBomLine用时:";
	usetime.append(std::to_string(duration.count() / 1000));
	WriteLog(true, usetime.c_str());
	printf("=========================是否包含子BOM End===================\n");
	return ifail;
}