#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"

#define ITK_err 919012

bool FindNode(TiXmlElement *pRoot, char * nodeName, int &index, TiXmlElement **&pNode);
bool isOK(tag_t item, char * where);
bool whereIsOK(tag_t item, char * where);

int ML_SetProcessNodeOwner(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);
	int sub_task_count = 0;
	tag_t * sub_tasks = NULLTAG;
	//得到根流程下的所有的一级节点
	EPM_ask_sub_tasks(rootTask, &sub_task_count, &sub_tasks);

	//获取参数
	char *argflag = NULL, *argvalue = NULL, *arg = NULL;
	string type;
	vector<string> types;
	char objectType[1024] = "", fileUid[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, "objectType") == 0)
			{
				if (argvalue != NULL)
				{
					//strcpy(objectType, argvalue);
					type.assign(argvalue);
				}
			}
			else if (stricmp(argflag, "fileUid") == 0)
			{
				if (argvalue != NULL)
				{
					strcpy(fileUid, argvalue);
				}
			}
		}
	}

	//解析handler参数
	{
		int strnum = 0;
		types = split2(type, ",");
		if (types.size() == 0)
		{
			EMH_store_error_s1(EMH_severity_error, ITK_err, "未配置流程参数：-type");
			ifail = 1;
			goto end;
		}

	}
	tag_t dataSet = NULLTAG;
	//根据Uid获取数据集
	ITK__convert_uid_to_tag(fileUid, &dataSet);
	AE_reference_type_t ae_ref;
	tag_t ref_obj = NULLTAG;
	AE_ask_dataset_named_ref2(dataSet, "XMLRendering", &ae_ref, &ref_obj);

	char out_path[SS_MAXPATHLEN] = "";
	strcpy(out_path, getenv("temp"));
	strcat(out_path, "\\");
	strcat(out_path, "流程审核人.xml");

	IMF_export_file(ref_obj, out_path);

	char * processName = NULL;
	EPM_ask_name2(rootTask, &processName);
	//cout << "流程名称" << processName << endl;

	TiXmlDocument* m_pDocument = new TiXmlDocument();
	if (!m_pDocument->LoadFile(out_path))
	{
		cout << "无法加载xml文件！" << endl;
	}
	//删除文件
	remove(out_path);
	if (NULL == m_pDocument)
	{
		printf("未找到文件===================\n");
	}
	//printf("找到文件===================\n");
	TiXmlElement *pRoot = m_pDocument->RootElement();
	if (NULL == pRoot)
	{
		printf("未找到根节点===================\n");
	}
	//printf("找到根节点===================\n");

	POM_AM__set_application_bypass(TRUE);
	//for (int ii = 0; ii < attachments_num; ii++)
	for (int ii = 0; ii < 1; ii++)
	{
		char *itemType = NULL;

		AOM_ask_value_string(attachments[ii], "object_type", &itemType);
		//printf("type_class : %s \r\n", itemType);
		//printf("type_class : %s,-objectType：%s \r\n", itemType, type.c_str());
		//if (strcmp(itemType, objectType) != 0)
		if (!isVecHasStr(types, itemType))
		{
			continue;
		}
		//printf("方法执行\n");
		int pTypeNodeCount = 0;
		TiXmlElement ** pTypeNode = (TiXmlElement **)MEM_alloc(1024 * sizeof(TiXmlElement *));

		FindNode(pRoot, itemType, pTypeNodeCount, pTypeNode);
		for (int jj = 0; jj < pTypeNodeCount; jj++)
		{
			int pNodeCount = 0;
			TiXmlElement ** pNode = (TiXmlElement **)MEM_alloc(1024 * sizeof(TiXmlElement *));

			FindNode(pTypeNode[jj], processName, pNodeCount, pNode);

			for (int i = 0; i < sub_task_count; i++)
			{
				char * cur_task_name = NULL;
				//获取节点名称
				EPM_ask_name2(sub_tasks[i], &cur_task_name);
				//cout << "流程节点名称" << cur_task_name << endl;

				for (int j = 0; j < pNodeCount; j++)
				{
					//获取流程节点
					int pChildProcessNodeCount = 0;
					TiXmlElement ** pChildProcessNode = (TiXmlElement **)MEM_alloc(1024 * sizeof(TiXmlElement *));
					FindNode(pNode[j], cur_task_name, pChildProcessNodeCount, pChildProcessNode);

					for (int k = 0; k < pChildProcessNodeCount; k++)
					{
						TiXmlElement *pChild = pChildProcessNode[k];
						//int i = 0;
						for (pChild = pChild->FirstChildElement(); pChild != NULL; pChild = pChild->NextSiblingElement())
						{
							//TiXmlElement *p = pChild;
							//判断是否满足条件
							TiXmlAttribute *pAttr = NULL;
							char * referenced = NULL;
							char * where = NULL;
							for (pAttr = pChild->FirstAttribute(); pAttr != NULL; pAttr = pAttr->Next())
							{
								string propertyName = pAttr->Name();
								string propertyValue = pAttr->Value();

								//cout << "属性名称" << propertyName << endl;
								//cout << "属性值" << U2G(propertyValue.c_str()) << endl;
								if (strcmp(propertyName.c_str(), "referenced") == 0)
								{
									referenced = U2G(propertyValue.c_str());
								}
								else if (strcmp(propertyName.c_str(), "where") == 0)
								{
									where = U2G(propertyValue.c_str());
								}
							}
							tag_t item = attachments[ii];
							char * propertyValue = NULL;
							if (referenced != NULL && strcmp(referenced, "") != 0)
							{
								//获取关系属性的值
								AOM_ask_value_tag(item, referenced, &item);
							}
							//判断是否满足条件
							if (isOK(item, where))
							{
								//获取下挂的用户
								TiXmlElement *pUserChild = pChild;
								//int i = 0;
								for (pUserChild = pUserChild->FirstChildElement(); pUserChild != NULL; pUserChild = pUserChild->NextSiblingElement())
								{
									TiXmlAttribute *pUserAttr = NULL;
									char * referenced = "";
									char * referenced2 = "";
									char * name = "";
									char * group = "";
									char * role = "";
									char * userID = "";
									for (pUserAttr = pUserChild->FirstAttribute(); pUserAttr != NULL; pUserAttr = pUserAttr->Next())
									{
										string propertyName = pUserAttr->Name();
										string propertyValue = pUserAttr->Value();

										//cout << "属性名称" << propertyName << endl;
										//cout << "属性值" << U2G(propertyValue.c_str()) << endl;
										if (strcmp(propertyName.c_str(), "referenced") == 0)
										{
											referenced = strdup(propertyValue.c_str());
										}
										else if (strcmp(propertyName.c_str(), "referenced2") == 0)
										{
											referenced2 = strdup(propertyValue.c_str());
										}
										else if (strcmp(propertyName.c_str(), "propertyName") == 0)
										{
											name = strdup(propertyValue.c_str());
										}
										else if (strcmp(propertyName.c_str(), "group") == 0)
										{
											group = U2G(propertyValue.c_str());
										}
										else if (strcmp(propertyName.c_str(), "role") == 0)
										{
											role = U2G(propertyValue.c_str());
										}
										else if (strcmp(propertyName.c_str(), "userID") == 0)
										{
											userID = strdup(propertyValue.c_str());
										}
									}
									AOM_refresh(attachments[ii], TRUE);
									tag_t userItem = attachments[ii];
									vector<tag_t> userVector;
									if (strcmp(name, "") != 0)
									{
										//判断是否需要通过关系查找
										if (strcmp(referenced, "") != 0)
										{
											int relationNum = 0;
											tag_t * relationTags = NULLTAG;
											AOM_ask_value_tags(userItem, referenced, &relationNum, &relationTags);
											if (relationNum == 0)
											{
												cout << "根据关系未获取到对象" << referenced << endl;
											}
											else
											{
												userItem = relationTags[0];
											}
											//获取关系属性的值
											//AOM_ask_value_tag(item, referenced, &userItem);
										}
										AOM_refresh(userItem, TRUE);
										if (strcmp(referenced2, "") != 0)
										{
											int relationNum = 0;
											tag_t * relationTags = NULLTAG;
											AOM_ask_value_tags(userItem, referenced2, &relationNum, &relationTags);
											if (relationNum == 0)
											{
												cout << "根据关系未获取到对象" << referenced2 << endl;
											}
											else
											{
												userItem = relationTags[0];
											}
											//获取关系属性的值
											//AOM_ask_value_tag(userItem, referenced2, &userItem);
										}
										//获取用户
										char * userId = NULL;
										tag_t user = NULLTAG;
										AOM_ask_value_string(userItem, name, &userId);
										if (strstr(userId, "(") != NULL)
										{
											//按照AND进行拆分。拆分条件
											int valueCount = 0;
											char ** valueChar = new char *[64];
											//分割字符串
											split(userId, "(", valueChar, &valueCount);
											if (valueCount > 1)
											{
												removeChar(valueChar[1], ')');
												userId = valueChar[1];
											}
										}
										SA_find_user2(userId, &user);
										userVector.push_back(user);

										DOFREE(userId);
									}
									if (strcmp(userID, "") != 0)
									{
										tag_t user = NULLTAG;
										SA_find_user2(userID, &user);
										userVector.push_back(user);
									}
									else if (strcmp(role, "") != 0)
									{
										if (strcmp(group, "") != 0)
										{
											tag_t groupTag = NULLTAG;
											int roleCount = 0;
											tag_t * roleTags = NULLTAG;
											SA_find_group(group, &groupTag);
											SA_ask_roles_from_group(groupTag, &roleCount, &roleTags);
											for (int ii = 0; ii < roleCount; ii++)
											{
												char * roleName = NULL;
												//获取角色名称
												SA_ask_role_name2(roleTags[ii], &roleName);
												if (strcmp(role, roleName) == 0)
												{
													int num_of_members = 0;
													tag_t * member_tags = NULLTAG;
													SA_find_groupmember_by_role(roleTags[ii], groupTag, &num_of_members, &member_tags);
													for (int jj = 0; jj < num_of_members; jj++)
													{
														tag_t processNodeUser = NULLTAG;
														SA_ask_groupmember_user(member_tags[jj], &processNodeUser);

														userVector.push_back(processNodeUser);
													}
													DOFREE(member_tags);
												}
												DOFREE(roleName);
											}
											DOFREE(roleTags);
										}
										else
										{
											/*tag_t roleTag = NULLTAG;
											SA_find_role2(role, &roleTag);

											userVector.push_back(roleTag);*/
										}
									}
									else if (strcmp(group, "") != 0)
									{
										tag_t groupTag = NULLTAG;
										SA_find_group(group, &groupTag);
										int num_of_members = 0;
										tag_t * member_tags = NULLTAG;
										SA_find_groupmembers_by_group(groupTag, &num_of_members, &member_tags);
										for (int ii = 0; ii < num_of_members; ii++)
										{
											tag_t processNodeUser = NULLTAG;
											SA_ask_groupmember_user(member_tags[ii], &processNodeUser);
											userVector.push_back(processNodeUser);
										}

										DOFREE(member_tags);
									}
									create_adhoc_signoff(sub_tasks[i], userVector);

									pUserAttr = NULL;
									userVector.clear();
									vector<tag_t>().swap(userVector);
									DOFREE(referenced);
									DOFREE(referenced2);
									DOFREE(name);
									DOFREE(group);
									DOFREE(role);
									DOFREE(userID);
								}

								pUserChild = NULL;
							}

							pAttr = NULL;
							DOFREE(referenced);
							DOFREE(where);
							DOFREE(propertyValue);
						}

						pChild = NULL;
					}

					pChildProcessNode = NULL;
				}


				DOFREE(cur_task_name);
			}

			pNode = NULL;
		}

		pTypeNode = NULL;
		DOFREE(itemType);
	}
	POM_AM__set_application_bypass(FALSE);
	m_pDocument = NULL;
	pRoot = NULL;
	DOFREE(attachments);
	DOFREE(sub_tasks);
	DOFREE(argflag);
	DOFREE(argvalue);
	DOFREE(arg);
	DOFREE(processName);

