#pragma warning (disable: 4996) 
#pragma warning (disable: 4819) 
#pragma warning (disable: 4995) 

#include <tc/tc.h>
#include <fclasses\tc_date.h>
#include "epm_handler_common.h"
#include <chrono>
#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 <iostream>
#include <sstream>
#include <fstream>
#include "ocilib.h"
#include <tc/preferences.h>
using namespace libxl;
typedef struct{
	string ITEM_ID;//id号
}PROPERTY_STRUCT;
void Split(string strArg, string spliter, vector<string> &ans);

string ws2s(const std::wstring& wstr)
{
	
	if (wstr.empty()||wstr.length()==0) return std::string();
	int size_needed = WideCharToMultiByte(CP_ACP, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
	std::string strTo(size_needed, 0);
	WideCharToMultiByte(CP_ACP, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
	return strTo;
}
std::string gettm(__int64 timestamp)
{
	__int64 milli = timestamp + (__int64)8 * 60 * 60 * 1000;
	auto mTime = std::chrono::milliseconds(milli);
	auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(mTime);
	auto tt = std::chrono::system_clock::to_time_t(tp);
	std::tm now;
	::gmtime_s(&now, &tt);
	char res[64] = { 0 };
	sprintf_s(res, _countof(res), "%4d-%02d-%02d %02d:%02d:%02d.%03d", now.tm_year + 1900, now.tm_mon + 1, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec, static_cast<int>(milli % 100));
	return std::string(res);
}
time_t getTimeStamp()
{
	std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp =
		std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
	auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
	time_t timestamp = tmp.count();
	return timestamp;
}

string callHttpserver2(string jsonStr,string jarName) {

	
	time_t now = time(0);
	tm* p = localtime(&now);
	char date[128] = "";
	sprintf_s(date, "%04d%02d%02d%02d%02d%02d", 1900 + p->tm_year, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);

	//把数据用写入文件
	char data_file[256] = "";
	strcat(data_file, getenv("temp"));
	//strcat(data_file, "C:\\PLM\\sendyunpi");
	strcat(data_file, "\\");
	strcat(data_file, date);
	strcat(data_file, ".txt");
	//AE_ask_dataset_named_ref
	ofstream file;
	file.open(data_file);
	file << jsonStr << endl; // 使用与cout同样的方式进行写入
	file.close();

	string strResult;

	//cmd指令
	char cmd[256] = "";
	strcpy(cmd, "java -jar \"");
	//strcat(cmd, jar_file);
	strcat(cmd, getenv("TC_ROOT"));
	strcat(cmd, "\\bin\\");
	strcat(cmd, jarName.c_str());
	strcat(cmd, "\" ");

	//传参
	cout << data_file << endl;
	strcat(cmd, data_file);

	printf("路径:\n%s\n", cmd);
	char buf[8000] = { 0 };
	FILE* pf = NULL;
	if ((pf = _popen(cmd, "r")) == NULL) {
		printf("接口返回:\n%s", "1");
	}

	while (fgets(buf, sizeof buf, pf)) {
		strResult += buf;
	}
	_pclose(pf);
	return strResult;

}

int chint_getChgOrde(EPM_action_message_t msg) {

	int ifail = ITK_ok;
	char tgt_type[WSO_name_size_c + 1] = "", type_class[TCTYPE_class_name_size_c + 1] = "";
	//流程节点相关
	tag_t root_task = NULLTAG, *sub_tasks = NULL, current_task = NULLTAG, type_tag = NULLTAG;
	current_task = msg.task;
	int occur_of_counts;
	tag_t *taskAttches;
	//获取属性
	ITKCALL(EPM_ask_root_task(msg.task, &root_task));
	ITKCALL(EPM_ask_attachments(root_task, EPM_target_attachment, &occur_of_counts, &taskAttches));
	for (int count = 0; count < occur_of_counts; count++) {
		tag_t changeTag = taskAttches[count];
		ITKCALL(TCTYPE_ask_object_type(changeTag, &type_tag));
		ITKCALL(ifail = TCTYPE_ask_class_name(type_tag, type_class));
		printf("type_class : %s", type_class);
		if (strcmp(type_class, "ZT2_Change")==0) {
			tag_t oGroup = NULLTAG, user = NULLTAG;
			char * plant = NULL, *projn = NULL, *pcod = NULL, *chgorder = NULL, *duser = NULL, *userid = NULL, *chginfo = NULL;
			string jsonStr, creationStr, zt2_ChangeStr;
			date_t creation_date, zt2_ChangeDate;
			jsonStr.append("[{");
			ITKCALL(AOM_ask_value_tag(changeTag,"owning_group", &oGroup));
			ITKCALL(AOM_ask_value_string(oGroup, "name", &plant));
			ITKCALL(AOM_ask_value_string(changeTag, "zt2_WBSNo", &projn));
			ITKCALL(AOM_ask_value_string(changeTag, "zt2_ContractNo", &pcod));
			ITKCALL(AOM_ask_value_string(changeTag, "item_id", &chgorder));
			ITKCALL(AOM_ask_value_tag(changeTag, "owning_user", &user));
			ITKCALL(AOM_ask_value_string(user,"user_name",&duser));
			ITKCALL(AOM_ask_value_string(user, "user_id", &userid));

			//lidy20250114项目名称：获取更改通知单本身属性zt2_ContractName中的值。若未填写，则给空值
			char *zt2_ContractName = NULL;
			ITKCALL(AOM_ask_value_string(changeTag, "zt2_ContractName", &zt2_ContractName));

			jsonStr.append("\"plant\":\"");
			jsonStr.append(plant);
			jsonStr.append("\",");

			jsonStr.append("\"projn\":\"");
			jsonStr.append(projn);
			jsonStr.append("\",");

			jsonStr.append("\"pcod\":\"");
			jsonStr.append(pcod);
			jsonStr.append("\",");

			jsonStr.append("\"string\":\"");
			jsonStr.append(zt2_ContractName ? zt2_ContractName : "");
			jsonStr.append("\",");

			jsonStr.append("\"chgorder\":\"");
			jsonStr.append(chgorder);
			jsonStr.append("\",");

			jsonStr.append("\"duser\":\"");
			jsonStr.append(duser);
			jsonStr.append("(");
			jsonStr.append(userid);
			jsonStr.append(")");
			jsonStr.append("\",");

			ITKCALL(AOM_ask_value_date(changeTag, "creation_date", &creation_date));//zt2_ChangeDate
			creationStr.append(to_string(creation_date.year)).append("-").
				append(to_string(creation_date.month+1)).append("-").
				append(to_string(creation_date.day)).append(" ").
				append(to_string(creation_date.hour)).append(":").
				append(to_string(creation_date.minute)).append(":").append(to_string(creation_date.second));

			ITKCALL(AOM_ask_value_date(changeTag, "zt2_ChangeDate", &zt2_ChangeDate));//zt2_ChangeDate
			zt2_ChangeStr.append(to_string(creation_date.year)).append("-").
				append(to_string(creation_date.month+1)).append("-").
				append(to_string(creation_date.day)).append(" ").
				append(to_string(creation_date.hour)).append(":").
				append(to_string(creation_date.minute)).append(":").append(to_string(creation_date.second));
			jsonStr.append("\"ctime\":\"");
			jsonStr.append(creationStr);
			jsonStr.append("\",");

			jsonStr.append("\"dtime\":\"");
			jsonStr.append(zt2_ChangeStr);
			jsonStr.append("\",");
			char **depment; int numdes;
			string depments = "";
			ITKCALL(AOM_ask_value_strings(changeTag, "zt2_ChangeUnit1",&numdes, &depment));
			/*for (int i = 0; i < numdes; i++) {
				if (i > 0) {
					depments.append(";");
				}
				depments.append(depment[i]);
			}*/
			printf("depment[0]==>%s", depment[0]);
			vector<string> split;
			Split(depment[0], "   ", split);
			for (int i = 0; i < split.size(); i++) {
				if (i > 0)
					depments.append(";");
				depments.append(split[i]);
			}
			printf("depments==>%s", depments.c_str());
			int num2; tag_t* second;
			jsonStr.append("\"depment\":\"");
			jsonStr.append(depments);
			jsonStr.append("\",");
			ITKCALL(ifail = AOM_ask_value_tags(changeTag, "IMAN_reference", &num2, &second));
			char *uid;
			if (num2 > 0) {
				ITK__convert_tag_to_uid(second[0], &uid);
			}

			jsonStr.append("\"chginfo\":\"");
			jsonStr.append(uid);
			jsonStr.append("\"");
			jsonStr.append("}]");
			string mes = callHttpserver2(jsonStr, "SendMesChange.jar");
			printf("mes==>%s", mes.c_str());
		}
	}

	return ifail;

}

int chint_getProduct(EPM_action_message_t msg) {

	int ifail = ITK_ok;
	char tgt_type[WSO_name_size_c + 1] = "", type_class[TCTYPE_class_name_size_c + 1] = "";
	//流程节点相关
	tag_t root_task = NULLTAG, *sub_tasks = NULL, current_task = NULLTAG, type_tag = NULLTAG;
	current_task = msg.task;
	int occur_of_counts;
	tag_t *taskAttches;
	//获取属性
	EPM_ask_root_task(msg.task, &root_task);
	EPM_ask_attachments(root_task, EPM_target_attachment, &occur_of_counts, &taskAttches);


	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;
		return ifail;
	}


	for (int count = 0; count < occur_of_counts; count++) {
		tag_t changeTag = taskAttches[count];
		TCTYPE_ask_object_type(changeTag, &type_tag);
		ifail = TCTYPE_ask_class_name(type_tag, type_class);
		printf("type_class : %s", type_class);
		if (strcmp(type_class, "ZT2_XXDRevision")==0) {
			tag_t oGroup, user;
			char * plant, *conlist,*version, *duser,*chgorder="";
			string jsonStr, creationStr, zt2_ChangeStr;
			date_t creation_date, zt2_ChangeDate;
			jsonStr.append("[{");
			AOM_ask_value_tag(changeTag, "owning_group", &oGroup);
			AOM_ask_value_string(oGroup, "name", &plant);

			AOM_ask_value_string(changeTag, "item_id", &conlist);
			AOM_ask_value_string(changeTag, "item_revision_id", &version);
			AOM_ask_value_tag(changeTag, "owning_user", &user);
			AOM_ask_value_string(user, "user_name", &duser);
			jsonStr.append("\"plant\":\"");
			jsonStr.append(plant);
			jsonStr.append("\",");

			jsonStr.append("\"conlist\":\"");
			jsonStr.append(conlist);
			jsonStr.append("\",");

			jsonStr.append("\"version\":\"");
			jsonStr.append(version);
			jsonStr.append("\",");

			int n_references = 0;
			int *levels = 0;
			tag_t * references_tag = NULLTAG;
			char ** relation_type_name = NULL;
			//通过引用关系查找到变更单
			WSOM_where_referenced(changeTag, 1, &n_references, &levels, &references_tag, &relation_type_name);
			printf("引用关系查找到的个数为：%d\n", n_references);
			//引用关系找变更单
			for (int i = 0; i < n_references; i++)
			{
				TCTYPE_ask_object_type(references_tag[i], &type_tag);
				ifail = TCTYPE_ask_class_name(type_tag, type_class);
				printf("type_class : %s", type_class);
				if (strcmp(type_class, "ZT2_Change")==0) {
					AOM_ask_value_string(references_tag[i],"item_id", &chgorder);
				}
			}
			jsonStr.append("\"chgorder\":\"");
			jsonStr.append(chgorder);
			jsonStr.append("\",");
			//数据库找 SELECT CPXH,CPMC,HTMC,HTH FROM "CHINT_XXD_000" where XXDH = 'YHX230027' and XXDBB = 'A'
			string sql = "SELECT CPXH,CPMC,HTMC,HTH FROM \"CHINT_XXD_000\" where XXDH = '";
			sql.append(conlist).append("' and XXDBB = '").append(version).append("'");
			int colmun = 0, count = 0;
			char ***outputValue = NULL;
			QuerySQLNoInputParam((char *)sql.c_str(), &colmun, &count, &outputValue);
			char *ccod="", *ctyp, *cnam, *hcod, *hnam,*projn;

			if (count > 0) {
				ctyp = outputValue[0][0];
				cnam = outputValue[0][1];
				hcod = outputValue[0][2];
				hnam = outputValue[0][3];
			}

			jsonStr.append("\"ccod\":\"");
			jsonStr.append(ccod);
			jsonStr.append("\",");

			jsonStr.append("\"ctyp\":\"");
			jsonStr.append(ctyp);
			jsonStr.append("\",");

			jsonStr.append("\"cnam\":\"");
			jsonStr.append(cnam);
			jsonStr.append("\",");

			jsonStr.append("\"hcod\":\"");
			jsonStr.append(hcod);
			jsonStr.append("\",");

			jsonStr.append("\"hnam\":\"");
			jsonStr.append(hnam);
			jsonStr.append("\",");
			string sql2 = "select WBS from CHINT_SCXXCDTZD where TYPE||TIME||FLOW||'-'||FLOW2 = '";
			sql2.append(conlist).append("'");

			int colmun2 = 0, count2 = 0;
			char ***outputValue2 = NULL;
			QuerySQLNoInputParam((char *)sql2.c_str(), &colmun2, &count2, &outputValue2);
			if (count2 > 0) {
				projn = outputValue2[0][0];
				printf("projn==%s\n", projn);
			}
			jsonStr.append("\"projn\":\"");
			jsonStr.append(projn);
			jsonStr.append("\",");

			jsonStr.append("\"duser\":\"");
			jsonStr.append(duser);
			jsonStr.append("\",");

			AOM_ask_value_date(changeTag, "creation_date", &creation_date);//zt2_ChangeDate
			creationStr.append(to_string(creation_date.year)).append("-").
				append(to_string(creation_date.month+1)).append("-").
				append(to_string(creation_date.day)).append(" ").
				append(to_string(creation_date.hour)).append(":").
				append(to_string(creation_date.minute)).append(":").append(to_string(creation_date.second));

			AOM_ask_value_date(changeTag, "date_released", &zt2_ChangeDate);//zt2_ChangeDate
			zt2_ChangeStr.append(to_string(creation_date.year)).append("-").
				append(to_string(creation_date.month+1)).append("-").
				append(to_string(creation_date.day)).append(" ").
				append(to_string(creation_date.hour)).append(":").
				append(to_string(creation_date.minute)).append(":").append(to_string(creation_date.second));
			jsonStr.append("\"ddate\":\"");
			jsonStr.append(creationStr);
			jsonStr.append("\",");
			jsonStr.append("\"pdate\":\"");
			jsonStr.append(zt2_ChangeStr);
			jsonStr.append("\",");

			int num2; tag_t* second;

			ITKCALL(ifail = AOM_ask_value_tags(changeTag, "IMAN_specification", &num2, &second));
			char *uid;
			if (num2 > 0) {
				ITK__convert_tag_to_uid(second[0], &uid);
			}

			jsonStr.append("\"cfile\":\"");
			jsonStr.append(uid);
			jsonStr.append("\"");
			jsonStr.append("}]");

			string mes = callHttpserver2(jsonStr, "SendMesXXD.jar");
			printf("mes==> %s ", mes.c_str());

		}
	}

	DisConnServer();
	return ifail;

}
string getDateStr(tag_t changeTag,char *propName) {
	date_t creation_date = NULLDATE;
	string zt2_ChangeStr = "";
	AOM_ask_value_date(changeTag, propName, &creation_date);//zt2_ChangeDate
	char *date;
	/*if (creation_date.year<=0) {
		ITK_date_to_string(creation_date, &date);
		zt2_ChangeStr.append(date);
	}*/
	if (creation_date.year >0) {
		zt2_ChangeStr.append(to_string(creation_date.year)).append("-").
			append(to_string(creation_date.month+1)).append("-").
			append(to_string(creation_date.day)).append(" ").
			append(to_string(creation_date.hour)).append(":").
			append(to_string(creation_date.minute)).append(":").append(to_string(creation_date.second));
	}
	
	return zt2_ChangeStr;
}
/*
	判断对象objtag是否属于type_name类型
*/
bool isTypeOf22(tag_t objtag, const char * type_name) {
	bool is_type = false;
	char *type_class;
	if (objtag == NULLTAG) {
		return is_type;
	}
	ITKCALL(AOM_ask_value_string(objtag,"object_type",&type_class));
	printf("type_class : %s type_name %s \n", type_class, type_name);
	if (type_class!=NULL && strcmp(type_name, type_class) == 0) {
		is_type = true;
	}
	return is_type;
}
void Split(string strArg, string spliter, vector<string> &ans)
{
	ans.clear();
	size_t index0;
	string one_arg;
	if (strArg.find_first_not_of(' ') == string::npos)
		strArg = "";
	while (strArg.size() > 0)
	{
		index0 = strArg.find(spliter);
		if (index0 != string::npos)
		{
			one_arg = strArg.substr(0, index0);
			strArg = strArg.substr(index0 + spliter.size());
			ans.push_back(one_arg);
		}
		else
		{
			ans.push_back(strArg);
			break;
		}
	}
}
string& replace_all(string& str, const  string& old_value, const  string& new_value)
{
	if (strstr(str.c_str(), old_value.c_str()) != NULL) {
		vector<string> type_vec;
		Split(str.c_str(), old_value.c_str(), type_vec);
		char new_str[512] = "\0";
		for (int i = 0; i < type_vec.size(); i++)
		{
			strcat(new_str, type_vec[i].c_str());
			if (i < type_vec.size() - 1) {
				strcat(new_str, new_value.c_str());
			}
		}
		string new_value(new_str);
		str = new_value.c_str();
	}
	return   str;
}
string getFacotryNoVal(string facNo) {
	string strResult;

	//cmd指令
	char cmd[256] = "";
	strcpy(cmd, "java -jar \"");
	//strcat(cmd, jar_file);
	strcat(cmd, getenv("TC_ROOT"));
	strcat(cmd, "\\bin\\");
	strcat(cmd, "getFactoryNum.jar");
	strcat(cmd, "\" ");

	strcat(cmd, facNo.c_str());

	printf("路径:\n%s\n", cmd);
	char buf[8000] = { 0 };
	FILE* pf = NULL;
	if ((pf = _popen(cmd, "r")) == NULL) {
		printf("接口返回:\n%s", "1");
	}

	while (fgets(buf, sizeof buf, pf)) {
		strResult += buf;
	}
	_pclose(pf);
	strResult = replace_all(strResult, "\n", "");
	printf("strResult %s \n", strResult.c_str());
	return strResult;
}
int chint_DesignProcess(EPM_action_message_t msg) {

	
	int ifail = ITK_ok;
	char tgt_type[WSO_name_size_c + 1] = "", type_class[TCTYPE_class_name_size_c + 1] = "";
	//流程节点相关
	tag_t root_task = NULLTAG, *sub_tasks = NULL, current_task = NULLTAG, type_tag = NULLTAG;
	current_task = msg.task;
	int occur_of_counts;
	tag_t *scheduleTasks;
	//获取属性
	EPM_ask_root_task(msg.task, &root_task);
	
	AOM_ask_value_tags(current_task,"fnd0RootScheduleTask", &occur_of_counts,&scheduleTasks);
	if (occur_of_counts > 0) {
		tag_t scheduleTask = scheduleTasks[0], oGroup;
		string jsonStr;
		jsonStr.append("{\"ITEMS\":[{");
		jsonStr.append("\"HEAD\":{");
		char *plant;
		AOM_ask_value_tag(scheduleTask, "owning_group", &oGroup);
		AOM_ask_value_string(oGroup, "name", &plant);

		//ZT2_ProjectItem 
		tag_t parentTask;
		AOM_ask_value_tag(scheduleTask, "fnd0ParentTask", &parentTask);
		

		int n_references = 0;
		int *levels = NULL;
		tag_t * references_tag = NULLTAG;
		tag_t schedule_tag;
		AOM_ask_value_tag(parentTask,"schedule_tag", &schedule_tag);
		char **relations = NULL;
		ITKCALL(WSOM_where_referenced(schedule_tag, 1, &n_references, &levels, &references_tag, &relations));
		printf("引用关系查找到的个数为：%d\n", n_references);
		//引用关系找变更单
		for (int ii = 0; ii < n_references; ii++)
		{
			/*string relType = relations[ii];
			printf("关系：%s\n", relType.c_str());
			if (strcmp(relType.c_str(),"contents") != 0 && strcmp(relType.c_str(), "内容") != 0) {
				continue;
			}*/
			char *name;
			AOM_ask_value_string(references_tag[ii],"object_name",&name);
			printf("name：%s\n", name);
			if (isTypeOf22(references_tag[ii], "ZT2_ProjectFolder")) { //isTypeOf22(references_tag[ii], "ZT2_ProjectFolder")
				//
				int n_references2 = 0;
				int *levels2 = 0;
				tag_t * references_tag2 = NULLTAG;
				char ** relation_type_name2 = NULL;
				WSOM_where_referenced(references_tag[ii], 1, &n_references2, &levels2, &references_tag2, &relation_type_name2);
				printf("引用关系查找到的个数为：%d\n", n_references2);
				for (int xx = 0; xx < n_references2; xx++)
				{
					printf("relation_type_name2：%s\n", relation_type_name2[xx]);
					if (isTypeOf22(references_tag2[xx], "ZT2_ProjectItem")) {
						char * zt2_WBSNo,*itemID;
						AOM_ask_value_string(references_tag2[xx], "zt2_WBSNo", &zt2_WBSNo);
						AOM_ask_value_string(references_tag2[xx], "item_id", &itemID);
						printf("zt2_WBSNo %s \n", zt2_WBSNo);
						/*AOM_ask_value_string(type_tag, "item_id", &chgorder);*/
						jsonStr.append("\"projn\":\"");
						jsonStr.append(zt2_WBSNo);
						jsonStr.append("\",");
						jsonStr.append("\"itemID\":\"");
						jsonStr.append(itemID);
						jsonStr.append("\",");
						int num1; tag_t *founds1;
						int cout = 0;
						string facNo;
						AOM_ask_value_tags(references_tag2[xx], "IMAN_reference", &num1, &founds1);
						printf("num1%d\n", num1);
						for (int t = 0; t < num1; t++) {
							char *fName1;
							AOM_ask_value_string(founds1[t], "object_name", &fName1);
							printf("项目启动 %s \n", fName1);
							if (strcmp(fName1, "项目启动") == 0) {
								int num2; tag_t *founds2;
								AOM_ask_value_tags(founds1[t], "contents", &num2, &founds2);
								for (int j = 0; j < num2; j++) {
									char *fName2;
									AOM_ask_value_string(founds2[j], "object_name", &fName2);
									if (strcmp(fName2, "出厂编号") == 0) {
										printf("出厂编号\n");
										int num3; tag_t *founds3;
										AOM_ask_value_tags(founds2[j], "contents", &num3, &founds3);
										printf("num3%d\n", num3);
										for (int tt = 0; tt < num3; tt++) {

											if (isTypeOf22(founds3[tt], "ZT2_FactoryNo")) {
												
												char *factoryNo;
												AOM_ask_value_string(founds3[tt], "item_id", &factoryNo);
												if (cout > 0) {
													facNo.append(";");
												}
												cout = cout + 1;
												facNo.append(factoryNo);
												printf("factoryNo %s \n", factoryNo);
											}
										}
									}
								}
							}
						}
						string facNoVal = getFacotryNoVal(facNo);
						jsonStr.append("\"sernum\":\"");
						jsonStr.append(facNoVal);
						jsonStr.append("\",");

						jsonStr.append("\"total\":\"");
						jsonStr.append(to_string(cout));
						jsonStr.append("\",");
					}
				}
				break;
			}
		}
		jsonStr.append("\"plant\":\"");
		jsonStr.append(plant);
		jsonStr.append("\",");

		printf("plant %s\n", plant);

		jsonStr.append("\"DESIGN_DETAIL\":[");
		tag_t *tasks;
		int taskConut;
		AOM_ask_value_tags(parentTask, "child_task_taglist",&taskConut, &tasks);

		printf("child_count %d\n", taskConut);

		for (int i = 0; i < taskConut; i++)
		{
			char *design,*userName,*wbscod;
			tag_t curTask = tasks[i],duser;
			//actual_finish_date actual_start_date  //start_date finish_date 
			//object_name design

			if (i > 0) {
				jsonStr.append(",");
			}

			jsonStr.append("{");
			
			AOM_ask_value_string(curTask,"object_name",&design);
			string pstime = getDateStr(curTask, "start_date");
			string rstime = getDateStr(curTask, "finish_date");
			string ptime = getDateStr(curTask, "actual_start_date");
			string rtime = getDateStr(curTask, "actual_finish_date");
			tag_t *dusers;
			int cot = 0;
			AOM_ask_value_tags(curTask,"ResourceAssignment",&cot,&dusers);
			if (cot > 0) {
				AOM_ask_value_string(dusers[0], "user_name", &userName);
			}
			AOM_ask_value_string(curTask, "wbs_code", &wbscod);

			jsonStr.append("\"design\":\"");
			jsonStr.append(design);
			jsonStr.append("\",");

			jsonStr.append("\"pstime\":\"");
			jsonStr.append(pstime);
			jsonStr.append("\",");

			jsonStr.append("\"rstime\":\"");
			jsonStr.append(rstime);
			jsonStr.append("\",");

			jsonStr.append("\"ptime\":\"");
			jsonStr.append(ptime);
			jsonStr.append("\",");

			jsonStr.append("\"rtime\":\"");
			jsonStr.append(rtime);
			jsonStr.append("\",");

			jsonStr.append("\"duser\":\"");
			jsonStr.append(userName);
			jsonStr.append("\",");

			jsonStr.append("\"wbscod\":\"");
			jsonStr.append(wbscod);
			jsonStr.append("\"}");
		}

		jsonStr.append("]");
		jsonStr.append("}}]}");

		string mes = callHttpserver2(jsonStr, "SendDesignProcess.jar");
		printf("mes==> %s ", mes.c_str());
	}

	return ifail;

}


int Check_range(EPM_rule_message_t msg)
{
	Book* book = nullptr;
	book = xlCreateBook();//xls文件用xlCreateBook() 函数打开，xlsx文件是xml结构，所以得用xlCreateXMLBook() 函数来打开。
	book->setKey(L"Halil Kural", L"windows-2723210a07c4e90162b26966a8jcdboe");//如果购买了该库，则设置相应的key，若没有购买，则不用这行
	EPM_decision_t decision = EPM_go;
	int ifail = ITK_ok, arg_cnt = 0, i = 0, att_cnt = 0;
	tag_t task_tag = NULLTAG, rootTask_tag = NULLTAG, *attachments = NULL;
	char * object_TYPE_1 = NULL;
	char * object_string = NULL;
	string value_type = "";
	string value_property="";
	
	//获取当前触发的任务
	task_tag = msg.task;
	//获取根流程节点
	ifail = EPM_ask_root_task(task_tag, &rootTask_tag);
	//获取目标引用对象
	ifail = EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &att_cnt, &attachments);
	//循环目标对象
	for (i = 0; i < att_cnt; i++) {
	//下载数据集
		tag_t rev_tag=attachments[i];
		AOM_ask_value_string(rev_tag,"object_type",&object_TYPE_1);
		cout<<object_TYPE_1<<endl;
	
			cout<<"111111111111111111"<<endl;
			tag_t relation_type_tag = NULLTAG;
			ifail = GRM_find_relation_type("TC_Attaches", &relation_type_tag);
			if (ifail != ITK_ok) { /* your error logic here */ }
			   int n_specs = 0;
			tag_t* specs = NULL;

			ITKCALL(ifail = GRM_list_secondary_objects_only(rev_tag, relation_type_tag, &n_specs, &specs));
			if (ifail != ITK_ok) { /* your error logic here */ }
			for (int j = 0; j < n_specs; j++)
			{
				cout<<"2222222222222"<<endl;
				 tag_t type_tag = NULLTAG; char type_name[TCTYPE_name_size_c+1] = "";
				ifail = TCTYPE_ask_object_type (specs[j], &type_tag);
				if (ifail != ITK_ok) { /* your error logic here */ }

				ifail = TCTYPE_ask_name(type_tag, type_name);
				if (ifail != ITK_ok) { /* your error logic here */ }
				cout<<"type_name-----------"<<type_name<<endl;
				if (strcmp(type_name, "MSExcel") == 0||strcmp(type_name, "MSExcelX") == 0)
				{
					cout<<"333333333333333"<<endl;
					int n_refs = 0;
					tag_t* refs = NULL;

					ifail = AE_ask_all_dataset_named_refs(specs[j], "excel", &n_refs, &refs);
					if (ifail != ITK_ok) { /* your error logic here */ }

					ifail = AE_export_named_ref(specs[j], "excel", "C:\\TEMP\\export_text.xls");
					if (ifail != ITK_ok) { /* your error logic here */ }
					//检查是否存在单元格名称为“xxxx”
					wstringstream excel_path;
					excel_path<<"C:\\TEMP\\export_text.xls";
					if (book->load(excel_path.str().c_str()))
					{
						cout<<"88888888"<<endl;
						Sheet* sheet = book->getSheet(0);//获得第1个sheet页
						string val ;
						val = ws2s(sheet->readStr(0, 15));//读取每个单元格的值
						printf("val:%s\n", val.c_str());
					
							
					}
				}
			}

	
		
		
	}
	if (attachments != NULL)
	{
		MEM_free(attachments);
		attachments = NULL;
	}
	if (object_TYPE_1 != NULL)
	{
		MEM_free(object_TYPE_1);
		object_TYPE_1 = NULL;
	}
	if (object_string != NULL)
	{
		MEM_free(object_string);
		object_string = NULL;
	}
	
	return EPM_nogo;
	
		
	}

