#include <epm/epm.h>
#include <string>
#include <map>
#include "epm_handler_common.h"
#include <tccore/aom.h>
#include <tccore/aom_prop.h>
#include "common_itk_util.h"
#include "tc_util.h"
#include <tc/preferences.h>
#include <fclasses\tc_date.h>
#include "CRUL_server_call_httpserver.h"
#include "ocilib.h"
#include "cJSON.h"
#include "tinyxml2.h"
using namespace std;
using namespace tinyxml2;



void connectSql() {
	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");
	}
}
void closeConn() {
	DisConnServer();
}

/*
	推送SRM接口
*/
int CHINT_SendSRM(EPM_action_message_t msg) {
	int ifail = ITK_ok;
	tag_t root_task = NULLTAG, *sub_tasks = NULL, current_task = NULLTAG, type_tag = NULLTAG;
	int sub_task_count = 0, attachCounts = 0;
	tag_t* taskAttches = NULLTAG;
	tag_t cur_task = NULLTAG;
	current_task = msg.task;
	EPM_ask_root_task(msg.task, &root_task);
	EPM_ask_sub_tasks(root_task, &sub_task_count, &sub_tasks);
	EPM_ask_attachments(root_task, EPM_target_attachment, &attachCounts, &taskAttches);

	//多流程对象 ZT2_Procure Revision 
	for (int i = 0; i < attachCounts; i++) {
		int pdfCnt = 0;
		char *objType, *wbsNo, *materialNo, *revisionId;
		tag_t procureRev = taskAttches[i], *datasets;
		AOM_ask_value_string(procureRev, OBJECTTYPE, &objType);
		if (strcmp(objType, PROCURETYPE) == 0) {
			//获取版本属性：wbs、物料编码、版本、获取规范关系下的pdf文件，获取文件和uid，通过接口推送至SRM系统
			AOM_ask_value_string(procureRev, WBSNO, &wbsNo);
			AOM_ask_value_string(procureRev, MATERIALNO, &materialNo);
			AOM_ask_value_string(procureRev, REVISIONID, &revisionId);
			AOM_ask_value_tags(procureRev, SPECIFICATION, &pdfCnt, &datasets);
			//获取数据集的名称和UID
			
		}
	}

	return ifail;
}

cJSON* getDrawingInform(tag_t changeTag) {
	int changeNo, signNo, placesNo, beforeNo, verBefNo, aftNo, aftVerNo, reasonNo, processTypeNo, prodNct;
	char **changeDrawingNos, **signs, **placesNos, **changeBefs, **versionBefs,
		 **changeAfts, **versionAfts, **changeReasons, **processTypes, **products;
	AOM_ask_value_strings(changeTag, "zt2_ChangeDrawingNo",&changeNo, &changeDrawingNos);
	AOM_ask_value_strings(changeTag, "zt2_Sign", &signNo, &signs);
	AOM_ask_value_strings(changeTag, "zt2_PlacesNo", &placesNo, &placesNos);
	AOM_ask_value_strings(changeTag, "zt2_ChangeBefore1", &beforeNo, &changeBefs);
	AOM_ask_value_strings(changeTag, "zt2_SZVersionbefore", &verBefNo, &versionBefs);
	AOM_ask_value_strings(changeTag, "zt2_ChangeAfter1", &aftNo, &changeAfts);
	AOM_ask_value_strings(changeTag, "zt2_SZRevisedversion", &aftVerNo, &versionAfts);
	AOM_ask_value_strings(changeTag, PROP_PRODUCT, &prodNct, &products);
	AOM_ask_value_strings(changeTag, "zt2_ChangeReason", &reasonNo, &changeReasons);
	AOM_ask_value_strings(changeTag, "zt2_ProcessType1", &processTypeNo, &processTypes);
	cJSON* infomations = cJSON_CreateArray();
	for (int i = 0; i < changeNo; i++) {
		cJSON* infomation = cJSON_CreateObject();
		cJSON_AddStringToObject(infomation, "S_DrawingCode", changeDrawingNos[i]);
		cJSON_AddStringToObject(infomation, "S_Sign", signNo > i ? signs[i] : "");
		cJSON_AddStringToObject(infomation, "S_CS", placesNo > i ? placesNos[i] : "");
		cJSON_AddStringToObject(infomation, "S_PreChange", beforeNo > i ? changeBefs[i] : "");
		cJSON_AddStringToObject(infomation, "S_PreChangeVersion", verBefNo > i ? versionBefs[i] : "");
		cJSON_AddStringToObject(infomation, "S_AfterChange", aftNo > i ? changeAfts[i] : "");
		cJSON_AddStringToObject(infomation, "S_AfterChangeVersion", aftVerNo > i ? versionAfts[i] : "");
		cJSON_AddStringToObject(infomation, JSON_TXDJ, prodNct > i ? products[i] : "");
		cJSON_AddStringToObject(infomation, "S_ChangeReason", reasonNo > i ? changeReasons[i] : "");
		cJSON_AddStringToObject(infomation, "S_ZZPCL", processTypeNo > i ? processTypes[i] : "");
		cJSON_AddItemToArray(infomations, infomation);
	}
	return infomations;
}

