#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 "ocilib.h"


char* incrementChar(char* input) {
	int num = atoi(input); // 将字符转换为整数
	num++; // 自增
	sprintf(input, "%02d", num); // 将整数转换为两位数的字符串

	return input;
}

int CHINT_cossheet_upgrade(EPM_action_message_t msg) {
	ECHO("=========================================================\n");
	ECHO("CHINT_cossheet_upgrade 开始执行\n");
	ECHO("=========================================================\n");
	int doc_num = 0;
	tag_t rootTask_tag = NULLTAG;
	tag_t task = NULLTAG;
	tag_t* doc_tags = NULLTAG;
	int arg_cnt = TC_number_of_arguments(msg.arguments);
	task = msg.task;
	ITKCALL(EPM_ask_root_task(task, &rootTask_tag));
	int ifail = ITK_ok;
	int url_num = 0;
	char** url_vals = NULL;
	PREF_ask_char_values("database_tc", &url_num, &url_vals);
	string url = url_vals[0];
	url.append("/").append(url_vals[2]);
	printf("url ==> %s \n", url.c_str());
	if (ConnServer(url_vals[3], url_vals[4], (char*)url.c_str()) == -1)
	{
		printf("提示:中间数据表访问失败\n");
		ifail = 1;
	}

	//获取流程下目标关系下的对象
	printf("获取流程下目标关系下的对象");
	ITKCALL(EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &doc_num, &doc_tags));

	for (int i = 0; i < doc_num;i++) {
		char* objtype = NULL;
		ITKCALL(AOM_ask_value_string(doc_tags[i], "object_type", &objtype));
		if (strstr(objtype,"ZT2_COSTSHEET") != nullptr) {
			char* cbdtype = NULL;
			ITKCALL(AOM_ask_value_string(doc_tags[i], "zt2_cbdlx", &cbdtype));
			char* desc;
			ITKCALL(AOM_ask_value_string(doc_tags[i], "object_desc", &desc));
			//查询数据
			string sqlTitle = "select \"cbdlx\",\"projectid\",\"revision\",\"factory\",\"proname\",\"tennumber\",\"promanager\",\"quantity\",\"tramodel\",\"capfactor\",\"volratio\",\"noloadloss\",\"loadloss\",\"impvoltage\",\"traweight\",\"totweight\",\"copconsumption\",\"cmarketprice\",\"vollevel\" from \"CHINT_ CUSQUOTATION_TITLE_TEMPLATE\" where \"cbdlx\" = '%s' and \"projectid\" = '%s' order by \"revision\" desc";
			char selectRecord[1000];
			sprintf(selectRecord, sqlTitle.c_str(), cbdtype,desc);
			int outputColumn = 0, outputValueCount = 0;
			char*** outputValue = NULL;
			printf("search3 ===> %s\n", selectRecord);
			QuerySQLNoInputParam(selectRecord, &outputColumn, &outputValueCount, &outputValue);
			printf("outputValueCount ===> %d\n", outputValueCount);
			//获取当前的明细版本
			string getrev;
			//插入一条新的数据，并且更新版本
			if (outputValueCount > 0) {
				getrev = outputValue[0][2];
				
				//获取自增后的版本
				char* newStr = incrementChar(outputValue[0][2]);
				string insert = "INSERT INTO \"CHINT_ CUSQUOTATION_TITLE_TEMPLATE\" (\"cbdlx\",\"projectid\",\"revision\",\"factory\",\"proname\",\"tennumber\",\"promanager\",\"quantity\",\"tramodel\",\"capfactor\",\"volratio\",\"noloadloss\",\"loadloss\",\"impvoltage\",\"traweight\",\"totweight\",\"copconsumption\",\"cmarketprice\",\"vollevel\") VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')";
				char selectRecord2[600];
				sprintf(selectRecord2, insert.c_str(),outputValue[0][0], outputValue[0][1], newStr, outputValue[0][3], outputValue[0][4], outputValue[0][5], outputValue[0][6], outputValue[0][7], outputValue[0][8], outputValue[0][9], outputValue[0][10], outputValue[0][11], outputValue[0][12], outputValue[0][13], outputValue[0][14], outputValue[0][15], outputValue[0][16], outputValue[0][17], outputValue[0][18]);
				printf("selectRecord2 ===> %s\n", selectRecord2);
				ExecuteSQLNoInputParam(selectRecord2);
				ExecuteSQLNoInputParam("commit");
			}
			else {
				printf("数据库没有查询到相关数据\n");
				break;
			}

			//查询数据
			string selSql = "select \"cbdlx\",\"projectid\",\"revision\",\"matgroup\",\"mgsnumber\",\"matcname\",\"matcnumber\",\"matcatname\",\"specifications\",\"manufacturer\",\"nwquantity\",\"utirate\",\"unit\",\"amomoney\",\"unitprice\",\"no\",\"totalprice\" from \"CHINT_ CUSQUOTATION_ DETAILS_TEMPLATE\" where \"cbdlx\" = '%s' and \"revision\" = '%s' and \"projectid\" = '%s' ";
			char selectRecord1[400];
			sprintf(selectRecord1, selSql.c_str(), cbdtype, getrev.c_str(),desc);
			int outputColumn1 = 0, outputValueCount1 = 0;
			char*** outputValue1 = NULL;
			printf("search3 ===> %s\n", selectRecord1);
			QuerySQLNoInputParam(selectRecord1, &outputColumn1, &outputValueCount1, &outputValue1);

			//插入一条新的数据，并且更新版本
			if (outputValueCount1 > 0) {
				
				//获取自增后的版本
				char* newStr = incrementChar(outputValue1[0][2]);
				string insert = "INSERT INTO \"CHINT_ CUSQUOTATION_ DETAILS_TEMPLATE\" (\"cbdlx\",\"projectid\",\"revision\",\"matgroup\",\"mgsnumber\",\"matcname\",\"matcnumber\",\"matcatname\",\"specifications\",\"manufacturer\",\"nwquantity\",\"utirate\",\"unit\",\"amomoney\",\"unitprice\",\"no\",\"totalprice\") VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')";
				char selectRecord2[600];
				for (int j = 0; j < outputValueCount1;j++) {
					sprintf(selectRecord2, insert.c_str(), outputValue1[j][0], outputValue1[j][1], newStr, outputValue1[j][3], outputValue1[j][4], outputValue1[j][5], outputValue1[j][6], outputValue1[j][7], outputValue1[j][8], outputValue1[j][9], outputValue1[j][10], outputValue1[j][11], outputValue1[j][12], outputValue1[j][13], outputValue1[j][14], outputValue1[j][15], outputValue1[j][16]);
					printf("selectRecord2 ===> %s\n", selectRecord2);
					ExecuteSQLNoInputParam(selectRecord2);
					ExecuteSQLNoInputParam("commit");
				}
				
			}
			DisConnServer();

			
			ECHO("=========================================================\n");
			ECHO("数据插入完成\n");
			ECHO("CHINT_cossheet_upgrade 执行结束\n");
			ECHO("=========================================================\n");
			break;
		}
	}
	




	return 0;
}