#include "epm_handler_common.h"
#include <tc\tc_macros.h>
#include <tc\emh.h>
#include <tc\preferences.h>
#include <property\propdesc.h>
#include <epm\epm.h>
#include <epm\epm_toolkit_tc_utils.h>
#include <tccore\item.h>
#include <tccore\grmtype.h>
#include <tccore\grm.h>
#include <tccore\imantype.h>
#include <sa\am.h>
#include <sa\sa.h>
#include <tccore\aom.h>
#include <tccore\aom_prop.h>
#include <property\prop_errors.h>
#include <tccore\workspaceobject.h>
#include <qry\qry.h>
#include <bom\bom_attr.h>
#include <bom\bom.h>
#include <epm\signoff.h>
#include <pom\pom\pom.h>
#include <pom\pom\pom_errors.h>
#include <fclasses\tc_date.h>
#include <epm\cr.h>
#include <cfm\cfm.h>
#include <sa\am.h>
#include "tc/envelope.h"
#include <stdio.h>
#include "ae/dataset.h"
#include <ctype.h>
#include <string.h>
#include <string>
#include <iostream>
#include <iterator>
#include <map>
#include <vector>
#include "ce/ce.h"
#include <time.h>
#include <windows.h>  
#include <iostream>  
#include <sql.h>  
#include <sqlext.h>  
#include "string"
#include "sstream"
#include <sa/tcfile.h>
#include <tccore/aom.h>
#include <sa/tcfile_cache.h>
#include <form/form.h>
#include "ctime"
#include "tchar.h"

using namespace std;
extern "C" int POM_AM__set_application_bypass(logical bypass);
/**
*	时间表流程显示时间表名称
*/
int jd_schedule_joint(EPM_action_message_t msg) {

	printf("===================================\n");
	printf("设置时间表名称 开始\n");
	printf("===================================\n");
	printf("开超级权限\n");
	POM_AM__set_application_bypass(true);
	int ifail = ITK_ok;
	tag_t tast_tag = NULL_TAG, job;
	tast_tag = msg.task;

	if (tast_tag != NULL_TAG) {
		char * task_name = NULL;
		int count = 0;
		tag_t * schdule_task_nodes = NULL;

		printf("开始获取SCHDULE任务 \n");
		AOM_ask_value_tags(tast_tag, "project_task_attachments", &count, &schdule_task_nodes);
		printf("获取SCHDULE任务 count =[%d] \n", count);
		//printf("task_name:%s\n",task_name);
		printf("task_name-------------\n");
		if (count >0) {

			char * schdule_task_node_type = NULL;
			AOM_ask_value_string(schdule_task_nodes[0], "object_type", &schdule_task_node_type);
			char * scheduleTaskName = NULL;
			//char finish_date_str[300] = "";
			printf("object_type:%s\n", schdule_task_node_type);
			if (strcmp(schdule_task_node_type, "ScheduleTask") == 0) {
				tag_t schdule = NULL_TAG;
				//	date_t finish_date ;
				AOM_ask_value_tag(schdule_task_nodes[0], "schedule_tag", &schdule);
				AOM_ask_value_string(schdule_task_nodes[0], "object_name", &scheduleTaskName);
				printf("scheduleTaskName:%s\n", scheduleTaskName);
				if (schdule != NULL_TAG) {

					char * job_name = NULL;
					EPM_ask_job(tast_tag, &job);
					AOM_ask_value_string(job, "object_name", &job_name);
					stringstream ss;
					tag_t* project_list;
					int project_cnt;
					char* project_name = NULL;
					char* project_id = NULL;
					AOM_ask_value_tags(schdule,"project_list",&project_cnt,&project_list);
					if (project_list!=NULL) {
						AOM_ask_value_string(project_list[0], "project_id", &project_id);
						AOM_ask_value_string(project_list[0], "object_name", &project_name);
						ss << job_name;
						ss << "-";
						ss << project_id;
						ss << "-";
						ss << project_name;
						printf("new job name %s\n", ss.str().c_str());

						AOM_lock(job);
						AOM_set_value_string(job, "object_name", ss.str().c_str());
						//printf("str:%s\n", finish_date_str);
						printf("设置属性\n");
						AOM_save(job);
						AOM_unlock(job);
					}
					
					if (job_name != NULL) {
						MEM_free(job_name);
						job_name = NULL;
					}
					if (project_id != NULL) {
						MEM_free(project_id);
						project_id = NULL;
					}
					if (project_name != NULL) {
						MEM_free(project_name);
						project_name = NULL;
					}
				}
			}
			if (schdule_task_node_type != NULL) {
				MEM_free(schdule_task_node_type);
				schdule_task_node_type = NULL;
			}
		}
		if (task_name != NULL) {
			MEM_free(task_name);
			task_name = NULL;
		}
		if (schdule_task_nodes != NULL) {
			MEM_free(schdule_task_nodes);
			schdule_task_nodes = NULL;
		}

	}
	printf("关超级权限\n");
	POM_AM__set_application_bypass(false);
	printf("===================================\n");
	printf("设置时间表名称 结束\n");
	printf("===================================\n");

	return ifail;

}
