#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"
#include <chrono>
#include <cctype>  // for std::isspace
#define EPM_HANDLER_COMMON
#define MAX_PATH_LENGTH 200
#define MAX_PRINTLINE_LENGTH 400000




std::string removeWhitespace1(const std::string& str) {
	std::string result;
	result.reserve(str.size()); // 预留空间以提高性能

								// 使用 std::copy_if 和 std::isspace 来过滤掉空白字符
	std::copy_if(str.begin(), str.end(), std::back_inserter(result),
		[](unsigned char ch) { return !std::isspace(ch); });

	return result;
}

// 转义json字符串
void escapeJson1(std::string& str) {
	int len = str.length();
	int newLen = len;
	for (int i = 0; i < len; i++) {
		if (str[i] == '"' || str[i] == '\\') {
			newLen++;
		}
	}
	std::string escapedStr;
	escapedStr.reserve(newLen);
	for (int i = 0; i < len; i++) {
		if (str[i] == '"' || str[i] == '\\') {
			escapedStr.push_back('\\');
		}
		escapedStr.push_back(str[i]);
	}
	str = escapedStr;
}



char* utog(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;
}

char* gtou(const char* src_str)
{
	int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, NULL, 0);
	wchar_t* wstr = (wchar_t*)MEM_alloc((len + 1) * sizeof(wchar_t));
	memset(wstr, 0, len + 1);
	MultiByteToWideChar(CP_ACP, 0, src_str, -1, wstr, len);
	len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* str = (char*)MEM_alloc((len + 1) * sizeof(char));

	memset(str, 0, len + 1);
	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
	return str;
}


// 写入回调函数实现
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
	size_t totalSize = size * nmemb;
	char* buffer = (char*)userp;

	// 追加内容到缓冲区
	strncat(buffer, (char*)contents, totalSize);

	return totalSize; // 返回实际接收的字节数
}

void feishuSplit(string strArg, string spliter, vector<string>& ans)
{
	ans.clear();
	size_t index0 = 0;
	string one_arg;
	if (strArg.find_first_not_of(' ') == string::npos)
		strArg = "";
	while (strArg.size() > 0)
	{
		index0 = strArg.find_first_of(spliter);
		if (index0 != string::npos)
		{
			one_arg = strArg.substr(0, index0);
			strArg = strArg.substr(index0 + 1);
			ans.push_back(one_arg);
		}
		else
		{
			ans.push_back(strArg);
			break;
		}
	}
}