cJSON* getEHDepart(string ecnCode, string section, string &uuid) {
	
	string searchSql = "SELECT \"actionstatement\",\"result\",\"pctime\",\"remark\",\"principal\" FROM CHINT_ECN_NOTIFICATION where \"ecncode\" = '%s' and \"section\" = '%s' order by \"no\"";
	char selectRecord2[2048];
	sprintf(selectRecord2, searchSql.c_str(), ecnCode.c_str(), section);
	int outputColumn1 = 0, outputValueCount1 = 0;
	char*** outputValue1 = NULL;
	printf("selectRecord2 ===> %s\n", selectRecord2);
	QuerySQLNoInputParam(selectRecord2, &outputColumn1, &outputValueCount1, &outputValue1);
	cJSON* infomations = cJSON_CreateArray();
	for (int i = 0; i < outputValueCount1; i++) {
		cJSON* infomation = cJSON_CreateObject();
		if (i == 0) {
			vector<string> uuidVec;
			Split(outputValue1[i][4],";", uuidVec);
			if (uuidVec.size() > 1) {
				printf("outputValue %zd===> %s\n", uuidVec.size(), uuidVec[1].c_str());
			}
			else {
				printf("outputValue %zd===> %s\n", uuidVec.size(), outputValue1[i][4]);
			}
			if (uuidVec.size() > 1 && strstr(uuidVec[1].c_str(),"null") == NULL) {
				uuid = uuidVec[1];
			}
		}
		cJSON_AddStringToObject(infomation, "S_JSBaction", outputValue1[i][0]);
		cJSON_AddStringToObject(infomation, "S_PD", outputValue1[i][1]);
		cJSON_AddStringToObject(infomation, "T_PlanTime", outputValue1[i][2]);
		cJSON_AddStringToObject(infomation, "S_Remark", outputValue1[i][3]);
		cJSON_AddItemToArray(infomations, infomation);
	}
	return infomations;
}
//上传文件到minio
string uploadMinio(string jarName, string logPath) {
	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, "\" ");
	string strResult;
	strcat(cmd, logPath.c_str());
	char buf[8000] = { 0 };
	printf("路径:\n%s\n", cmd);
	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;
	//return;
}
/*
	复制文件
*/
int file_xcopy_stod(const char* source_dir, const char* destination_dir)
{
	ifstream infile(source_dir, ios::in | ios::binary);//二进制形式打开
	if (infile.is_open() == 0) {//出错处理
		cout << "文件" << source_dir << "打开失败" << endl;
		return -1;
	}
	ofstream outfile(destination_dir, ios::out | ios::binary);//二进制形式打开
	if (outfile.is_open() == 0) {//出错处理
		cout << "文件" << destination_dir << "打开失败" << endl;
		infile.close();//记得关闭
		return -1;
	}
	//开始读写
	const int FLUSH_NUM = 1024 * 1024;//缓冲区大小设置为1M
	char* ch = new(nothrow)char[FLUSH_NUM];
	if (ch == NULL) {//出错处理
		cout << "动态申请内存失败" << endl;
		infile.close();//记得关闭
		outfile.close();//记得关闭
		return -1;
	}
	while (!infile.eof()) {
		infile.read(ch, FLUSH_NUM);
		outfile.write(ch, infile.gcount());//写入读入的成功个数
	}
	delete[]ch;//记得释放
	infile.close();//记得关闭
	outfile.close();//记得关闭
	return 0;
}

