#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"

int ML_ChangeObjectDesc(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);
	bool isRelease = false;

	char *argflag = NULL, *argvalue = NULL, *arg = NULL;
	char arg1value[1024] = "";
	//获取参数
	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);
			//获取参数的名称和值
			ifail = ITK_ask_argument_named_value((const char*)arg, &argflag, &argvalue);
			if (stricmp(argflag, "release") == 0)
			{
				isRelease = true;
			}
		}
	}

	//获取Meling_voltage首选项的值
	int pref_cnt = 0;
	char ** pref_vals = NULL;
	//获取首选项的值
	PREF_ask_char_values("Meling_voltage", &pref_cnt, &pref_vals);

	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))
		{
			int releaseCount = 0;
			tag_t * releaseTag = NULL_TAG;
			//判断子件是否发布
			AOM_ask_value_tags(attachments[i], "release_status_list", &releaseCount, &releaseTag);
			if (releaseCount > 0 && !isRelease)
			{
				DOFREE(releaseTag);
				continue;
			}
			DOFREE(releaseTag);
			//获取对象的类型
			tag_t item = NULLTAG;
			ITEM_ask_item_of_rev(attachments[i], &item);
			char * itemObjectType = NULL;
			AOM_ask_value_string(item, "object_type", &itemObjectType);

			//获取通用化标识
			char * generalIdentify = NULL;
			AOM_ask_value_string(attachments[i], "ml8_GeneralIdentify", &generalIdentify);
			if (strcmp(generalIdentify, "标准件") == 0)
			{
				DOFREE(itemObjectType);
				DOFREE(generalIdentify);
				continue;
			}
			//获取销售方式
			char * propertyValue = NULL;
			AOM_ask_value_string(attachments[i], "ml8_SalesMode", &propertyValue);

			if (propertyValue == NULL)
			{
				DOFREE(itemObjectType);
				DOFREE(propertyValue);
				continue;
			}

			//获取拼接所需要的属性
			int projectFormNum = 0;
			tag_t * projectForm = NULLTAG;
			char * itemId = NULL;//物料名称
			char * objectName = NULL;//产品名称
			char * productType = NULL;//产品型号
			char * colour = NULL;//颜色名称
			char * domesticSales = NULL;//订单客户
			char * place = NULL;//产地
			char * productCode = NULL;//产品代号
			AOM_ask_value_string(attachments[i], "item_id", &itemId);
			AOM_ask_value_string(attachments[i], "object_name", &objectName);
			//AOM_ask_value_string(attachments[i], "ml8_ProductType", &productType);
			AOM_ask_value_string(attachments[i], "ml8_Colour", &colour);
			AOM_ask_value_string(attachments[i], "ml8_DomesticSales", &domesticSales);
			AOM_ask_value_string(attachments[i], "ml8_Place", &place);
			//AOM_ask_value_string(attachments[i], "ml8_ProductCode", &productCode);
			AOM_ask_value_tags(attachments[i], "ML8_ProjectRelation", &projectFormNum, &projectForm);
			if (projectFormNum > 0)
			{
				AOM_ask_value_string(projectForm[0], "ml8_ProductType", &productType);
				AOM_ask_value_string(projectForm[0], "ml8_ProductCode", &productCode);

			}

			printf("propertyValue:%s Start===================\n", propertyValue);
			char objectDesc[100] = "";
			//判断销售方式是否为内销、出口基本型、出口扩展型
			if ((strcmp(propertyValue, "内销") == 0) || (strcmp(propertyValue, "出口基本型") == 0)
				|| (strcmp(propertyValue, "出口扩展型") == 0))
			{
				string err = "";
				if (objectName != NULL && strcmp(objectName, "") != 0 && strcmp(objectName, "/") != 0)
				{
					strcat(objectDesc, objectName);
				}
				else if(objectName == NULL || strcmp(objectName, "") == 0)
				{
					printf("产品名称为空\n");
					err += "产品名称为空  ";
				}

				if (productType != NULL && strcmp(productType, "") != 0 && strcmp(productType, "/") != 0)
				{
					strcat(objectDesc, productType);
				}
				else if(productType == NULL || strcmp(productType, "")==0)
				{
					printf("产品型号为空\n");
					err += "产品型号为空 ";
				}

				if (productCode != NULL && strcmp(productCode, "") != 0 && strcmp(productCode, "/") != 0)
				{

					strcat(objectDesc, "(");
					strcat(objectDesc, productCode);
					strcat(objectDesc, ")");
					////产品状态做特殊处理
					//if (strstr(productCode, "CKD") != NULL)
					//{
					//	strcat(objectDesc, "/");
					//	strcat(objectDesc, "大散");
					//}
					//else if (strstr(productCode, "CKD") != NULL)
					//{
					//	strcat(objectDesc, "/");
					//	strcat(objectDesc, "散");
					//}
				}
				else if(productCode == NULL || strcmp(productCode, "") == 0)
				{
					printf("产品代号为空\n");
					err += "产品代号为空 ";
				}


				if (colour != NULL && strcmp(colour, "") != 0 && strcmp(colour, "/") != 0)
				{
					//颜色做特殊处理获取颜色名称PCM彩板_暗灰拉丝(ML-C1072)
					if (strstr(colour, "(") != NULL)
					{
						//按照_进行拆分。拆分条件
						int valueCount = 0;
						char ** valueChar = new char *[64];
						//分割字符串
						split(colour, "(", valueChar, &valueCount);

						//将第二个字符串按照_拆分
						if (strstr(valueChar[0], "_") != NULL)
						{
							//按照_进行拆分。拆分条件
							int colorNameCount = 0;
							char ** colorNameChar = new char *[64];
							//分割字符串
							split(valueChar[0], "_", colorNameChar, &colorNameCount);

							colour = colorNameChar[1];
						}
					}
					strcat(objectDesc, colour);
				}
				else if(colour == NULL || strcmp(colour, "") == 0)
				{
					printf("颜色为空\n");
					err += "颜色为空";
				}

				if (domesticSales != NULL && strcmp(domesticSales, "") != 0 && strcmp(domesticSales, "/") != 0)
				{
					strcat(objectDesc, "(");
					strcat(objectDesc, domesticSales);
					strcat(objectDesc, ")");
				}
				if (place != NULL && strcmp(place, "") != 0 && strcmp(place, "/") != 0 && (strcmp(place, "江西") == 0 || strcmp(place, "绵阳") == 0))
				{
					strcat(objectDesc, "(");
					strcat(objectDesc, place);
					strcat(objectDesc, ")");
				}
				else if(place == NULL || strcmp(place, "") == 0)
				{
					printf("产地号为空\n");
					err += "产地为空";
				}

				
				

				printf("objectDesc:%s Start===================\n", objectDesc);
				//判断物料描述是否超过40个字符
				/*int len = gbk_strlen(objectDesc);
				if (len > 40)
				{
				strcat(resultString, "物料描述：");
				strcat(resultString, objectDesc);
				strcat(resultString, "已超过40个字符");
				strcat(resultString, "/n");

				goto loop;
				}*/
				printf("err%s\n", err.c_str());
				//非空判断提示
				if (strcmp(err.c_str(), "") != 0) {
					EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, err.c_str());
					return 1;
				}
				////判断是否不需要更改
				//if (strcmp(itemObjectDesc, objectDesc) == 0)
				//{
				//	goto loop;
				//}
				
				//查重物料描述
				int n_entries = 1;
				char *qry_entries[1] = { "描述" }, *qry_values[1] = { objectDesc };
				int n_found = 0;
				tag_t * dba_mbrs = NULLTAG;
				char queryName[256] = "";
				//strcat(queryName, itemObjectType);
				strcat(queryName, "所有物料描述查询");
				TCFindItem(queryName, n_entries, qry_entries, qry_values, &n_found, &dba_mbrs);
				printf("找到了%d个重复的物料描述查询\n", n_found);
				bool isHaveItem = false;
				for (int ii = 0; ii < n_found; ii++)
				{
					char* checkId;
					AOM_ask_value_string(dba_mbrs[ii], "item_id", &checkId);
					//忽略id中含有"-"的
					if (strstr(checkId, "-") != NULL)
					{
						continue;
					}

					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");
						DOFREE(otherItemId);
						isHaveItem = false;
						continue;
					}
					isHaveItem = true;
					strcat(resultString, otherItemId);
					strcat(resultString, "：");
					strcat(resultString, "已存在物料描述：");
					strcat(resultString, objectDesc);
					strcat(resultString, "\n");
					DOFREE(otherItemId);
					//break;
				}

				/*
								if (n_found > 0)
								{
									strcat(resultString, "系统中已存在物料描述：");
									strcat(resultString, objectDesc);
									strcat(resultString, "/n");

									goto result;
								}*/
								//设置物料描述和标准描述
				if (strlen(resultString) == 0) {
					AOM_lock(attachments[i]);
					AOM_set_value_string(attachments[i], "object_desc", objectDesc);
					AOM_set_value_string(attachments[i], "ml8_StdDesc", objectDesc);
					AOM_save(attachments[i]);
					AOM_unlock(attachments[i]);
					AOM_refresh(attachments[i], false);
				}
				

			result:
				DOFREE(itemObjectType);
			}
			else if ((strcmp(propertyValue, "出口订单") == 0))
			{
				string err = "";
				//char * voltage = NULL;//电压
				//char * frequency = NULL;//频率
				char * exportCode = NULL;//出口国代码
				//AOM_ask_value_string(attachments[i], "ml8_Voltage", &voltage);
				//AOM_ask_value_string(attachments[i], "ml8_Frequency", &frequency);
				AOM_ask_value_string(attachments[i], "ml8_ExportCode", &exportCode);

				//更改频率
				/*if (strcmp(frequency, "50Hz") == 0)
				{
					frequency = "";
				}
				else if (strcmp(frequency, "60Hz") == 0)
				{
					frequency = "60";
				}
				else if (strcmp(frequency, "50/60Hz") == 0)
				{
					frequency = "50/60";
				}*/
				//获取电压
				//bool isHave = false;
				//for (int j = 0; j < pref_cnt; j++)
				//{
				//	if (strstr(pref_vals[j], "=") != NULL)
				//	{
				//		//按照_进行拆分。拆分条件
				//		int valueCount = 0;
				//		char ** valueChar = new char *[64];
				//		//分割字符串
				//		split(pref_vals[j], "=", valueChar, &valueCount);

				//		if (strcmp(voltage, valueChar[0]) == 0)
				//		{
				//			isHave = true;
				//			voltage = valueChar[1];
				//		}
				//	}
				//}
				//if (!isHave)
				//{
				//	voltage = "";
				//	/*strcat(resultString, "未找到匹配的电压值：");
				//	strcat(resultString, voltage);
				//	strcat(resultString, "\n");

				//	goto result2;*/
				//}

				if (objectName != NULL && strcmp(objectName, "") != 0 && strcmp(objectName, "/") != 0)
				{
					strcat(objectDesc, objectName);
				}
				else if(objectName == NULL || strcmp(objectName, "") == 0)
				{
					printf("产品名称为空\n");
					err += "产品名称为空 ";
				}

				if (productType != NULL && strcmp(productType, "") != 0 && strcmp(productType, "/") != 0)
				{
					strcat(objectDesc, productType);
				}
				else if(productType == NULL || strcmp(productType, "") == 0)
				{
					printf("出口型号为空\n");
					err += "出口型号为空 ";
				}

				if (productCode != NULL && strcmp(productCode, "") != 0 && strcmp(productCode, "/") != 0)
				{
					strcat(objectDesc, "(");
					strcat(objectDesc, (productCode + 5));
					strcat(objectDesc, ")");
				}
				else if(productCode == NULL || strcmp(productCode, "") == 0)
				{
					printf("产品代号为空\n");
					err += "产品代号为空 ";
				}

				if (colour != NULL && strcmp(colour, "") != 0 && strcmp(colour, "/") != 0)
				{
					//颜色做特殊处理获取颜色名称PCM彩板_暗灰拉丝(ML-C1072)
					if (strstr(colour, "(") != NULL)
					{
						//按照_进行拆分。拆分条件
						int valueCount = 0;
						char ** valueChar = new char *[64];
						//分割字符串
						split(colour, "(", valueChar, &valueCount);
						//将第二个字符串按照(拆分
						if (strstr(valueChar[1], ")") != NULL)
						{
							//按照_进行拆分。拆分条件
							int colorNameCount = 0;
							char ** colorNameChar = new char *[64];
							//分割字符串
							split(valueChar[1], ")", colorNameChar, &colorNameCount);
							if (strstr(colorNameChar[0], "-") != NULL)
							{
								//按照_进行拆分。拆分条件
								int colorName = 0;
								char ** colorChar = new char *[64];
								//分割字符串
								split(colorNameChar[0], "-", colorChar, &colorName);
								char colorChars[256] = "";
								for (int kkk = 0; kkk < colorName; kkk++)
								{
									if (kkk == 0 && strcmp(colorChar[kkk], "ML") == 0)
									{
										continue;
									}
									strcat(colorChars, colorChar[kkk]);
									if (kkk < (colorName - 1))
									{
										strcat(colorChars, "-");
									}
								}
								colour = colorChars;
							}
							else
							{
								colour = colorNameChar[0];
							}
						}
					}
					strcat(objectDesc, colour);
				}else if (colour == NULL || strcmp(colour, "") == 0)
				{
					printf("颜色为空\n");
					err += "颜色为空 ";
				}
				/*if (voltage != NULL && strcmp(voltage, "") != 0 && strcmp(voltage, "/") != 0)
				{
					strcat(objectDesc, voltage);
				}
				if (frequency != NULL && strcmp(frequency, "") != 0 && strcmp(frequency, "/") != 0)
				{
					strcat(objectDesc, frequency);
				}*/
				if (exportCode != NULL && strcmp(exportCode, "") != 0 && strcmp(exportCode, "/") != 0)
				{
					strcat(objectDesc, "(");
					strcat(objectDesc, exportCode);
					//strcat(objectDesc, ")");//出口国代码后的括号去掉
				}
				////判断物料描述是否超过40个字符
				//int len = gbk_strlen(objectDesc);
				//if (len > 40)
				//{
				//	strcat(resultString, "物料描述：");
				//	strcat(resultString, objectDesc);
				//	strcat(resultString, "已超过40个字符");
				//	strcat(resultString, "/n");

				//	goto result2;
				//}
				printf("err%s\n", err.c_str());
				//非空判断提示
				if (strcmp(err.c_str(), "") != 0) {
					EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, err.c_str());
					return 1;
				}

				

				char * desc = NULL;
				char * stdDesc = NULL;

				AOM_ask_value_string(attachments[i], "object_desc", &desc);
				AOM_ask_value_string(attachments[i], "ml8_StdDesc", &stdDesc);

				////判断是否不需要更改
				//if (strcmp(itemObjectDesc, objectDesc) == 0 && desc != NULL && stdDesc != NULL && strcmp(desc, "") != 0 && strcmp(stdDesc, "") != 0)
				//{
				//	DOFREE(desc);
				//	DOFREE(stdDesc);
				//	goto result2;
				//}
				DOFREE(desc);
				DOFREE(stdDesc);
				
				//查重物料描述
				
				int n_entries = 1;
				char *qry_entries[1] = { "描述" }, *qry_values[1] = { objectDesc };
				int n_found = 0;
				tag_t * dba_mbrs = NULLTAG;
				char queryName[256] = "";
				//strcat(queryName, itemObjectType);
				strcat(queryName, "所有物料描述查询");
				TCFindItem(queryName, n_entries, qry_entries, qry_values, &n_found, &dba_mbrs);
				printf("找到了%d个重复的物料描述查询\n", n_found);
				//过滤当前对象
				bool isHaveItem = false;
				for (int ii = 0; ii < n_found; ii++)
				{
					char* checkId;
					AOM_ask_value_string(dba_mbrs[ii], "item_id", &checkId);
					//忽略id中含有"-"的
					if (strstr(checkId, "-") != NULL)
					{
						continue;
					}

					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);
						continue;
					}
					isHaveItem = true;
					strcat(resultString, otherItemId);
					strcat(resultString, "：");
					strcat(resultString, "已存在物料描述：");
					strcat(resultString, objectDesc);
					strcat(resultString, "\n");
					DOFREE(otherItemId);
					//break;
				}

				/*if (isHaveItem)
				{
					strcat(resultString, "系统中已存在物料描述：");
					strcat(resultString, objectDesc);
					strcat(resultString, "\n");

					DOFREE(dba_mbrs);
					goto result2;
				}*/
				//设置物料描述和标准描述
				AOM_lock(attachments[i]);
				AOM_set_value_string(attachments[i], "object_desc", objectDesc);
				AOM_set_value_string(attachments[i], "ml8_StdDesc", objectDesc);
				AOM_save(attachments[i]);
				AOM_unlock(attachments[i]);
				AOM_refresh(attachments[i], false);

				DOFREE(dba_mbrs);

			result2:
				/*DOFREE(voltage);
				DOFREE(frequency);*/
				DOFREE(exportCode);
			}

		loop:
			DOFREE(itemObjectType);
			DOFREE(propertyValue);
			DOFREE(objectName);
			DOFREE(productType);
			DOFREE(colour);
			DOFREE(domesticSales);
			DOFREE(place);
			DOFREE(productCode);

		}
		else if ((strcmp(itemType, "ML8_PartRevision") == 0) || (strcmp(itemType, "ML8_RawMaterialRevision") == 0))
		{
			int releaseCount = 0;
			tag_t * releaseTag = NULL_TAG;
			//判断子件是否发布
			AOM_ask_value_tags(attachments[i], "release_status_list", &releaseCount, &releaseTag);
			if (releaseCount > 0 && !isRelease)
			{
				DOFREE(releaseTag);
				continue;
			}
			DOFREE(releaseTag);
			//获取对象的类型
			tag_t item = NULLTAG;
			ITEM_ask_item_of_rev(attachments[i], &item);
			char * itemObjectType = NULL;
			AOM_ask_value_string(item, "object_type", &itemObjectType);
			//获取通用化标识
			/*char * generalIdentify = NULL;
			AOM_ask_value_string(attachments[i], "ml8_GeneralIdentify", &generalIdentify);
			if (strcmp(generalIdentify, "标准件") != 0)
			{
				DOFREE(itemObjectType);
				DOFREE(generalIdentify);
				continue;
			}*/
			//获取危害物约束
			char * propertyValue = NULL;
			AOM_ask_value_string(attachments[i], "ml8_HazardConstraint", &propertyValue);

			if (propertyValue == NULL)
			{
				DOFREE(itemObjectType);
				DOFREE(propertyValue);
				continue;
			}

			char objectDesc[50] = "";

			//获取拼接所需要的属性
			char * itemId = NULL;//物料名称
			char * objectName = NULL;//物料名称
			char * externalWorking = NULL;//外加工状态
			char * colour = NULL;//颜色
			char * figureNum = NULL;//图号
			char * identification = NULL;//识别码
			char * specifications = NULL;//规格型号
			AOM_ask_value_string(attachments[i], "item_id", &itemId);
			AOM_ask_value_string(attachments[i], "object_name", &objectName);
			AOM_ask_value_string(attachments[i], "ml8_ExternalWorking", &externalWorking);
			AOM_ask_value_string(attachments[i], "ml8_Colour", &colour);
			if ((strcmp(itemType, "ML8_PartRevision") == 0))
			{
				AOM_ask_value_string(attachments[i], "ml8_FigureNum", &figureNum);
				AOM_ask_value_string(attachments[i], "ml8_Identification", &identification);
			}
			AOM_ask_value_string(attachments[i], "ml8_Specifications", &specifications);
			string err = "";
			//判断害物约束是否为RoHS指令、非RoHS指令
			if ((strcmp(propertyValue, "RoHS指令") == 0))
			{
				strcat(objectDesc, "R-");
				if (objectName != NULL && strcmp(objectName, "") != 0 && strcmp(objectName, "/") != 0)
				{
					strcat(objectDesc, objectName);
				}
				else if (objectName == NULL || strcmp(objectName, "") == 0)
				{
					printf("物料名称为空\n");
					err += "物料名称为空 ";
				}
				bool isHaveExternalWorking = false;
				if (externalWorking != NULL && strcmp(externalWorking, "") != 0 && strcmp(externalWorking, "/") != 0)
				{
					isHaveExternalWorking = true;
					strcat(objectDesc, "(");
					strcat(objectDesc, externalWorking);
				}
				if (colour != NULL && strcmp(colour, "") != 0 && strcmp(colour, "/") != 0)
				{
					if (!isHaveExternalWorking)
					{
						strcat(objectDesc, "(");
					}
					//颜色做特殊处理获取颜色名称PCM彩板_暗灰拉丝(ML-C1072)
					if (strstr(colour, "(") != NULL)
					{
						//按照_进行拆分。拆分条件
						int valueCount = 0;
						char ** valueChar = new char *[64];
						//分割字符串
						split(colour, "(", valueChar, &valueCount);
						//将第二个字符串按照(拆分
						if (strstr(valueChar[1], ")") != NULL)
						{
							//按照_进行拆分。拆分条件
							int colorNameCount = 0;
							char ** colorNameChar = new char *[64];
							//分割字符串
							split(valueChar[1], ")", colorNameChar, &colorNameCount);
							if (strstr(colorNameChar[0], "-") != NULL)
							{
								//按照_进行拆分。拆分条件
								int colorName = 0;
								char ** colorChar = new char *[64];
								//分割字符串
								split(colorNameChar[0], "-", colorChar, &colorName);
								char colorChars[256] = "";
								for (int kkk = 0; kkk < colorName; kkk++)
								{
									if (kkk == 0 && strcmp(colorChar[kkk], "ML") == 0)
									{
										continue;
									}
									strcat(colorChars, colorChar[kkk]);
									if (kkk < (colorName - 1))
									{
										strcat(colorChars, "-");
									}
								}
								colour = colorChars;
							}
							else
							{
								colour = colorNameChar[0];
							}
						}
					}
					strcat(objectDesc, colour);
					strcat(objectDesc, ")");
				}
				else if(colour == NULL || strcmp(colour, "") == 0)
				{
					printf("颜色为空\n");
					err += "颜色为空 ";
				}
				else if (isHaveExternalWorking)
				{
					strcat(objectDesc, ")");
				}


				if ((figureNum != NULL && strcmp(figureNum, "") != 0 && strcmp(figureNum, "/") != 0) ||
					(identification != NULL && strcmp(identification, "") != 0 && strcmp(identification, "/") != 0) ||
					(specifications != NULL && strcmp(specifications, "") != 0 && strcmp(specifications, "/") != 0))
				{
					strcat(objectDesc, "_");
				}
				if (figureNum != NULL && strcmp(figureNum, "") != 0 && strcmp(figureNum, "/") != 0)
				{
					strcat(objectDesc, figureNum);
				}
				if (identification != NULL && strcmp(identification, "") != 0 && strcmp(identification, "/") != 0)
				{
					strcat(objectDesc, identification);
				}
				if (specifications != NULL && strcmp(specifications, "") != 0 && strcmp(specifications, "/") != 0)
				{
					strcat(objectDesc, specifications);
				}
				////判断物料描述是否超过40个字符
				//int len = gbk_strlen(objectDesc);
				//if (len > 40)
				//{
				//	strcat(resultString, "物料描述：");
				//	strcat(resultString, objectDesc);
				//	strcat(resultString, "已超过40个字符");
				//	strcat(resultString, "/n");

				//	goto loop2;
				//}

				//非空判断提示
				if (strcmp(err.c_str(), "") != 0) {
					EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, err.c_str());
					return 1;
				}


				char * desc = NULL;
				char * stdDesc = NULL;

				AOM_ask_value_string(attachments[i], "object_desc", &desc);
				AOM_ask_value_string(attachments[i], "ml8_StdDesc", &stdDesc);
				////判断是否不需要更改
				//if (strcmp(itemObjectDesc, objectDesc) == 0 && desc != NULL && stdDesc != NULL && strcmp(desc, "") != 0 && strcmp(stdDesc, "") != 0)
				//{
				//	DOFREE(desc);
				//	DOFREE(stdDesc);
				//	goto loop2;
				//}
				DOFREE(desc);
				DOFREE(stdDesc);

				


				//查重物料描述
				int n_entries = 1;
				char *qry_entries[1] = { "描述" }, *qry_values[1] = { objectDesc };
				int n_found = 0;
				tag_t * dba_mbrs = NULLTAG;
				char queryName[256] = "";
				//strcat(queryName, itemObjectType);
				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* checkId;
					AOM_ask_value_string(dba_mbrs[ii], "item_id", &checkId);
					//忽略id中含有"-"的
					if (strstr(checkId, "-") != NULL)
					{
						continue;
					}
					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);
						continue;
					}
					isHaveItem = true;
					strcat(resultString, otherItemId);
					strcat(resultString, "：");
					strcat(resultString, "已存在物料描述：");
					strcat(resultString, objectDesc);
					strcat(resultString, "\n");
					DOFREE(otherItemId);
					//break;
				}
				if (strlen(resultString)==0) {
					AOM_lock(attachments[i]);
					AOM_set_value_string(attachments[i], "object_desc", objectDesc);
					AOM_set_value_string(attachments[i], "ml8_StdDesc", objectDesc);
					AOM_save(attachments[i]);
					AOM_unlock(attachments[i]);
					AOM_refresh(attachments[i], false);
				}
				//设置物料描述和标准描述
				

				DOFREE(dba_mbrs);
			}
			else
			{
				if (objectName != NULL && strcmp(objectName, "") != 0 && strcmp(objectName, "/") != 0)
				{
					strcat(objectDesc, objectName);
				}
				else if(objectName == NULL || strcmp(objectName, "") == 0)
				{
					printf("产品名称为空\n");
					err += "产品名称为空";
				}
				bool isHaveExternalWorking = false;
				if (externalWorking != NULL && strcmp(externalWorking, "") != 0 && strcmp(externalWorking, "/") != 0)
				{
					isHaveExternalWorking = true;
					strcat(objectDesc, "(");
					strcat(objectDesc, externalWorking);
				}
				if (colour != NULL && strcmp(colour, "") != 0 && strcmp(colour, "/") != 0)
				{
					if (!isHaveExternalWorking)
					{
						strcat(objectDesc, "(");
					}
					//颜色做特殊处理获取颜色名称PCM彩板_暗灰拉丝(ML-C1072)
					if (strstr(colour, "(") != NULL)
					{
						//按照_进行拆分。拆分条件
						int valueCount = 0;
						char ** valueChar = new char *[64];
						//分割字符串
						split(colour, "(", valueChar, &valueCount);
						//将第二个字符串按照(拆分
						if (strstr(valueChar[1], ")") != NULL)
						{
							//按照_进行拆分。拆分条件
							int colorNameCount = 0;
							char ** colorNameChar = new char *[64];
							//分割字符串
							split(valueChar[1], ")", colorNameChar, &colorNameCount);

							if (strstr(colorNameChar[0], "-") != NULL)
							{
								//按照_进行拆分。拆分条件
								int colorName = 0;
								char ** colorChar = new char *[64];
								//分割字符串
								split(colorNameChar[0], "-", colorChar, &colorName);
								char colorChars[256] = "";
								for (int kkk = 0; kkk < colorName; kkk++)
								{
									if (kkk == 0 && strcmp(colorChar[kkk], "ML") == 0)
									{
										continue;
									}
									strcat(colorChars, colorChar[kkk]);
									if (kkk < (colorName - 1))
									{
										strcat(colorChars, "-");
									}
								}
								colour = colorChars;
							}
							else
							{
								colour = colorNameChar[0];
							}

						}
					}
					strcat(objectDesc, colour);
					strcat(objectDesc, ")");
				}
				else if (colour == NULL || strcmp(colour, "") == 0)
				{
					printf("颜色为空\n");
					err += "颜色为空 ";
				}
				else if (isHaveExternalWorking)
				{
					strcat(objectDesc, ")");
				}
				if ((figureNum != NULL && strcmp(figureNum, "") != 0 && strcmp(figureNum, "/") != 0) ||
					(identification != NULL && strcmp(identification, "") != 0 && strcmp(identification, "/") != 0) ||
					(specifications != NULL && strcmp(specifications, "") != 0 && strcmp(specifications, "/") != 0))
				{
					strcat(objectDesc, "_");
				}
				if (figureNum != NULL && strcmp(figureNum, "") != 0 && strcmp(figureNum, "/") != 0)
				{
					strcat(objectDesc, figureNum);
				}
				if (identification != NULL && strcmp(identification, "") != 0 && strcmp(identification, "/") != 0)
				{
					strcat(objectDesc, identification);
				}
				if (specifications != NULL && strcmp(specifications, "") != 0 && strcmp(specifications, "/") != 0)
				{
					strcat(objectDesc, specifications);
				}
				////判断物料描述是否超过40个字符
				//int len = gbk_strlen(objectDesc);
				//if (len > 40)
				//{
				//	strcat(resultString, "物料描述：");
				//	strcat(resultString, objectDesc);
				//	strcat(resultString, "已超过40个字符");
				//	strcat(resultString, "/n");

				//	goto loop2;
				//}
				//非空判断提示
				printf("err%s\n", err.c_str());
				if (strcmp(err.c_str(), "") != 0) {
					EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, err.c_str());
					return 1;
				}
				char * desc = NULL;
				char * stdDesc = NULL;

				AOM_ask_value_string(attachments[i], "object_desc", &desc);
				AOM_ask_value_string(attachments[i], "ml8_StdDesc", &stdDesc);
				//判断是否不需要更改
				if (strcmp(itemObjectDesc, objectDesc) == 0 && desc != NULL && stdDesc != NULL && strcmp(desc, "") != 0 && strcmp(stdDesc, "") != 0)
				{
					
					//查重物料描述
					int n_entries = 1;
					char* qry_entries[1] = { "描述" }, * qry_values[1] = { objectDesc };
					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* checkId;
						AOM_ask_value_string(dba_mbrs[ii], "item_id", &checkId);
						//忽略id中含有"-"的
						if (strstr(checkId, "-") != NULL)
						{
							continue;
						}
						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);
							continue;
						}
						isHaveItem = true;
						strcat(resultString, otherItemId);
						strcat(resultString, "：");
						strcat(resultString, "已存在物料描述：");
						strcat(resultString, objectDesc);
						strcat(resultString, "\n");
						DOFREE(otherItemId);
						//break;
					}

					DOFREE(desc);
					DOFREE(stdDesc);
					goto loop2;
				}
				DOFREE(desc);
				DOFREE(stdDesc);

				//查重物料描述
				int n_entries = 1;
				char *qry_entries[1] = { "描述" }, *qry_values[1] = { objectDesc };
				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* checkId;
					AOM_ask_value_string(dba_mbrs[ii], "item_id", &checkId);
					//忽略id中含有"-"的
					if (strstr(checkId, "-") != NULL)
					{
						continue;
					}
					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);
						continue;
					}
					isHaveItem = true;
					strcat(resultString, otherItemId);
					strcat(resultString, "：");
					strcat(resultString, "已存在物料描述：");
					strcat(resultString, objectDesc);
					strcat(resultString, "\n");
					DOFREE(otherItemId);
					//break;
				}
				if (isHaveItem)
				{
					goto loop2;
				}

				/*if (n_found > 0)
				{
					strcat(resultString, "系统中已存在物料描述：");
					strcat(resultString, objectDesc);
					strcat(resultString, "/n");

					DOFREE(dba_mbrs);
					goto loop2;
				}*/
				//设置物料描述和标准描述
				if (strlen(resultString) == 0) {
					AOM_lock(attachments[i]);
					AOM_set_value_string(attachments[i], "object_desc", objectDesc);
					AOM_set_value_string(attachments[i], "ml8_StdDesc", objectDesc);
					AOM_save(attachments[i]);
					AOM_unlock(attachments[i]);
					AOM_refresh(attachments[i], false);
				}
				

				DOFREE(dba_mbrs);
			}

		loop2:
			DOFREE(itemObjectType);
			DOFREE(propertyValue);
			DOFREE(objectName);
			DOFREE(externalWorking);
			DOFREE(colour);
			DOFREE(figureNum);
			DOFREE(identification);
			DOFREE(specifications);
		}

		DOFREE(itemType);
		DOFREE(itemObjectDesc);
	}
	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_ChangeObjectDesc用时:" << duration.count() / 1000 << std::endl;
	string usetime = "ML_ChangeObjectDesc用时:";
	usetime.append(std::to_string(duration.count() / 1000));
	WriteLog(true, usetime.c_str());
	printf("=========================更改物料描述 End===================\n");
	return ifail;
}