#include "cJSON.h"

#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 <ae/datasettype.h>
#include "chint_Handler.h"
#include <regex>
#include <ics/ics.h>
#include "ado.h"
#include "ocilib.h"
#include <property/nr.h>
#include "CRUL_server_call_httpserver.h"
#include "common_itk_util.h"
#define EPM_HANDLER_COMMON
#define MAX_PATH_LENGTH 200
#define MAX_PRINTLINE_LENGTH 400000
using namespace std;




void WriteLog5(logical debug, const char* format, ...)
{
	va_list arg;
	char tmp[MAX_PRINTLINE_LENGTH];
	char date_string[MAX_PATH_LENGTH];
	time_t now;
	struct tm* p;

	//get the message
	memset(tmp, 0, sizeof(tmp));
	va_start(arg, format);
	vsprintf(tmp, format, arg);
	va_end(arg);
	//----------print to command window for trace--------//
	//printf("%s\n", tmp);
	printf("%s\n", tmp);
	//print message to log file
	TC_write_syslog("%s\n", tmp);
}
int chint_check_materialstatus(EPM_action_message_t msg) {

	tag_t task_tag = NULL_TAG,
		root_task_tag = NULLTAG,
		*attachments;;
	task_tag = msg.task;
	int att_cnt = 0, ifail = 0;

	int count = 1;

	WriteLog5(true, "------------------------------------------------------------------------------------");
	WriteLog5(true, "=========================");
	WriteLog5(true, "chint_check_materialstatus start");
	WriteLog5(true, "=========================");

	EPM_ask_root_task(task_tag, &root_task_tag);
	EPM_ask_attachments(root_task_tag, EPM_target_attachment, &att_cnt, &attachments);
	WriteLog5(true, "流程目标下的对象数量：%d", att_cnt);

	//停用集合
	std::map<std::string, std::string> tyMap;
	//封存集合
	std::map<std::string, std::string> fcMap;
	POM_AM__set_application_bypass(true);
	for (int i = 0; i < att_cnt; i++)
	{
		tag_t target = NULLTAG;
		target = attachments[i];
		char* objectType = NULL;
		char* fcStr = NULL, *tyStr = NULL, *zt2_State = NULL;
		//三种处理逻辑 物料  物料bom 图纸
		ITKCALL(AOM_ask_value_string(target, "object_type", &objectType));
		//物料
		printf("objectType======%s\n", objectType);
		if (tc_strcmp(objectType, "Part Revision") == 0) {
			//获取物料编码和物料名称
			char* zt2_MaterialNo = NULL;
			char* object_name = NULL;
			ITKCALL(AOM_ask_value_string(target, "object_name", &object_name));
			ITKCALL(AOM_ask_value_string(target, "zt2_MaterialNo", &zt2_MaterialNo));
			ITKCALL(AOM_ask_value_string(target, "object_desc", &tyStr));
			ITKCALL(AOM_UIF_ask_value(target, "zt2_State", &zt2_State));
			printf("tyStr======%s\n", tyStr);
			if (tc_strstr(tyStr, "停用")) {
				//放入停用map
				tyMap[zt2_MaterialNo] = object_name;
				continue;
			}
			else if (tc_strstr(zt2_State, "封存")) {
				//放入封存map
				fcMap[zt2_MaterialNo] = object_name;
				continue;
			}

		}

		//物料bom ：获取第一层
		if (tc_strcmp(objectType, "BOMView Revision") == 0) {
			//获取第一层
			////遍历子BOM获取属性
			//tag_t window_0_;
			//tag_t top_bom_line;
			//tag_t* childs;
			//int childNum;
			//char* json = NULL;
			////遍历BOM结构
			//ITKCALL(BOM_create_window(&window_0_));

			//tag_t topRev = NULLTAG;
			////bomline
			//int top_bl_line_object;
			//ITKCALL(BOM_line_look_up_attribute("bl_line_object", &top_bl_line_object));
			//ITKCALL(BOM_line_ask_attribute_tag(target, top_bl_line_object, &topRev));

			//ITKCALL(BOM_set_window_top_line(window_0_, NULLTAG, topRev, NULLTAG, &top_bom_line));
			//获取子

			tag_t bom_window_tag = NULLTAG,
				top_line_tag = NULLTAG;
			ITKCALL(BOM_create_window(&bom_window_tag));//创建window视图
			ITKCALL(BOM_set_window_top_line_bvr(bom_window_tag, target, &top_line_tag));//获取顶层bomline


			tag_t* childs;
			int childNum;
			ITKCALL(BOM_line_ask_child_lines(top_line_tag, &childNum, &childs));

			printf("childNum======%d\n", childNum);
			for (int i = 0; i < childNum; i++) {
				tag_t rev = NULLTAG;
				//bomline
				int bl_line_object;
				ITKCALL(BOM_line_look_up_attribute("bl_line_object", &bl_line_object));
				ITKCALL(BOM_line_ask_attribute_tag(childs[i], bl_line_object, &rev));
				char* objectType_rev = NULL;
				//获取rev属性判断
				ITKCALL(AOM_ask_value_string(rev, "object_type", &objectType_rev));
				//物料

				if (tc_strcmp(objectType_rev, "Part Revision") == 0) {
					//获取物料编码和物料名称
					char* zt2_MaterialNo = NULL;
					char* object_name = NULL;
					ITKCALL(AOM_ask_value_string(rev, "object_name", &object_name));
					ITKCALL(AOM_ask_value_string(rev, "zt2_MaterialNo", &zt2_MaterialNo));
					ITKCALL(AOM_ask_value_string(rev, "object_desc", &tyStr));
					ITKCALL(AOM_UIF_ask_value(rev, "zt2_State", &zt2_State));

					if (tc_strstr(zt2_State, "封存")) {
						//放入封存map
						fcMap[zt2_MaterialNo] = object_name;
					}
					else if (tc_strstr(tyStr, "停用")) {
						//放入停用map
						tyMap[zt2_MaterialNo] = object_name;
					}
				}
			}
			ITKCALL(BOM_close_window(bom_window_tag));//BOM_close_window
		}
		////图纸
		//if (strcmp(objectType, "ZT2_Design3DRevision") == 0) {
		//	//获取关系文件夹下的物料
		//	int gxNum; tag_t* gxTags; char* object_string = NULL;
		//	ITKCALL(AOM_ask_value_tags(target, "pseudo_folder", &gxNum, &gxTags));
		//	for (int i = 0; i < gxNum; i++) {
		//		ITKCALL(AOM_ask_value_string(gxTags[i], "object_string", &object_string));
		//		if (strcmp(object_string,"物料")) {
		//			//获取物料文件夹下的对象
		//			int wlNum; tag_t* wlTags;
		//			ITKCALL(AOM_ask_value_tags(target, "representation_for", &wlNum, &wlTags));
		//			for (int j = 0; j < gxNum; j++) {
		//				//获取rev属性判断
		//				char* objectType_rev = NULL;
		//				ITKCALL(AOM_ask_value_string(wlTags[j], "object_type", &objectType_rev));
		//				//物料

		//				if (strcmp(objectType_rev, "Part Revision") == 0) {
		//					//获取物料编码和物料名称
		//					char* zt2_MaterialNo = NULL;
		//					char* object_name = NULL;
		//					ITKCALL(AOM_ask_value_string(wlTags[j], "object_name", &object_name));
		//					ITKCALL(AOM_ask_value_string(wlTags[j], "zt2_MaterialNo", &zt2_MaterialNo));
		//					ITKCALL(AOM_ask_value_string(wlTags[j], "object_desc", &tyStr));
		//					ITKCALL(AOM_UIF_ask_value(wlTags[j], "zt2_State", &zt2_State));
		//					if (strstr(tyStr, "停用")) {
		//						//放入停用map
		//						tyMap[zt2_MaterialNo] = object_name;
		//						continue;
		//					}
		//					else if (strstr(zt2_State, "封存")) {
		//						//放入封存map
		//						fcMap[zt2_MaterialNo] = object_name;
		//						continue;
		//					}

		//				}
		//			}
		//		}
		//	}
		//}


		//图纸
		if (tc_strcmp(objectType, "ZT2_Design3DRevision") == 0) {
			//获取关系文件夹下的物料

			int wlNum; tag_t* wlTags;
			ITKCALL(AOM_ask_value_tags(target, "representation_for", &wlNum, &wlTags));
			printf("wlNum======%d\n", wlNum);
			for (int j = 0; j < wlNum; j++) {
				//获取rev属性判断
				char* objectType_rev = NULL;
				ITKCALL(AOM_ask_value_string(wlTags[j], "object_type", &objectType_rev));
				//物料
				printf("objectType_rev======%s\n", objectType_rev);
				if (tc_strcmp(objectType_rev, "Part Revision") == 0) {
					//获取物料编码和物料名称
					char* zt2_MaterialNo = NULL;
					char* object_name = NULL;
					ITKCALL(AOM_ask_value_string(wlTags[j], "object_name", &object_name));
					ITKCALL(AOM_ask_value_string(wlTags[j], "zt2_MaterialNo", &zt2_MaterialNo));
					ITKCALL(AOM_ask_value_string(wlTags[j], "object_desc", &tyStr));
					ITKCALL(AOM_UIF_ask_value(wlTags[j], "zt2_State", &zt2_State));
					printf("zt2_State======%s\n", zt2_State);
					if (tc_strstr(tyStr, "停用")) {
						//放入停用map
						tyMap[zt2_MaterialNo] = object_name;
						printf("00000000000000000000000000\n");
						continue;
					}
					else if (tc_strstr(zt2_State, "封存")) {
						//放入封存map
						fcMap[zt2_MaterialNo] = object_name;
						printf("11111111111111111111111111\n");
						continue;
					}
				}
			}
		}
	}
	POM_AM__set_application_bypass(false);
	//收集完map数据，进行拼接报错
	string errMsg = "";
	for (auto it = tyMap.begin(); it != tyMap.end(); it++) {
		errMsg.append(std::to_string(count++)).append(".").append(it->first).append("-").append(it->second).append(":物料已停用\n");
	}

	for (auto it = fcMap.begin(); it != fcMap.end(); it++) {
		errMsg.append(std::to_string(count++)).append(".").append(it->first).append("-").append(it->second).append(":物料已封存\n");
	}

	if (fcMap.size() > 0) {
		EMH_store_error_s1(EMH_severity_error, EMH_USER_error_base, errMsg.c_str()); //错误弹窗
		return -1;
	}


	if (tyMap.size() > 0) {
		EMH_store_error_s1(EMH_severity_warning, EMH_USER_error_base, errMsg.c_str()); //错误弹窗
	}


	DOFREE(attachments);
	WriteLog5(true, "=========================");
	WriteLog5(true, "chint_check_materialstatus end");
	WriteLog5(true, "=========================");
	return ifail;
}