#include "tc_util.h"
#include "chint_Handler.h"

#include <stdlib.h>
// #include "common_itk_util.h"
#pragma warning(disable:4996)
// #include "util.h"

using namespace std;
void sendMessage(string user_id, string messag, string method);

int ChintSendMessage_1(EPM_action_message_t msg)
{
	// LINFO << "ChintSendMessage发送文本信息";
	char *task_type, *user_id, *job_name, *task_userID;
	tag_t task = msg.task;
	char *argflag =NULL,*argvalue=NULL ,*arg = NULL,arg_type[20] = "";
	int arg_cnt=0,i2=0,ifail=0;
	// cout<<"1111"<<endl;

	char *comments2=NULL;
	date_t decision_date;
	EPM_signoff_decision_t dec;
	
	arg_cnt = TC_number_of_arguments(msg.arguments);

	for (i2=0;i2<arg_cnt;i2++)
	{
		arg = TC_next_argument(msg.arguments);
		ITKCALL(ifail = ITK_ask_argument_named_value((const char*)arg, &argflag, &argvalue));
		if (stricmp(argflag, "type") == 0)
		{
			if(argvalue != NULL)
			{
				strcpy(arg_type,argvalue);
			}
		}

	}
	
	string code="1";
	if(strcmp("start",arg_type)==0)
	{
		code="1";
	}else if(strcmp("end",arg_type)==0)
	{
		code="2";
	}
	if(strcmp(code.c_str(),"1")==0)
	{
		ITKCALL(AOM_ask_value_string(task, "task_type", &task_type));
		//printf("task_type:%s\r\n",task_type);
		// LINFO << "节点task_type[" << task_type << "]";
		POM_get_user_id(&user_id);
		// LINFO << "当前登录用户ID[" << user_id << "]";
		tag_t user=NULL;
		char	*sub_task_object_type, *task_name;
		ITKCALL(AOM_ask_value_string(task, "object_type", &sub_task_object_type));
		// LINFO << "节点Object_type[" << sub_task_object_type << "]";
		ITKCALL(AOM_ask_value_string(task, "object_name", &job_name));
		tag_t workflow;
		ITKCALL(EPM_ask_root_task(task, &workflow));
		ITKCALL(AOM_ask_value_string(workflow, "job_name", &task_name));
		// LINFO << "节点名称:" << job_name;
		// LINFO << "流程名称：" << task_name;	
		stringstream id;
		if (strcmp(task_type, "EPMDoTask") == 0)
		{
			// LINFO << "DO节点";
			// tag_t user;
			EPM_ask_responsible_party(task, &user);

			
			if (user == NULL)
			{
				// LINFO << "DO节点未指派责任人";
				return 0;
			}
			POM_ask_user_id(user, &task_userID);
			id << task_userID;
		}
		else if ((strstr(sub_task_object_type, "ReviewTask") != NULL) || (strstr(sub_task_object_type, "AcknowledgeTask") != NULL))
		{
			// LINFO << "审核/认可节点";
			tag_t tmp_select_signoff_task;
			ITKCALL(EPM_ask_sub_task(task, EPM_select_signoff_team_task, &tmp_select_signoff_task));
			int user_count = 0;
			tag_t  *signoffs;
			ITKCALL(EPM_ask_task_reviewers_users(tmp_select_signoff_task, &user_count, &signoffs));
			if (user_count == 0)
			{
				// LINFO << "当前节点未指派用户->" << job_name;
				return 0;
			}
			else if (user_count>0)
			{
				id.str("");
				for (int i = 0; i<user_count; i++)
				{
					if (signoffs[i] == NULL)
					{
						// LINFO<<"为空";
						continue;
					}

					ITKCALL(POM_ask_user_id(signoffs[i], &task_userID));
					id << task_userID;
					if (i != (user_count - 1))
					{
						id << ";";
					}
				}

			}
		}
		else if (strcmp(task_type, "EPMPerformSignoffTask") == 0 || strcmp(task_type, "EPMSelectSignoffTask") == 0)
		{
			tag_t parent_task;
			ITKCALL(EPM_ask_parent_task(task, &parent_task));
			ITKCALL(AOM_ask_value_string(parent_task, "object_name", &job_name));
			// LINFO << "父节点名称:" << job_name;
			// LINFO << "签审节点";
			int user_count = 0;
			tag_t  *signoffs;
			ITKCALL(EPM_ask_task_reviewers_users(task, &user_count, &signoffs));
			if (user_count == 0)
			{
				// LINFO << "当前节点未指派用户->" << job_name;
				return 0;
			}
			else if (user_count>0)
			{
				id.str("");
				for (int i = 0; i<user_count; i++)
				{
					ITKCALL(POM_ask_user_id(signoffs[i], &task_userID));
					id << task_userID;
					if (i != (user_count - 1))
					{
						id << ";";
					}
				}

			}
		}
		// LINFO << "指派用户ID：" << id.str();
		//printf("即将要发送的用户ID:%s\r\n",user_id);	
		string name = task_name;
		if (name.find("\\") != string::npos)
		{
			name = name.replace(name.find("\\"), 1, "-");
		}
		if (name.find("/") != string::npos)
		{
			name = name.replace(name.find("/"), 1, "-");
		}

		string mess = "您有一个待处理的工作流程(";
		mess.append(name);
		mess.append("),请尽快登陆PLM系统处理。");
		// sendMessage(id.str().c_str(), mess, "ChintSendMessage");
		// LINFO << "ChintSendMessage文本信息发送结束";
	}else if(strcmp(code.c_str(),"2")==0)
	{
		ITKCALL(AOM_ask_value_string(task, "task_type", &task_type));
		//printf("task_type:%s\r\n",task_type);
		// LINFO << "节点task_type[" << task_type << "]";
		POM_get_user_id(&user_id);
		// LINFO << "当前登录用户ID[" << user_id << "]";
		tag_t user=NULL;
		char	*sub_task_object_type=NULL, *task_name=NULL,*owningUserId=NULL;
		tag_t owning_user=NULL;
		ITKCALL(AOM_ask_value_string(task, "object_type", &sub_task_object_type));
		// LINFO << "节点Object_type[" << sub_task_object_type << "]";
		ITKCALL(AOM_ask_value_string(task, "object_name", &job_name));
		tag_t workflow;
		ITKCALL(EPM_ask_root_task(task, &workflow));
		ITKCALL(AOM_ask_value_string(workflow, "job_name", &task_name));//流程名称
		AOM_ask_value_tag(workflow,"owning_user",&owning_user);//流程发起者
		AOM_UIF_ask_value(owning_user,"user_id",&owningUserId);

		// LINFO << "节点名称:" << job_name;
		// LINFO << "流程名称：" << task_name;	
		stringstream id;
		cout<<"===="<<task_type<<endl;
		if (strcmp(task_type, "EPMPerformSignoffTask") == 0 || strcmp(task_type, "EPMSelectSignoffTask") == 0)
		{
			//流程节点名称
			char *parent_name=NULL;
			AOM_ask_value_string(task,"parent_name",&parent_name);

			string trueName;
			EPM_ask_signoff_decision(task, &dec, &comments2, &decision_date);
			// cout<<"0"<<endl;
			trueName.append("您的流程\"").append(task_name).append("\"已通过\"").append(parent_name).append("\"，处理人");
			// cout<<"1"<<trueName<<endl;
		// EPM_ask_signoff_details()
			// cout<<"dec======"<<dec<<endl;
			
				int user_count = 0;
				tag_t  *reviewers=NULL;
				ITKCALL(EPM_ask_task_reviewers_users(task, &user_count, &reviewers));
				char *reviewerName=NULL;
				if(user_count>0)
				{
					for(int j=0;j<user_count;j++)
					{
						AOM_UIF_ask_value(reviewers[j],"person",&reviewerName);
						trueName.append(reviewerName).append("   ");

					}
				}
				trueName.append("，请及时处理！");
			/*if(dec=='Y')
			{
				cout<<"Y"<<endl;
				int user_count = 0;
				tag_t  *reviewers=NULL;
				ITKCALL(EPM_ask_task_reviewers_users(task, &user_count, &reviewers));
				char *reviewerName=NULL;
				if(user_count>0)
				{
					for(int j=0;j<user_count;j++)
					{
						AOM_UIF_ask_value(reviewers[j],"person",&reviewerName);
						trueName.append(reviewerName).append("   ");

					}
				}
				trueName.append("，请及时处理！");
				
		cout<<trueName.c_str()<<endl;
				// sendMessage(owningUserId, trueName, "ChintSendMessage");
			}else if(dec=='N')
			{
				cout<<"N"<<endl;
			}*/
			// cout<<"2"<<endl;
			cout<<"2"<<trueName<<endl;
			// sendMessage(owningUserId, trueName, "ChintSendMessage");
		}
		
	}

	return 0;
}

void sendMessage(string user_id, string messag, string method)
{
	char cmd[256] = "";
	string filesStr;
	filesStr.append("\"");
	filesStr.append(messag);
	filesStr.append("\"");
	char * tc_root_dir = getenv("TC_ROOT");
	string execStr;
	execStr.append("cmd.exe /c java -jar ");
	execStr.append(tc_root_dir);
	execStr.append("\\bin\\DesignTask.jar ");
	execStr.append(method);
	execStr.append(" ");
	execStr.append(user_id);
	execStr.append(" ");
	execStr.append(filesStr);
	// LINFO << "调用jar=" << execStr.c_str();
	printf("\n%s\n", execStr.c_str());
	system(execStr.c_str());

}