void WriteLog4(logical debug, const char* format, ...)
{
	va_list arg;
	char tmp[MAX_PRINTLINE_LENGTH];
	char date_string[MAX_PATH_LENGTH];
	time_t now;
	struct tm* p;

	//get the message
	memset(tmp, 0, sizeof(tmp));
	va_start(arg, format);
	vsprintf(tmp, format, arg);
	va_end(arg);
	//----------print to command window for trace--------//
	//printf("%s\n", tmp);
	printf("%s\n", tmp);
	//print message to log file
	TC_write_syslog("%s\n", tmp);
}
int chint_changenotice(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 = NULL;
	ITKCALL(PREF_ask_char_values("CHINT_ChangeNotice", &c_sql_value_count, &c_sql_values));

	WriteLog4(true, "------------------------------------------------------------------------------------");
	WriteLog4(true, "=========================");
	WriteLog4(true, "chint_changenotice start");
	WriteLog4(true, "=========================");

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

	//分割首选项
	std::map<std::string, std::string> prefMap;//统计集合
	for (int j = 0; j < c_sql_value_count; j++) {

		/*值：第一行为通知信息。
		其余行为通知的人员，“组真实值”:“邮箱域名前缀”，多个用户之间用“; ”隔开。
		例：用户“邵俊齐”所在的组真实值为M060，邮箱为shaojq@chint.com，
		则值为M060 : shaojq*/
		if (j != 0) {
			vector<string> ans;
			feishuSplit(c_sql_values[j], ":", ans);
			prefMap[ans[0]] = ans[1];
		}

	}
	WriteLog4(true, "首选项条数：%d", prefMap.size());
	for (int i = 0; i < att_cnt; i++)
	{
		tag_t tagt = NULLTAG;
		tagt = attachments[i];
		char* type;
		//获取版本下的数据集
		int dataset_num = 0;
		tag_t* dataset_tags = NULL;
		char* tempValue = NULL;
		char taskpuid[56] = "\0";
		ITK__convert_tag_to_uid(task_tag, &tempValue);
		tc_strcpy(taskpuid, tempValue);
		ITKCALL(AOM_ask_value_string(tagt, "object_type", &type));
		printf("object_type=%s\n", type);
		//获取当前登入人组
		//char* groupName = NULL;
		//tag_t t_group = NULLTAG;
		//ITKCALL(POM_ask_group(&groupName, &t_group));
		//printf("g=%s\n", groupName);
		//获取当前登入人的名称和id

		tag_t group;
		char* group_name;
		ITKCALL(AOM_ask_value_tag(tagt, "owning_group", &group));
		ITKCALL(AOM_ask_value_string(group, "name", &group_name));


		char* name;
		char* id;
		tag_t user;
		ITKCALL(POM_get_user(&name, &user));
		ITKCALL(POM_get_user_id(&id));
		printf("name================%s\n", name);
		printf("id================%s\n", id);
		string users = "";
		//判断组是否在首选项中存在
		for (auto it = prefMap.begin(); it != prefMap.end(); it++) {
			//当前组和首选项中组匹配
			if (strcmp(it->first.c_str(), group_name) == 0) {
				users = it->second;
				break;
			}
		}

		printf("users================%s\n", users);
		if (strcmp(users.c_str(), "") != 0) {
			//判断目标下是否存在  ZT2_Change

			if (strcmp("ZT2_Change", type) == 0) {




				//拼接json
				char* json = NULL;
				string contentStr;
				cJSON* top = cJSON_CreateObject();
				cJSON* emails = cJSON_CreateArray();
				cJSON* mobiles = cJSON_CreateArray();



				//拼接emails
				vector<string> usersVector;





				feishuSplit(users, ";", usersVector);
				for (int h = 0; h < usersVector.size(); h++) {
					cJSON* new_string = cJSON_CreateString(usersVector[h].c_str());
					cJSON_AddItemToArray(emails, new_string);
				}


				//找项目信息



				//拼接content  
				tag_t user;
				char* duser, *itemId;
				char* zt2_WBSNo, *zt2_ContractName;
				ITKCALL(AOM_ask_value_string(tagt, "item_id", &itemId));
				ITKCALL(AOM_ask_value_string(tagt, "zt2_WBSNo", &zt2_WBSNo));
				ITKCALL(AOM_ask_value_string(tagt, "zt2_ContractName", &zt2_ContractName));

				string th = "";
				int wlNum; tag_t* wlTags;
				ITKCALL(AOM_ask_value_tags(tagt, "ZT2_ChangeAfter", &wlNum, &wlTags));
				printf("wlNum======%d\n", wlNum);
				for (int j = 0; j < wlNum; j++) {
					char* thType;
					ITKCALL(AOM_ask_value_string(wlTags[j], "object_type", &thType));
					if (strcmp(thType, "ZT2_Design3DRevision") == 0) {
						char* thid;
						ITKCALL(AOM_ask_value_string(wlTags[j], "item_id", &thid));
						th.append(thid).append(";");
					}

				}
				printf("th======%s\n", th);

				ITKCALL(AOM_ask_value_tag(tagt, "owning_user", &user));
				ITKCALL(AOM_ask_value_string(user, "user_name", &duser));

				//获取当前时间

				auto now = std::chrono::system_clock::now();
				std::time_t time = std::chrono::system_clock::to_time_t(now);
				std::tm* ptm = std::localtime(&time);
				char buf[128];
				strftime(buf, sizeof(buf), "%Y.%m.%d", ptm);
				printf("time===%s\n", buf);
				contentStr.append(duser).append(" 发起的").append(itemId).append(c_sql_values[0]).append("(").append(th).append("-").append(zt2_WBSNo).append("-").append(zt2_ContractName).append(") 发起人:").append(duser).append("发布时间:").append(buf);
				cJSON_AddStringToObject(top, "content", contentStr.c_str());

				//sms
				cJSON_AddStringToObject(top, "sms", "false");



				cJSON_AddItemToObject(top, "emails", emails);
				cJSON_AddItemToObject(top, "mobiles", mobiles);

				json = cJSON_Print(top);

				printf("json================%s\n", json);
				string result;
				result.append(json);
				escapeJson1(result);


				string result1 = removeWhitespace1(result);
				printf("result1=============%s\n", result1.c_str());
				// lidy20240829新接口
				string cmd = "java -jar \"";
				cmd.append(getenv("TC_ROOT")).append("\\bin\\uploadMinio.jar\" FS \"").append(result1).append("\"");
				printf("cmd==>%s", cmd.c_str());
				FILE* pf = NULL;
				string strResult;
				char buf2[8000] = { 0 };
				if ((pf = _popen(cmd.c_str(), "r")) == NULL) {
					printf("接口返回1\n");
				}
				else {
					printf("接口返回2\n");
					while (fgets(buf2, sizeof buf2, pf)) {
						strResult += buf2;
					}
					_pclose(pf);
				}



				//调用接口
				/*CURL* curl;
				CURLcode res;
				char readBuffer[10000];
				memset(readBuffer, 0, 10000);
				curl = curl_easy_init();
				if (curl) {
				curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
				curl_easy_setopt(curl, CURLOPT_URL, "http://10.128.10.170/api/feishu/Message/sendMessages");
				curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
				curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
				struct curl_slist* headers = NULL;
				curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
				// 设置写入数据的目标缓冲区

				curl_easy_setopt(curl, CURLOPT_WRITEDATA, readBuffer);
				headers = curl_slist_append(headers, "Content-Type: application/json");
				curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
				//const char* data = "{\"emails\":[\"shaojq@chint.com\",\"zouy5@chint.com\"],\"mobiles\":[],\"sms\":false,\"content\":\"项目编号:P2303344售前资料已经获取完毕，请 登陆PLM系统查看。\"}";
				curl_easy_setopt(curl, CURLOPT_POSTFIELDS, gtou(json));
				res = curl_easy_perform(curl);
				}
				curl_easy_cleanup(curl);*/


				char* gbk_readBuffer = utog(strResult.c_str());
				printf("json================%s\n", gbk_readBuffer);
				cJSON* result_json = cJSON_Parse(gbk_readBuffer);
				cJSON* code = cJSON_GetObjectItem(result_json, "code");

				int code_int = code->valueint;

				if (code_int != 0) {
					printf("发送飞书失败\n");
				}

			}



		}











	}


	DOFREE(attachments);
	WriteLog4(true, "=========================");
	WriteLog4(true, "chint_changenotice end");
	WriteLog4(true, "=========================");
	return ifail;
}