#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 <tccore/item_errors.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 "common_itk_util.h"
#include <fclasses/tc_date.h>
#include "string_utils.h"
#include <tc/folder.h>
extern "C" int POM_AM__set_application_bypass(logical bypass);
/*
 * 后操作，当新建零组件类型为"Hxc8XM"，点击完成后，将对象发送到文件夹下
 */
int HXC_create_item_post(METHOD_message_t* msg , va_list va)
{
    fprintf(stdout,"get into post method HXC_create_item_post====================== \n");
    int ifail = ITK_ok ;
	char  *object_type=NULL;//类型
	char  *hxc8nianfen=NULL;//年份
	char  *hxc8type=NULL;//项目类型
	char  localization_status;
	logical  master  ; 
    char  new_item_rev_name[ITEM_type_size_c+1] = "";
	char errorStr[10240] = "\0";
	string error_string;
    //	logical istrue = FALSE;
    const char* item_id = va_arg(va,const char*); 
    const char* item_name = va_arg(va,const char*);
    const char* item_type = va_arg(va,const char*);
    const char* rev_id = va_arg(va,const char*);
    tag_t* item_tag = va_arg(va,tag_t*);
	cout<<"item_type------"<<item_type<<endl;
	//对对象过滤一下
	if(strcmp(item_type,"Hxc8XM")!=0)
	{
		return  ifail;
	}
   // tag_t* rev_tag = va_arg(va,tag_t*);
	char * name=NULL;
	ITKCALL(AOM_ask_value_string(*item_tag,"object_name",&name));
	cout<<"name------------------------>"<<name<<endl;
	//获得本地化的三个值  object_type（类型） hxc8nianfen（年份） hxc8type（项目类型）
//	ITKCALL(ifail = AOM_UIF_ask_localized_value_string(* item_tag,"object_type","zh_CN",&object_type,&localization_status,&master));//项目等级LOV
	ITKCALL(ifail = AOM_UIF_ask_localized_value_string(* item_tag,"hxc8nianfen","zh_CN",&hxc8nianfen,&localization_status,&master));//ninfen
	ITKCALL(ifail = AOM_UIF_ask_localized_value_string(* item_tag,"hxc8type","zh_CN",&hxc8type,&localization_status,&master));//项目等级LOV
	
	ITKCALL(ifail = AOM_ask_value_string(* item_tag,"hxc8nianfen",&hxc8nianfen));//ninfen
	ITKCALL(ifail = AOM_ask_value_string(* item_tag,"hxc8type",&hxc8type));//ninfen
	cout<<"hxc8nianfen------------------>"<<hxc8nianfen<<endl;
	cout<<"hxc8type------------------>"<<hxc8type<<endl;

	string depart=hxc8type;
	string year=hxc8nianfen;
	char** entries;
	char** values;
	
    if(*item_tag != NULLTAG && strcmp(item_type,"Hxc8XM")==0){
		int count = 0 ,entry_count = 0,status = 0,num_found=0;
		char ** values_list = NULL ;//** values = NULL, ** entries = NULL;

		tag_t query_tag = NULLTAG ,*results = NULL;
		
		//构建器
		ITKCALL(QRY_find("常规...",&query_tag));
		if(query_tag==NULLTAG)
		{
			printf("没找到 [%s]查询构建器！\n","常规.." );
			return 0;
		}
		printf("找到 [%s]查询构建器！\n","常规.." );
		
		ITKCALL(QRY_find_user_entries(query_tag,&entry_count,&entries,&values));//entries：查询条件表达式；values：查询条件值（MEM_free释放）
	if(entry_count==0)
	{
		cout<<"查询构建器中没有配置查询条件！"<<endl;
		ITK_exit_module(1);
		if(entries!=NULL){
			MEM_free(entries);
			entries= NULL;
		}
		if(values!=NULL){
			MEM_free(values);
			values= NULL;
		}
		return 0;
	}
	for(int n=0;n<entry_count;n++)
		{
		cout<<"查询名称属性"<<endl;	
		printf("entries[%d]=%s\n",n,entries[n]);

			printf("values[%d]=%s\n",n,values[n]);

		}

			char *other_values[2] ;
			char *other_entrys[2] ;
			other_values[0]= (char*)calloc(48,sizeof(char));
			other_values[1]= (char*)calloc(48,sizeof(char));
			other_entrys[0] = (char*)calloc(48,sizeof(char));
			other_entrys[1] = (char*)calloc(48,sizeof(char));
			tc_strcpy(other_entrys[0],"类型");
			tc_strcpy(other_entrys[1],"名称");
			tc_strcpy(other_values[0],"宏协承项目文件夹");
			tc_strcpy(other_values[1],"宏协承项目库");
//			printf("执行查询[%s][%s]\n",other_entrys[0],other_values[0]);
			ITKCALL(status = QRY_execute(query_tag,2,other_entrys,other_values,&num_found,&results));
			printf("执行查询结果[%d]\n",status);
			free(other_values[0]);
			free(other_values[1]);
			free(other_entrys[0]);
			free(other_entrys[1]);
			//printf("11111111111111\n");
			if(status !=ITK_ok){
				char * message =NULL;
				EMH_ask_error_text(status, &message);
				printf("查询异常 : \"%d\",\"%s\"\n",status,message);
				MEM_free(message);
				message =NULL;
				return 0;
			}
			
		int counnt1=0;
		int counnt2=0;
		tag_t * folders1=NULLTAG;
		tag_t * folders2=NULLTAG;
		char * name0=NULL;
		char * name1=NULL;
		char * name2=NULL;
		cout<<"00000000000000000"<<endl;
		for(int j =0;j < num_found;j++){
			//记录错误信息
			cout<<"111111111111111"<<endl;
			tag_t  folder=NULLTAG;
			folder=results[j];
//			ITKCALL(status=AOM_ask_value_string(folder,"object_name",&name0));
//			if(strcmp(type.c_str(),name0)==0)
//			{
				AOM_ask_value_tags(folder,"contents",&counnt1,&folders1);

				for (int q=0;q<counnt1;q++)
				{
					cout<<"22222222222222222222"<<endl;
					ITKCALL(status=AOM_ask_value_string(folders1[q],"object_name",&name1));
					if(strcmp(depart.c_str(),name1)==0)
					{
						ITKCALL(status=AOM_ask_value_tags(folders1[q],"contents",&counnt2,&folders2));
						for (int w=0;w<counnt2;w++)
						{
							cout<<"33333333333333"<<endl;
							ITKCALL(status=AOM_ask_value_string(folders2[w],"object_name",&name2));
							cout<<"name2"<<endl;
							if(strcmp(year.c_str(),name2)==0)
							{
								cout<<"444444444444444444444"<<endl;
								POM_AM__set_application_bypass(true);
								ITKCALL(status=(FL_insert(folders2[w],*item_tag,999)));
								//保存
								ITKCALL(status=AOM_save(folders2[w]));
								(AOM_unlock(folders2[w]));//20241211
								POM_AM__set_application_bypass(false);
							}
						}

					}
				}
//			}



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

		}
		if(folders1!=NULL){
			MEM_free(folders1);
			folders1 =NULL;
		}
		if(folders2!=NULL){
			MEM_free(folders2);
			folders2 =NULL;
		}	
		if(name0!=NULL){
			MEM_free(name0);
			name0 =NULL;
		}
		if(name1!=NULL){
			MEM_free(name1);
			name1 =NULL;
		}
		if(name2!=NULL){
			MEM_free(name2);
			name2 =NULL;
		}
		if(object_type!=NULL){
			MEM_free(object_type);
			object_type =NULL;
		}
		if(hxc8nianfen!=NULL){
			MEM_free(hxc8nianfen);
			hxc8nianfen =NULL;
		}
		if(hxc8type!=NULL){
			MEM_free(hxc8type);
			hxc8type =NULL;
		}


		//释放属性
		
		if(values_list!=NULL){
			for(int i = 0 ;i < count;i++){
				if(values_list[i]!=NULL){
					MEM_free(values_list[i]);
					values_list[i] = NULL;
				}
			}
			MEM_free(values_list);
			values_list =NULL;	
		}
    }
	if(ITEM_unable_to_create == ifail){
		//	添加错误信息
		EMH_store_error_s1(EMH_severity_user_error,ERROR_QRY_NOT_FOUND,error_string.c_str());
		error_string.clear();
	}
    	
    
    return ifail;
}