//变更对象发送到OA
int CHINT_ECN_SendOA(EPM_action_message_t msg) {

	int ifail = ITK_ok;
	ECHO("=========================================================\n");
	ECHO("CHINT_ECN_SendOA 开始执行\n");
	ECHO("=========================================================\n");
	tag_t root_task = NULLTAG, *sub_tasks = NULL, current_task = NULLTAG, type_tag = NULLTAG;
	int sub_task_count = 0, occur_of_counts = 0, url_num;
	tag_t* taskAttches = NULLTAG;
	tag_t cur_task = NULLTAG;
	char* jobName = NULL, **url_vals = NULL;
	current_task = msg.task;
	ITKCALL(AOM_ask_value_string(current_task, "job_name", &jobName));
	ITKCALL(EPM_ask_root_task(msg.task, &root_task));
	ITKCALL(EPM_ask_sub_tasks(root_task, &sub_task_count, &sub_tasks));
	ITKCALL(EPM_ask_attachments(root_task, EPM_target_attachment, &occur_of_counts, &taskAttches));

	ITKCALL(PREF_ask_char_values("database_tc", &url_num, &url_vals));
	string base_url = url_vals[0];
	base_url.append("/").append(url_vals[2]);
	printf("url ==>%s\n", base_url.c_str());
	if (ConnServer(url_vals[3], url_vals[4], (char*)base_url.c_str()) == -1) {
		printf("中间数据表访问失败\n");
		ifail = 919050;
		ITKCALL(EMH_store_error_s1(EMH_severity_error, ifail, "database_tc"));
		return ifail;
	}
	//获取url地址
	char* url = NULL, *urlId = NULL;//
	ITKCALL(PREF_ask_char_value("CHINT_ECNSendOAUrl", 0, &url));
	ITKCALL(PREF_ask_char_value("CHINT_OAUserID_RUL", 0, &urlId));
	char* log_file = NULL;
	CreateLogFile("CHINT_ECN_SendOA", &log_file); //
	//获取当前流程目标
	//connectSql();
	//获取登陆组
	tag_t group;
	char *groupName, *taskUid;
	ITKCALL(POM_ask_group(&groupName, &group));
	ITKCALL(AOM_ask_value_string(group, PROP_GROUP_NAME, &groupName));
	ITK__convert_tag_to_uid(msg.task, &taskUid); //流程uid
	for (int count = 0; count < occur_of_counts; count++) {
		char *type;
		tag_t taskTag = taskAttches[count];
		ITKCALL(AOM_ask_value_string(taskTag, "object_type", &type));
		if (strcmp(type, "ZT2_Change") == 0) {
			//如果当前流程目标下的变更单，对应的影响分析如果仅包含技术工艺，无其他部门，则不需要触发此handler 
			char *itemId = NULL;
			ITKCALL(AOM_ask_value_string(taskTag, "item_id", &itemId));
			printf("id ===>%s\n", itemId);
			string sql = "select \"principal\",\"section\" FROM CHINT_ECN_NOTIFICATION where \"result\"='是' and \"ecncode\"='";
			sql.append(itemId).append("'");
			int outputColumn1 = 0, outputValueCount1 = 0;
			char*** outputValue1 = NULL;
			printf("sql ===>%s\n", sql.c_str());
			QuerySQLNoInputParam((char*)sql.c_str(), &outputColumn1, &outputValueCount1, &outputValue1);
			printf("outputValueCount1 ===>%d\n", outputValueCount1);
			boolean pro = true;
			for (int i = 0; i < outputValueCount1; i++) {
				if (tc_strcmp(outputValue1[i][1], "技术部") != 0 && tc_strcmp(outputValue1[i][1], "工艺部") != 0) {
					pro = false;
					break;
				}
			}
			if (pro) {
				continue;
			}
			//获取属性 查询数据库填写JSON
			int unitCnt, processCnt = 0;
			char* changeType, *productModel,
				*productName, *fileName, *contractNo, *contractName,
				*changeDateStr, **changeUnits, *changeRequestNo, **processTypes;
			date_t changeDate;
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_ChangeType", &changeType));//变更类型
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_ProductModel", &productModel));//产品型号
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_ProductName", &productName)); // 产品名称
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_FileName", &fileName)); // 	文件代号及名称
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_ContractNo", &contractNo)); // 合同代号
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_ContractName", &contractName)); // 合同名称
			ITKCALL(AOM_ask_value_date(taskTag, "zt2_ChangeDate", &changeDate)); // 变更实施日期
			ITKCALL(AOM_ask_value_strings(taskTag, "zt2_ChangeUnit1",&unitCnt, &changeUnits)); // 合同名称
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_ChangRequestNo", &changeRequestNo)); // 更改通知单号
			ITKCALL(AOM_ask_value_strings(taskTag, PROP_PROCESS, &processCnt, &processTypes));
			cJSON* paramValue = cJSON_CreateObject();
			//json添加字段
			/*S_ProductFeature	产品特性	字符串	A; B; C
				S_GCFeeature	过程特性	字符串	A; B; C*/
			cJSON_AddStringToObject(paramValue, S_GCDM, groupName);//工厂
			cJSON_AddStringToObject(paramValue, "S_ChangeCode", changeRequestNo);//更改申请单号
			cJSON_AddStringToObject(paramValue, "S_PLMCode", taskUid);
			cJSON_AddStringToObject(paramValue, "S_ChangeNotificationCode", itemId);
			string productFeature, changeTypeStr;
			if (processCnt > 0) {
				char *type = processTypes[0];
				if (strcmp(type, "Y") == 0) {
					productFeature = S_Product;
				}
				else {
					productFeature = S_Process;
				}
			}
			if (strstr(changeType, S_ZSChange) != NULL) {
				changeTypeStr = S_ZSChange;
			}
			else {
				changeTypeStr = S_LSChange;
			}
			cJSON_AddStringToObject(paramValue, "S_ProductFeature", productFeature.c_str());
			cJSON_AddStringToObject(paramValue, "S_ChangeTypes", changeTypeStr.c_str());
			cJSON_AddStringToObject(paramValue, "S_ProductNumber", productModel);
			cJSON_AddStringToObject(paramValue, "S_ProductName", productName);
			cJSON_AddStringToObject(paramValue, "S_Pcodename", fileName);
			cJSON_AddStringToObject(paramValue, "S_Contractcode", contractNo);
			cJSON_AddStringToObject(paramValue, "S_ContractName", contractName);
			ITKCALL(DATE_date_to_string(changeDate, "%Y-%m-%d", &changeDateStr));
			cJSON_AddStringToObject(paramValue, "T_MaterialDate", changeDateStr);
			vector<string> split;
			Split(changeUnits[0], "   ", split);
			string ffbm = "";
			for (int i = 0; i < split.size(); i++) {
				if (i > 0)
					ffbm.append(";");
				ffbm.append(split[i]);
			}
			cJSON_AddStringToObject(paramValue, "S_FFBM", ffbm.c_str());//changeUnits[0]
			//EH_DrawingInformation图样信息
			cJSON* infomations = getDrawingInform(taskTag);
			cJSON_AddItemToObject(paramValue, "EH_DrawingInformation", infomations);
			string jsbUUid = "", gybUUid = "", zlbUUid = "", 
				scbUUid = "", cgbUUid = "", xszxUUid = "", cwbUUid = "";
			cJSON* jsbZx = getEHDepart(itemId, "技术部", jsbUUid);
			cJSON_AddItemToObject(paramValue, "EH_JSBZX", jsbZx);
			cJSON* gybZx = getEHDepart(itemId, "工艺部", gybUUid);
			cJSON_AddItemToObject(paramValue, "EH_GYBZX", gybZx);
			cJSON* zlbZx = getEHDepart(itemId, "质量部", zlbUUid);
			cJSON_AddItemToObject(paramValue, "EH_ZLBZX", zlbZx);
			cJSON* scbZx = getEHDepart(itemId, "生产部", scbUUid);
			cJSON_AddItemToObject(paramValue, "EH_SCBZX", scbZx);
			cJSON* cgbZx = getEHDepart(itemId, "采购部", cgbUUid);
			cJSON_AddItemToObject(paramValue, "EH_CGBZX", cgbZx);
			cJSON* saleCenter = getEHDepart(itemId, "销售中心", xszxUUid);
			cJSON_AddItemToObject(paramValue, "EH_XSZXZX", saleCenter);
			cJSON* cwbZx = getEHDepart(itemId, "财务部", cwbUUid);
			cJSON_AddItemToObject(paramValue, "EH_CWBZX", cwbZx);
			cJSON_AddStringToObject(paramValue, "S_JSBZRR", jsbUUid.c_str());
			cJSON_AddStringToObject(paramValue, "S_GYBZRR", gybUUid.c_str());
			cJSON_AddStringToObject(paramValue, "S_ZLBZRR", zlbUUid.c_str());
			cJSON_AddStringToObject(paramValue, "S_SCBZRR", scbUUid.c_str());
			cJSON_AddStringToObject(paramValue, "S_CGBZRR", cgbUUid.c_str());
			cJSON_AddStringToObject(paramValue, "S_XSZXZRR", xszxUUid.c_str());
			cJSON_AddStringToObject(paramValue, "S_CWBZRR", cwbUUid.c_str());

			//ITKCALL(POM_get_user_id(&userId));
			tag_t user = NULLTAG;
			char* userId = NULL;
			ITKCALL(AOM_ask_value_tag(taskTag, "owning_user", &user));
			ITKCALL(AOM_ask_value_string(user, "user_id", &userId));
			string email = userId;
			email.append("@chint.com");
			string userXML = getUserIdOA(email, urlId);
			string userCode = readXmlId(userXML);

			char *json_to_char = cJSON_Print(paramValue);
			//lidy20250211 &转义
			vector<string> v1;
			Split(json_to_char, "&", v1);
			string json_str = "";
			for (int i = 0; i < v1.size(); i++) {
				if (json_str.size() > 0)
					json_str.append("&amp;");
				json_str.append(v1[i]);
			}
			
			WriteLog("\nworkflowCode=EH_TE_ChangeNotice\nuserCode=%s\nfinishStart=true\n", userCode.c_str());
			//WriteLog("组织之后的JSON\n %s\n", json_to_char);
			string msg = "组织之后的JSON\n";
			msg.append(json_str).append("\n");
			WriteLog2(msg.c_str());
			string returnMsg = ecnSendOA(userCode.c_str(), json_str, url);
			WriteLog("回传信息\n %s\n", returnMsg.c_str());
			if (!returnMsg.empty()) {
				ITKCALL(EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, returnMsg.c_str()));
			}
		}
	
	}
	DisConnServer();
	//closeConn();
	CloseLog();
	//string str = uploadMinio("uploadMinio.jar", log_file);
	char date_string[MAX_PATH_LENGTHE], logFileName[MAX_PATH_LENGTHE];;
	time_t now;
	struct tm *p;
	time(&now);
	//current_time(&status_now);
	p = localtime(&now);
	memset(date_string, 0, sizeof(date_string));
	sprintf(date_string, "%4d%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);
	sprintf(logFileName, "%s\\%s_%s.log", LOG_PATH, taskUid, date_string);
	file_xcopy_stod(log_file, logFileName);
	//printf("str %s \n", str.c_str());
	return ifail;
}

