#include"kutil.h" #include #include #include #include #include "libxl.h" #include #include #include #include #include #include #include #include #include #include #include #include "libxl.h" #include #include #include #include #include using namespace libxl; using namespace std::chrono_literals; using namespace std; #define debug true #define ITK_err 919012 std::string generateJsonString(const char* MATNR, const char* FLAG, const char* WERKS, const char* MATEREV, const char* ZXPLC) { std::string jsonStr = "[\n"; jsonStr += " {\n"; jsonStr += " \"MATNR\":\"" + std::string(MATNR) + "\",\n"; jsonStr += " \"MATEREV\":\"" + std::string(MATEREV) + "\",\n"; jsonStr += " \"ZXPLC\":\"" + std::string(ZXPLC) + "\",\n"; jsonStr += " \"FLAG\":\"" + std::string(FLAG) + "\",\n"; jsonStr += " \"I_MARC\":\n"; jsonStr += " [\n"; std::vector werksVec; std::string werksStr(WERKS); size_t pos = werksStr.find(";"); if (pos != std::string::npos) { // WERKS contains comma, split it size_t startPos = 0; while (pos != std::string::npos) { std::string werksValue = werksStr.substr(startPos, pos - startPos); startPos = pos + 1; pos = werksStr.find(";", startPos); werksVec.push_back(werksValue); } werksVec.push_back(werksStr.substr(startPos)); } else { // WERKS does not contain comma werksVec.push_back(werksStr); } for (size_t i = 0; i < werksVec.size(); ++i) { jsonStr += " {\n"; jsonStr += " \"WERKS\":\"" + werksVec[i] + "\"\n"; jsonStr += (i == werksVec.size() - 1) ? " }\n" : " },\n"; } jsonStr += " ]\n"; jsonStr += " }\n"; jsonStr += "]\n"; return jsonStr; } // 转义json字符串 void escapeJson(std::string& str) { int len = str.length(); int newLen = len; for (int i = 0; i < len; i++) { if (str[i] == '"' || str[i] == '\\') { newLen++; } } std::string escapedStr; escapedStr.reserve(newLen); for (int i = 0; i < len; i++) { if (str[i] == '"' || str[i] == '\\') { escapedStr.push_back('\\'); } escapedStr.push_back(str[i]); } str = escapedStr; } /** * * @handlerName: LD_PartCompFLAG * @Description: 物料状态同步接口,根据参数设置,将数据通过接口传递到SAP和MES,用户通过流程触发选择基于某些工厂停用 * @change 增加接口字段传递 MATEREV字段需要获取-type参数中指定版本上的item_revision_id属性值 ZXPLC字段获取-type参数中指定版本上的ld6_ifNewProduct,如未找到此属性,则传递“常规" * @changeDate 2024/1/23 * @author hcj */ int LD_PartCompFLAG(EPM_action_message_t msg) { printf("开始执行"); int att_cnt = 0; int doc_num = 0; tag_t rootTask_tag = NULLTAG; tag_t task = NULLTAG; tag_t* attachments = NULLTAG; tag_t* doc_tags = NULLTAG; string error; char* arg = NULL, * argflag = NULL, * argvalue = NULL; int arg_cnt = TC_number_of_arguments(msg.arguments); char* WERKS = NULL, * FLAG = NULL, * type = NULL; task = msg.task; string erro2 = ""; string erro = ""; int send1 = 0; int send2 = 0; ITKCALL(EPM_ask_root_task(task, &rootTask_tag)); if (arg_cnt > 0) { for (int i = 0; i < arg_cnt; i++) { arg = TC_next_argument(msg.arguments); ITKCALL(ITK_ask_argument_named_value((const char*)arg, &argflag, &argvalue)); if (strcmp(argflag, "WERKS") == 0) { if (argvalue != NULL) { printf("获取的值%s:\n", argvalue); WERKS = argvalue; } } if (strcmp(argflag, "FLAG") == 0) { if (argvalue != NULL) { printf("获取的值%s:\n", argvalue); FLAG = argvalue; } } if (strcmp(argflag, "type") == 0) { if (argvalue != NULL) { printf("获取的值%s:\n", argvalue); type = argvalue; } } } //获取流程下目标关系下的对象 printf("获取流程下目标关系下的对象"); ITKCALL(EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &doc_num, &doc_tags)); for (int i = 0; i < doc_num; i++) { char* objtype = NULL; ITKCALL(AOM_ask_value_string(doc_tags[i], "object_type", &objtype)); if (strstr(type, objtype) != nullptr) { //获取物料编码 char* MATNR = NULL; char* MATEREV = NULL; char* ZXPLC = "常规"; ITKCALL(AOM_ask_value_string(doc_tags[i], "item_id", &MATNR)); ITKCALL(AOM_ask_value_string(doc_tags[i], "item_revision_id", &MATEREV)); try { AOM_ask_value_string(doc_tags[i], "ld6_ifNewProduct", &ZXPLC); } catch (const std::exception& e) { printf("没有获取到属性值ld6_ifNewProduct\n"); } //获取首选项的值 char* url = NULL; PREF_ask_char_value("LD_PLM2MES_PartCompFLAG_Config",0,&url); printf("url:%s\n", url); //获取要发送的json字符串 std::string jsonString = generateJsonString(MATNR, FLAG, WERKS,MATEREV,ZXPLC); printf("jsonString:%s\n", jsonString.c_str()); std::regex regex("\\s+"); std::string result = std::regex_replace(jsonString, regex, ""); // 转义处理后的json字符串 escapeJson(result); //调用jar包向接口发送数据 printf("开始执行cmd方法"); char cmd[1024] = ""; strcpy(cmd, "java -jar D:\\Siemens\\Teamcenter13\\bin\\sapjco3.jar"); strcat(cmd, " "); strcat(cmd, url); strcat(cmd, " "); strcat(cmd, result.c_str()); strcat(cmd, " "); strcat(cmd, "1"); printf("\n%s\n", cmd); system(cmd); //读取本地文件获取执行结果 //读取本地的result文件(SAP) string id = MATNR; string SAPfile = "D:\\TCTOSAP\\"+ id +"result.txt"; std::ifstream file(SAPfile); if (!file.is_open()) { std::cerr << "Failed to open file!" << std::endl; char* error = "SAP服务未响应"; EMH_store_error_s1(EMH_severity_error, EMH_AE_error_base, error); //EMH_store_error_s2(EMH_severity_error, ITK_err, "提示", error); return 1; } else { std::map> resultMap; std::string line; while (std::getline(file, line)) { std::istringstream iss(line); std::string key; std::string value; if (std::getline(iss, key, ':') && std::getline(iss, value)) { // 去除首尾的空格 key = key.substr(key.find_first_not_of(' ')); key = key.substr(0, key.find_last_not_of(' ') + 1); value = value.substr(value.find_first_not_of(' ')); value = value.substr(0, value.find_last_not_of(' ') + 1); // 将value字符串转换为vector std::vector valueVec; std::istringstream issValue(value); std::string singleValue; while (std::getline(issValue, singleValue, ',')) { valueVec.push_back(singleValue); } resultMap[key] = valueVec; } } file.close(); //获取表单 int tagnum; tag_t* tags = NULL; tag_t form = NULL; ITKCALL(AOM_ask_value_tags(doc_tags[i], "LD6_CompanyFormRelation", &tagnum, &tags)); if (tagnum>0) { for (int h = 0; h < tagnum;h++) { char* type; ITKCALL(AOM_ask_value_string(tags[h], "object_type", &type)); if (strcmp(type,"LD6_CompanyForm") == 0) { form = tags[h]; break; } } } else { printf("LD6_CompanyTable关系错误"); } //开始填充表格数据 for (const auto& entry : resultMap) { std::cout << "Key: " << entry.first << std::endl; int num; tag_t* rows; ITKCALL(AOM_ask_table_rows(form, "ld6_CompanyTable", &num, &rows)); int flnum = 0; for (int a = 0; a < num; a++) { char* getValue; ITKCALL(AOM_ask_value_string(rows[a], "ld6_company", &getValue)); if (strcmp(entry.first.c_str(), getValue) == 0) { std::vector valueMap = resultMap[entry.first]; AOM_lock(rows[a]); printf("FLAG:%s\n", FLAG); printf("valueMap[0]:%s\n", valueMap[0].c_str()); ITKCALL(AOM_set_value_string(rows[a], "ld6_partcompifdel", FLAG)); ITKCALL(AOM_set_value_string(rows[a], "ld6_partstatussyncsap", valueMap[0].c_str())); AOM_save(rows[a]); AOM_unlock(rows[a]); if (strcmp(valueMap[0].c_str(), "E") == 0) { erro = erro + entry.first + valueMap[1] +","; send1++; } } } } } //清空result文件的信息 std::ofstream file8(SAPfile, std::ios::trunc); // 打开文件并设置为写入模式,使用 trunc 模式会清空文件内容 if (!file8) { //删除result文件C:\TCTOLIMS remove(SAPfile.c_str()); } else { //删除result文件C:\TCTOLIMS remove(SAPfile.c_str()); file8.close(); // 关闭文件流 } //读取本地的result文件(MAS) string MASpath = "D:\\TCTOMAS\\" + id + "result.txt"; std::ifstream file1(MASpath); if (!file1.is_open()) { std::cerr << "Failed to open file!" << std::endl; char* error = "MES服务未响应"; EMH_store_error_s1(EMH_severity_error, EMH_AE_error_base, error); //EMH_store_error_s2(EMH_severity_error, ITK_err, "提示", error); return 1; } else { std::stringstream buffer1; buffer1 << file1.rdbuf(); std::string resultStr = buffer1.str(); printf("返回的值%s:\n", resultStr.c_str()); AOM_lock(doc_tags[i]); if (strcmp(resultStr.c_str(),"保存成功") == 0) { ITKCALL(AOM_set_value_string(doc_tags[i], "ld6_partstatusSyncMES", "成功")); } else{ erro2 = erro2 + resultStr; //EMH_store_error_s2(EMH_severity_error, ITK_err, "提示", resultStr.c_str()); ITKCALL(AOM_set_value_string(doc_tags[i], "ld6_partstatusSyncMES", "失败")); send2++; } AOM_save(doc_tags[i]); AOM_unlock(doc_tags[i]); AOM_refresh(doc_tags[i], true); } //清空删除文件 //清空result文件的信息 std::ofstream file2(MASpath, std::ios::trunc); // 打开文件并设置为写入模式,使用 trunc 模式会清空文件内容 if (!file2) { //删除result文件C:\TCTOLIMS remove(MASpath.c_str()); } else { //删除result文件C:\TCTOLIMS remove(MASpath.c_str()); file2.close(); // 关闭文件流 } } } //去掉最后一个逗号 if (!strcmp(erro.c_str(), "") == 0) { size_t lastCommaPos = erro.find_last_of(','); if (lastCommaPos != std::string::npos) { erro = erro.substr(0, lastCommaPos); } } if (send1>0 && send2>0) { string senderror = "错误信息:发送SAP" + erro + ",发送MES" + erro2; EMH_store_error_s1(EMH_severity_error, EMH_AE_error_base, senderror.c_str()); //EMH_store_error_s2(EMH_severity_error, ITK_err, "提示", ); return 1; }else if(send1 > 0 && send2 == 0) { string senderror = "错误信息:发送SAP" + erro; EMH_store_error_s1(EMH_severity_error, EMH_AE_error_base, senderror.c_str()); //EMH_store_error_s2(EMH_severity_error, ITK_err, "提示", senderror.c_str()); return 1; } else if (send1 == 0 && send2 > 0) { string senderror = "错误信息:发送MES" + erro2; EMH_store_error_s1(EMH_severity_error, EMH_AE_error_base, senderror.c_str()); //EMH_store_error_s2(EMH_severity_error, ITK_err, "提示", senderror.c_str()); return 1; } } return 0; }