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.

356 lines
11 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 <stdio.h>
#include <stdlib.h>
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<std::string> 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£¬Óû§Í¨¹ýÁ÷³Ì´¥·¢Ñ¡Ôñ»ùÓÚijЩ¹¤³§Í£ÓÃ
* @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<std::string, std::vector<std::string>> 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<string>
std::vector<std::string> 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<std::string> 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;
}