#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"


void setProperty(tag_t formValue, char * propertyName, tag_t toValue, char * toPropertyName);

int ML_ChangeFormProperty(EPM_action_message_t msg)
{
	printf("=========================更改表单属性 Start===================\n");
	auto startTime = std::chrono::high_resolution_clock::now();
	int ifail = ITK_ok;
	int  attachments_num = 0, targetAttachments_num = 0;
	tag_t  rootTask = NULLTAG, *attachments = NULLTAG, *targetAttachments = NULLTAG;
	//获取任务对象
	EPM_ask_root_task(msg.task, &rootTask);
	//获取任务目标对象
	EPM_ask_attachments(rootTask, EPM_target_attachment, &targetAttachments_num, &targetAttachments);
	//获取任务目标对象
	EPM_ask_attachments(rootTask, EPM_reference_attachment, &attachments_num, &attachments);

	int pref_cnt = 0;
	char ** pref_vals = NULL;
	//获取首选项的值
	PREF_ask_char_values("ML_InputFormProperty_Issue", &pref_cnt, &pref_vals);
	map<string, string> typePropertyType;
	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);
			typePropertyType[valueChar[0]] = valueChar[1];// .insert(std::pair<char*, char*>(valueChar[0], valueChar[1]));
		}
	}

	for (int i = 0; i < attachments_num; i++)
	{
		char *itemType = NULL;

		AOM_ask_value_string(attachments[i], "object_type", &itemType);
		printf("type_class : %s \r\n", itemType);
		//过滤掉非时间表的对象
		if ((strstr(itemType, "Schedule") == NULL))
		{
			DOFREE(itemType);
			continue;
		}
		char * customer_name = NULL;
		//获取时间表的项目类别属性
		AOM_ask_value_string(attachments[i], "customer_name", &customer_name);
		
		tag_t formValue = NULLTAG;
		if (targetAttachments_num > 0)
		{
			formValue = targetAttachments[0];
		}

		if (formValue == NULLTAG)
		{
			DOFREE(itemType);
			DOFREE(pref_vals);
			DOFREE(customer_name);
			DOFREE(formValues);
			DOFREE(objectType);
			continue;
		}
		POM_AM__set_application_bypass(true);
		if (strcmp(customer_name, "产品类") == 0)
		{
			int tagNum = 0;
			tag_t * tagValue = NULLTAG;
			AOM_ask_value_tags(attachments[i], "IMAN_reference", &tagNum, &tagValue);
			for (int j = 0; j < tagNum; j++)
			{
				char * objectType = NULL;

				AOM_ask_value_string(tagValue[j], "object_type", &objectType);

				if ((strstr(objectType, "ML8_ProductQCDForm1") == NULL))
				{
					DOFREE(objectType);
					continue;
				}
				string propertyNames = typePropertyType[objectType];
				if (propertyNames.c_str() != NULL)
				{
					const char * constName = propertyNames.c_str();
					char* name = new char[2048];//足够长
					strcpy(name, constName);
					if (strstr(name, ",") != NULL)
					{
						//按照,进行拆分。拆分条件
						int valueCount = 0;
						char ** valueChar = new char *[64];
						//分割字符串
						split(name, ",", valueChar, &valueCount);
						for (int k = 0; k < valueCount; k++)
						{
							if (strstr(valueChar[k], "=") != NULL)
							{
								//按照-进行拆分。拆分条件
								int valueNum = 0;
								char ** values = new char *[64];
								//分割字符串
								split(valueChar[k], "=", values, &valueNum);
								setProperty(formValue, values[0], tagValue[j], values[1]);
							}
						}

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

						setProperty(formValue, valueChar[0], tagValue[j], valueChar[1]);

						DOFREE(propertyValue);
					}

					DOFREE(constName);
				}
				DOFREE(objectType);

			}
			DOFREE(tagValue);
		}
		else if (strcmp(customer_name, "技术类") == 0)
		{
			int tagNum = 0;
			tag_t * tagValue = NULLTAG;
			AOM_ask_value_tags(attachments[i], "IMAN_reference", &tagNum, &tagValue);
			for (int j = 0; j < tagNum; j++)
			{
				char * objectType = NULL;

				AOM_ask_value_string(tagValue[j], "object_type", &objectType);

				if ((strstr(objectType, "ML8_TechQCDForm1") == NULL))
				{
					DOFREE(objectType);
					continue;
				}
				string propertyNames = typePropertyType[objectType];
				if (propertyNames.c_str() != NULL)
				{
					const char * constName = propertyNames.c_str();
					char* name = new char[2048];//足够长
					strcpy(name, constName);
					if (strstr(name, ",") != NULL)
					{
						//按照-进行拆分。拆分条件
						int valueCount = 0;
						char ** valueChar = new char *[64];
						//分割字符串
						split(name, ",", valueChar, &valueCount);
						for (int k = 0; k < valueCount; k++)
						{
							if (strstr(valueChar[k], "=") != NULL)
							{
								//按照-进行拆分。拆分条件
								int valueNum = 0;
								char ** values = new char *[64];
								//分割字符串
								split(valueChar[k], "=", values, &valueNum);

								setProperty(formValue, values[0], tagValue[j], values[1]);


								//char * propertyValue = NULL;
								//AOM_ask_value_string(formValue, values[0], &propertyValue);
								//if (propertyValue != NULL)
								//{
								//	//先lock
								//	AOM_lock(tagValue[j]);
								//	AOM_set_value_string(tagValue[j], values[1], propertyValue);
								//	//再save
								//	AOM_save(tagValue[j]);
								//	//最后unlock
								//	AOM_unlock(tagValue[j]);
								//	AOM_refresh(tagValue[j], FALSE);
								//}
								//DOFREE(propertyValue);
							}
						}

					}
					else if (strstr(name, "=") != NULL)
					{
						//按照-进行拆分。拆分条件
						int valueCount = 0;
						char ** valueChar = new char *[64];
						//分割字符串
						split(name, "=", valueChar, &valueCount);
						setProperty(formValue, valueChar[0], tagValue[j], valueChar[1]);
					}

					DOFREE(constName);
				}
				DOFREE(objectType);
				
			}
			DOFREE(tagValue);
		}
		POM_AM__set_application_bypass(false);
		DOFREE(itemType);
		DOFREE(pref_vals);
		DOFREE(customer_name);
		DOFREE(formValues);
		DOFREE(objectType);
	}
	DOFREE(attachments);

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

