#pragma warning (disable: 4996) 
#pragma warning (disable: 4819) 
#pragma warning (disable: 4995) 

#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 <fclasses/tc_date.h>
#include "hx_custom.h"

int sum2(int y,int m,int d)
{
	unsigned char x[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	int i,s=0;
	for(i=1;i<y;i++)
		if(i%4==0 && i%100!=0 || i%400==0)
			s+=366;//闰年
		else
			s+=365;//平年
	if(y%4==0 && y%100!=0 || y%400==0)
		x[2]=29;
	for(i=1;i<m;i++)
		s+=x[i];//整月的天数
	s+=d;//日的天数
	return s;//返回总天数,相对公元1年
}
int jk_decide_time(EPM_rule_message_t msg)
{
	EPM_decision_t decision = EPM_go;
	int ifail = ITK_ok, arg_cnt = 0, i = 0, att_cnt = 0;
	tag_t task_tag = NULLTAG, rootTask_tag = NULLTAG, *attachments = NULL;
	char * object_TYPE_1 = NULL;
//	char * object_string1=NULL;
	string value_type = "";
	string value_property="";
	char * ref_type=NULL;
	int actualNum=0;
	tag_t *actualtags=NULL;
	string errorMessage="";
	//获取当前触发的任务
	task_tag = msg.task;
	//获取根流程节点
	ifail = EPM_ask_root_task(task_tag, &rootTask_tag);
	//获取目标引用对象
	ifail = EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &att_cnt, &attachments);
	boolean warn=false;
	date_t  dateValue1 ,dateValue2;
	int date1,date2;
	vector<string> shijian;
	vector<string> dates;
	vector<string> times;
	string bianzhitime;
	char * argflag = NULL ,
		*argvalue =NULL ,
		*arg = NULL , 
		arg_value[1024] = "\0";
	string minute="";
	//获取handler参数的个数
	arg_cnt = TC_number_of_arguments(msg.arguments);
	ECHO("参数个数为：%d\n",arg_cnt);
	if (arg_cnt > 0)
	{
		for (i=0;i<arg_cnt;i++)
		{
			
			//获取下一个参数（从0开始）
			arg = TC_next_argument(msg.arguments);
			//获取参数的名称和值
			ITK_ask_argument_named_value((const char*)arg, &argflag, &argvalue);
			//需要检查哪些对象的类型
			if (stricmp(argflag, "minute") == 0)
			{
				cout<<"1-------------"<<endl;
				if(argvalue != NULL)
				{
						cout<<"1.1-------------"<<endl;
					strcpy(arg_value,argvalue);
						cout<<"2-------------"<<endl;
					minute=arg_value;
				}else{
					EMH_store_error_s1(EMH_severity_warning, EMH_AE_error_base, "缺少minute参数");
						cout<<"3-------------"<<endl;
					return EPM_nogo;
				}
			}
		}
		MEM_free(argflag);
		MEM_free(argvalue);
		
			cout<<"4-------------"<<endl;
	}




	//循环目标对象
	for (i = 0; i < att_cnt; i++) {
		ifail = AOM_ask_value_string(attachments[i], "object_type", &object_TYPE_1);//对象类型
		cout << object_TYPE_1;
		cout << endl;
		if(strcmp("JK8MaterialRevision",object_TYPE_1)==0)//如果是虚拟件版本类型，就检查其下的实体物料的关系下有没有实体物料对象，并且对象下有发布的版本
		{
			ifail=AOM_ask_value_tags(attachments[i],"fnd0ActuatedInteractiveTsks",&actualNum,&actualtags);//已启动的交互任务
			for (int j=0;j<actualNum;j++)
			{
				char * object_name = NULL;
				AOM_ask_value_string(actualtags[j],"object_name",&object_name);
				//如果关系下的对象类型符合，查看下面有没有最新发布的版本
				if(strcmp(object_name,"批准")==0)
				{

					ITKCALL(AOM_ask_value_date(actualtags[j],"fnd0EndDate",&dateValue1));//编制的结束日期
					char * date_format = NULL;
					DATE_default_date_format(&date_format);
					char * date_str = NULL;

					DATE_date_to_string(dateValue1,date_format,&date_str);
					cout<<date_str<<endl;
					bianzhitime=date_str;
					Split(date_str," ",shijian);
					Split(shijian[0],"-",dates);
					Split(shijian[1],":",times);
					int year=atoi(dates[0].c_str());
					cout<<atoi(dates[0].c_str())<<atoi(dates[1].c_str())<<atoi(dates[2].c_str())<<endl;
					cout<<atoi(times[0].c_str())<<atoi(times[1].c_str())<<endl;
					date1= sum2(atoi(dates[0].c_str()),atoi(dates[1].c_str()),atoi(dates[2].c_str()))*10000+atoi(times[0].c_str())*100+atoi(times[1].c_str());
					if(date_format!=NULL)
					{
						MEM_free(date_format);
						date_format=NULL;
					}	
					if(date_str!=NULL)
					{
						MEM_free(date_str);
						date_str=NULL;
					}	
				}else if(strcmp(object_name,"通知")==0)
				{
					warn=true;
				



				}
				if(object_name!=NULL)
				{
					MEM_free(object_name);
					object_name=NULL;
				}	
			}
			
			if(actualtags!=NULL)
			{
				MEM_free(actualtags);
				actualtags=NULL;
			}
		

		}
		if(object_TYPE_1!=NULL)
		{
			MEM_free(object_TYPE_1);
			object_TYPE_1=NULL;
		}

	}
	time_t now;
	struct tm *p;	
	time(&now);
	p = localtime(&now);
	date2=sum2((p->tm_year + 1900),p->tm_mon,p->tm_mday)*10000+p->tm_hour*100+p->tm_min;
	cout<<(p->tm_year + 1900)<<"-"<<p->tm_mon<<"-"<<p->tm_mday<<"    "<<p->tm_hour<<":"<<p->tm_min<<endl;
	if(sum2((p->tm_year + 1900),p->tm_mon+1,p->tm_mday)>sum2(atoi(dates[0].c_str()),atoi(dates[1].c_str()),atoi(dates[2].c_str())))
	{
		warn=false;
	}else if(sum2((p->tm_year + 1900),p->tm_mon+1,p->tm_mday)==sum2(atoi(dates[0].c_str()),atoi(dates[1].c_str()),atoi(dates[2].c_str())))
	{
		int time=atoi(minute.c_str());
		if(p->tm_hour*60+p->tm_min-time>atoi(times[0].c_str())*60+atoi(times[1].c_str()))
		{
			warn=false;
		}else
		{
			warn=true;
		}
	}
	if(warn==true)
	{
		cout<<"提醒------------"<<endl;
	}else
	{
		cout<<"不提醒------------"<<endl;
	}


	if(warn==true)
	{
		errorMessage="请于传递物料的节点审批完成后，即"+bianzhitime+"，  "+minute+"分钟后再进行操作!";
		EMH_store_error_s1(EMH_severity_warning, EMH_AE_error_base, errorMessage.c_str());

		return EPM_nogo;
	}else
	{
		return EPM_go;
	}
}