#include <tc/tc.h>
#include "epm_handler_common.h"
#include <ict/ict_userservice.h>
#include <tccore/custom.h>
#include <epm/epm_toolkit_tc_utils.h>
#include <tccore/aom.h>
#include <tccore/aom_prop.h>
#include <tccore/item.h>
#include <bom/bom.h>
#include "ps/ps.h";
#include "ps/vrule.h"
#include "sstream"
#include <tccore/grm.h>
#include "epm/epm.h"
#include "sa/sa.h"
#include "libxl.h"
#include <map>
#include "epm/signoff.h"
#include <ctime>
#include <vector>
#include <locale>
#include <codecvt>
#include "ae/dataset.h"
#include <iostream>
#include <stdlib.h>
#include <tccore/aom.h>
#include <tc/emh.h>
#include <ict/ict_userservice.h>
#include <tc/tc.h>
#include <tccore/tctype.h>
#include <sa/tcfile.h>
#include <ss/ss_errors.h>
#include <ae/datasettype.h>
// #include "hx_custom.h"
#include "tc_log.h"
// #include "jk_custom.h"
#include "chint_Handler.h"
#include "tc_util.h"
#include <regex>
#include <ics/ics.h>
#include "ado.h"
#include "ocilib.h"
using namespace std;

//电子签名
int chint_pdf_signoff(EPM_action_message_t msg) {
	int ifail = ITK_ok;
	/**/
	//流程节点相关
	//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;
	//////循环内部变量
	//tag_t cur_task = NULLTAG;
	////循环变量
	//int count = 0;

	////节点循环处理变量
	//tag_t itemrevision = NULLTAG, master_form_rel_type = NULLTAG;
	//int form_count = 0;
	//tag_t* form_list = NULL, master_form = NULLTAG;

	//current_task = msg.task;
	////CreateLogFile("PLA8_signoff",&txtfile);
	//ECHO("=========================================================\n");
	//ECHO("电子签名 开始执行\n");
	//ECHO("=========================================================\n");
	////参数相关

	//POM_AM__set_application_bypass(true);

	//int url_num = 0;
	//char** url_vals = NULL;
	//PREF_ask_char_values("CHINT_SignPic", &url_num, &url_vals);
	//tag_t group;
	//char *groupId;
	//AOM_ask_value_tag(msg.task, "owning_group", &group);
	//AOM_ask_value_string(group,"name", &groupId);
	//printf("groupId==>%s\n", groupId);
	//map<string, tag_t> typeUidMap;
	//for (int i = 0; i < url_num;i++) {
	//	vector<string> vec2;
	//	Split(url_vals[i], ";", vec2);
	//	if (vec2.size() == 3 && vec2[0].compare(groupId)==0) {
	//		tag_t tz_target;
	//		ITK__convert_uid_to_tag(vec2[2].c_str(), &tz_target);
	//		typeUidMap[vec2[1]] = tz_target;
	//	}
	//}

	////获取属性
	//EPM_ask_root_task(msg.task, &root_task);
	//EPM_ask_sub_tasks(root_task, &sub_task_count, &sub_tasks);
	//EPM_ask_attachments(root_task, EPM_target_attachment, &occur_of_counts, &taskAttches);
	//ECHO("%d target attachment found", occur_of_counts);

	//for (count = 0; count < occur_of_counts; count++)
	//{
	//	ITKCALL(TCTYPE_ask_object_type(taskAttches[count], &type_tag));
	//	ITKCALL(ifail = TCTYPE_ask_class_name2(type_tag, &type_class));

	//	//过滤掉非版本对象
	//	if (typeUidMap.count(type_class)>0)
	//	{
	//		tag_t tz_target = typeUidMap[type_class];
	//		char tz_path[512] = "";
	//		int target_count = 0;
	//		tag_t* target_tags = NULL;
	//		char* target_type;
	//		ITKCALL(AOM_ask_value_tags(tz_target, "ref_list", &target_count, &target_tags));
	//		ITKCALL(AOM_ask_value_string(target_tags[0], "file_ext", &target_type));
	//		time_t now;
	//		struct tm* p;
	//		time(&now);
	//		p = localtime(&now);

	//		sprintf_s(tz_path, "%s\\TZ_[%d-%d-%d-%d-%02d-%02d].%s", getenv("TEMP"), 1900 + p->tm_year, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, target_type);

	//		IMF_export_file(target_tags[0], tz_path);

	//		itemrevision = taskAttches[count];
	//		ITKCALL(GRM_find_relation_type(TC_master_form_rtype, &master_form_rel_type));
	//		ITKCALL(GRM_list_secondary_objects_only(itemrevision, master_form_rel_type, &form_count, &form_list));
	//		//获得数据集
	//		tag_t relation_type = NULLTAG;
	//		//tag_t attach_relation_type = NULLTAG;
	//		ITKCALL(GRM_find_relation_type(TC_specification_rtype, &relation_type));
	//		tag_t* secondary_objects = NULLTAG;
	//		int ds_count = 0;
	//		char* dataset_type = NULL, *desc_value = NULL, *file_path = NULL, *desc_path;

	//		ITKCALL(GRM_list_secondary_objects_only(itemrevision, relation_type, &ds_count, &secondary_objects));

	//		for (int j = 0; j < ds_count; j++)
	//		{
	//			printf("进1\n");
	//			ITKCALL(AOM_ask_value_string(secondary_objects[j], "object_type", &dataset_type));
	//			int des_count = 0;
	//			tag_t* dess = NULL;

	//			printf("dataset_type=%s\n", dataset_type);
	//			if (strcmp(dataset_type, "PDF") != 0) {
	//				if (dataset_type != NULL) {
	//					MEM_free(dataset_type);
	//					dataset_type = NULL;
	//				}

	//				continue;
	//			}
	//			ITKCALL(AOM_ask_value_tags(secondary_objects[j], "ref_list", &des_count, &dess));
	//			if (des_count < 1) {
	//				printf("该数据集引用数量错误\n");
	//				continue;
	//			}
	//			else {
	//				char* sizeFile;
	//				AOM_ask_value_string(dess[0], "file_size", &sizeFile);
	//				printf("sizeFile %s \n", sizeFile);
	//				if (strcmp(sizeFile, "0 bytes") == 0) {
	//					printf("文件大小是0bytes\n");
	//					continue;
	//				}
	//			}
	//			tag_t spec_dataset_rev = NULLTAG,
	//				ref_object = NULLTAG;
	//			char* ref_object_name = NULL;
	//			AE_reference_type_t reference_type;
	//			char* datasetName = NULL;
	//			char* pathname;
	//			char* origin_file_name;
	//			vector<string> type_vec;
	//			printf("开始判断类型\n");
	//			if (strcmp(dataset_type, "PDF") == 0) {
	//				printf("pdf类型\n");

	//				AOM_ask_value_string(secondary_objects[j], "object_name", &datasetName);
	//				AE_ask_dataset_latest_rev(secondary_objects[j], &spec_dataset_rev);
	//				AOM_ask_value_string(spec_dataset_rev, "object_name", &ref_object_name);
	//				printf("ref_object_name=%s\n", ref_object_name);
	//				char ref_name[WSO_name_size_c + 1] = "PDF_Reference";

	//				AE_ask_dataset_named_ref2(spec_dataset_rev, ref_name, &reference_type, &ref_object);
	//				if (ref_object == NULLTAG)
	//				{
	//					printf("\nref_object is NULLTAG\n");
	//					return ITK_ok;
	//				}
	//				printf("reference_type=%d\n", reference_type);
	//				if (reference_type == AE_PART_OF)
	//				{
	//					ITKCALL(IMF_ask_file_pathname2(ref_object, SS_WNT_MACHINE, &pathname));
	//					IMF_ask_original_file_name2(ref_object, &origin_file_name);
	//					char new_ds_name[WSO_name_size_c + 1] = "";
	//					char* new_file_name = USER_new_file_name(new_ds_name, ref_name, "pdf", 0);
	//					char* temp_dir = getenv("temp");
	//					char temp_file[SS_MAXPATHLEN] = "";
	//					char temp_file2[SS_MAXPATHLEN] = "";
	//					strcpy(temp_file, temp_dir);
	//					strcat(temp_file, "\\");
	//					strcat(temp_file, new_file_name);
	//					IMF_export_file(ref_object, temp_file);
	//					
	//					if (strstr(new_file_name, ".pdf") != NULL) {
	//						sprintf(temp_file2, "%s\\1%s", temp_dir, new_file_name);
	//					}
	//					else {
	//						sprintf(temp_file2, "%s\\1%s.pdf", temp_dir, new_file_name);
	//					}
	//					//sprintf(temp_file2, "%s.pdf", temp_file);
	//					printf("\ntemp_file=%s\n", temp_file);
	//					char cmd[256] = "";
	//					char* tc_root_dir = getenv("tc_root");
	//					strcpy(cmd, "java -jar ");
	//					strcat(cmd, tc_root_dir);
	//					strcat(cmd, "\\bin\\CHINT_PDFSignoff.jar");
	//					strcat(cmd, " \"");
	//					/*strcat(cmd, path);
	//					strcat(cmd, "\" \"");*/
	//					strcat(cmd, temp_file);
	//					strcat(cmd, "\" \"");
	//					strcat(cmd, temp_file2);
	//					strcat(cmd, "\" \"");
	//					strcat(cmd, tz_path);
	//					strcat(cmd, "\"");
	//					printf("\n%s\n", cmd);
	//					system(cmd);
	//					/*char txtPath[256] = "";
	//					strcpy(txtPath, "DEL /f ");
	//					strcat(txtPath, path);
	//					system(txtPath);*/
	//					tag_t new_file_tag = NULLTAG;
	//					IMF_file_t file_descriptor;
	//					if (temp_file2 != NULL) {
	//						IMF_import_file(temp_file2, new_file_name, SS_BINARY, &new_file_tag, &file_descriptor);
	//					}
	//					else
	//					{
	//						IMF_import_file(temp_file, new_file_name, SS_BINARY, &new_file_tag, &file_descriptor);
	//					}
	//					IMF_set_original_file_name2(new_file_tag, origin_file_name);
	//					IMF_close_file(file_descriptor);
	//					AOM_save(new_file_tag);
	//					AOM_unlock(new_file_tag);

	//					AOM_lock(spec_dataset_rev);

	//					AE_remove_dataset_named_ref_by_tag2(spec_dataset_rev, ref_name, ref_object);

	//					AE_add_dataset_named_ref2(spec_dataset_rev, ref_name, AE_PART_OF, new_file_tag);
	//					AOM_save(spec_dataset_rev);
	//					AOM_unlock(spec_dataset_rev);
	//					char tempPath[256] = "";
	//					char tempPath2[256] = "";
	//					char tempPath3[256] = "";
	//					strcpy(tempPath, "DEL /f ");
	//					strcat(tempPath, temp_file);
	//					system(tempPath);
	//					strcpy(tempPath2, "DEL /f ");
	//					strcat(tempPath2, temp_file2);
	//					system(tempPath2);
	//					/*strcpy(tempPath3, "DEL /f ");
	//					strcat(tempPath3, tz_path);
	//					system(tempPath3);*/
	//					//ITKCALL(ifail = import_dataset_file(secondary_objects[j], "T2_DWG", "dwg", temp_file, datasetName));
	//				}
	//			}
	//			MEM_free(dataset_type);
	//		}
	//		DOFREE(secondary_objects);
	//	}
	//}

	//POM_AM__set_application_bypass(false);
	//DOFREE(taskAttches);
	//ECHO("=========================================================\n");
	//ECHO("电子签名 结束\n");
	//ECHO("=========================================================\n");
	return ifail;
}