#include "cJSON.h"

#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 "chint_Handler.h"
#include <regex>
#include <ics/ics.h>
#include "ado.h"
#include "ocilib.h"
#include <property/nr.h>
#include "CRUL_server_call_httpserver.h"
#include "common_itk_util.h"
#define EPM_HANDLER_COMMON
#define MAX_PATH_LENGTH 200
#define MAX_PRINTLINE_LENGTH 400000
void WriteLog3(logical debug, const char* format, ...)
{
	va_list arg;
	char tmp[MAX_PRINTLINE_LENGTH];
	char date_string[MAX_PATH_LENGTH];
	time_t now;
	struct tm* p;

	//get the message
	memset(tmp, 0, sizeof(tmp));
	va_start(arg, format);
	vsprintf(tmp, format, arg);
	va_end(arg);
	//----------print to command window for trace--------//
	//printf("%s\n", tmp);
	printf("%s\n", tmp);
	//print message to log file
	TC_write_syslog("%s\n", tmp);
}
int CHINT_DRAWING_TO_SRM(EPM_action_message_t msg) {

	tag_t task_tag = NULL_TAG,
		root_task_tag = NULLTAG,
		*attachments;;
	task_tag = msg.task;
	int att_cnt = 0, ifail = 0;
	int c_sql_value_count = 0;
	char** c_sql_values;
	ITKCALL(PREF_ask_char_values("CHINT_SRM_SQL_CONNECT", &c_sql_value_count, &c_sql_values));

	WriteLog3(true, "------------------------------------------------------------------------------------");
	WriteLog3(true, "=========================");
	WriteLog3(true, "CHINT_SRM_SQL_CONNECT start");
	WriteLog3(true, "=========================");

	EPM_ask_root_task(task_tag, &root_task_tag);
	EPM_ask_attachments(root_task_tag, EPM_target_attachment, &att_cnt, &attachments);
	WriteLog3(true, "流程目标下的对象数量：%d", att_cnt);
	for (int i = 0; i < att_cnt; i++)
	{
		tag_t tagt = NULLTAG;
		tagt = attachments[i];
		char* type1;
		//获取版本下的数据集
		int dataset_num = 0;
		tag_t* dataset_tags = NULL;
		char* tempValue = NULL;
		char taskpuid[56] = "\0";
		ITK__convert_tag_to_uid(task_tag, &tempValue);
		tc_strcpy(taskpuid, tempValue);
		ITKCALL(AOM_ask_value_string(tagt, "object_type", &type1));
		printf("object_type=%s\n", type1);
		//获取当前登入人组
		char* groupName = NULL;
		tag_t t_group = NULLTAG;
		ITKCALL(POM_ask_group(&groupName, &t_group));
		printf("g=%s\n", groupName);
		//获取当前登入人的名称和id
		char* name;
		char* id;
		tag_t user;
		POM_get_user(&name, &user);
		POM_get_user_id(&id);
		printf("name================%s\n", name);
		printf("id================%s\n", id);



		if (strcmp("ZT2_ProcureRevision", type1) == 0) {
			//判断是否存在dwg文件
			//获取版本下的数据集


			char* file_name;
			char* origin_file_name;
			tag_t* ref_object = NULL;
			int n_found = 0;
			ITKCALL(AOM_ask_value_tags(tagt, "IMAN_specification", &dataset_num, &dataset_tags));



			for (int k = 0; k < dataset_num; k++)
			{

				ITKCALL(AOM_ask_value_string(dataset_tags[k], "object_name", &file_name));
				printf("file_name=%s\n", file_name);
				if (strstr(file_name, "pdf") || strstr(file_name, "PDF")) {
					//获取数据集文件名称
					ITKCALL(AOM_ask_value_string(dataset_tags[k], "current_name", &origin_file_name));
					ITKCALL(AE_ask_dataset_named_refs(dataset_tags[k], &n_found, &ref_object));
					printf("n_found=%d\n", n_found);
					if (n_found > 0) {
						//存在.dwg数据集
						//DFL_ DRAWING_TO_SRM_TASK存储信息

						if (ConnServer(c_sql_values[1], c_sql_values[2], c_sql_values[0]))//"tc11","infodba","//172.16.50.40/tc11"    "TC12","infodba","172.16.68.13/tc1"
						{
							printf("提示:中间数据表访问失败\n");
							return ifail;
						}
						else {
							printf("提示:中间数据表访问成功\n");
							char sql1[1024] = "\0";


							sprintf(sql1, "insert into CHINT_DRAWING_TO_SRM_TASK(tasksource,flowid,status,factory,username,userid) values ('%s','%s','%s','%s','%s','%s')", "工作流程", taskpuid, "未开始", groupName, name, id);
							printf("提示:sql1==%s\n", sql1);
							if (ExecuteSQLNoInputParam(sql1) == -1)
							{
								printf("提示:数据插入 失败, %s \n", sql1);
								ifail = 1;
							}
							else {
								ExecuteSQLNoInputParam("commit");
							}
							break;
						}
					}
				}
			}
			break;//如果发起多个对象，只写一条数据
		}
		else if (strcmp("ZT2_Design3DRevision", type1) == 0) {

			//判断是否存在物料
			int material_num = 0;
			tag_t* material_tags = NULL;
			ITKCALL(AOM_ask_value_tags(tagt, "representation_for", &material_num, &material_tags));
			if (material_num > 0) {
				if (ConnServer(c_sql_values[1], c_sql_values[2], c_sql_values[0]))//"tc11","infodba","//172.16.50.40/tc11"    "TC12","infodba","172.16.68.13/tc1"
				{
					printf("提示:中间数据表访问失败\n");
					return ifail;
				}
				else {
					printf("提示:中间数据表访问成功\n");
					char sql1[1024] = "\0";
					sprintf(sql1, "insert into CHINT_DRAWING_TO_SRM_TASK(tasksource,flowid,status,factory,username,userid) values ('%s','%s','%s','%s','%s','%s')", "工作流程", taskpuid, "未开始", groupName, name, id);
					printf("提示:sql1==%s\n", sql1);
					if (ExecuteSQLNoInputParam(sql1) == -1)
					{
						printf("提示:数据插入 失败, %s \n", sql1);
						ifail = 1;
					}
					else {
						ExecuteSQLNoInputParam("commit");
					}
					break;
				}
			}
		}


	}

	if (ifail == 1)
	{
		EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, "插入数据失败\n"); //错误弹窗
	}
	DOFREE(attachments);
	WriteLog3(true, "=========================");
	WriteLog3(true, "DFL_DRAWING_TO_SRM end");
	WriteLog3(true, "=========================");
	return ifail;
}