#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"

void removeSubstringRelease(char* str, const char* toRemove) {
	char* pos = strstr(str, toRemove);
	if (pos != NULL) {
		// 将子串之后的内容前移覆盖
		memmove(pos, pos + strlen(toRemove), strlen(pos + strlen(toRemove)) + 1);
	}
}

int RB_SendErpItem_New_Rule_Release(EPM_rule_message_t msg)
{

	char* log_file = NULL;
	printf("创建日志文件\n");
	//char* TO_SRM = (char*)malloc(sizeof("TO_SRM"));
	char tc_log_file_name[200] = "";
	strcpy(tc_log_file_name, "RB_SendErpItem_New_Rule_Release");
	CreateLogFile(tc_log_file_name, &log_file);


	WriteLog("=========================对象下发到ERP Start rule release===================\n");
	
	int ifail = EPM_go;
	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* job_name = NULL;
	ITKCALL(AOM_ask_value_string(rootTask, "job_name", &job_name));
	WriteLog("流程名称：%s\n", job_name);
	int email_count = 0;
	char** email_values;
	ITKCALL(PREF_ask_char_values("RB_SEND_ERP_EMAIL", &email_count, &email_values));

	vector<string> mail_addrs;

	if (email_count > 0) {

		int valueCount = 0;
		char** valueChar = new char* [128];
		//分割字符串
		split(email_values[0], ";", valueChar, &valueCount);
		for (int i = 0; i < valueCount; i++) {
			mail_addrs.push_back(valueChar[i]);
		}
	}


	int user_cnt = mail_addrs.size();
	WriteLog("user_cnt===%d\n", user_cnt);
	
	
	
	int pref_cnt = 0;
	char** pref_vals = NULL;
	//获取首选项的值
	PREF_ask_char_values("RB_ItemSendErp_Type_Attr", &pref_cnt, &pref_vals);
	map<string, map<string, string>> itemTypeMap;
	map<string, string> typePropertyType;
	for (int i = 0; i < pref_cnt; i++)
	{
		if (strstr(pref_vals[i], ":") != NULL)
		{
			//按照-进行拆分。拆分条件
			int valueCount = 0;
			char** valueChar = new char* [64];
			//分割字符串
			split(pref_vals[i], ":", valueChar, &valueCount);
			if (strstr(valueChar[1], "|") != NULL)
			{
				int attrCount = 0;
				char** attrChar = new char* [128];
				//分割字符串
				split(valueChar[1], "|", attrChar, &attrCount);
				for (int j = 0; j < attrCount; j++)
				{
					if (strstr(attrChar[j], "=") != NULL)
					{
						int count = 0;
						char** attrs = new char* [64];
						//分割字符串
						split(attrChar[j], "=", attrs, &count);
						typePropertyType[attrs[0]] = attrs[1];
						DOFREE(attrs);
					}
				}
				DOFREE(attrChar);
			}
			itemTypeMap[valueChar[0]] = typePropertyType;
			typePropertyType.clear();
			DOFREE(valueChar);

		}
	}
	int unit_pref_cnt = 0;
	char** unit_pref_vals = NULL;
	//获取首选项的值
	PREF_ask_char_values("RB_SEND_UNIT_MATCHING", &unit_pref_cnt, &unit_pref_vals);
	map<string, string> unitMap;
	for (int i = 0; i < unit_pref_cnt; i++)
	{
		if (strstr(unit_pref_vals[i], "=") != NULL)
		{
			//按照-进行拆分。拆分条件
			int valueCount = 0;
			char** valueChar = new char* [64];
			//分割字符串
			split(unit_pref_vals[i], "=", valueChar, &valueCount);
			unitMap[valueChar[0]] = valueChar[1];
			DOFREE(valueChar);
		}
	}
	int smzq_pref_cnt = 0;
	char** smzq_pref_vals = NULL;
	//获取首选项的值
	PREF_ask_char_values("RB3_WL_SMZQ", &smzq_pref_cnt, &smzq_pref_vals);
	map<string, string> smzqMap;
	for (int i = 0; i < smzq_pref_cnt; i++)
	{
		if (strstr(smzq_pref_vals[i], "=") != NULL)
		{
			//按照-进行拆分。拆分条件
			int valueCount = 0;
			char** valueChar = new char* [64];
			//分割字符串
			split(smzq_pref_vals[i], "=", valueChar, &valueCount);
			smzqMap[valueChar[0]] = valueChar[1];
			DOFREE(valueChar);
		}
	}
	int c_sql_value_count = 0;
	char** c_sql_values;
	ITKCALL(PREF_ask_char_values("RB3_SQL_Connect2", &c_sql_value_count, &c_sql_values));
	if (c_sql_value_count != 3)
	{
		WriteLog("首选项 RB3_SQL_Connect2 配置错误 数量%d  ", c_sql_value_count);
		CloseLog();
		return 1;
	}

	char* tc_root_file = getenv("tc_root");		//C:\Siemens\Teamcenter11
	//+++
	char parameters[100000] = "";//写入到文件的值
	strcat(parameters, "[");
	//获取当前时间
	time_t now = time(0);
	tm* p = localtime(&now);
	char date[128] = "";
	sprintf_s(date, "%04d%02d%02d%02d%02d%02d", 1900 + p->tm_year, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);

	//"tc11","infodba","//172.16.50.40/tc11"    "TC12","infodba","172.16.68.13/tc1"

	map<string, map<string, int>> classMap;
	for (int i = 0; i < attachments_num; i++)
	{
		int status = ConnServer(c_sql_values[1], c_sql_values[2], c_sql_values[0]);
		WriteLog("status：%d\n", status);
		char* itemType = NULL;
		POM_AM__set_application_bypass(true);
		AOM_ask_value_string(attachments[i], "object_type", &itemType);
		POM_AM__set_application_bypass(false);
		WriteLog("itemType：%s\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;
		}

		char* itemId1 = NULL;
		AOM_ask_value_string(attachments[i], "item_id", &itemId1);
		WriteLog("itemId1：%s\n", itemId1);
		if (itemTypeMap.find(itemType) != itemTypeMap.end()) {
			map<string, string> propMap = itemTypeMap[itemType];
			if (strstr(parameters, "[{") != NULL) {
				strcat(parameters, ",");
			}
			strcat(parameters, "{");
			tag_t ico_tag = NULLTAG;
			char* classId = NULL;
			map<string, int> attrMap;
			ICS_ask_classification_object(attachments[i], &ico_tag);
			if (ico_tag != NULLTAG) {
				ICS_ico_ask_class(ico_tag, &classId);
				WriteLog(">> classId: %s\n", classId);
				//分类属性名称对应id map存储
				if (classMap.find(classId) == classMap.end()) {
					int count = 0;
					int* ids;
					int* arraySize;
					int* formats;
					int* options;
					char** names;
					char** shortNames;
					char** annotations;
					char** unit;
					char** minValues;
					char** maxValues;
					char** defaultValues;
					char** descriptions;
					ITKCALL(ICS_class_describe_attributes(classId, &count, &ids, &names, &shortNames, &annotations, &arraySize,
						&formats, &unit, &minValues, &maxValues, &defaultValues, &descriptions, &options));
					for (int i = 0; i < count; i++)
					{
						attrMap.insert(pair<string, int>(names[i], ids[i]));
					}
					classMap.insert(pair<string, map<string, int>>(classId, attrMap));
					if (ids != NULL) {
						DOFREE(ids);
					}
					if (arraySize != NULL) {
						DOFREE(arraySize);
					}
					if (formats != NULL) {
						DOFREE(formats);
					}
					if (options != NULL) {
						DOFREE(options);
					}
					if (names != NULL) {
						DOFREE(names);
					}
					if (shortNames != NULL) {
						DOFREE(shortNames);
					}
					if (annotations != NULL) {
						DOFREE(annotations);
					}
					if (unit != NULL) {
						DOFREE(unit);
					}
					if (minValues != NULL) {
						DOFREE(minValues);
					}
					if (maxValues != NULL) {
						DOFREE(maxValues);
					}
					if (defaultValues != NULL) {
						DOFREE(defaultValues);
					}
					if (descriptions != NULL) {
						DOFREE(descriptions);
					}
				}
				else {
					attrMap = classMap[classId];
				}
			}

			char* classCode = NULL;
			char* cgCode = NULL;
			char* className = NULL;
			char* organization = NULL;
			char* punit = NULL;
			char factoryCode[2048] = "\0";
			//这里写服务名,不能用实例名(名称相同除外)
			if (status)
			{
				WriteLog("提示:中间数据表访问失败\n");
			}
			else {
				char* field1 = NULL;
				char* field2 = NULL;
				int outputColumn = 0, outputValueCount = 0;
				char*** outputValue = NULL;
				WriteLog("提示:中间数据表访问成功\n");
				//物料分类特殊处理,通过数据库表匹配
				char sql[256] = "\0";
				if (strcmp("RB3_LBJRevision", itemType) == 0 || strcmp("RB3_GNZCRevision", itemType) == 0
					|| strcmp("RB3_GNLBJRevision", itemType) == 0 || strcmp("RB3_GYZYRevision", itemType) == 0 || strcmp("RB3_YCLRevision", itemType) == 0) {
					if (strcmp("RB3_LBJRevision", itemType) == 0 && ico_tag != NULLTAG)
					{
						ITKCALL(AOM_UIF_ask_value(attachments[i], "object_name", &field1));
						char* value = NULL;
						ICS_ask_attribute_value(ico_tag, "类别", &value);
						if (value != NULL) {
							int id = attrMap["类别"];
							getClassValue(value, id, &field2);
							DOFREE(value);
						}
					}
					else if (strcmp("RB3_GNZCRevision", itemType) == 0 && ico_tag != NULLTAG)
					{
						char* value = NULL;
						ICS_ask_attribute_value(ico_tag, "产品类别", &value);
						if (value != NULL) {
							int id = attrMap["产品类别"];
							getClassValue(value, id, &field1);
							DOFREE(value);
						}
						ITKCALL(AOM_UIF_ask_value(attachments[i], "object_name", &field2));
					}
					else if (strcmp("RB3_GNLBJRevision", itemType) == 0 && ico_tag != NULLTAG)
					{
						ITKCALL(AOM_UIF_ask_value(attachments[i], "object_name", &field1));
						char* value = NULL;
						ICS_ask_attribute_value(ico_tag, "产品类别", &value);
						if (value != NULL) {
							int id = attrMap["产品类别"];
							getClassValue(value, id, &field2);
							DOFREE(value);
						}
					}
					else if (strcmp("RB3_GYZYRevision", itemType) == 0 && ico_tag != NULLTAG)
					{

						if (startsWith(classId, '1', '2') || startsWith(classId, '1', '7') || startsWith(classId, '1', '3') || startsWith(classId, '1', '4')) {
						
							char* new_classId = NULL;
							if (startsWith(classId, '1', '2') || startsWith(classId, '1', '7'))
							{
								new_classId = getFirstStr(classId, 2);
							}
							else
							{
								new_classId = getFirstStr(classId, 4);
							}
							tag_t classTag = NULLTAG;
							char* id = NULL;
							if (new_classId != NULL) {
								ITKCALL(ICS_find_class(new_classId, &classTag));
								if (classTag != NULLTAG)
								{
									ITKCALL(ICS_ask_id_name(classTag, &id, &field1));
								}
							}
							if (id != NULL) {
								DOFREE(id);
							}
							if (classTag != NULL) {
								DOFREE(classTag);
							}
							if (new_classId != NULL) {
								DOFREE(new_classId);
							}
						}
						else {
							strcat(parameters, "}");
							continue;
						
						}


					}
					else if (strcmp("RB3_YCLRevision", itemType) == 0 && ico_tag != NULLTAG)
					{
						ITKCALL(AOM_UIF_ask_value(attachments[i], "rb3_bjlx", &field1));
						char* value = NULL;
						ICS_ask_attribute_value(ico_tag, "材料形状", &value);
						if (value != NULL) {
							int id = attrMap["材料形状"];
							getClassValue(value, id, &field2);
							DOFREE(value);
						}
					}
					if (field1 != NULL) {
						WriteLog("1field1=== %s \n", field1);
						if (field1 != NULL && field2 != NULL) {
							sprintf(sql, "select PCLASSCODE,PCLASSNAME,NEW_COLUMN,PUNIT from PLM_ERP_CLASS_TABLE where  PTYPE= '%s' and PFIELD01= '%s' and PFIELD02= '%s'", itemType, field1, field2);
						}
						else {
							sprintf(sql, "select PCLASSCODE,PCLASSNAME,NEW_COLUMN,PUNIT from PLM_ERP_CLASS_TABLE where PTYPE= '%s' and PFIELD01= '%s'", itemType, field1);
						}
					}
				}
				else {
					if (strcmp("RB3_ZCRevision", itemType) == 0)
					{
						ITKCALL(AOM_UIF_ask_value(attachments[i], "rb3_zclx", &field1));
					}
					else if (strcmp("RB3_BZJRevision", itemType) == 0 || strcmp("RB3_BZJBJRevision", itemType) == 0
						|| strcmp("RB3_YZRevision", itemType) == 0 || strcmp("RB3_GQRevision", itemType) == 0
						|| strcmp("RB3_GQBJRevision", itemType) == 0 || strcmp("RB3_GZRevision", itemType) == 0
						|| strcmp("RB3_GZBJRevision", itemType) == 0)
					{
						ITKCALL(AOM_UIF_ask_value(attachments[i], "object_name", &field1));
					}
					else if (strcmp("RB3_SLRevision", itemType) == 0
						|| strcmp("RB3_XJRevision", itemType) == 0 || strcmp("RB3_WJTLRevision", itemType) == 0)
					{
						ITKCALL(AOM_UIF_ask_value(attachments[i], "rb3_bjlx", &field1));
					}
					else if (strcmp("RB3_XZCPRevision", itemType) == 0)
					{
						ITKCALL(AOM_UIF_ask_value(attachments[i], "rb3_cplx2", &field1));
					}
					else if (strcmp("RB3_XZLBJRevision", itemType) == 0 && ico_tag != NULLTAG)
					{
						char* value = NULL;
						ICS_ask_attribute_value(ico_tag, "零件类别", &value);
						if (value != NULL) {
							int id = attrMap["零件类别"];
							getClassValue(value, id, &field1);
							DOFREE(value);
						}
					}
					if (field1 != NULL) {
						sprintf(sql, "select PCLASSCODE,PCLASSNAME,NEW_COLUMN,PUNIT from PLM_ERP_CLASS_TABLE where PTYPE= '%s' and PFIELD01= '%s'", itemType, field1);
					}
				}
				WriteLog("sql==- %s \n", sql);
				if (strcmp("", sql) != 0) {
					WriteLog("提示:sql==%s\n", sql);
					if (QuerySQLNoInputParam(sql, &outputColumn, &outputValueCount, &outputValue) == -1 || outputValueCount == 0)
					{
						WriteLog("提示:物料分类查询 失败, %s \n", sql);
						WriteLog("2field1=== %s \n", field1);
						if (field1 != NULL) {
							char sql2[256] = "\0";
							sprintf(sql2, "select PCLASSCODE,PCLASSNAME,NEW_COLUMN,PUNIT from PLM_ERP_CLASS_TABLE where PTYPE= '%s' and PFIELD01= '%s'", itemType, field1);
							WriteLog("提示:sql2==%s\n", sql2);
							if (QuerySQLNoInputParam(sql2, &outputColumn, &outputValueCount, &outputValue) == -1)
							{
								WriteLog("提示:物料分类查询 失败2, %s \n", sql2);
							}
							WriteLog("1111\n");
						}
					}
					WriteLog("outputValueCount=%d\n", outputValueCount);
					WriteLog("outputColumn=%d\n", outputColumn);
					//free(sql);
					if (outputValueCount > 0) {
						classCode = outputValue[0][0];
						className = outputValue[0][1];
						organization = outputValue[0][2];
						punit = outputValue[0][3];
						WriteLog("物料分类====%s\n", classCode);
						WriteLog("物料名称====%s\n", className);
						WriteLog("组织====%s\n", organization);
						WriteLog("单位====%s\n", punit);
					}
					else {
						string errMsg = "";
						errMsg.append(itemId1);
						errMsg.append("物料分类查询请检查表PLM_ERP_CLASS_TABLE");
						EMH_store_error_s2(EMH_severity_error, 919031, errMsg.c_str(), "失败");
						DisConnServer();


						WriteLog("发送邮件：%d", user_cnt);
						for (int i = 0; i < user_cnt; i++) {
								char cmd[10240] = ""; // 64KB
							char jarfile[512] = "";
							string mail_addr = mail_addrs[i];
							if (mail_addr.size() > 0) {
								WriteLog(">> %d. 发送邮件给[%s]", i + 1, mail_addr.c_str());
								sprintf(jarfile, "%s\\bin\\EMailSender.jar", getenv("TC_ROOT"));
								strcpy_s(cmd, "java -jar ");
								strcat_s(cmd, jarfile);
								strcat_s(cmd, " \"");
								strcat_s(cmd, mail_addr.c_str());
								strcat_s(cmd, "\" \"【TeamCenter】传递ERP-ITEM提醒\" \"");
								strcat_s(cmd, job_name);
								strcat_s(cmd, ":组织数据失败->");
								strcat_s(cmd, errMsg.c_str());
								strcat_s(cmd, "\"");
								WriteLog("CMD：%s", cmd);
								system(cmd);
								WriteLog("执行完成");
							}
							DOFREE(cmd);
						}

						return EPM_nogo;
						CloseLog();
					}
				}
				else {

					continue;

				}


				// 
				//工厂组织编码特殊处理,通过数据库表匹配
				char** factorys = NULL;
				int factoryCount = 0;
				if (strcmp("RB3_XZCPRevision", itemType) == 0 || strcmp("RB3_XZLBJRevision", itemType) == 0) {
					ITKCALL(AOM_ask_value_strings(attachments[i], "rb3_xzscgc", &factoryCount, &factorys));
				}
				else if (strcmp("RB3_GNZCRevision", itemType) == 0 || strcmp("RB3_GNLBJRevision", itemType) == 0
					|| strcmp("RB3_GYZYRevision", itemType) == 0 || strcmp("RB3_BZJRevision", itemType) == 0) {
					ITKCALL(AOM_ask_value_strings(attachments[i], "rb3_scgc", &factoryCount, &factorys));
				}
				else if (strcmp("RB3_YCLRevision", itemType) == 0 || strcmp("RB3_XJRevision", itemType) == 0 || strcmp("RB3_WJTLRevision", itemType) == 0 || strcmp("RB3_SLRevision", itemType) == 0) {
					ITKCALL(AOM_ask_value_strings(attachments[i], "rb3_sygc1", &factoryCount, &factorys));
				}
				else {
					ITKCALL(AOM_ask_value_strings(attachments[i], "rb3_scgc1", &factoryCount, &factorys));
				}
				WriteLog("factoryCount=%d\n", factoryCount);
				for (int j = 0; j < factoryCount; j++)
				{
					char*** factoryValue = NULL;
					int factoryColumn = 0, factoryValueCount = 0;
					char factorySql[128] = "\0";
					WriteLog("factorys[j]==%s\n", factorys[j]);
					if (factorys[j] == NULL || strcmp(factorys[j],"") == 0 || strcmp(factorys[j], "0") == 0) {
						sprintf(factorySql, "select PFACTORYCODE from PLM_ERP_FACTORY_TABLE where PFACTORY= 'globa100'");
					
					}
					else {
						sprintf(factorySql, "select PFACTORYCODE from PLM_ERP_FACTORY_TABLE where PFACTORYEN= '%s'", factorys[j]);
					
					}
					
					WriteLog("提示:factorySql==%s\n", factorySql);
					if (QuerySQLNoInputParam(factorySql, &factoryColumn, &factoryValueCount, &factoryValue) == -1)
					{
						WriteLog("提示:组织编码查询 失败, %s \n", factorySql);
					}
					WriteLog("factoryValueCount=%d\n", factoryValueCount);
					WriteLog("factoryColumn=%d\n", factoryColumn);
					//free(sql);
					if (factoryValueCount > 0) {

						if (strcmp("", factoryCode) != 0) {
							strcat(factoryCode, ",");
						}

						strcat(factoryCode, factoryValue[0][0]);
						WriteLog("组织编码====%s\n", factoryCode);
					}
					else {

						string errMsg = "";
						errMsg.append(itemId1);
						errMsg.append("组织编码查询,请检查表PLM_ERP_FACTORY_TABLE");
						EMH_store_error_s2(EMH_severity_error, 919031, errMsg.c_str(), "失败");
						DisConnServer();

						WriteLog("发送邮件：%d", user_cnt);
						for (int i = 0; i < user_cnt; i++) {
								char cmd[10240] = ""; // 64KB
							char jarfile[512] = "";
							string mail_addr = mail_addrs[i];
							if (mail_addr.size() > 0) {
							WriteLog(">> %d. 发送邮件给[%s]", i + 1, mail_addr.c_str());
							sprintf(jarfile, "%s\\bin\\EMailSender.jar", getenv("TC_ROOT"));
							strcpy_s(cmd, "java -jar ");
							strcat_s(cmd, jarfile);
							strcat_s(cmd, " \"");
							strcat_s(cmd, mail_addr.c_str());
							strcat_s(cmd, "\" \"【TeamCenter】传递ERP-ITEM提醒\" \"");
							strcat_s(cmd, job_name);
							strcat_s(cmd, ":组织数据失败->");
							strcat_s(cmd, errMsg.c_str());
							strcat_s(cmd, "\"");
							WriteLog("CMD：%s", cmd);
							system(cmd);
							WriteLog("执行完成");
						}
							DOFREE(cmd);
					}
						return EPM_nogo;
						CloseLog();
					}
					WriteLog("1\n");
					if (factoryValue != NULL) {
						WriteLog("2\n");
						DOFREE(factoryValue);
					}
				}
				WriteLog("3\n");
				

				//采购分类特殊处理,通过数据库表匹配
				// 

				char* limit_value = NULL;
				char* object_name = NULL;
				ITKCALL(AOM_UIF_ask_value(attachments[i], "object_name", &object_name));
				WriteLog("4\n");
				char* cgField1 = NULL;
				char* cgField2 = NULL;
				char* cgField3 = NULL;
				char cgSql[256] = "\0";
				char cgSql2[256] = "\0";
				if (strcmp("RB3_YZRevision", itemType) == 0 || strcmp("RB3_BZJRevision", itemType) == 0) {
					//获取object_name与【字段值2】进行匹配，获取到对应【采购分类编码】进行传递	
					sprintf(cgSql,
						"select PCGCODE from PLM_ERP_CG_TABLE where PTYPE= '%s' and PFIELDVALUE02= '%s'",
						itemType, object_name);
				}
				else if (strcmp("RB3_GQRevision", itemType) == 0) {
					/*
					获取分类字段【球类别】的值与【字段值2】进行匹配，
					再获取【字段3】的值，如果【字段3】为空则直接匹配到对应【采购分类编码】进行传递；
					如果有值获取后与【字段值3】进行比对：获取到的值小于第一个数值，即匹配【采购分类编码】的第一个值进行传递；
					获取到的值大于等于第一个数值并小于第二个数值，即匹配【采购分类编码】的第二个值进行传递；
					获取到的值大于等于第二个数值，即匹配【采购分类编码】的第三个值进行传递；
					*/
					char* value = NULL;
					ICS_ask_attribute_value(ico_tag, "球类别", &value);
					if (value != NULL) {
						int id = attrMap["球类别"];
						getClassValue(value, id, &cgField1);
						DOFREE(value);
					}
					ICS_ask_attribute_value(ico_tag, "球规格", &value);
					if (value != NULL) {
						int id = attrMap["球规格"];
						getClassValue(value, id, &cgField3);
						DOFREE(value);
					}
					sprintf(cgSql,
						"select PCGCODE,PFIELDVALUE03 from PLM_ERP_CG_TABLE where PTYPE= '%s' and PFIELDVALUE02= '%s'",
						itemType, cgField1);
				}
				else if (strcmp("RB3_GYZYRevision", itemType) == 0) {
					//先根据获取到的【ERP物料分类】匹配【字段值1】，再根据【字段2】的内容获取编码前六位or编码前四位，
					//获取的数值与【字段值2】进行匹配，从而获取到对应【采购分类编码】进行传递；
					//IN41 IN38 取前4位
					char* newClassId = NULL;
					if (strcmp("IN41", classCode) == 0 || strcmp("IN38", classCode) == 0) {
						newClassId = getFirstStr(classId, 4);
					}
					else {
						newClassId = getFirstStr(classId, 6);
					}
					sprintf(cgSql,
						"select PCGCODE from PLM_ERP_CG_TABLE where PTYPE= '%s' and PFIELDVALUE01= '%s' and PFIELDVALUE02= '%s'",
						itemType, classCode, newClassId);
				}
				else if (strcmp("RB3_GZRevision", itemType) == 0) {
					/*
					获取对应分类字段【滚动体类型】的值与【字段值2】进行匹配，再获取【字段3】的值，
					如果【字段3】为空则直接匹配到对应【采购分类编码】进行传递；
					如果有值获取后与【字段值3】进行比对
					*/
					char* value = NULL;
					ICS_ask_attribute_value(ico_tag, "滚动体类型", &value);
					if (value != NULL) {
						int id = attrMap["滚动体类型"];
						getClassValue(value, id, &cgField1);
						DOFREE(value);
					}
					AOM_UIF_ask_value(attachments[i], "rb3_gzzj", &cgField3);
					sprintf(cgSql,
						"select PCGCODE,PFIELDVALUE03 from PLM_ERP_CG_TABLE where PTYPE= '%s' and PFIELDVALUE02= '%s'",
						itemType, cgField1);
				}
				else if (strcmp("RB3_LBJRevision", itemType) == 0) {
					/*
				如果为IN04、IN03，获取物料分类倒数第二级，与【字段值2】进行匹配，再获取【字段3】的值，如果【字段3】为空则直接匹配到对应【采购分类编码】进行传递；如果有值获取object_name与【字段值3】进行匹配；再获取【字段4】的值，如果【字段4】为空则直接匹配到对应【采购分类编码】进行传递；如果【字段4】有值，则以字段4的值获取对应分类属性的值，与【字段值4】匹配获取采购分类，比对逻辑参照上述区间数值匹配的逻辑举例说明；
				如果为IN08，直接获取分类字段【结构属性】与【字段值3】进行匹配，获取到对应【采购分类编码】进行传递；
				如果为IN09、IN24，获取object_name与【字段值2】进行匹配，获取到对应【采购分类编码】进行传递；
				*/

					if (classCode == NULL) {

						string errMsg = "";
						errMsg.append(itemId1);
						errMsg.append("ERP分类获取错误,请检查物料分类数据!");
						EMH_store_error_s2(EMH_severity_error, 919031, errMsg.c_str(), "失败");


						WriteLog("发送邮件：%d", user_cnt);
						for (int i = 0; i < user_cnt; i++) {
								char cmd[10240] = ""; // 64KB
							char jarfile[512] = "";
							string mail_addr = mail_addrs[i];
							if (mail_addr.size() > 0) {
							WriteLog(">> %d. 发送邮件给[%s]", i + 1, mail_addr.c_str());
							sprintf(jarfile, "%s\\bin\\EMailSender.jar", getenv("TC_ROOT"));
							strcpy_s(cmd, "java -jar ");
							strcat_s(cmd, jarfile);
							strcat_s(cmd, " \"");
							strcat_s(cmd, mail_addr.c_str());
							strcat_s(cmd, "\" \"【TeamCenter】传递ERP-ITEM提醒\" \"");
							strcat_s(cmd, job_name);
							strcat_s(cmd, ":组织数据失败->");
							strcat_s(cmd, errMsg.c_str());
							strcat_s(cmd, "\"");
							WriteLog("CMD：%s", cmd);
							system(cmd);
							WriteLog("执行完成");
						}
							DOFREE(cmd);
					}
						return EPM_nogo;
						CloseLog();
					}
					
					if (strcmp("IN04", classCode) == 0 || strcmp("IN03", classCode) == 0) {
						WriteLog("5\n");
						tag_t parentClass = NULLTAG;
						char* parentClassId = NULL;
						char* parentClassName = NULL;
						if (strstr(object_name, "外圈") != NULL || strcmp("座圈", object_name) == 0) {
							//外形尺寸_外径(mm)
							char* value = NULL;
							ICS_ask_attribute_value(ico_tag, "外形尺寸_外径(mm)", &value);
							if (value != NULL) {
								int id = attrMap["外形尺寸_外径(mm)"];
								getClassValue(value, id, &cgField3);
								DOFREE(value);
							}
						}
						else {
							//	外形尺寸_内径(mm)
							char* value = NULL;
							ICS_ask_attribute_value(ico_tag, "外形尺寸_内径(mm)", &value);
							if (value != NULL) {
								int id = attrMap["外形尺寸_内径(mm)"];
								getClassValue(value, id, &cgField3);
								DOFREE(value);
							}
						}
						ITKCALL(ICS_class_ask_parent(classId, &parentClass, &parentClassId));
						if (parentClass != NULLTAG) {
							ITKCALL(ICS_ask_id_name(parentClass, &parentClassId, &parentClassName));
							sprintf(cgSql,
								"select PCGCODE,PFIELDVALUE04 from PLM_ERP_CG_TABLE where PTYPE= '%s' and PFIELDVALUE02= '%s' and PFIELDVALUE03= '%s'  and PFIELDVALUE01= '%s'",
								itemType, parentClassName, object_name, classCode);
							sprintf(cgSql2,
								"select PCGCODE,PFIELDVALUE04 from PLM_ERP_CG_TABLE where PTYPE= '%s' and PFIELDVALUE02= '%s'  and PFIELDVALUE01= '%s'",
								itemType, parentClassName, classCode);
						}
					}
					else if (strcmp("IN08", classCode) == 0) {
						WriteLog("6\n");
						char* value = NULL;
						ICS_ask_attribute_value(ico_tag, "保持架类型", &value);
						if (value != NULL) {
							WriteLog("value=====%s\n", value);
							int id = attrMap["保持架类型"];
							WriteLog("id=====================%d\n", id);
							getClassValue(value, id, &cgField1);
							DOFREE(value);
						}
						ICS_ask_attribute_value(ico_tag, "结构属性", &value);
						if (value != NULL) {
							WriteLog("value=====%s\n", value);
							int id = attrMap["结构属性"];
							WriteLog("id=====================%d\n", id);
							getClassValue(value, id, &cgField2);
							DOFREE(value);
						}
						sprintf(cgSql,
							"select PCGCODE from PLM_ERP_CG_TABLE where PTYPE= '%s' and PFIELDVALUE02= '%s' and PFIELDVALUE03= '%s'",
							itemType, cgField1, cgField2);
					}
					else if (strcmp("IN09", classCode) == 0 || strcmp("IN24", classCode) == 0) {
						WriteLog("7\n");
						sprintf(cgSql,
							"select PCGCODE from PLM_ERP_CG_TABLE where PTYPE= '%s' and PFIELDVALUE02= '%s'",
							itemType, object_name);
					}
				}
				else if (strcmp("RB3_SLRevision", itemType) == 0 || strcmp("RB3_XJRevision", itemType) == 0) {
					//直接获取到对应【采购分类编码】进行传递
					sprintf(cgSql,
						"select PCGCODE from PLM_ERP_CG_TABLE where PTYPE= '%s'",
						itemType);
				}
				else if (strcmp("RB3_YCLRevision", itemType) == 0) {
					//直接获取分类字段【材料形状】与【字段值2】进行匹配，再获取【字段3】的值：
					//如果【字段3】为空则直接匹配到对应【采购分类编码】进行传递；
					//如果【字段3】的值为材料规格，则获取分类字段【材料规格】值与【字段值3】进行比对，
					// 比对逻辑参照上述区间数值匹配的逻辑举例说明；
					//如果【字段3】的值为用途，则获取分类字段【用途】值与【字段值3】进行匹配，
					//再获取【字段4】的值，如果【字段4】为空则直接匹配到对应【采购分类编码】进行传递；
					//如果【字段4】有值，则以字段4的值获取对应分类属性的值，与【字段值4】匹配获取采购分类
					char* value = NULL;
					ICS_ask_attribute_value(ico_tag, "材料形状", &value);
					if (value != NULL) {
						int id = attrMap["材料形状"];
						getClassValue(value, id, &cgField1);
						DOFREE(value);
					}
					ICS_ask_attribute_value(ico_tag, "用途", &value);
					if (value != NULL) {
						int id = attrMap["用途"];
						getClassValue(value, id, &cgField2);
						DOFREE(value);
					}
					ICS_ask_attribute_value(ico_tag, "材料规格", &value);
					if (value != NULL) {
						int id = attrMap["材料规格"];
						getClassValue(value, id, &cgField3);
						DOFREE(value);
					}
					if (strcmp("IN01", classCode) == 0 || strcmp("IN02", classCode) == 0 || strcmp("IN53", classCode) == 0) {
						sprintf(cgSql,
							"select PCGCODE,PFIELDVALUE04 from PLM_ERP_CG_TABLE where PTYPE= '%s' and PFIELDVALUE02= '%s'",
							itemType, cgField1);
					}
					//20250721增加 1、匹配采购分类时，先匹配表中BIP物料分类；
					else if (strcmp("IN03", classCode) == 0 || strcmp("IN04", classCode) == 0) {
					
						sprintf(cgSql,
							"select PCGCODE,PFIELDVALUE04 from PLM_ERP_CG_TABLE where PTYPE= '%s' and PFIELDVALUE02= '%s' and PFIELDVALUE03= '%s'  and PFIELDVALUE01= '%s'",
							itemType, cgField1, cgField2, classCode);
					}
					else {
						sprintf(cgSql,
							"select PCGCODE,PFIELDVALUE04 from PLM_ERP_CG_TABLE where PTYPE= '%s' and PFIELDVALUE02= '%s' and PFIELDVALUE03= '%s' and PFIELDVALUE01= '%s'",
							itemType, cgField1, cgField2, classCode);
					}
				}

				int cgOutputColumn = 0, cgOutputValueCount = 0;

				char*** cgOutputValue = NULL;

				


				if (strcmp("", cgSql) != 0) {
					WriteLog("提示:cgSql==%s\n", cgSql);
					if (QuerySQLNoInputParam(cgSql, &cgOutputColumn, &cgOutputValueCount, &cgOutputValue) == -1 || cgOutputValueCount == 0)
					{
						if (cgOutputValue != NULL) {
							for (int i1 = 0; i1 < cgOutputValueCount; i1++) {
								for (int i2 = 0; i2 < cgOutputColumn; i2++) {
									DOFREE(cgOutputValue[i1][i2]);
								}
								DOFREE(cgOutputValue[i1]);
							}
							DOFREE(cgOutputValue);
						}
						WriteLog("I2 => C=%d,R=%d\n", cgOutputColumn, cgOutputValueCount);
						cgOutputColumn = 0;
						cgOutputValueCount = 0;
						cgOutputValue = NULL;
						WriteLog("提示:采购分类查询 失败, %s \n", cgSql);
						if (QuerySQLNoInputParam(cgSql2, &cgOutputColumn, &cgOutputValueCount, &cgOutputValue) == -1)
						{
							WriteLog("提示:采购分类查询2 失败, %s \n", cgSql2);
						}
					}
					WriteLog("cgOutputValueCount=%d\n", cgOutputValueCount);
					WriteLog("cgOutputColumn=%d\n", cgOutputColumn);

					/*if (cgOutputValueCount <= 0) {
						string errMsg = "";
						errMsg.append(itemId1);
						errMsg.append("采购分类查询失败,请检查表PLM_ERP_CG_TABLE");
						EMH_store_error_s2(EMH_severity_error, 919031, errMsg.c_str(), "失败");
						return EPM_nogo;
					}*/

					//free(sql);
					//20250721  增加分割类型判断
					if (cgField3 != NULL && (strcmp("IN01", classCode) == 0 || strcmp("IN03", classCode) == 0 || strcmp("IN04", classCode) == 0 || strcmp("IN07", classCode) == 0 || strcmp("IN52", classCode) == 0)) {
						WriteLog("111\n");
						WriteLog("cgField3======%s\n", cgField3);
						char result[200]; // 确保result足够大，以存储结果
					// 计算第二个字符的位置
						size_t second_char_pos = 2;
						if (strstr(cgField3, "φ") != NULL)
						{
							// 截取第二个字符之后的所有内容
							strcpy(result, cgField3 + second_char_pos);
							WriteLog("1\n");
						}
						if (strstr(cgField3, "Φ") != NULL)
						{
							// 截取第二个字符之后的所有内容
							strcpy(result, cgField3 + second_char_pos);
							WriteLog("2\n");
						}
						WriteLog("cgField3====%s\n", result);
						double result_num = atof(result); // 转换为整数
						if (cgOutputValueCount > 0) {
							cgCode = cgOutputValue[0][0];
							limit_value = cgOutputValue[0][1];
							WriteLog("采购分类====%s\n", cgCode);
							WriteLog("值区间====%s\n", limit_value);
							if (limit_value != NULL
								&& strcmp(limit_value, "") != 0
								&& strcmp(limit_value, " ") != 0) {
								if (strstr(limit_value, "-") != NULL)
								{
									//按照-进行拆分。拆分条件
									int numCount = 0;
									char** numChar = new char* [64];
									int valCount = 0;
									char** valChar = new char* [64];
									//分割字符串
									split(limit_value, "-", numChar, &numCount);
									split(cgCode, ",", valChar, &valCount);
									for (int ii = 0; ii < numCount; ii++)
									{
										double int_value1 = atof(numChar[ii]);
										if (result_num < int_value1) {
											cgCode = valChar[ii];
											break;
										}
										else {
											if (ii != numCount - 1) {
												double int_value2 = atof(numChar[ii + 1]);
												if (result_num < int_value2) {
													cgCode = valChar[ii + 1];
													break;
												}
											}
											else {
												cgCode = valChar[numCount - 1];
												break;
											}
										}
									}
									DOFREE(numChar);
								}
							}
							WriteLog("采购分类====%s\n", cgCode);
						}
					}
					else {
						WriteLog("222\n");
						if (cgOutputValueCount > 0) {
							cgCode = cgOutputValue[0][0];
						}
						/*if (true) {
							return 0;
						}*/
						WriteLog("333\n");
					}
					if (object_name != NULL) {
						DOFREE(object_name);
					}
					if (limit_value != NULL) {
						DOFREE(limit_value);
					}
				}
				

				if (factorys != NULL) {
					DOFREE(factorys);
				}
				if (outputValue != NULL) {
					DOFREE(outputValue);
				}
				if (field1 != NULL) {
					DOFREE(field1);
				}
				if (field2 != NULL) {
					DOFREE(field2);
				}
				if (cgField1 != NULL) {
					DOFREE(cgField1);
				}
				if (cgField2 != NULL) {
					DOFREE(cgField2);
				}
				if (cgField3 != NULL) {
					DOFREE(cgField3);
				}
			}
			if (factoryCode != NULL) {
				strcat(parameters, "\"orgCode\":\"");
				int fattrCount = 0;
				char** fattrChar = new char* [128];
				WriteLog("factoryCode=============%s\n", factoryCode);
				split(factoryCode, ",", fattrChar, &fattrCount);

				string factoryCodeStr = "";
				for (int g = 0; g < fattrCount; g++) {

					if (!strstr(factoryCodeStr.c_str(), fattrChar[g])) {
						factoryCodeStr.append(fattrChar[g]);
						factoryCodeStr.append(",");
					}
				}
				WriteLog("factoryCodeStr.c_str()=============%s\n", factoryCodeStr.c_str());
				strcat(parameters, factoryCodeStr.c_str());
				if (organization != NULL && strcmp(organization,"") != 0) {
					strcat(parameters, ",");
					strcat(parameters, organization);
				}
				strcat(parameters, "\",");
			}
			strcat(parameters, "\"manageClassCode\":\"");
			if (classCode != NULL) {
				strcat(parameters, classCode);
			}
			strcat(parameters, "\",");
			strcat(parameters, "\"purchaseClassCode\":\"");
			if (cgCode != NULL) {
				strcat(parameters, cgCode);
			}
			else {
				strcat(parameters, "");
			}
			strcat(parameters, "\",");
			// 使用迭代器遍历map
			int index = 0;
			for (std::map<string, string>::iterator it = propMap.begin(); it != propMap.end(); ++it) {
				string key = it->first;
				string attr = it->second;
				WriteLog("key: %s \n", key.c_str());
				WriteLog("attr: %s \n", attr.c_str());
				if (strcmp("orgCode", key.c_str()) == 0) {
					index++;
					continue;
				}
				if (strstr(attr.c_str(), ".") != NULL)
				{
					int attrCount = 0;
					char** attrChar = new char* [128];
					char* attrName = new char[2048];//足够长
					strcpy(attrName, attr.c_str());
					WriteLog("attrName: %s \n", attrName);
					//分割字符串
					split(attrName, ".", attrChar, &attrCount);
					WriteLog("attrCount=========%d\n", attrCount);
					char* value = NULL;
					if (strcmp("rev", attrChar[0]) == 0) {
						//单位特殊处理
						if (strcmp("uom_tag", attrChar[1]) == 0)
						{
							//tag_t item = NULLTAG;
							//ITKCALL(ITEM_ask_item_of_rev(attachments[i], &item));
							//tag_t unitTag = NULLTAG;
							//ITKCALL(AOM_ask_value_tag(item, attrChar[1], &unitTag));
							//if (unitTag != NULL) {
							//	ITKCALL(AOM_UIF_ask_value(unitTag, "object_string", &value));
							//	WriteLog("uom_tag: %s \n", value);
							//	//单位换算
							//	// 特定物料分类单位换算：滚动体计量单位：L（粒）转WL（万粒）；油脂的计量单位：GM(克) 转KG（千克）;钢材，钢管计量单位：GM(克)转T（吨）
							//	if (className != NULL) {
							//		if (strcmp("滚动体", className) == 0 && strcmp("L(粒)", value) == 0)
							//		{
							//			value = "WL(万粒)";
							//		}
							//		else if (strcmp("油脂", className) == 0 && strcmp("GM(克)", value) == 0)
							//		{
							//			value = "KG(千克)";
							//		}if ((strcmp("钢材", className) == 0 || strcmp("钢管", className) == 0) && strcmp("GM(克)", value) == 0)
							//		{
							//			value = "T(吨)";
							//		}
							//	}
							//	strcat(parameters, "\"");
							//	strcat(parameters, key.c_str());
							//	strcat(parameters, "\":\"");
							//	if (unitMap.find(value) != unitMap.end()) {
							//		strcat(parameters, unitMap[value].c_str());
							//	}
							//	else {
							//		strcat(parameters, value);
							//	}
							//	strcat(parameters, "\"");
							//}
							//else {
							//	strcat(parameters, "\"");
							//	strcat(parameters, key.c_str());
							//	strcat(parameters, "\":\"");
							//	if (punit != NULL) {
							//		strcat(parameters, punit);
							//	}
							//	strcat(parameters, "\"");

							//}
							//if (item != NULL) {
							//	DOFREE(item);
							//}
							//if (unitTag != NULL) {
							//	DOFREE(unitTag);
							//}


							//20250721修改   5、单位字段由原先特定转换改为全部按BIP分类获取物料分类对照表中的单位    
							strcat(parameters, "\"");
							strcat(parameters, key.c_str());
							strcat(parameters, "\":\"");
							if (punit != NULL) {
								strcat(parameters, punit);
							}
							strcat(parameters, "\"");
						}
						//重量特殊处理
						else if (strcmp("netWeight", key.c_str()) == 0)
						{
							strcat(parameters, "\"netWeightUnitCode\":\"KG\",");
							ITKCALL(AOM_UIF_ask_value(attachments[i], attrChar[1], &value));
							WriteLog("value=============================%s\n", value);
							if (value != NULL && strcmp("", value) != 0 && strcmp(value,"/") != 0) {
								double dValue = std::stod(value);
								dValue = dValue * 10000;
								long lValue = static_cast<long>(dValue); // 转换为long
								strcat(parameters, "\"");
								strcat(parameters, key.c_str());
								strcat(parameters, "\":");
								char netWeight[64] = "\0";
								sprintf(netWeight, "%d", lValue);
								strcat(parameters, netWeight);
							}
							else {
								strcat(parameters, "\"");
								strcat(parameters, key.c_str());
								strcat(parameters, "\":0");
							}
						}
						//生命周期特殊处理
						else if (strcmp("material_status", key.c_str()) == 0)
						{
							ITKCALL(AOM_UIF_ask_value(attachments[i], attrChar[1], &value));
							strcat(parameters, "\"");
							strcat(parameters, key.c_str());
							strcat(parameters, "\":\"");
							if (smzqMap.find(value) != smzqMap.end()) {
								strcat(parameters, smzqMap[value].c_str());
							}
							else {
								strcat(parameters, value);
							}
							strcat(parameters, "\"");
						}
						//rb3_th 特殊处理
						else if (strcmp("rb3_th", attrChar[1]) == 0)
						{
							ITKCALL(AOM_UIF_ask_value(attachments[i], attrChar[1], &value));
							WriteLog("rb3_th====%s\n", value);

							if (strstr(value, "XX-") || strstr(value, "SF-")) {
								removeSubstringRelease(value, "XX-");
								removeSubstringRelease(value, "SF-");
							}
							WriteLog("rb3_th===========%s\n", value);

							strcat(parameters, "\"");
							strcat(parameters, key.c_str());
							strcat(parameters, "\":\"");
							strcat(parameters, value);
							strcat(parameters, "\"");
						}
						else {
							ITKCALL(AOM_UIF_ask_value(attachments[i], attrChar[1], &value));
						
							strcat(parameters, "\"");
							strcat(parameters, key.c_str());
							strcat(parameters, "\":\"");
							strcat(parameters, value);
							strcat(parameters, "\"");

						}
					}
					//20250721 增加去掉"无" 拼接的逻辑
					else {
						if (ico_tag != NULLTAG) {
							if (strstr(attrChar[1], "&") != NULL)
							{
								int attrNum = 0;
								char** allAttrs = new char* [128];
								//分割字符串
								split(attrChar[1], "&", allAttrs, &attrNum);
								WriteLog("---attrChar===========%s\n", attrChar[1]);
								char values[256] = "";//多个属性拼接
								for (int i = 0; i < attrNum; i++)
								{
									if (attrMap.find(allAttrs[i]) != attrMap.end())
									{
										ICS_ask_attribute_value(ico_tag, allAttrs[i], &value);

										WriteLog("---value===========%s\n", value);
										WriteLog("---allAttrs[i]===========%s\n", allAttrs[i]);

										if (strcmp(allAttrs[i],"振动加速度") == 0 || strcmp(allAttrs[i], "振动等级") == 0 || strcmp(allAttrs[i], "振动速度") == 0) {
										if (value != NULL) {
											int id = attrMap[allAttrs[i]];
											char* disValue;
											getClassValue(value, id, &disValue);
											WriteLog("---disValue1===========%s\n", disValue);
											if (strcmp(disValue,"无") == 0) {
												strcat(values, "");
											}
											else {
												if (strstr(disValue, ":")) {
													int mhNum = 0;
													char** mhAttrs = new char* [128];
													//分割字符串
													split(disValue, ":", mhAttrs, &mhNum);
													WriteLog("---mhAttrs===========%s\n", mhAttrs[0]);
													strcat(values, mhAttrs[0]);
												}
												else {
												
													int id = attrMap[allAttrs[i]];
													char* disValue;
													getClassValue(value, id, &disValue);
													WriteLog("---disValue22===========%s\n", disValue);
													strcat(values, disValue);
												}

											}

										
											DOFREE(disValue);
										}
										}
										else {
											if (value != NULL) {
												int id = attrMap[allAttrs[i]];
												char* disValue;
												getClassValue(value, id, &disValue);
												WriteLog("---disValue===========%s\n", disValue);
												strcat(values, disValue);
												DOFREE(disValue);
											}
										
										}

									}
									else {
										strcat(values, "");
									}
								}
								strcat(parameters, "\"");
								strcat(parameters, key.c_str());
								strcat(parameters, "\":\"");
								strcat(parameters, values);
								strcat(parameters, "\"");
								DOFREE(allAttrs);
							}
							else {
								//保持架板厚特殊处理
								if (strcmp("CU0261", key.c_str()) == 0)
								{
									if (attrMap.find("保持架板厚(mm)") != attrMap.end())
									{
										attrChar[1] = "保持架板厚(mm)";
									}
									else if (attrMap.find("冲压保持架板厚(mm)") != attrMap.end())
									{
										attrChar[1] = "冲压保持架板厚(mm)";
									}
									else if (attrMap.find("保持架板厚(铁保)") != attrMap.end())
									{
										attrChar[1] = "保持架板厚(铁保)";
									}
								}
								//密封颜色特殊处理
								else if (strcmp("CU0262", key.c_str()) == 0)
								{
									if (attrMap.find("密封颜色") != attrMap.end())
									{
										//WriteLog("1111\n");
										attrChar[1] = "密封颜色";
									}
									else if (attrMap.find("密封颜色(带轮端)") != attrMap.end())
									{
										attrChar[1] = "密封颜色(带轮端)";
									}
									else if (attrMap.find("(内密封件颜色)") != attrMap.end())
									{
										attrChar[1] = "(内密封件颜色)";
									}
								}
								//密封材料特殊处理
								else if (strcmp("CU0263", key.c_str()) == 0)
								{
									if (attrMap.find("密封材料") != attrMap.end())
									{
										//WriteLog("2222\n");
										attrChar[1] = "密封材料";
									}
									else if (attrMap.find("密封材料(带轮端)") != attrMap.end())
									{
										attrChar[1] = "密封材料(带轮端)";
									}
									else if (attrMap.find("(内密封件材料)") != attrMap.end())
									{
										attrChar[1] = "(内密封件材料)";
									}
								}
								//WriteLog("attrChar[1] = %s \n", attrChar[1]);
								if (attrMap.find(attrChar[1]) != attrMap.end())
								{
									ICS_ask_attribute_value(ico_tag, attrChar[1], &value);
									if (value != NULL && strcmp(value, "") != 0 && strcmp(value, " ") != 0) {
										int id = attrMap[attrChar[1]];
										char* disValue;
										getClassValue(value, id, &disValue);
										/*								WriteLog("id = %d \n", id);
																		WriteLog("disValue = %s \n", disValue);*/
										strcat(parameters, "\"");
										strcat(parameters, key.c_str());
										strcat(parameters, "\":\"");
										strcat(parameters, disValue);
										strcat(parameters, "\"");
										DOFREE(disValue);
									}
									else {
										strcat(parameters, "\"");
										strcat(parameters, key.c_str());
										strcat(parameters, "\":\"\"");
									}
								}
								else {
									strcat(parameters, "\"");
									strcat(parameters, key.c_str());
									strcat(parameters, "\":\"\"");
								}
							}
						}
						else {
							strcat(parameters, "\"");
							strcat(parameters, key.c_str());
							strcat(parameters, "\":\"\"");
						}
					}
					WriteLog("value: %s \n", value);
					DOFREE(value);
					DOFREE(attrChar);
					DOFREE(attrName);
				}
				else {
					if (strcmp("CU0266", key.c_str()) == 0 && strcmp("RB3_LBJRevision", itemType) == 0 && ico_tag != NULLTAG)
					{
						WriteLog("零部件特殊处理\n");
						char* value = NULL;
						if (attrMap.find("类别") != attrMap.end())
						{
						
							ICS_ask_attribute_value(ico_tag, "类别", &value);
							WriteLog("类别 : %s \n", value);
							if (value != NULL && strcmp(value,"") != 0) {
								int id = attrMap["类别"];
								char* disValue;
								getClassValue(value, id, &disValue);
								WriteLog("disValue : %s \n", disValue);
								if (disValue != NULL && strcmp("C:车工件", disValue) == 0) {
									tag_t parentClass = NULLTAG;
									char* parentClassId = NULL;
									char* parentClassName = NULL;
									ITKCALL(ICS_class_ask_parent(classId, &parentClass, &parentClassId));
									if (parentClass != NULLTAG) {
										ITKCALL(ICS_ask_id_name(parentClass, &parentClassId, &parentClassName));
										strcat(parameters, "\"");
										strcat(parameters, key.c_str());
										strcat(parameters, "\":\"");
										strcat(parameters, parentClassName);
										strcat(parameters, "\"");
									}
									else {
										strcat(parameters, "\"");
										strcat(parameters, key.c_str());
										strcat(parameters, "\":\"\"");
									}
									DOFREE(parentClass);
									DOFREE(parentClassId);
									DOFREE(parentClassName);
								}
								else {
									strcat(parameters, "\"");
									strcat(parameters, key.c_str());
									strcat(parameters, "\":\"\"");
								}
								DOFREE(disValue);
							}
							else {
								strcat(parameters, "\"");
								strcat(parameters, key.c_str());
								strcat(parameters, "\":\"\"");
							}
						}
						else
						{
							strcat(parameters, "\"");
							strcat(parameters, key.c_str());
							strcat(parameters, "\":\"\"");
						}
						DOFREE(value);
					}//重量特殊处理
					else if (strcmp("netWeight", key.c_str()) == 0)
					{
						strcat(parameters, "\"netWeightUnitCode\":\"KG\",");
						strcat(parameters, "\"");
						strcat(parameters, key.c_str());
						strcat(parameters, "\":0");
					}
					else
					{
						WriteLog("key: %s 默认为空值\n", key.c_str());
						strcat(parameters, "\"");
						strcat(parameters, key.c_str());
						strcat(parameters, "\":\"\"");
					}
				}
				if (index < propMap.size() - 1)
				{
					strcat(parameters, ",");
				}
				index++;
			}
			if (classId != NULL) {
				DOFREE(classId);
			}
			if (ico_tag != NULL) {
				DOFREE(ico_tag);
			}
			if (classCode != NULL) {
				DOFREE(classCode);
			}
			if (cgCode != NULL) {
				DOFREE(cgCode);
			}
			if (className != NULL) {
				DOFREE(className);
			}
			if (organization != NULL) {
				DOFREE(organization);
			}
			if (punit != NULL) {
				DOFREE(punit);
			}
			strcat(parameters, "}");
			attrMap.clear();
		}
		DOFREE(itemType);
		DisConnServer();
	}

	strcat(parameters, "]");
	//WriteLog("parameters: %s\n", parameters);
	//把数据用写入文件
	char data_file[SS_MAXPATHLEN] = "";
	strcat(data_file, tc_root_file);
	strcat(data_file, "\\");
	strcat(data_file, date);
	strcat(data_file, ".txt");
	WriteLog("data_file: %s\n", data_file);
	ofstream file;
	file.open(data_file);
	file << parameters << endl; // 使用与cout同样的方式进行写入
	file.close();

	string strResult;

	//cmd指令
	char cmd[256] = "";
	strcpy(cmd, "java -jar \"");
	strcat(cmd, tc_root_file);
	strcat(cmd, "\\portal\\plugins\\");
	strcat(cmd, "RB_SendErp_New.jar");
	strcat(cmd, "\" ");
	//传参
	//cout << data_file << endl;
	strcat(cmd, data_file);
	//用来传递本流程的流程名称(@分割)
	strcat(cmd, "@WL");
	//WriteLog("路径:\n%s\n", cmd);
	char buf[8000] = { 0 };
	FILE* pf = NULL;
	if ((pf = _popen(cmd, "r")) == NULL) {
		WriteLog("接口返回:\n%s", "1");
	}

	while (fgets(buf, sizeof buf, pf)) {
		strResult += buf;
	}
	_pclose(pf);

	cout << strResult << endl;
	unsigned int iSize = strResult.size();
	char errorMessage[100000] = "";//写入到文件的值
	if (iSize > 0 && strResult[iSize - 1] == '\n' && strlen(parameters) > 0)
	{

		strResult = strResult.substr(0, iSize - 1);
		//WriteLog("下发失败\n");
		//cout << strResult << endl;

		if (strstr(strResult.c_str(), "\"code\":200") != NULL)
		{
			WriteLog("下发成功\n");


		}
		else {
			ifail = EPM_nogo;
			strcat(errorMessage, strResult.c_str());

			WriteLog("发送邮件：%d", user_cnt);
			for (int i = 0; i < user_cnt; i++) {
				char cmd[10240] = ""; // 64KB
				char jarfile[512] = "";
				string mail_addr = mail_addrs[i];
				if(mail_addr.size() > 0){
				WriteLog(">> %d. 发送邮件给[%s]", i + 1, mail_addr.c_str());
				sprintf(jarfile, "%s\\bin\\EMailSender.jar", getenv("TC_ROOT"));
				strcpy_s(cmd, "java -jar ");
				strcat_s(cmd, jarfile);
				strcat_s(cmd, " \"");
				strcat_s(cmd, mail_addr.c_str());
				strcat_s(cmd, "\" \"【TeamCenter】传递ERP-ITEM提醒\" \"");
				strcat_s(cmd, job_name);
				strcat_s(cmd, ":发送数据失败->");
				strcat_s(cmd, errorMessage);
				strcat_s(cmd, "\"");
				WriteLog("CMD：%s", cmd);
				system(cmd);
				WriteLog("执行完成");
			}
				DOFREE(cmd);
			}

		}
	}
	WriteLog("ifail: %d\n", ifail);
	if (attachments != NULL) {
		MEM_free(attachments);
	}
	if (pref_vals != NULL) {
		MEM_free(pref_vals);
	}
	//if (tc_root_file != NULL) {
	//	MEM_free(tc_root_file);
	//}


	if (ifail == EPM_nogo){
		EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, errorMessage);

	}

	WriteLog("=========================对象下发到ERP rule release End===================\n");
	CloseLog();
	return ifail;
}