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.

297 lines
8.4 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden 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 <stdio.h>
#include <stdlib.h>
#include <filesystem>
#include <sys/stat.h>
#include <iostream>
using namespace libxl;
using namespace std::chrono_literals;
using namespace std;
#define debug true
#define ITK_err 919012
std::string generateJsonString2(char *folwId, vector<string> id, vector<string> version, vector<string> fileName) {
std::string jsonStr = "{\n";
jsonStr += " \"flowId\":\"" + std::string(folwId) + "\",\n";
jsonStr += " \"file\":\n";
jsonStr += " [\n";
for (int i = 0; i < id.size();i++) {
jsonStr += " {\n";
if (i == id.size()-1) {
jsonStr += " \"id\":\"" + std::string(id[i]) + "\",\n";
jsonStr += " \"version\":\"" + std::string(version[i]) + "\",\n";
jsonStr += " \"url\":\"" + std::string(fileName[i]) + "\"\n";
jsonStr += " }\n";
break;
}
jsonStr += " \"id\":\"" + std::string(id[i]) + "\",\n";
jsonStr += " \"version\":\"" + std::string(version[i]) + "\",\n";
jsonStr += " \"type\":\"" + std::string(fileName[i]) + "\"\n";
jsonStr += " },\n";
}
jsonStr += " ]\n";
jsonStr += " }\n";
return jsonStr;
}
// תÒåjson×Ö·û´®
void escapeJson2(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;
}
bool copyFileToFolder(const std::string& filePath, const std::string& folderPath) {
// ´ò¿ªÔ´Îļþ
std::ifstream sourceFile(filePath, std::ios::binary);
if (!sourceFile) {
std::cout << "Îļþ²»´æÔÚ»òÎÞ·¨´ò¿ª" << std::endl;
return false;
}
// »ñÈ¡ÎļþÃû
size_t lastSlashIndex = filePath.find_last_of("/\\");
std::string fileName = filePath.substr(lastSlashIndex + 1);
// ¹¹ÔìÄ¿±ê·¾¶
std::string destinationPath = folderPath + "/" + fileName;
// ´ò¿ªÄ¿±êÎļþ
std::ofstream destinationFile(destinationPath, std::ios::binary);
if (!destinationFile) {
std::cout << "ÎÞ·¨´´½¨Ä¿±êÎļþ" << std::endl;
return false;
}
// ¸´ÖÆÎļþ
destinationFile << sourceFile.rdbuf();
// ¹Ø±ÕÎļþ
sourceFile.close();
destinationFile.close();
std::cout << "ÎļþÒѳɹ¦¸´ÖƵ½Ä¿±êÎļþ¼Ð" << std::endl;
return true;
}
int LD_SendDJJDD_To_MES(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* doc_tags = NULLTAG;
string error;
char* arg = NULL, * argflag = NULL, * argvalue = NULL;
int arg_cnt = TC_number_of_arguments(msg.arguments);
char* types = NULL;
string erro2 = "";
string erro = "";
int send1 = 0;
int send2 = 0;
task = msg.task;
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, "type") == 0) {
if (argvalue != NULL)
{
printf("»ñÈ¡µÄÖµ%s:\n", argvalue);
types = argvalue;
}
}
}
printf("»ñÈ¡UID");
char* flowId;
ITK__convert_tag_to_uid(rootTask_tag, &flowId);
printf("%s\n", flowId);
//»ñÈ¡Á÷³ÌÏÂÄ¿±ê¹ØÏµÏµĶÔÏó
printf("8888888888888888888888888888888888888888888\n");
printf("»ñÈ¡Á÷³ÌÏÂÄ¿±ê¹ØÏµÏµĶÔÏó\n");
ITKCALL(EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &doc_num, &doc_tags));
printf("¿ªÊ¼Ñ­»·»ñÈ¡µÄÄ¿±ê¶ÔÏó%d\n", doc_num);
vector<string>ids;
vector<string> revisions;
vector<string> fileNames;
//»ñÈ¡Ê×Ñ¡ÏîµÄÖµ
printf("»ñÈ¡Ê×Ñ¡ÏîµÄÖµ\n");
char* url = NULL, *sendfilepath = NULL,*fileURL = NULL;
PREF_ask_char_value("LD_PLM2MES_DJJDD_Config", 0, &url);
PREF_ask_char_value("LD_PLM2MES_DJJDD_Config", 1, &sendfilepath);
PREF_ask_char_value("LD_PLM2MES_DJJDD_Config", 2, &fileURL);
printf("url:%s\n", url);
printf("sendfilepath:%s\n", sendfilepath);
printf("fileURL:%s\n", fileURL);
for (int i = 0; i < doc_num; i++) {
char* objtype = NULL;
printf("»ñÈ¡ÀàÐÍ\n");
ITKCALL(AOM_ask_value_string(doc_tags[i], "object_type", &objtype));
vector<string> ans;
Split(types, ";", ans);
for (int n = 0; n < ans.size(); n++) {
string gettype = ans[n];
printf("ÓëÁ÷³Ì²ÎÊýtype¶Ô±È%s=%s\n",gettype.c_str(), objtype);
if (strcmp(gettype.c_str(), objtype) == 0) {
//»ñÈ¡id
printf("»ñÈ¡id\n");
char* id = NULL;
ITKCALL(AOM_ask_value_string(doc_tags[i], "item_id", &id));
ids.push_back(id);
//»ñÈ¡revision
printf("»ñÈ¡revision\n");
char* version = NULL;
ITKCALL(AOM_ask_value_string(doc_tags[i], "item_revision_id", &version));
revisions.push_back(version);
//»ñÈ¡Êý¾Ý¼¯Îļþ
printf("»ñÈ¡Êý¾Ý¼¯Îļþ\n");
int das_num = 0;
tag_t* das_tags;
ITKCALL(AOM_ask_value_tags(doc_tags[i], "IMAN_specification", &das_num, &das_tags));
string files;
for (int m = 0; m < das_num;m++) {
char* fileName = NULL;
ITKCALL(AOM_ask_value_string(das_tags[m], "object_name", &fileName));
time_t nowtime;
time(&nowtime); //»ñÈ¡1970Äê1ÔÂ1ÈÕ0µã0·Ö0Ãëµ½ÏÖÔÚ¾­¹ýµÄÃëÊý
tm p;
localtime_s(&p, &nowtime); //½«ÃëÊýת»»Îª±¾µØÊ±¼ä,Äê´Ó1900ËãÆð,ÐèÒª+1900,ÔÂΪ0-11,ËùÒÔÒª+1
char fileDate[128] = "";
char buffer[80];
sprintf_s(buffer, "%04d%d%02d%02d%02d%02d", 1900 + p.tm_year, p.tm_mon + 1, p.tm_mday,p.tm_hour,p.tm_min,p.tm_sec);
printf("»ñÈ¡µÄʱ¼ä£º%s\n",buffer);
char sendname[SS_MAXPATHLEN] = "";
strcat(sendname, buffer);
strcat(sendname, "-");
strcat(sendname, fileName);
printf("ºÏ²¢ºóµÄ×Ö·û´®£º % s\n", sendname);
printf("¿ªÊ¼¸´ÖÆÎļþµ½file¿Õ¼ä\n");
tag_t ref_object = NULLTAG;
printf("1");
AE_reference_type_t reference_type;
printf("2");
char ref_name[WSO_name_size_c + 1] = "excel";
ITKCALL(AE_ask_dataset_named_ref2(das_tags[m], ref_name, &reference_type, &ref_object));
printf("3");
if (reference_type == AE_PART_OF)
{
printf("7");
char temp_file[SS_MAXPATHLEN] = "";
strcpy(temp_file, sendfilepath);
strcat(temp_file, "\\");
strcat(temp_file, sendname);
printf("temp_file%s", temp_file);
ITKCALL(IMF_export_file(ref_object, temp_file));
}
char temp_fileurl[SS_MAXPATHLEN] = "";
strcpy(temp_fileurl, fileURL);
strcat(temp_fileurl, sendname);
files = files + temp_fileurl + ",";
}
if (!files.empty()) {
files = files.substr(0, files.length() - 1);
fileNames.push_back(files);
}
else {
fileNames.push_back("");
}
}
}
}
printf("»ñȡҪ·¢Ë͵Äjson×Ö·û´®\n");
printf("flowId:%s\n", flowId);
//»ñȡҪ·¢Ë͵Äjson×Ö·û´®
std::string jsonString = generateJsonString2(flowId, ids, revisions,fileNames);
printf("jsonString:%s\n", jsonString.c_str());
std::regex regex("\\s+");
std::string result = std::regex_replace(jsonString, regex, "");
// תÒå´¦ÀíºóµÄjson×Ö·û´®
printf("תÒå´¦ÀíºóµÄjson×Ö·û´®\n");
escapeJson2(result);
printf("jsonString:%s\n", result.c_str());
//µ÷ÓÃ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, "3");
printf("\n%s\n", cmd);
system(cmd);
//¶ÁÈ¡±¾µØÎļþ»ñȡִÐнá¹û
//¶ÁÈ¡±¾µØµÄresultÎļþ£¨SAP£©
string fileid = flowId;
string MESfile = "D:\\TCTOMES\\" + fileid + "result.txt";
std::ifstream file(MESfile);
if (!file.is_open()) {
std::cerr << "Failed to open file!" << std::endl;
char* error = "MES·þÎñδÏìÓ¦";
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());
if (strcmp(resultStr.c_str(), "³É¹¦") == 0) {
return 0;
}
else {
EMH_store_error_s2(EMH_severity_error, ITK_err, "Ìáʾ", resultStr.c_str());
return 1;
}
}
return 0;
}
}