#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"

int ML_DesignPartCopy(METHOD_message_t* msg, va_list args)
{
	printf("=========================Design-Part Start===================\n");
	int ifail = ITK_ok;
	tag_t prop_tag = va_arg(args, tag_t);//NULL
	int count = va_arg(args, int);//
	tag_t* values = va_arg(args, tag_t*);//

	tag_t design = (*msg).object_tag;
	int c0 = 0, c = 0, cnt1 = 0, cnt2 = 0;
	tag_t *v0 = NULL, *tirb = NULL, rf = NULLTAG, *released = NULL, *workflow = NULL;
	char *os = NULL;

	ITKCALL(WSOM_ask_release_status_list(design, &cnt1, &released));
	ITKCALL(AOM_ask_value_tags(design, "fnd0StartedWorkflowTasks", &cnt2, &workflow));
	//if (cnt1 > 0 && cnt2 == 0) //20231219 cnt1发布状态,cnt2是否流程中
	if (cnt1 > 0 )//只要状态为发布，对象还在流程也可以
	{
		return ifail;
	}

	ITKCALL(GRM_find_relation_type("representation_for", &rf));
	ITKCALL(AOM_ask_value_tags(design, "representation_for", &c0, &v0));
	for (int i = 0; i < c0; i++) {
		char *name = NULL;
		AOM_ask_name(v0[i], &name);
		printf("v0：%s\n", name);
	}
	ITKCALL(AOM_ask_value_string(design, "object_string", &os));

	printf("count：%d\n", count);
	for (int i = 0; i < count; i++) {
		char *type = NULL;
		//AOM_ask_name(values[i], &name);
		//printf("name：%s\n", name);
		ITKCALL(WSOM_ask_object_type2(values[i], &type));
		printf("type：%s\n", type);
		if (strcmp(type, "ML8_ColdRevision") != 0 && strcmp(type, "ML8_OthersRevision") != 0 
			&& strcmp(type, "ML8_RefrigeratorRevision") != 0 && strcmp(type, "ML8_WashingRevision") != 0 
			&& strcmp(type, "ML8_RawMaterialRevision") != 0 && strcmp(type, "ML8_PartRevision") != 0) {
			continue;
		}
		int c = 0;
		tag_t *tirb = NULL;
		ITKCALL(AOM_ask_value_tags(values[i], "TC_Is_Represented_By", &c, &tirb));
		bool find = true;
		for (int j = 0; j < c; j++) {
			char *tirbs = NULL;
			ITKCALL(AOM_ask_value_string(tirb[j], "object_string", &tirbs));
			if (strcmp(os, tirbs) == 0) {
				find = false;
				break;
			}
		}
		if (find) {
			tag_t relation = NULLTAG;
			int r = 0;
			//ITKCALL(GRM_find_relation(design, values[i], rf, &relation));
			//ITKCALL(r = GRM_delete_relation(relation));
			printf("%s remove %d\n", os, r);
			ifail = 919700;
		}
	}

	printf("=========================Design-Part End===================\n");
	if (ifail != 0) {
		EMH_store_error_s1(EMH_severity_user_error, ifail, "设计文档的表示不能直接粘贴物料");
		//ITKCALL(EMH_store_error(EMH_severity_user_error, ifail));EMH_USER_error_base
	}
	return ifail;
}