#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"

int ML_CheckObjectDesc(EPM_action_message_t msg)
{
	printf("========================检查物料描述 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);

	char resultString[2048] = "";
	for (int i = 0; i < attachments_num; i++)
	{
		
		char *itemType = NULL;
		AOM_ask_value_string(attachments[i], "object_type", &itemType);

		char *itemObjectDesc = NULL;
		AOM_ask_value_string(attachments[i], "object_desc", &itemObjectDesc);
		//过滤掉非版本的对象
		if ((strcmp(itemType, "ML8_RefrigeratorRevision") == 0) || (strcmp(itemType, "ML8_WashingRevision") == 0)
			|| (strcmp(itemType, "ML8_ColdRevision") == 0) || (strcmp(itemType, "ML8_OthersRevision") == 0)
			|| (strcmp(itemType, "ML8_PartRevision") == 0) || (strcmp(itemType, "ML8_RawMaterialRevision") == 0))
		{
			char resultMess[2048] = "";
			char * itemId = NULL;//物料名称
			AOM_ask_value_string(attachments[i], "item_id", &itemId);


			//获取对象的类型
			tag_t item = NULLTAG;
			ITEM_ask_item_of_rev(attachments[i], &item);
			char * itemObjectType = NULL;
			AOM_ask_value_string(item, "object_type", &itemObjectType);
			//查重物料描述
			int n_entries = 1;
			char *qry_entries[1] = { "描述" }, *qry_values[1] = { itemObjectDesc };
			int n_found = 0;
			tag_t * dba_mbrs = NULLTAG;
			char queryName[256] = "";
			strcat(queryName, "所有物料描述查询");
			TCFindItem(queryName, n_entries, qry_entries, qry_values, &n_found, &dba_mbrs);
			bool isHaveItem = false;
			for (int ii = 0; ii < n_found; ii++) {
				char* id;
				char* name;
				AOM_ask_value_string(dba_mbrs[ii], "item_id", &id);
				AOM_ask_value_string(dba_mbrs[ii], "object_name", &name);
				printf("id:%s---名称:%s ===================\n", id, name);
			}
			for (int ii = 0; ii < n_found; ii++)
			{
				int releaseCount = 0;
				tag_t* releaseTag = NULL_TAG;
				//获取子件的发布状态
				AOM_ask_value_tags(dba_mbrs[ii], "release_status_list", &releaseCount, &releaseTag);
				if (releaseCount>0) {
					bool flag = false;
					for (int a = 0; a < releaseCount;a++) {
						char* releaseName;
						AOM_ask_value_string(releaseTag[a], "object_name", &releaseName);
						if (strcmp(releaseName,"被取代") == 0 || strcmp(releaseName,"ML8_Replaced") == 0) {
							//进行下一次循环
							flag = true;
							break;
						}
					}
					if (flag) {
						isHaveItem = false;
						continue;
						
					}
				}

				char * otherItemId = NULL;
				AOM_ask_value_string(dba_mbrs[ii], "item_id", &otherItemId);
				printf("对比Item_id:%s---%s Start===================\n", otherItemId, itemId);
				if (strcmp(otherItemId, itemId) == 0)
				{
					DOFREE(otherItemId);
					isHaveItem = false;
					continue;
				}
				//忽略id中含有"-"的
				if (strstr(otherItemId, "-") != NULL)
				{
					printf("忽略id中包含-的对象\n");
					isHaveItem = false;
					continue;
				}
				isHaveItem = true;
				strcat(resultMess, otherItemId);
				strcat(resultMess, "：");
				strcat(resultMess, "已存在物料描述：");
				strcat(resultMess, itemObjectDesc);
				strcat(resultMess, "\n");

				//break;
			}

			if (gbk_strlen(resultMess) > 2)
			{
				strcat(resultString, resultMess);
				strcat(resultString, "\n");

				DOFREE(dba_mbrs);
			}
		}

	}
		
	DOFREE(attachments);
	if (gbk_strlen(resultString) > 2)
	{
		ifail = 1;
		EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, resultString);
	}

	auto stopTime = std::chrono::high_resolution_clock::now();
	auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stopTime - startTime);
	//std::cout << "ML_CheckObjectDesc用时:" << duration.count() / 1000 << std::endl;
	string usetime = "ML_CheckObjectDesc用时:";
	usetime.append(std::to_string(duration.count() / 1000));
	WriteLog(true, usetime.c_str());
	printf("=========================更改物料描述 End===================\n");
	return ifail;
}