You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
8.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include"kutil.h"
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include "libxl.h"
#include <io.h>
#include <direct.h>
#include <cfm/cfm.h>
#include <wchar.h>
#include <locale>
#include <codecvt>
#include <chrono>
#include <Windows.h>
#include <thread>
#include <regex>
#include <res\reservation.h>
#include "libxl.h"
#include <map>
#include <fstream>
#include <iostream>
#include <cstdio>
using namespace libxl;
using namespace std::chrono_literals;
using namespace std;
#define debug true
#define ITK_err 919012
std::string generateJsonString2(char* MATNR, char* FLAG,char * MATEREV,char * ZXPLC) {
// 拼接JSON字符串
std::string jsonString = "[\n";
jsonString += " {\n";
jsonString += " \"MATNR\":\"";
jsonString += MATNR;
jsonString += "\",\n";
jsonString += " \"MATEREV\":\"";
jsonString += MATEREV;
jsonString += "\",\n";
jsonString += " \"ZXPLC\":\"";
jsonString += ZXPLC;
jsonString += "\",\n";
jsonString += " \"FLAG\":\"";
jsonString += FLAG;
jsonString += "\"\n";
jsonString += " }\n";
jsonString += "]";
// 返回JSON字符串
return jsonString;
}
// 转义json字符串
void escapeJson1(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_PartChange
* @Description: 物料状态解冻\冻结同步接口1. 用户通过流程触发将物料状态解冻\冻结状态传递给MES和SAP
* @change 增加接口字段传递 MATEREV字段需要获取-type参数中指定版本上的item_revision_id属性值
ZXPLC字段获取-type参数中指定版本上的ld6_ifNewProduct,如未找到此属性,则传递“常规"
* @changeDate 2024/1/23
* @author hcj
*/
int LD_PartChange(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 * 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, "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_PartChange_Config", 0, &url);
//获取要发送的json字符串
std::string jsonString = generateJsonString2(MATNR, FLAG, MATEREV, ZXPLC);
printf("jsonString:%s\n", jsonString.c_str());
std::regex regex("\\s+");
std::string result = std::regex_replace(jsonString, regex, "");
// 转义处理后的json字符串
escapeJson1(result);
//调用jar包向接口发送数据
printf("开始执行cmd方法");
char cmd[1024] = "";
strcpy(cmd, "java -jar D:\\Siemens\\Teamcenter13\\bin\\sapjco3.jar");
strcat(cmd, " ");
strcat(cmd, " ");
strcat(cmd, url);
strcat(cmd, " ");
strcat(cmd, result.c_str());
strcat(cmd, " ");
strcat(cmd, "2");
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::stringstream buffer1;
buffer1 << file.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_partiffrozentosap", "成功"));
}
else {
//EMH_store_error_s2(EMH_severity_error, ITK_err, "提示", resultStr.c_str());
ITKCALL(AOM_set_value_string(doc_tags[i], "ld6_partiffrozentosap", "失败"));
erro = erro + resultStr;
}
AOM_save(doc_tags[i]);
AOM_unlock(doc_tags[i]);
AOM_refresh(doc_tags[i], true);
}
//清空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());
if (strcmp(resultStr.c_str(), "保存成功") == 0) {
ITKCALL(AOM_set_value_string(doc_tags[i], "ld6_partiffrozentomes", "成功"));
}
else{
erro2 = erro2 + resultStr;
ITKCALL(AOM_set_value_string(doc_tags[i], "ld6_partiffrozentomes", "失败"));
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, "提示", senderror.c_str());
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;
}