end:

	auto stopTime = std::chrono::high_resolution_clock::now();
	auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stopTime - startTime);
	//std::cout << "ML_SetProcessNodeOwner用时:" << duration.count() / 1000 << std::endl;
	string usetime = "ML_SetProcessNodeOwner用时:";
	usetime.append(std::to_string(duration.count() / 1000));
	WriteLog(true, usetime.c_str());
	printf("=========================设置节点负责人 End===================\n");
	return 0;
}

bool isOK(tag_t item, char * where)
{
	bool result = true;

	if (strstr(where, "AND") != NULL)
	{
		//按照AND进行拆分。拆分条件
		int valueCount = 0;
		char ** valueChar = new char *[5000];
		//分割字符串
		split(where, "AND", valueChar, &valueCount);

		for (int i = 0; i < valueCount; i++)
		{
			//如果拆分出来包含OR的（列：(A OR B)）
			if (strstr(valueChar[i], "(") != NULL && strstr(valueChar[i], ")") != NULL && strstr(valueChar[i], "OR") != NULL)
			{
				removeChar(valueChar[i], '(');
				removeChar(valueChar[i], ')');
				removeChar(valueChar[i], ' ');

				//根据OR进行拆分
				int orValueCount = 0;
				char ** orValueChar = new char *[5000];
				//分割字符串
				split(valueChar[i], "OR", orValueChar, &orValueCount);
				bool isOK = false;
				for (int j = 0; j < orValueCount; j++)
				{
					//判断是否满足条件
					if (whereIsOK(item, orValueChar[j]))
					{
						isOK = true;
						break;
					}
				}

				delete[]orValueChar;
				orValueChar = NULL;

				if (!isOK)
				{
					result = false;
					break;
				}
			}
			//如果拆分出的AND包含括号的（列：(A AND B) 拆分后就是：(A ）
			else if (strstr(valueChar[i], "(") != NULL)
			{
				//去除左括号
				removeChar(valueChar[i], '(');
				removeChar(valueChar[i], ' ');
				//判断是否满足条件
				if (!whereIsOK(item, valueChar[i]))
				{
					result = false;
					break;
				}
			}
			//如果拆分出的AND包含括号的（列：(A AND B) 拆分后就是：B) ）
			else if (strstr(valueChar[i], ")") != NULL)
			{
				//去除右括号
				removeChar(valueChar[i], ')');
				removeChar(valueChar[i], ' ');
				//判断是否满足条件
				if (!whereIsOK(item, valueChar[i]))
				{
					result = false;
					break;
				}

			}
			//如果拆分出的AND包含OR但不包含括号的（列：A AND B OR C 拆分后就是：A、B OR C ）
			else if (strstr(valueChar[i], "OR") != NULL)
			{
				removeChar(valueChar[i], ' ');
				//根据OR进行拆分
				int orValueCount = 0;
				char ** orValueChar = new char *[5000];
				//分割字符串
				split(valueChar[i], "OR", orValueChar, &orValueCount);

				bool isOk = false;

				for (int j = 1; j < orValueCount; j++)
				{
					if (whereIsOK(item, orValueChar[j]))
					{
						isOk = true;
						break;
					}
				}

				delete[]orValueChar;
				orValueChar = NULL;

				if (!whereIsOK(item, orValueChar[0]) && !isOk)
				{
					isOk = false;
					break;
				}

			}
			else
			{
				removeChar(valueChar[i], ' ');
				//判断是否满足条件
				if (!whereIsOK(item, valueChar[i]))
				{
					result = false;
					break;
				}
			}
		}

		delete[]valueChar;
		valueChar = NULL;
	}
	else
	{
		removeChar(where, ' ');
		result = whereIsOK(item, where);
	}

	return result;
}

