
#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"

char* Utf8ToGbk(const char* src_str)
{
	int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0);
	wchar_t* wszGBK = (wchar_t*)MEM_alloc((len + 1) * sizeof(wchar_t));
	memset(wszGBK, 0, len * 2 + 2);
	MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len);
	len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
	char* szGBK = (char*)MEM_alloc((len + 1) * sizeof(char));

	memset(szGBK, 0, len + 1);
	WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
	return szGBK;
}

int CHINT_SendOAMaterial(EPM_action_message_t msg) {
	char* log_file = NULL;

	printf("创建日志文件\n");
	char* CHINT_SendOAMaterial = (char*)malloc(sizeof("CHINT_SendOAMaterial"));
	strcpy(CHINT_SendOAMaterial, "TO_SRM");
	CreateLogFile(CHINT_SendOAMaterial, &log_file); //
	int ifail = ITK_ok;
	int att_cnt = 0, rev_count = 0;
	tag_t  rootTask_tag = NULLTAG;
	tag_t* attachments = NULL,
		*rev_tags = NULL;

	char* objtype = NULL;
	time_t timep;
	time(&timep); //获取从1970至今过了多少秒，存入time_t类型的timep
	//用ctime将秒数转化成字符串格式，输出：Thu Feb 28 14:14:17 2019
	WriteLog("%s创建日志文件完成\n", ctime(&timep));
	EPM_ask_root_task(msg.task, &rootTask_tag);
	EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &att_cnt, &attachments);
	WriteLog("获取到了目标对象，数量为%d\n", att_cnt);

	if (att_cnt > 0) {

		char* cPCode = NULL;
		char* zt2_formnumber = NULL;
		char* zt2_MaterialNo = NULL;
		char* sfId = NULL;

		tag_t* target = NULL;//关系下物料版本对象
		int  target_count = 0;
		cJSON* array = cJSON_CreateArray();

		for (int i = 0; i < att_cnt; i++) {
			ITKCALL(AOM_ask_value_string(attachments[i], "object_type", &objtype));
			if (strcmp("ZT2_FrockRevision", objtype) == 0) {


				ITKCALL(AOM_ask_value_string(attachments[i], "item_id", &cPCode));
				ITKCALL(AOM_ask_value_string(attachments[i], "zt2_formnumber", &zt2_formnumber));
				ITKCALL(AOM_ask_value_string(attachments[i], "zt2_sfid", &sfId));




			}
			if (strcmp("ZT2_Design3DRevision", objtype) == 0) {
				ITKCALL(ifail = AOM_ask_value_tags(attachments[i], "representation_for", &target_count, &target));
				if (target) {
					for (int i = 0; i < target_count; i++) {

						ITKCALL(ifail = AOM_ask_value_string(target[i], "zt2_MaterialNo", &zt2_MaterialNo));
						WriteLog("target下对象zt2_MaterialNo为：%s", zt2_MaterialNo);
					}

				}
			}

		}


		//修改数据库状态
		int url_num = 0;
		char** url_vals = NULL;
		PREF_ask_char_values("database_tc", &url_num, &url_vals);
		string urlSql = url_vals[0];
		urlSql.append("/").append(url_vals[2]);
		string errorBuff;
		//map<string,
		printf("urlSql ==> %s \n", urlSql.c_str());
		if (ConnServer(url_vals[3], url_vals[4], (char*)urlSql.c_str()) == -1)
		{
			printf("提示:中间数据表访问失败\n");
			//ifail = 1;
		}
		char sql5[1024] = "\0";
		sprintf(sql5, "update \"CHINT_ FROCK\" set STATE = '%s' where instanceId = '%s'", "设计中", zt2_formnumber);
		printf("提示:sql5==%s\n", sql5);
		if (ExecuteSQLNoInputParam(sql5) == -1)
		{
			printf("提示:数据插入 失败, %s \n", sql5);
			return 1;
		}
		else {
			ExecuteSQLNoInputParam("commit");
		}



		WriteLog("item_id->%s", cPCode);
		WriteLog("zt2_formnumber->%s", zt2_formnumber);
		WriteLog("zt2_MaterialNo->%s", zt2_MaterialNo);
		WriteLog("json开始拼接\n");
		/* 创建一个JSON数据对象(链表头结点) */
			//cJSON_AddStringToObject(cpgy, "sfId", sfId);

		cJSON* cpgx = cJSON_CreateObject();
		cJSON_AddStringToObject(cpgx, "ItemName", "S_wlh");
		cJSON_AddStringToObject(cpgx, "ItemValue", zt2_MaterialNo);
		cJSON_AddItemToArray(array, cpgx);
		string json_to_char;

		WriteLog("JSON拼接完成\n");
		json_to_char = cJSON_Print(array);
		WriteLog("json->%s\n", json_to_char.c_str());
		//获取首选项url
		char* url;
		PREF_ask_char_value("CHINT_SendOAURL", 0, &url);
		WriteLog("url=%s\n", url);
		string msg = sendOA(sfId, zt2_formnumber, json_to_char, url);
		char* a = "false";
		if (strstr(msg.c_str(), a) != NULL) {//存在
			EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, "接口传递失败！");

			ifail = 1;
		}
		else {
			//接口传递成功
			char sql[1024] = "\0";
			sprintf(sql, "update \"CHINT_ FROCK\" set STATE = '%s' where instanceId = '%s'", "已完成", zt2_formnumber);
			printf("提示:sql==%s\n", sql);
			if (ExecuteSQLNoInputParam(sql) == -1)
			{
				printf("提示:数据插入 失败, %s \n", sql);
				return 1;
			}
			else {
				ExecuteSQLNoInputParam("commit");
			}
		}
	}
	CloseLog();
	return ifail;
}