#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>
#include <ae/datasettype.h>
// #include "hx_custom.h"
#include "tc_log.h"
// #include "jk_custom.h"
#include "chint_Handler.h"
#include "tc_util.h"
#include <regex>
#include <ics/ics.h>

int chint_check_exist_ebom(EPM_action_message_t msg) {

	int ifail = ITK_ok;
	tag_t root_task = NULLTAG, *sub_tasks = NULL, current_task = NULLTAG, type_tag = NULLTAG;
	int sub_task_count = 0, occur_of_counts = 0;
	tag_t* taskAttches = NULLTAG;
	tag_t cur_task = NULLTAG, owning_user;

	current_task = msg.task;
	ECHO("=========================================================\n");
	ECHO("chint_check_exist_ebom 开始执行\n");
	ECHO("=========================================================\n");
	EPM_ask_root_task(msg.task, &root_task);
	EPM_ask_sub_tasks(root_task, &sub_task_count, &sub_tasks);
	EPM_ask_attachments(root_task, EPM_target_attachment, &occur_of_counts, &taskAttches);
	AOM_ask_value_tag(root_task, "owning_user", &owning_user);
	char *userId;
	AOM_ask_value_string(owning_user, "user_id", &userId);
	string return_data;
	return_data.append("\n以下图纸没有EBOM,请转换EBOM之后执行:\n");
	for (int count = 0; count < occur_of_counts; count++)
	{
		//ITKCALL(TCTYPE_ask_object_type(taskAttches[count], &type_tag));
		char *type,*idItem;
		tag_t taskTag = taskAttches[count];
		AOM_ask_value_string(taskTag, "object_type", &type);
		
		printf("type===>%s\n", type);
		//ITKCALL(EPM_remove_attachments(root_task, 1, &taskTag));
		if (strcmp(type, "ZT2_Design3DRevision") == 0) {
			AOM_ask_value_string(taskTag, "item_id", &idItem);
			printf("v===%s\n", idItem);
			if (strstr(idItem, "1ZD") != NULL) {
				char *source;
				AOM_ask_value_string(taskTag, "zt2_Source", &source);
				printf("source===%s\n", source);
				if (strcmp(source, "S1") == 0 || strcmp(source, "自制") == 0) {
					tag_t* mantrs;
					int num = 0;
					ITKCALL(AOM_ask_value_tags(taskTag, "representation_for", &num, &mantrs));
					for (int n = 0; n < num;n++) {
						tag_t mantr = mantrs[n],*structure_revisions;
						int revNum = 0;
						char *zt2_ifpbom;
						AOM_ask_value_string(mantr, "zt2_ifpbom", &zt2_ifpbom);
						printf("zt2_ifpbom===%s\n", zt2_ifpbom);
						if (strcmp(zt2_ifpbom,"P") == 0) {
							continue;
						}
						AOM_ask_value_tags(mantr, "structure_revisions", &revNum, &structure_revisions);
						printf("revNum===%d\n", revNum);
						if (revNum == 0) {
							char *objName;
							AOM_ask_value_string(taskTag, "object_name", &objName);
							ifail = 1;
							return_data.append(idItem).append("/").append(objName).append(".\n");
						}
					}
				}
			}
			//ITKCALL(EPM_remove_attachments(root_task, 1, &taskTag));
		}
	}
	if (ifail==1) {
		EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, return_data.c_str());
	}
	return ifail;
}