//PLM推送变更通知单数据流信息
int CHINT_CHANGEITEM_TO_SRM(EPM_action_message_t msg) {

	int ifail = ITK_ok;
	ECHO("=========================================================\n");
	ECHO("CHINT_CHANGEITEM_TO_SRM 开始执行\n");
	ECHO("=========================================================\n");
	tag_t root_task = NULLTAG, *sub_tasks = NULL, current_task = NULLTAG, type_tag = NULLTAG;
	int sub_task_count = 0, occur_of_counts = 0;
	tag_t* taskAttches = NULLTAG;
	tag_t cur_task = NULLTAG;
	char* jobName = NULL;
	current_task = msg.task;
	ITKCALL(AOM_ask_value_string(current_task, "job_name", &jobName));
	ITKCALL(EPM_ask_root_task(msg.task, &root_task));
	ITKCALL(EPM_ask_sub_tasks(root_task, &sub_task_count, &sub_tasks));
	ITKCALL(EPM_ask_attachments(root_task, EPM_target_attachment, &occur_of_counts, &taskAttches));

	int url_num = 0;
	char** url_vals = NULL;
	ITKCALL(PREF_ask_char_values("database_tc", &url_num, &url_vals));
	string sqlurl = url_vals[0];
	sqlurl.append("/").append(url_vals[2]);
	printf("sqlurl ==> %s \n", sqlurl.c_str());
	if (ConnServer(url_vals[3], url_vals[4], (char*)sqlurl.c_str()) == -1) {
		ITKCALL(EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, "错误:TC数据库连接失败"));
		return EMH_USER_error_base;
	}
	//获取url地址
	int url_cnt = 0;
	char** url = NULL, *urlId = NULL;//
	ITKCALL(PREF_ask_char_values("CHINT_ChangeURL", &url_cnt, &url));
	vector<string> urls;
	for (int i = 0; i < url_cnt; i++) {
		vector<string> vec1, vec2;
		Split(url[i], "=", vec1);
		Split(vec1[0], ":", vec2);
		if (tc_strcmp(vec2[1].c_str(), "1") == 0) {
			urls.push_back(vec1[1]);
		}
	}
	if (urls.size() == 0) {
		printf("CHINT_ChangeURL首选项中未找到可用地址\n");
		return ifail;
	}
	ITKCALL(PREF_ask_char_value("CHINT_OAUserID_RUL", 0, &urlId));
	char* log_file = NULL;
	CreateLogFile("CHINT_CHANGEITEM_TO_SRM", &log_file); //
														 //获取当前流程目标
	//获取登陆组
	tag_t group;
	char *groupName, *taskUid;
	ITKCALL(POM_ask_group(&groupName, &group));
	ITKCALL(AOM_ask_value_string(group, PROP_GROUP_NAME, &groupName));
	//如果发放部门的采购勾选了，那么需要找首选项CHINT_ChangeNoticeBM中的对应的人员，加到字段：S_CGBZRR
	int pref_cnt = 0;
	char **prefs = NULL;
	ITKCALL(PREF_ask_char_values("CHINT_ChangeNoticeBM", &pref_cnt, &prefs));
	vector<string> bms;
	for (int i = 0; i < pref_cnt; i++) {
		vector<string> v1;
		Split(prefs[i], "|", v1);
		if (tc_strcmp(v1[0].c_str(), groupName) != 0) {
			continue;
		}
		vector<string> v2;
		Split(v1[1], ";", v2);
		for (int j = 0; j < v2.size(); j++) {
			vector<string> v3;
			Split(v2[j], ":", v3);
			if (tc_strcmp(v3[0].c_str(), "采购") == 0) {
				Split(v3[1], ",", bms);
				break;
			}
		}
		break;
	}
	ITK__convert_tag_to_uid(msg.task, &taskUid); //流程uid
	for (int count = 0; count < occur_of_counts; count++) {
		char *type;
		tag_t taskTag = taskAttches[count];
		ITKCALL(AOM_ask_value_string(taskTag, "object_type", &type));
		if (strcmp(type, "ZT2_Change") == 0) {
			//如果当前流程目标下的变更单，对应的影响分析如果仅包含技术工艺，无其他部门，则不需要触发此handler 
			char *itemId = NULL;
			ITKCALL(AOM_ask_value_string(taskTag, "item_id", &itemId));
			printf("id ===>%s\n", itemId);
			//获取属性 查询数据库填写JSON
			int unitCnt, processCnt = 0;
			char* changeType, *productModel,
				*productName, *fileName, *contractNo, *contractName,
				*changeDateStr, **changeUnits, *changeRequestNo, **processTypes,
				*wbsno = NULL, *owninguser = NULL, *releasedate = NULL;
			date_t changeDate;
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_WBSNo", &wbsno));//wbs号
			ITKCALL(AOM_UIF_ask_value(taskTag, "owning_user", &owninguser));//变更发起人
			ITKCALL(AOM_UIF_ask_value(taskTag, "date_released", &releasedate));//发布时间
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_ChangeType", &changeType));//变更类型
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_ProductModel", &productModel));//产品型号
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_ProductName", &productName)); // 产品名称
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_FileName", &fileName)); // 	文件代号及名称
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_ContractNo", &contractNo)); // 合同代号
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_ContractName", &contractName)); // 合同名称
			ITKCALL(AOM_ask_value_date(taskTag, "zt2_ChangeDate", &changeDate)); // 变更实施日期
			ITKCALL(AOM_ask_value_strings(taskTag, "zt2_ChangeUnit1", &unitCnt, &changeUnits)); // 合同名称
			ITKCALL(AOM_ask_value_string(taskTag, "zt2_ChangRequestNo", &changeRequestNo)); // 更改通知单号
			ITKCALL(AOM_ask_value_strings(taskTag, PROP_PROCESS, &processCnt, &processTypes));
			cJSON* paramValue = cJSON_CreateObject();
			//json添加字段
			/*S_ProductFeature	产品特性	字符串	A; B; C
			S_GCFeeature	过程特性	字符串	A; B; C*/
			//cJSON_AddStringToObject(paramValue, S_GCDM, groupName);//工厂
			cJSON_AddStringToObject(paramValue, "wbsno", wbsno);
			cJSON_AddStringToObject(paramValue, "owninguser", owninguser);
			cJSON_AddStringToObject(paramValue, "factory", groupName);
			int ref_cnt = 0;
			tag_t *refs = NULLTAG;
			ITKCALL(AOM_ask_value_tags(taskTag, "IMAN_reference", &ref_cnt, &refs));
			string ecnuid = "";
			bool ifbomchange = false;
			for (int i = 0; i < ref_cnt; i++) {
				char *type = NULL;
				ITKCALL(WSOM_ask_object_type2(refs[i], &type));
				if (tc_strcmp(type, "ZT2_BOMEC") == 0) {
					ifbomchange = true;
				}
				else if (tc_strcmp(type, "MSExcelX") == 0) {
					char* uid = NULL;
					ITK__convert_tag_to_uid(refs[i], &uid);
					ecnuid = uid;
				}
			}
			ref_cnt = 0;
			bool iffilechange = false;
			tag_t *afters = NULLTAG;
			ITKCALL(AOM_ask_value_tags(taskTag, "ZT2_ChangeAfter", &ref_cnt, &afters));
			//printf("ZT2_ChangeAfter ===>%d\n", ref_cnt);
			for (int i = 0; i < ref_cnt; i++) {
				char *type = NULL;
				ITKCALL(WSOM_ask_object_type2(afters[i], &type));
				//printf("type ===>%s\n", type);
				if (tc_strcmp(type, "ZT2_Design3DRevision") == 0 || tc_strcmp(type, "ZT2_ProcureRevision") == 0) {
					iffilechange = true;
					break;
				}
			}
			cJSON_AddStringToObject(paramValue, "ifbomchange", ifbomchange ? "Y" : "N");
			cJSON_AddStringToObject(paramValue, "iffilechange", iffilechange ? "Y" : "N");
			cJSON_AddStringToObject(paramValue, "ecnuid", ecnuid.c_str());
			cJSON_AddStringToObject(paramValue, "releasedate", releasedate);

			string productFeature, changeTypeStr;
			if (processCnt > 0) {
				char *type = processTypes[0];
				if (strcmp(type, "Y") == 0) {
					productFeature = S_Product;
				}
				else {
					productFeature = S_Process;
				}
			}
			if (strstr(changeType, S_ZSChange) != NULL) {
				changeTypeStr = S_ZSChange;
			}
			else {
				changeTypeStr = S_LSChange;
			}
			cJSON_AddStringToObject(paramValue, "S_ChangeTypes", changeTypeStr.c_str());
			cJSON_AddStringToObject(paramValue, "S_ProductFeature", productFeature.c_str());
			cJSON_AddStringToObject(paramValue, "S_ProductNumber", productModel);
			cJSON_AddStringToObject(paramValue, "S_ProductName", productName);
			cJSON_AddStringToObject(paramValue, "S_Pcodename", fileName);
			cJSON_AddStringToObject(paramValue, "S_Contractcode", contractNo);
			cJSON_AddStringToObject(paramValue, "S_ContractName", contractName);
			ITKCALL(DATE_date_to_string(changeDate, "%Y-%m-%d", &changeDateStr));
			cJSON_AddStringToObject(paramValue, "T_MaterialDate", changeDateStr);
			vector<string> split;
			Split(changeUnits[0], "   ", split);
			string ffbm = "";
			for (int i = 0; i < split.size(); i++) {
				if (i > 0)
					ffbm.append(";");
				ffbm.append(split[i]);
			}
			cJSON_AddStringToObject(paramValue, "S_FFBM", ffbm.c_str());//changeUnits[0]
			cJSON_AddStringToObject(paramValue, "S_FFBMOther", "");
			cJSON_AddStringToObject(paramValue, "S_ChangeCode", changeRequestNo);//更改申请单号
			cJSON_AddStringToObject(paramValue, "S_PLMCode", taskUid);
			cJSON_AddStringToObject(paramValue, "S_ChangeNotificationCode", itemId);
			//EH_DrawingInformation图样信息
			cJSON* infomations = getDrawingInform(taskTag);
			cJSON_AddItemToObject(paramValue, "EH_DrawingInformation", infomations);
			string jsbUUid = "", gybUUid = "", zlbUUid = "",
				scbUUid = "", cgbUUid = "", xszxUUid = "", cwbUUid = "";
			cJSON* jsbZx = getEHDepart(itemId, "技术部", jsbUUid);
			cJSON_AddItemToObject(paramValue, "EH_JSBZX", jsbZx);
			cJSON* gybZx = getEHDepart(itemId, "工艺部", gybUUid);
			cJSON_AddItemToObject(paramValue, "EH_GYBZX", gybZx);
			cJSON* zlbZx = getEHDepart(itemId, "质量部", zlbUUid);
			cJSON_AddItemToObject(paramValue, "EH_ZLBZX", zlbZx);
			cJSON* scbZx = getEHDepart(itemId, "生产部", scbUUid);
			cJSON_AddItemToObject(paramValue, "EH_SCBZX", scbZx);
			cJSON* cgbZx = getEHDepart(itemId, "采购部", cgbUUid);
			cJSON_AddItemToObject(paramValue, "EH_CGBZX", cgbZx);
			cJSON* saleCenter = getEHDepart(itemId, "销售中心", xszxUUid);
			cJSON_AddItemToObject(paramValue, "EH_XSZXZX", saleCenter);
			cJSON* cwbZx = getEHDepart(itemId, "财务部", cwbUUid);
			cJSON_AddItemToObject(paramValue, "EH_CWBZX", cwbZx);
			cJSON_AddStringToObject(paramValue, "S_JSBZRR", jsbUUid.c_str());
			cJSON_AddStringToObject(paramValue, "S_GYBZRR", gybUUid.c_str());
			cJSON_AddStringToObject(paramValue, "S_ZLBZRR", zlbUUid.c_str());
			cJSON_AddStringToObject(paramValue, "S_SCBZRR", scbUUid.c_str());
			if (tc_strstr(ffbm.c_str(), "采购") != NULL && bms.size() > 0) {
				//拼接发放部门对应配置的首选项CHINT_ChangeNoticeBM的人员，最终去重，以;拼接，写入此字段
				if (find(bms.begin(), bms.end(), cgbUUid) != bms.end()) {
					cgbUUid = "";
				}
				for (int i = 0; i < bms.size(); i++) {
					if (cgbUUid.size() > 0)
						cgbUUid.append(";");
					cgbUUid.append(bms[i]);
				}
			}
			cJSON_AddStringToObject(paramValue, "S_CGBZRR", cgbUUid.c_str());
			cJSON_AddStringToObject(paramValue, "S_XSZXZRR", xszxUUid.c_str());
			cJSON_AddStringToObject(paramValue, "S_CWBZRR", cwbUUid.c_str());

			char *json_to_char = cJSON_Print(paramValue);
			//lidy20240710
			string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" ";
			xml.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SOAP-ENV:Body><m:ChangeNotificationSync xmlns:m=\"http://tempuri.org/\"><m:data>");
			xml.append(json_to_char);
			xml.append("</m:data></m:ChangeNotificationSync></SOAP-ENV:Body></SOAP-ENV:Envelope>");

			//WriteLog("\nworkflowCode=EH_TE_ChangeNotice\nuserCode=%s\nfinishStart=true\n", userCode.c_str());
			string msg = "组织之后的XMLJSON：\n";
			msg.append(xml);
			//WriteLog("组织之后的XMLJSON：\n%s\n", xml.c_str());
			WriteLog2(msg.c_str());
			string returnMsg = "";// ecnSendOA(userCode.c_str(), json_to_char, url);
			for (int i = 0; i < urls.size(); i++) {
				returnMsg.append(ecnSendSRM(xml, urls[i]));//.append("\n")
			}
			WriteLog("解析结果：\n%s", returnMsg.c_str());
			//printf("解析结果：%zd\n", returnMsg.size());
			if (tc_strcmp(returnMsg.c_str(), "S") != 0) {
				ITKCALL(EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, returnMsg.c_str()));
			}
		}

	}
	CloseLog();
	//string str = uploadMinio("uploadMinio.jar", log_file);
	char date_string[MAX_PATH_LENGTHE], logFileName[MAX_PATH_LENGTHE];;
	time_t now;
	struct tm *p;
	time(&now);
	//current_time(&status_now);
	p = localtime(&now);
	memset(date_string, 0, sizeof(date_string));
	sprintf(date_string, "%4d%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);
	sprintf(logFileName, "%s\\%s_%s.log", LOG_PATH, taskUid, date_string);
	file_xcopy_stod(log_file, logFileName);
	//printf("str %s \n", str.c_str());
	return ifail;
}