
#include "epm/epm.h"
#include <tc/preferences.h>
#include <tccore/aom_prop.h>
#include <tccore/item.h>
#include <ics/ics.h>
#include <ics/ics2.h>
#include "ocilib.h"
#include "string_utils.h"
#include "epm_handler_common.h"
#include "tc_util.h"


string getCshVal2(string name, string code) {

	string strResult = "", schVal = "";
	//cmd指令
	char cmd[256] = "";
	strcpy(cmd, "java -jar \"");
	//strcat(cmd, jar_file);
	strcat(cmd, getenv("TC_ROOT"));
	strcat(cmd, "\\bin\\findCshSql.jar");
	strcat(cmd, "\" \"");
	strcat(cmd, name.c_str());
	strcat(cmd, "\" \"");
	strcat(cmd, code.c_str());
	strcat(cmd, "\"");
	printf("cmd：%s\n", cmd);
	char buf[8000] = { 0 };
	FILE* pf = NULL;
	if ((pf = _popen(cmd, "r")) == NULL) {
		printf("接口返回1");
	}

	while (fgets(buf, sizeof buf, pf)) {
		strResult += buf;
	}
	printf("接口返回：%s\n", strResult.c_str());
	_pclose(pf);

	return strResult;
}

EPM_decision_t chint_csh_check(EPM_rule_message_t msg) {
	printf("*********************  chint_csh_check is comming ！ *********************\n");

	EPM_decision_t customError = EPM_go;

	int url_num = 0;
	char** url_vals = NULL;
	ITKCALL(PREF_ask_char_values("database_tc", &url_num, &url_vals));
	string url = url_vals[0];
	url.append("/").append(url_vals[2]);
	printf("url ==> %s \n", url.c_str());
	if (ConnServer(url_vals[3], url_vals[4], (char*)url.c_str()) == -1)
	{
		ITKCALL(EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, "错误:TC数据库连接失败"));
		customError = EPM_nogo;
		return customError;
	}
	//获取登陆组
	tag_t group = NULLTAG;
	char *groupName = NULL;
	ITKCALL(POM_ask_group(&groupName, &group));
	ITKCALL(AOM_ask_value_string(group, PROP_GROUP_NAME, &groupName));

	tag_t pRoot = NULLTAG;
	ITKCALL(EPM_ask_root_task(msg.task, &pRoot));
	int target_count = 0;
	tag_t *targets = NULL;
	ITKCALL(EPM_ask_attachments(pRoot, EPM_target_attachment, &target_count, &targets));
	//map<string, string> map;
	string msgs = "";
	for (int t = 0; t < target_count; t++) {
		char* type = NULL;
		ITKCALL(WSOM_ask_object_type2(targets[t], &type));
		if (tc_strcmp(type, "ZT2_Design3DRevision") != 0) {
			continue;
		}
		char* item_id = NULL;
		ITKCALL(AOM_ask_value_string(targets[t], "item_id", &item_id));
		string id = item_id;
		//总装图号为前四位+300000P 拼接-后的尾缀
		string topID = id.substr(0, 4);
		topID.append("300000P").append(id.substr(11));
		tag_t top_classificationObject = NULLTAG, topItem = NULLTAG;
		ITKCALL(ITEM_find_item(topID.c_str(), &topItem));
		ITKCALL(ICS_ask_classification_object(topItem, &top_classificationObject));
		if (top_classificationObject == NULLTAG) {
			msgs.append(id).append("总装图未发送分类;\n");
			continue;
		}
		//找到总装图的分类属性：是否参数化设计，如果值为是，则进行下述的判断逻辑，如果为否，则跳过，无法检查
		char* value = NULL;
		ITKCALL(ICS_ask_attribute_value(top_classificationObject, "是否参数化设计", &value));
		printf("是否参数化设计==>%s\n", value);
		if (!value || tc_strlen(value) == 0) {
			//没维护的话，提示让维护总装图分类属性：是否参数化设计
			msgs.append("请维护总装图分类属性：是否参数化设计;\n");
		}
		else if (strcmp(value, "是") == 0) {
			char selectRxfs[200];
			sprintf(selectRxfs, "select \"param_code\" from CHINT_CSH_CHECK_RULE where \"factory\"='%s' and \"drawingno\"='%s'", groupName, id.substr(0, 11).c_str());
			int outputColumn3 = 0, outputValueCount3 = 0;
			char*** outputValue3 = NULL;
			printf("search CHINT_CSH_CHECK_RULE ===> %s\n", selectRxfs);
			QuerySQLNoInputParam(selectRxfs, &outputColumn3, &outputValueCount3, &outputValue3);
			if (outputValueCount3 == 0) {
				//errs.append(id).append("，");
				//msgs.append(id).append("图纸未查询到参数代号;\n");
			}
			else {
				string errs = "";
				for (int i = 0; i < outputValueCount3; i++) {
					string val = getCshVal2(id.substr(12).c_str(), outputValue3[i][0]);
					if (val.size() == 0) {
						errs.append(outputValue3[i][0]).append(";");
					}
				}
				if (errs.size() > 0) {
					msgs.append(id).append("图纸所需的参数化数据：").append(errs).append("缺少数据，请先去参数化软件提交后，再执行流程提交;\n");
				}
			}
		}
	}
	printf("msgs：%s\n", msgs.c_str());
	DisConnServer();
	if (msgs.size() > 0) {
		ITKCALL(EMH_store_error_s1(EMH_severity_user_error, 919502, msgs.c_str()));
		customError = EPM_nogo;
	}
	return customError;
}