
#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"
#include <iostream>  
#include <string>  
#include <fstream>  

using namespace std;
extern "C" int POM_AM__set_application_bypass(logical bypass);

int jd_add_attachments(EPM_action_message_t msg) {
	POM_AM__set_application_bypass(true);
	tag_t root_task, *attachments;
	int att_cnt;
	EPM_ask_root_task(msg.task, &root_task);
	EPM_ask_attachments(root_task, EPM_target_attachment, &att_cnt, &attachments);
	TC_argument_list_t *arguments = msg.arguments;
	int arg_cnt = TC_number_of_arguments(arguments);
	map<string, string> paras;
	for (auto i = 0; i < arg_cnt; i++) {
		char *temp_key, *temp_val;
		ITK_ask_argument_named_value(TC_next_argument(arguments), &temp_key, &temp_val);
		paras[temp_key] = temp_val;
	}
	const char *relation = paras["realtion"].c_str();
	const char *item = paras["item"].c_str();
	printf("relation:%s\n", relation);
	printf("item:%s\n", item);

	vector<tag_t> csjh_rev;
	vector<int> csjh_type;
	for (int i = 0;i < att_cnt;i++) {
		char *att_type;
		AOM_ask_value_string(attachments[i], "object_type", &att_type);
		if (tc_strcmp(att_type, "ItemRevision") == 0) {
			tag_t *csjh;
			int csjh_cnt;
			AOM_ask_value_tags(attachments[i], relation, &csjh_cnt, &csjh);
			for (int ii = 0;ii < csjh_cnt;ii++) {
				char *item_type;
				AOM_ask_value_string(csjh[ii], "object_type", &item_type);
				if (tc_strcmp(item_type, item) == 0) {
					tag_t *csjh_revs;
					int rev_cnt;
					date_t date = NULLDATE;
					AOM_ask_value_tags(csjh[i], "revision_list", &rev_cnt, &csjh_revs);
					AOM_ask_value_date(csjh_revs[rev_cnt - 1], "date_released", &date);
					if (DATE_IS_NULL(date)) {
						char *name;
						AOM_ask_value_string(csjh_revs[rev_cnt - 1], "object_string", &name);
						printf("%s add success\n", name);
						csjh_rev.push_back(csjh_revs[rev_cnt - 1]);
						csjh_type.push_back(EPM_target_attachment);
					}
					MEM_free(csjh_revs);
				}
			}
			MEM_free(csjh);
		}
	}

	if (csjh_rev.size() > 0) {
		tag_t *atts = &csjh_rev[0];
		int *types = &csjh_type[0];
		EPM_add_attachments(root_task, csjh_rev.size(), atts, types);
	}

	POM_AM__set_application_bypass(false);
	return 0;
}