//判断条件（例：item.owning_user.user_id=1111）是否满足
bool whereIsOK(tag_t item, char * where)
{
	//cout << "条件" << where << endl;
	bool result = false;
	if (strstr(where, "!=") != NULL)
	{
		//根据等号拆分条件和值
		int valueCount = 0;
		char ** valueChar = new char *[1000];
		//分割字符串
		split(where, "!=", valueChar, &valueCount);

		char * itemWhere = valueChar[0];
		char * value = valueChar[1];
		//判断是否包含.号
		if (strstr(itemWhere, ".") != NULL)
		{
			char *itemType = NULL;

			AOM_ask_value_string(item, "object_type", &itemType);

			//根据等号拆分条件和值
			int itemPropertyCount = 0;
			char ** itemPropertyChar = new char *[64];
			//分割字符串
			split(itemWhere, ".", itemPropertyChar, &itemPropertyCount);

			char * itemAddress = itemPropertyChar[0];
			//判断传进来的是版本还是对象
			if (strstr(itemType, "Revision") != NULL)
			{
				//传进来的是版本
				if (strcmp(itemAddress, "item") == 0)
				{
					ITEM_ask_item_of_rev(item, &item);
				}
				else if (strcmp(itemAddress, "revMaster") == 0)
				{
					tag_t master_form_rel_type = NULLTAG, *form_list = NULLTAG;
					int form_count = 0;
					//获取版本表单
					GRM_find_relation_type(TC_master_form_rtype, &master_form_rel_type);
					GRM_list_secondary_objects_only(item, master_form_rel_type, &form_count, &form_list);
					if (form_count == 0)
					{
						//printf("未找到form,继续...");
					}
					else
					{
						item = form_list[form_count - 1];
					}
					DOFREE(form_list);
				}
			}
			else if (strstr(itemType, "Schedule") != NULL)
			{
			}
			else
			{
				tag_t revisions = NULLTAG;

				ITEM_ask_latest_rev(item, &revisions);
				//传进来的是对象
				if (strcmp(itemAddress, "rev") == 0)
				{
					item = revisions;
				}
				else if (strcmp(itemAddress, "revMaster") == 0)
				{
					tag_t master_form_rel_type = NULLTAG, *form_list = NULLTAG;
					int form_count = 0;
					//获取版本表单
					GRM_find_relation_type(TC_master_form_rtype, &master_form_rel_type);
					GRM_list_secondary_objects_only(revisions, master_form_rel_type, &form_count, &form_list);
					if (form_count == 0)
					{
						//printf("未找到form,继续...");
					}
					else
					{
						item = form_list[form_count - 1];
					}
					DOFREE(form_list);
				}
			}

			//判断拆分出的数量是否大于2
			if (itemPropertyCount > 2)
			{
				//遍历查找关系对象
				for (int i = 1; i < itemPropertyCount - 1; i++)
				{
					//AOM_ask_value_tag(item, itemPropertyChar[i], &item);
					int relationNum = 0;
					tag_t * relationTags = NULLTAG;
					AOM_ask_value_tags(item, itemPropertyChar[i], &relationNum, &relationTags);
					if (relationNum == 0)
					{
						//cout << "根据关系未获取到对象" << itemPropertyChar[i] << endl;
					}
					else
					{
						item = relationTags[0];
					}
				}
				//获取属性值
				char * getValue = NULL;
				AOM_ask_value_string(item, itemPropertyChar[itemPropertyCount - 1], &getValue);
				//cout << "比较条件" << getValue << value << endl;
				if (strcmp("空", value) == 0)
				{
					if (getValue != NULL && strcmp("", getValue) != 0)
					{
						result = true;
					}
				}
				else
				{
					if (strcmp(getValue, value) != 0)
					{
						result = true;
					}
				}
				DOFREE(getValue);
			}
			else if (itemPropertyCount == 2)
			{
				//获取属性值
				char * getValue = NULL;
				AOM_ask_value_string(item, itemPropertyChar[itemPropertyCount - 1], &getValue);
				//cout << "比较条件" << getValue << value << endl;

				if (strcmp("空", value) == 0)
				{
					if (getValue != NULL && strcmp("", getValue) != 0)
					{
						result = true;
					}
				}
				else
				{
					if (strcmp(getValue, value) != 0)
					{
						result = true;
					}
				}
				DOFREE(getValue);
			}

			delete[]itemPropertyChar;
			itemPropertyChar = NULL;
			DOFREE(itemType);
			DOFREE(itemAddress);

		}

		delete[]valueChar;
		valueChar = NULL;
		DOFREE(itemWhere);
		DOFREE(value);
	}
	else if (strstr(where, "=") != NULL)
	{
		//根据等号拆分条件和值
		int valueCount = 0;
		char ** valueChar = new char *[64];
		//分割字符串
		split(where, "=", valueChar, &valueCount);

		char * itemWhere = valueChar[0];
		char * value = valueChar[1];
		//cout << "比较值" << value << endl;
		//判断是否包含.号
		if (strstr(itemWhere, ".") != NULL)
		{
			char *itemType = NULL;

			AOM_ask_value_string(item, "object_type", &itemType);

			//根据等号拆分条件和值
			int itemPropertyCount = 0;
			char ** itemPropertyChar = new char *[64];
			//分割字符串
			split(itemWhere, ".", itemPropertyChar, &itemPropertyCount);

			char * itemAddress = itemPropertyChar[0];
			//判断传进来的是版本还是对象
			if (strstr(itemType, "Revision") != NULL)
			{
				//传进来的是版本
				if (strcmp(itemAddress, "item") == 0)
				{
					ITEM_ask_item_of_rev(item, &item);
				}
				else if (strcmp(itemAddress, "revMaster") == 0)
				{
					tag_t master_form_rel_type = NULLTAG, *form_list = NULLTAG;
					int form_count = 0;
					//获取版本表单
					GRM_find_relation_type(TC_master_form_rtype, &master_form_rel_type);
					GRM_list_secondary_objects_only(item, master_form_rel_type, &form_count, &form_list);
					if (form_count == 0)
					{
						//printf("未找到form,继续...");
					}
					else
					{
						item = form_list[form_count - 1];
					}
					DOFREE(form_list);
				}
			}
			else if (strstr(itemType, "Schedule") != NULL)
			{
			}
			else
			{
				tag_t revisions = NULLTAG;

				ITEM_ask_latest_rev(item, &revisions);
				//传进来的是对象
				if (strcmp(itemAddress, "rev") == 0)
				{
					item = revisions;
				}
				else if (strcmp(itemAddress, "revMaster") == 0)
				{
					tag_t master_form_rel_type = NULLTAG, *form_list = NULLTAG;
					int form_count = 0;
					//获取版本表单
					GRM_find_relation_type(TC_master_form_rtype, &master_form_rel_type);
					GRM_list_secondary_objects_only(revisions, master_form_rel_type, &form_count, &form_list);
					if (form_count == 0)
					{
						//printf("未找到form,继续...");
					}
					else
					{
						item = form_list[form_count - 1];
					}
					DOFREE(form_list);
				}
			}

			//判断拆分出的数量是否大于2
			if (itemPropertyCount > 2)
			{
				//遍历查找关系对象
				for (int i = 1; i < itemPropertyCount - 1; i++)
				{
					PROP_value_type_t propertyType;
					char * valtype_n = NULL;
					//获取属性类型
					/*AOM_ask_value_type(item, itemPropertyChar[i], &propertyType, &valtype_n);
					if (propertyType == PROP_typed_relation)
					{
						int relationNum = 0;
						tag_t * relationTags = NULLTAG;
						AOM_ask_value_tags(item, itemPropertyChar[i], &relationNum,&relationTags);
						if (relationNum == 0)
						{
							cout << "根据关系未获取到对象" << itemPropertyChar[i] << endl;
						}
						else
						{
							item = relationTags[0];
						}
					}
					else
					{
						AOM_ask_value_tag(item, itemPropertyChar[i], &item);
					}*/
					int relationNum = 0;
					tag_t * relationTags = NULLTAG;
					AOM_ask_value_tags(item, itemPropertyChar[i], &relationNum, &relationTags);
					if (relationNum == 0)
					{
						//cout << "根据关系未获取到对象" << itemPropertyChar[i] << endl;
					}
					else
					{
						item = relationTags[0];
					}
					if (item == NULLTAG)
					{
						//cout << "根据关系未获取到对象" << itemPropertyChar[i] << endl;
					}
				}
				//获取属性值
				char * getValue = NULL;
				//cout << "获取属性值" << itemPropertyChar[itemPropertyCount - 1] << endl;
				AOM_ask_value_string(item, itemPropertyChar[itemPropertyCount - 1], &getValue);
				if (getValue == NULL)
				{
					cout << "获取值为NULL" << endl;
				}
				//cout << "值" << getValue << endl;
				//cout << "比较条件" << getValue << value << endl;
				if (strcmp("空", value) == 0)
				{
					if (getValue == NULL || strcmp("", getValue) == 0)
					{
						result = true;
					}
				}
				else
				{
					if (strcmp(getValue, value) == 0)
					{
						result = true;
					}
				}
				DOFREE(getValue);
			}
			else if (itemPropertyCount == 2)
			{
				//获取属性值
				char * getValue = NULL;
				AOM_ask_value_string(item, itemPropertyChar[itemPropertyCount - 1], &getValue);
				//cout << "比较条件" << getValue << value << endl;
				if (strcmp("空", value) == 0)
				{
					if (getValue == NULL || strcmp("", getValue) == 0)
					{
						result = true;
					}
				}
				else
				{
					if (strcmp(getValue, value) == 0)
					{
						result = true;
					}
				}
				DOFREE(getValue);
			}

			delete[]itemPropertyChar;
			itemPropertyChar = NULL;
			DOFREE(itemType);
			DOFREE(itemAddress);
		}

		delete[]valueChar;
		valueChar = NULL;
		DOFREE(itemWhere);
		DOFREE(value);
	}
	else if (strstr(where, "NOT IN") != NULL)
	{
		//根据等号拆分条件和值
		int valueCount = 0;
		char ** valueChar = new char *[64];
		//分割字符串
		split(where, "NOT IN", valueChar, &valueCount);

		char * itemWhere = valueChar[0];
		char * value = valueChar[1];
		vector<char *> vectorValues;

		if (strstr(value, ",") != NULL)
		{
			int vectorValueCount = 0;
			char ** vectorValueChar = new char *[64];
			split(where, ",", vectorValueChar, &vectorValueCount);
			for (int i = 0; i < vectorValueCount; i++)
			{
				vectorValues.push_back(vectorValueChar[i]);
			}
		}
		else
		{
			vectorValues.push_back(value);
		}
		//判断是否包含.号
		if (strstr(itemWhere, ".") != NULL)
		{
			char *itemType = NULL;

			AOM_ask_value_string(item, "object_type", &itemType);

			//根据等号拆分条件和值
			int itemPropertyCount = 0;
			char ** itemPropertyChar = new char *[64];
			//分割字符串
			split(itemWhere, ".", itemPropertyChar, &itemPropertyCount);

			char * itemAddress = itemPropertyChar[0];
			//判断传进来的是版本还是对象
			if (strstr(itemType, "Revision") != NULL)
			{
				//传进来的是版本
				if (strcmp(itemAddress, "item") == 0)
				{
					ITEM_ask_item_of_rev(item, &item);
				}
				else if (strcmp(itemAddress, "revMaster") == 0)
				{
					tag_t master_form_rel_type = NULLTAG, *form_list = NULLTAG;
					int form_count = 0;
					//获取版本表单
					GRM_find_relation_type(TC_master_form_rtype, &master_form_rel_type);
					GRM_list_secondary_objects_only(item, master_form_rel_type, &form_count, &form_list);
					if (form_count == 0)
					{
						//printf("未找到form,继续...");
					}
					else
					{
						item = form_list[form_count - 1];
					}
					DOFREE(form_list);
				}
			}
			else if (strstr(itemType, "Schedule") != NULL)
			{
			}
			else
			{
				tag_t revisions = NULLTAG;

				ITEM_ask_latest_rev(item, &revisions);
				//传进来的是对象
				if (strcmp(itemAddress, "rev") == 0)
				{
					item = revisions;
				}
				else if (strcmp(itemAddress, "revMaster") == 0)
				{
					tag_t master_form_rel_type = NULLTAG, *form_list = NULLTAG;
					int form_count = 0;
					//获取版本表单
					GRM_find_relation_type(TC_master_form_rtype, &master_form_rel_type);
					GRM_list_secondary_objects_only(revisions, master_form_rel_type, &form_count, &form_list);
					if (form_count == 0)
					{
						//printf("未找到form,继续...");
					}
					else
					{
						item = form_list[form_count - 1];
					}
					DOFREE(form_list);
				}
			}

			//判断拆分出的数量是否大于2
			if (itemPropertyCount > 2)
			{
				//遍历查找关系对象
				for (int i = 1; i < itemPropertyCount - 1; i++)
				{
					int relationNum = 0;
					tag_t * relationTags = NULLTAG;
					AOM_ask_value_tags(item, itemPropertyChar[i], &relationNum, &relationTags);
					if (relationNum == 0)
					{
						//cout << "根据关系未获取到对象" << itemPropertyChar[i] << endl;
					}
					else
					{
						item = relationTags[0];
					}
					//AOM_ask_value_tag(item, itemPropertyChar[i], &item);
				}
				//获取属性值
				char * getValue = NULL;
				AOM_ask_value_string(item, itemPropertyChar[itemPropertyCount - 1], &getValue);
				//cout << "比较条件" << getValue << value << endl;
				if (!count(vectorValues, getValue))
				{
					result = true;
				}
				DOFREE(getValue);
			}
			else if (itemPropertyCount == 2)
			{
				//获取属性值
				char * getValue = NULL;
				AOM_ask_value_string(item, itemPropertyChar[itemPropertyCount - 1], &getValue);
				//cout << "比较条件" << getValue << value << endl;
				if (!count(vectorValues, getValue))
				{
					result = true;
				}
				DOFREE(getValue);
			}

			delete[]itemPropertyChar;
			itemPropertyChar = NULL;
			DOFREE(itemType);
			DOFREE(itemAddress);
		}

		delete[]valueChar;
		valueChar = NULL;
		DOFREE(itemWhere);
		DOFREE(value);
	}
	else if (strstr(where, "IN") != NULL)
	{
		//根据等号拆分条件和值
		int valueCount = 0;
		char ** valueChar = new char *[64];
		//分割字符串
		split(where, "IN", valueChar, &valueCount);

		char * itemWhere = valueChar[0];
		char * value = valueChar[1];
		vector<char *> vectorValues;

		if (strstr(value, ",") != NULL)
		{
			int vectorValueCount = 0;
			char ** vectorValueChar = new char *[64];
			split(where, ",", vectorValueChar, &vectorValueCount);
			for (int i = 0; i < vectorValueCount; i++)
			{
				vectorValues.push_back(vectorValueChar[i]);
			}
		}
		else
		{
			vectorValues.push_back(value);
		}
		//判断是否包含.号
		if (strstr(itemWhere, ".") != NULL)
		{
			char *itemType = NULL;

			AOM_ask_value_string(item, "object_type", &itemType);

			//根据等号拆分条件和值
			int itemPropertyCount = 0;
			char ** itemPropertyChar = new char *[64];
			//分割字符串
			split(itemWhere, ".", itemPropertyChar, &itemPropertyCount);

			char * itemAddress = itemPropertyChar[0];
			//判断传进来的是版本还是对象
			if (strstr(itemType, "Revision") != NULL)
			{
				//传进来的是版本
				if (strcmp(itemAddress, "item") == 0)
				{
					ITEM_ask_item_of_rev(item, &item);
				}
				else if (strcmp(itemAddress, "revMaster") == 0)
				{
					tag_t master_form_rel_type = NULLTAG, *form_list = NULLTAG;
					int form_count = 0;
					//获取版本表单
					GRM_find_relation_type(TC_master_form_rtype, &master_form_rel_type);
					GRM_list_secondary_objects_only(item, master_form_rel_type, &form_count, &form_list);
					if (form_count == 0)
					{
						//printf("未找到form,继续...");
					}
					else
					{
						item = form_list[form_count - 1];
					}
					DOFREE(form_list);
				}
			}
			else if (strstr(itemType, "Schedule") != NULL)
			{
			}
			else
			{
				tag_t revisions = NULLTAG;

				ITEM_ask_latest_rev(item, &revisions);
				//传进来的是对象
				if (strcmp(itemAddress, "rev") == 0)
				{
					item = revisions;
				}
				else if (strcmp(itemAddress, "revMaster") == 0)
				{
					tag_t master_form_rel_type = NULLTAG, *form_list = NULLTAG;
					int form_count = 0;
					//获取版本表单
					GRM_find_relation_type(TC_master_form_rtype, &master_form_rel_type);
					GRM_list_secondary_objects_only(revisions, master_form_rel_type, &form_count, &form_list);
					if (form_count == 0)
					{
						//printf("未找到form,继续...");
					}
					else
					{
						item = form_list[form_count - 1];
					}
					DOFREE(form_list);
				}
			}

			//判断拆分出的数量是否大于2
			if (itemPropertyCount > 2)
			{
				//遍历查找关系对象
				for (int i = 1; i < itemPropertyCount - 1; i++)
				{
					int relationNum = 0;
					tag_t * relationTags = NULLTAG;
					AOM_ask_value_tags(item, itemPropertyChar[i], &relationNum, &relationTags);
					if (relationNum == 0)
					{
						//cout << "根据关系未获取到对象" << itemPropertyChar[i] << endl;
					}
					else
					{
						item = relationTags[0];
					}
					//AOM_ask_value_tag(item, itemPropertyChar[i], &item);
				}
				//获取属性值
				char * getValue = NULL;
				AOM_ask_value_string(item, itemPropertyChar[itemPropertyCount - 1], &getValue);
				//cout << "比较条件" << getValue << value << endl;
				if (count(vectorValues, getValue))
				{
					result = true;
				}
				DOFREE(getValue);
			}
			else if (itemPropertyCount == 2)
			{
				//获取属性值
				char * getValue = NULL;
				AOM_ask_value_string(item, itemPropertyChar[itemPropertyCount - 1], &getValue);
				//cout << "比较条件" << getValue << value << endl;
				if (count(vectorValues, getValue))
				{
					result = true;
				}
				DOFREE(getValue);
			}

			delete[]itemPropertyChar;
			itemPropertyChar = NULL;
			DOFREE(itemType);
			DOFREE(itemAddress);
		}

		delete[]valueChar;
		valueChar = NULL;
		DOFREE(itemWhere);
		DOFREE(value);
	}
	else if (strstr(where, "NOT LIKE") != NULL)
	{
		//根据等号拆分条件和值
		int valueCount = 0;
		char ** valueChar = new char *[64];
		//分割字符串
		split(where, "!=", valueChar, &valueCount);

		char * itemWhere = valueChar[0];
		char * value = valueChar[1];
		//判断是否包含.号
		if (strstr(itemWhere, ".") != NULL)
		{
			char *itemType = NULL;

			AOM_ask_value_string(item, "object_type", &itemType);

			//根据等号拆分条件和值
			int itemPropertyCount = 0;
			char ** itemPropertyChar = new char *[64];
			//分割字符串
			split(itemWhere, ".", itemPropertyChar, &itemPropertyCount);

			char * itemAddress = itemPropertyChar[0];
			//判断传进来的是版本还是对象
			if (strstr(itemType, "Revision") != NULL)
			{
				//传进来的是版本
				if (strcmp(itemAddress, "item") == 0)
				{
					ITEM_ask_item_of_rev(item, &item);
				}
				else if (strcmp(itemAddress, "revMaster") == 0)
				{
					tag_t master_form_rel_type = NULLTAG, *form_list = NULLTAG;
					int form_count = 0;
					//获取版本表单
					GRM_find_relation_type(TC_master_form_rtype, &master_form_rel_type);
					GRM_list_secondary_objects_only(item, master_form_rel_type, &form_count, &form_list);
					if (form_count == 0)
					{
						//printf("未找到form,继续...");
					}
					else
					{
						item = form_list[form_count - 1];
					}
					DOFREE(form_list);
				}
			}
			else if (strstr(itemType, "Schedule") != NULL)
			{
			}
			else
			{
				tag_t revisions = NULLTAG;

				ITEM_ask_latest_rev(item, &revisions);
				//传进来的是对象
				if (strcmp(itemAddress, "rev") == 0)
				{
					item = revisions;
				}
				else if (strcmp(itemAddress, "revMaster") == 0)
				{
					tag_t master_form_rel_type = NULLTAG, *form_list = NULLTAG;
					int form_count = 0;
					//获取版本表单
					GRM_find_relation_type(TC_master_form_rtype, &master_form_rel_type);
					GRM_list_secondary_objects_only(revisions, master_form_rel_type, &form_count, &form_list);
					if (form_count == 0)
					{
						//printf("未找到form,继续...");
					}
					else
					{
						item = form_list[form_count - 1];
					}
					DOFREE(form_list);
				}
			}

			//判断拆分出的数量是否大于2
			if (itemPropertyCount > 2)
			{
				//遍历查找关系对象
				for (int i = 1; i < itemPropertyCount - 1; i++)
				{
					int relationNum = 0;
					tag_t * relationTags = NULLTAG;
					AOM_ask_value_tags(item, itemPropertyChar[i], &relationNum, &relationTags);
					if (relationNum == 0)
					{
						//cout << "根据关系未获取到对象" << itemPropertyChar[i] << endl;
					}
					else
					{
						item = relationTags[0];
					}
					//AOM_ask_value_tag(item, itemPropertyChar[i], &item);
				}
				//获取属性值
				char * getValue = NULL;
				AOM_ask_value_string(item, itemPropertyChar[itemPropertyCount - 1], &getValue);
				//cout << "比较条件" << getValue << value << endl;
				if (strstr(getValue, value) == NULL)
				{
					result = true;
				}
				DOFREE(getValue);
			}
			else if (itemPropertyCount == 2)
			{
				//获取属性值
				char * getValue = NULL;
				AOM_ask_value_string(item, itemPropertyChar[itemPropertyCount - 1], &getValue);
				//cout << "比较条件" << getValue << value << endl;
				if (strstr(getValue, value) == NULL)
				{
					result = true;
				}
				DOFREE(getValue);
			}

			delete[]itemPropertyChar;
			itemPropertyChar = NULL;
			DOFREE(itemType);
			DOFREE(itemAddress);

		}

		delete[]valueChar;
		valueChar = NULL;
		DOFREE(itemWhere);
		DOFREE(value);
	}
	else if (strstr(where, "LIKE") != NULL)
	{
		//根据等号拆分条件和值
		int valueCount = 0;
		char ** valueChar = new char *[64];
		//分割字符串
		split(where, "LIKE", valueChar, &valueCount);

		char * itemWhere = valueChar[0];
		char * value = valueChar[1];
		//判断是否包含.号
		if (strstr(itemWhere, ".") != NULL)
		{
			char *itemType = NULL;

			AOM_ask_value_string(item, "object_type", &itemType);

			//根据等号拆分条件和值
			int itemPropertyCount = 0;
			char ** itemPropertyChar = new char *[64];
			//分割字符串
			split(itemWhere, ".", itemPropertyChar, &itemPropertyCount);

			char * itemAddress = itemPropertyChar[0];
			//判断传进来的是版本还是对象
			if (strstr(itemType, "Revision") != NULL)
			{
				//传进来的是版本
				if (strcmp(itemAddress, "item") == 0)
				{
					ITEM_ask_item_of_rev(item, &item);
				}
				else if (strcmp(itemAddress, "revMaster") == 0)
				{
					tag_t master_form_rel_type = NULLTAG, *form_list = NULLTAG;
					int form_count = 0;
					//获取版本表单
					GRM_find_relation_type(TC_master_form_rtype, &master_form_rel_type);
					GRM_list_secondary_objects_only(item, master_form_rel_type, &form_count, &form_list);
					if (form_count == 0)
					{
						//printf("未找到form,继续...");
					}
					else
					{
						item = form_list[form_count - 1];
					}
					DOFREE(form_list);
				}
			}
			else if (strstr(itemType, "Schedule") != NULL)
			{
			}
			else
			{
				tag_t revisions = NULLTAG;

				ITEM_ask_latest_rev(item, &revisions);
				//传进来的是对象
				if (strcmp(itemAddress, "rev") == 0)
				{
					item = revisions;
				}
				else if (strcmp(itemAddress, "revMaster") == 0)
				{
					tag_t master_form_rel_type = NULLTAG, *form_list = NULLTAG;
					int form_count = 0;
					//获取版本表单
					GRM_find_relation_type(TC_master_form_rtype, &master_form_rel_type);
					GRM_list_secondary_objects_only(revisions, master_form_rel_type, &form_count, &form_list);
					if (form_count == 0)
					{
						//printf("未找到form,继续...");
					}
					else
					{
						item = form_list[form_count - 1];
					}
					DOFREE(form_list);
				}
			}

			//判断拆分出的数量是否大于2
			if (itemPropertyCount > 2)
			{
				//遍历查找关系对象
				for (int i = 1; i < itemPropertyCount - 1; i++)
				{
					int relationNum = 0;
					tag_t * relationTags = NULLTAG;
					AOM_ask_value_tags(item, itemPropertyChar[i], &relationNum, &relationTags);
					if (relationNum == 0)
					{
						//cout << "根据关系未获取到对象" << itemPropertyChar[i] << endl;
					}
					else
					{
						item = relationTags[0];
					}
					//AOM_ask_value_tag(item, itemPropertyChar[i], &item);
				}
				//获取属性值
				char * getValue = NULL;
				AOM_ask_value_string(item, itemPropertyChar[itemPropertyCount - 1], &getValue);
				//cout << "比较条件" << getValue << value << endl;
				if (strstr(getValue, value) != NULL)
				{
					result = true;
				}
				DOFREE(getValue);
			}
			else if (itemPropertyCount == 2)
			{
				//获取属性值
				char * getValue = NULL;
				AOM_ask_value_string(item, itemPropertyChar[itemPropertyCount - 1], &getValue);
				//cout << "比较条件" << getValue << value << endl;
				if (strstr(getValue, value) != NULL)
				{
					result = true;
				}
				DOFREE(getValue);
			}

			delete[]itemPropertyChar;
			itemPropertyChar = NULL;
			DOFREE(itemType);
			DOFREE(itemAddress);
		}

		delete[]valueChar;
		valueChar = NULL;
		DOFREE(itemWhere);
		DOFREE(value);
	}

	return result;
}

//根据节点属性值，获取节点的集合
bool FindNode(TiXmlElement *pRoot, char * nodeName, int &index, TiXmlElement **&pNode)
{
	TiXmlElement *p = pRoot;
	//int i = 0;
	for (p = p->FirstChildElement(); p != NULL; p = p->NextSiblingElement())
	{
		TiXmlAttribute *pAttr = NULL;
		for (pAttr = p->FirstAttribute(); pAttr != NULL; pAttr = pAttr->Next())
		{
			char * value = NULL;
			value = U2G(pAttr->Value());
			if (strcmp(value, nodeName) == 0)
			{
				pNode[index] = p;
				index++;
			}
		}
	}
	return false;
}