#include <epm\epm.h>
#include <tccore\grm.h>
#include <tccore\grmtype.h>
#include <tccore\aom_prop.h>
#include <ae\dataset.h>
#include <tccore\aom.h>
#include <fclasses\tc_date.h>
#include <epm\signoff.h>
#include <tccore\item.h>
#include <tccore\item.h>
#include <sa\user.h>
#include <string>
#include <vector>
#include <map>
#include <time.h>
#include <bom/bom.h>
#include <tc\folder.h>
#include <curl\curl.h>
#include "ocilib.h"
#include "connor_itk_util.h"
#include "epm_handler_common.h"
using namespace std;
#define EPM_HANDLER_COMMON
#define MAX_PATH_LENGTH 200
#define MAX_PRINTLINE_LENGTH 400000

int RB_BOM_TO_ERP(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("RB3_SQL_Connect2", &c_sql_value_count, &c_sql_values));
	if (c_sql_value_count != 3)
	{
		WriteLog("首选项 RB3_SQL_Connect2 配置错误 数量%d  ", c_sql_value_count);
		CloseLog();
		return 1;
	}
	printf("------------------------------------------------------------------------------------");
	printf("=========================");
	printf("RB_BOM_TO_ERP start");
	printf("=========================");

	EPM_ask_root_task(task_tag, &root_task_tag);
	EPM_ask_attachments(root_task_tag, EPM_target_attachment, &att_cnt, &attachments);
	printf("流程目标下的对象数量：%d", att_cnt);


	char* tempValue = NULL;
	char taskpuid[56] = "\0";
	ITK__convert_tag_to_uid(task_tag, &tempValue);
	tc_strcpy(taskpuid, tempValue);
	
	time_t curtime1;
	time(&curtime1);
	tm* nowtime1 = localtime(&curtime1);
	string dealBeginTime;
	dealBeginTime.append(std::to_string(1900 + nowtime1->tm_year)).append("-").append(std::to_string(1 + nowtime1->tm_mon)).append("-").append(std::to_string(nowtime1->tm_mday)).append(" ").append(std::to_string(nowtime1->tm_hour)).append(":").append(std::to_string(nowtime1->tm_min)).append(":").append(std::to_string(nowtime1->tm_sec));

	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 RB_BOM_TO_ERP(flowid,status,starttime) values ('%s','%s',TO_DATE('%s', 'YYYY-MM-DD HH24:MI:SS'))", taskpuid, "0",dealBeginTime.c_str());
		printf("提示:sql1==%s\n", sql1);
		if (ExecuteSQLNoInputParam(sql1) == -1)
		{
			printf("提示:数据插入 失败, %s \n", sql1);
			ifail = 1;
		}
		else {
			ExecuteSQLNoInputParam("commit");
		}
	}


	DOFREE(attachments);
	printf("=========================");
	printf("RB_BOM_TO_ERP end");
	printf("=========================");
	return ifail;
}