#pragma warning (disable: 4996) 
#pragma warning (disable: 4819) 
#pragma warning (disable: 4995) 
#include <cstdio>


#include <epm/epm.h>
#include <ict/ict_userservice.h>
#include <tccore/item.h>
#include <ae/ae.h>
#include <tc/folder.h>
#include <tccore/aom.h>
#include <sa/sa.h>
#include <tccore/aom_prop.h>
#include <tccore/workspaceobject.h>
#include <tccore//grm.h>
#include <tccore/grmtype.h>
#include <bom/bom.h>
#include <fclasses/tc_date.h>
#include <ics/ics.h>
#include <ics/ics2.h>
#include "connor_erp_mes.h"


// #include <stdio.h>
// #include <string.h>
#include <vector>
#include <epm/signoff.h>
#include <iostream>
#include "kutil.h"
using namespace std;
extern "C" int POM_AM__set_application_bypass(logical bypass);



int QF_SendTo_CRM(EPM_action_message_t msg)
{
	//EPM_rule_message_t rul_msg;
	int ifail = ITK_ok, rcode = 0;
	//参数相关
	char arg1value[1024] = "";
	char *argflag = NULL, *argvalue = NULL, *arg = NULL;
	BOOL bypass = FALSE, setboth = TRUE, signPic = FALSE;
	int arg_cnt = 0;
	//流程节点相关
	tag_t root_task = NULLTAG, *sub_tasks = NULL, current_task = NULLTAG, type_tag = NULLTAG;
	int sub_task_count = 0;
	char root_task_name[128] = "", task_name[128] = "";
	int occur_of_counts = 0;
	tag_t *taskAttches = NULLTAG;
	char tgt_type[WSO_name_size_c + 1] = "", type_class[TCTYPE_class_name_size_c + 1] = "";
	////循环内部变量
	tag_t cur_task = NULLTAG;
	char task_type[WSO_name_size_c + 1] = "";
	//循环变量
	int i = 0, j = 0, k = 0, count = 0, n = 0;
	//节点信息变量

	//节点循环处理变量
	tag_t itemrevision = NULLTAG;

	int form_count = 0;
	tag_t master_form = NULLTAG;
	char local_path[128] = "";
	//编制节点


	//char *txtfile = NULL;
	//会签相关
	int n_prop = 0, q = 0;
	char **prop_names = NULL, hq_name[128] = "", hq_time_name[128] = "", *hq_value = NULL;
	char exclude_type[256] = "", item_type[ITEM_type_size_c + 1] = "";
	tag_t item = NULLTAG;
	logical is_sign = false;

	//得到触发此函数的节点
	current_task = msg.task;
	//CreateLogFile("PLA8_signoff",&txtfile);
	printf("=========================================================\n");
	printf("JY_Save_CLS 开始执行\n");
	printf("=========================================================\n");
	//获取handler的参数的个数
	arg_cnt = TC_number_of_arguments(msg.arguments);
	printf("参数个数为：%d\n", arg_cnt);
	if (arg_cnt > 0)
	{
		for (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, "type") == 0)
			{
				//strcmp("","");
				if (argvalue != NULL)
				{
					strcpy(arg1value, argvalue);
				}
			}

		}
		MEM_free(argflag);
		MEM_free(argvalue);
	}

	printf("type : %s\n", arg1value);

	tag_t task_tag = msg.task;
	tag_t rootTask_tag = NULL;
	int att_cnt = 0;
	tag_t *attachments = NULL;
	//获取根流程节点
	ifail = EPM_ask_root_task(task_tag, &rootTask_tag);
	//获取目标引用对象
	ifail = EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &att_cnt, &attachments);
	//循环目标对象
	//通过首选项获取数据集模板QZ2bpmxb
	/*vector<string>		pref_vec1;

	map<string, string> lov_map;//键：属性名称；值：leylovid
	getPrefStrings1("JY8lovid", TC_preference_site, pref_vec1);
	for (int i=0;i<pref_vec1.size();i++){
	string lovid=pref_vec1[i];
	vector<string> map_vec;
	Split(lovid, "=", map_vec);

	lov_map.insert(pair<string, string>(map_vec[0], map_vec[1]));
	}*/


	char *lovValue = NULL;
	char *key_lov_name = NULL;
	int  options = 0;
	int  n_lov_entries = 0;
	char **lov_keys;
	char **lov_values;
	logical *deprecated_staus;
	char *owning_site = NULL;
	int n_shared_sites = 0;
	char **shared_sites;

	/*tag_t keyTag=NULL;
	ICS_keylov_ask_tag("-20050252",&keyTag);



	char *keyid=NULL;
	ICS_keylov_ask_id(keyTag,&keyid);
	printf("当前keyid为   %s\n",keyid);*/
	POM_AM__set_application_bypass(true);

	for (i = 0; i < att_cnt; i++) {

		char *object_TYPE_1 = NULL;
		ifail = AOM_ask_value_string(attachments[i], "object_type", &object_TYPE_1);//对象类型
		printf("当前类型对象为   %s\n", object_TYPE_1);

		if (strcmp(arg1value, object_TYPE_1) == 0)
		{
			map<string, string> class_map;//存放属性信息，键：属性中文名称；值：属性显示值
			printf("类型匹配........\n");


			tag_t cpItem = NULL;
			ITEM_ask_item_of_rev(attachments[i], &cpItem);

			//获取分类属性
			logical    isClassified;
			ITKCALL(ICS_is_wsobject_classified(cpItem, &isClassified));//判断是否已经分类
			if (isClassified)
			{
				printf("已分类........\n");
				tag_t classObj, attribute_tag;
				ICS_ask_classification_object(cpItem, &classObj);
				tag_t ics_tag;
				char * class_id = NULL;
				char *className = NULL;
				int  	theAttributeCount;
				ICS_view_attribute_descriptor_p_t  attributes;
				ICS_view_descriptor_p_t   theViewDescriptor = NULL;
				ITKCALL(ICS_ico_ask_class_tag(classObj, &ics_tag));
	




				ITKCALL(ICS_ico_ask_class(classObj, &class_id));
				ITKCALL(ICS_view_describe(class_id, "", "", FALSE, &theViewDescriptor));
				theAttributeCount = theViewDescriptor->attributeCount;
				attributes = theViewDescriptor->attributes;
				className=theViewDescriptor->name;
				cout << "className--------------" << className << endl;
				for (int j = 0; j < theAttributeCount; j++) {
					//WriteLog("ICO attr id = %d \n",attributes[i].id);
					//WriteLog("ICO attr name = %s \n",attributes[i].name);
					//WriteLog("ICO attr format = %d \n",attributes[i].format);
					string attr_name;//分类属性名称
					string attr_value;//分类属性值
					string attr_id;
					cout << "id--------------" << attributes[j].id << endl;
					cout << "format----------------" << attributes[j].format << endl;
					cout << "name-------------------" << attributes[j].name << endl;
					//获取分类属性的真实值和显示值
					logical isLocalizable = false;
					ICS_ico_property_isLocalizable(classObj, attributes[j].id,&isLocalizable);
					if (isLocalizable == true) {
						int theAttributeValCount = 0;
						char**     theAttributeValues = NULL;
						char*      localization_statuses = NULL;
						logical    isMaster = false;
						ICS_ico_ask_localized_value_strings(classObj, attributes[j].id, "zh_CN", &theAttributeValCount, &theAttributeValues, &localization_statuses, &isMaster);
						for (size_t n = 0; n < theAttributeValCount; n++)
						{
							cout << theAttributeValues[n] << endl;
						}
						DOFREE(localization_statuses);
						if (theAttributeValues != NULL) {
							MEM_free(theAttributeValues);
							theAttributeValues = NULL;
						}

					}
					char newLsm[15];
					itoa(attributes[j].id, newLsm, 10);
					cout << "newLsm-------------------" << newLsm << endl;
					attr_id = newLsm;
					attr_name = attributes[j].name;
					//获取某个属性
					int classificationCount = 0;
					char **values = NULL;


					AOM_ask_value_strings(attachments[i], "jy8ClassLovInfo", &classificationCount, &values);
					map<string, string> lovMap;
					for (int i1 = 0; i1 < classificationCount; i1++)
					{
						string     lovMapValue = values[i1];
						vector<string> map_vec;
						printf("values[i1].......%s\n", values[i1]);
						Split(lovMapValue, "=", map_vec);
						lovMap.insert(pair<string, string>(map_vec[0], map_vec[1]));

					}

					char *classValue = NULL;

					ICS_ask_attribute_value(classObj, attributes[j].name, &classValue);
					attr_value = classValue;

					string keyId = lovMap[attr_id];
					if (keyId.size() > 0) {
						ICS_keylov_get_keylov(keyId.c_str(), &key_lov_name, &options, &n_lov_entries, &lov_keys, &lov_values, &deprecated_staus,
							&owning_site, &n_shared_sites, &shared_sites);//电源线长度"-20050249"
						for (int o = 0; o < n_lov_entries; o++)
						{
							if (strcmp(classValue, lov_keys[o]) == 0) {
								printf("分类属性%s为%s\n", attributes[j].name, lov_values[o]);
								attr_value = lov_values[o];
								break;
							}
						}

					}



					printf("分类属性名称：%s;分类属性值：%s\n", attributes[j].name, attr_value);
					class_map.insert(pair<string, string>(attr_name, attr_value));

				}
				DOFREE(class_id);
				cout << "9===================" << endl;

			}
			else {
				printf("没有分类...........");

			}
			DOFREE(object_TYPE_1);
		}
		else {
			printf("类型不匹配........\n");
		}
		cout << "10===================" << endl;
	}
	POM_AM__set_application_bypass(false);
	cout << "11===================" << endl;
	//if (debug)
	DOFREE(attachments);
	cout << "12===================" << endl;
	DOFREE(lovValue);
	cout << "13===================" << endl;
	DOFREE(key_lov_name);
	cout << "14===================" << endl;
	DOFREE(owning_site);
	cout << "15===================" << endl;
	{
		printf("=========================================================\n");
		printf("hander 获取属性信息 执行完成\n");
		printf("=========================================================\n");
	}
	return rcode;
}