void setProperty(tag_t formValue, char * propertyName, tag_t toValue, char * toPropertyName)
{
	PROP_value_type_t propertyType;
	char * valtype_n = NULL;
	//获取属性类型
	AOM_ask_value_type(formValue, propertyName, &propertyType, &valtype_n);
	switch (propertyType)
	{
	case PROP_date:
		date_t propertyValue;
		AOM_ask_value_date(formValue, propertyName, &propertyValue);
		//先lock
		AOM_lock(toValue);
		AOM_set_value_date(toValue, toPropertyName, propertyValue);
		//再save
		AOM_save(toValue);
		//最后unlock
		AOM_unlock(toValue);
		AOM_refresh(toValue, FALSE);
		break;
	case PROP_double:
		double propertyValue2;
		AOM_ask_value_double(formValue, propertyName, &propertyValue2);
		//先lock
		AOM_lock(toValue);
		AOM_set_value_double(toValue, toPropertyName, propertyValue2);
		//再save
		AOM_save(toValue);
		//最后unlock
		AOM_unlock(toValue);
		AOM_refresh(toValue, FALSE);
		break;
	case PROP_int:
		int propertyValue3;
		AOM_ask_value_int(formValue, propertyName, &propertyValue3);
		//先lock
		AOM_lock(toValue);
		AOM_set_value_int(toValue, toPropertyName, propertyValue3);
		//再save
		AOM_save(toValue);
		//最后unlock
		AOM_unlock(toValue);
		AOM_refresh(toValue, FALSE);
		break;
	case PROP_string:
		char * propertyValue4;
		AOM_ask_value_string(formValue, propertyName, &propertyValue4);
		//先lock
		AOM_lock(toValue);
		AOM_set_value_string(toValue, toPropertyName, propertyValue4);
		//再save
		AOM_save(toValue);
		//最后unlock
		AOM_unlock(toValue);
		AOM_refresh(toValue, FALSE);
		DOFREE(propertyValue4);
		break;
	default:
		break;
	}
	DOFREE(valtype_n);
}