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.

517 lines
15 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.

#define _CRT_SECURE_NO_WARNINGS
#include "epm_handler_common.h"
void getTCMMesItemJson(int index, tag_t topLine, tag_t BomLine, map<string, string> typePropertyType, char parameters[100000]);
void getTCMMesBomLineProperty(tag_t topLine, tag_t BomLine, char * prodOrderNo, char * ERPCode, map<string, string> typePropertyType, char parameters[100000]);
bool isMesBomStart = true;
void getTCMMesItemBomProperty(tag_t item, map<string, string> typePropertyType, char parameters[100000])
{
char *itemType = NULL;
AOM_ask_value_string(item, "object_type", &itemType);
printf("itemType%s Start===================\n", itemType);
//过滤掉非版本的对象
if ((strstr(itemType, "Revision") == NULL) || (strstr(itemType, "Master") != NULL)
|| (strstr(itemType, "master") != NULL) || (strstr(itemType, "BOM") != NULL) || (strstr(itemType, "bom") != NULL) || (strstr(itemType, "Bom") != NULL))
{
DOFREE(itemType);
return;
}
//获取子项
tag_t top_line = NULLTAG, window = NULLTAG;
BOM_create_window(&window);
//设置顶层BOM
BOM_set_window_top_line(window, NULLTAG, item, NULLTAG, &top_line);
//获取单个的json字符串
if (isMesBomStart)
{
getTCMMesItemJson(-1, top_line, NULLTAG, typePropertyType, parameters);
isMesBomStart = false;
}
else
{
getTCMMesItemJson(-1, top_line, NULLTAG, typePropertyType, parameters);
}
BOM_close_window(window);
MEM_free(itemType);
}
int ML_TCMSendMesBom(EPM_action_message_t msg)
{
printf("=========================TCM BOM下发到SAP mes Start===================\n");
auto startTime = std::chrono::high_resolution_clock::now();
//POM_AM__set_application_bypass(true);
int ifail = ITK_ok;
int attachments_num = 0;
tag_t rootTask = NULLTAG, *attachments = NULLTAG;
//获取任务对象
EPM_ask_root_task(msg.task, &rootTask);
//获取任务目标对象
EPM_ask_attachments(rootTask, EPM_target_attachment, &attachments_num, &attachments);
//获取参数
char *argflag = NULL, *argvalue = NULL, *arg = NULL;
char messageUser[1024] = "", messageResult[1024] = "";
int arg_cnt = TC_number_of_arguments(msg.arguments);
printf("参数个数为:%d\n", arg_cnt);
if (arg_cnt > 0)
{
for (int i = 0; i < arg_cnt; i++)
{
//获取下一个参数从0开始
arg = TC_next_argument(msg.arguments);
//获取参数的名称和值
ifail = ITK_ask_argument_named_value((const char*)arg, &argflag, &argvalue);
if (stricmp(argflag, "ownerId") == 0)
{
if (argvalue != NULL)
{
strcpy(messageUser, argvalue);
}
}
else if (stricmp(argflag, "messageResult") == 0)
{
if (argvalue != NULL)
{
strcpy(messageResult, argvalue);
}
}
}
}
vector<char *> messageUserValues;
if (strstr(messageUser, ",") != NULL)
{
int vectorValueCount = 0;
char ** vectorValueChar = new char *[64];
split(messageUser, ",", vectorValueChar, &vectorValueCount);
for (int i = 0; i < vectorValueCount; i++)
{
messageUserValues.push_back(vectorValueChar[i]);
}
}
else
{
messageUserValues.push_back(messageUser);
}
int pref_cnt = 0;
char ** pref_vals = NULL;
//获取首选项的值
PREF_ask_char_values("ML_TCMBOMSendSapProperty", &pref_cnt, &pref_vals);
map<string, string> typePropertyType;
for (int j = 0; j < pref_cnt; j++)
{
if (strstr(pref_vals[j], "-") != NULL)
{
//按照-进行拆分。拆分条件
int valueCount = 0;
char ** valueChar = new char *[64];
//分割字符串
split(pref_vals[j], "-", valueChar, &valueCount);
typePropertyType[valueChar[0]] = valueChar[1];// .insert(std::pair<char*, char*>(valueChar[0], valueChar[1]));
}
}
char *tc_root_file = getenv("tc_root"); //C:\Siemens\Teamcenter11
char parameters[100000] = "";//写入到文件的值
strcat(parameters, "getTCMMesBomPropertys}}");
strcat(parameters, messageUser);
strcat(parameters, "}}");
/*strcat(parameters, "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">");
strcat(parameters, "<soapenv:Header/>");
strcat(parameters, "<soapenv:Body>");
strcat(parameters, "<tem:PostProcessesBOMData>");
strcat(parameters, "<tem:dataList>");*/
strcat(parameters, "[");
for (int i = 0; i < attachments_num; i++)
{
char* log;
AOM_ask_value_string(attachments[i], "object_string", &log);
printf("log:%s", log);
getTCMMesItemBomProperty(attachments[i], typePropertyType, parameters);
}
strcat(parameters, "]");
/*strcat(parameters, "</tem:dataList>");
strcat(parameters, "</tem:PostProcessesBOMData>");
strcat(parameters, "</soapenv:Body>");
strcat(parameters, "</soapenv:Envelope>");*/
isMesBomStart = true;
//获取当前时间
time_t now = time(0);
tm *p = localtime(&now);
char date[128] = "";
sprintf_s(date, "%04d%02d%02d%02d%02d%02d", 1900 + p->tm_year, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
//把数据用写入文件
char data_file[SS_MAXPATHLEN] = "";
strcat(data_file, tc_root_file);
strcat(data_file, "\\");
strcat(data_file, date);
strcat(data_file, ".txt");
ofstream file;
file.open(data_file);
file << parameters << endl; // 使用与cout同样的方式进行写入
file.close();
string strResult;
//cmd指令
char cmd[256] = "";
strcpy(cmd, "java -jar \"");
//strcat(cmd, jar_file);
strcat(cmd, tc_root_file);
strcat(cmd, "\\portal\\plugins\\");
strcat(cmd, "ML_SendSap.jar");
strcat(cmd, "\" ");
//传参
cout << data_file << endl;
strcat(cmd, data_file);
//用来传递本流程的流程名称(@分割)
//strcat(cmd,"@");
//strcat(cmd,handler_name);
printf("路径:\n%s\n", cmd);
char buf[8000] = { 0 };
FILE *pf = NULL;
if ((pf = _popen(cmd, "r")) == NULL) {
printf("接口返回:\n%s", "1");
}
while (fgets(buf, sizeof buf, pf)) {
strResult += buf;
}
_pclose(pf);
cout << strResult << endl;
unsigned int iSize = strResult.size();
if (iSize > 0 && strResult[iSize - 1] == '\n' && strlen(parameters) > 0)
{
strResult = strResult.substr(0, iSize - 1);
printf("下发失败\n");
cout << strResult << endl;
//发送消息到企业微信
for (auto messageUserValue : messageUserValues) {
BOMSendToWeixin(messageUserValue, rootTask, strResult);
}
if (strcmp(messageResult, "1") == 0)
{
ifail = 1;
}
}
else
{
POM_AM__set_application_bypass(TRUE);
//获取首次发送时间
for (int i = 0; i < attachments_num; i++)
{
char *itemType = NULL;
AOM_ask_value_string(attachments[i], "object_type", &itemType);
//过滤掉非版本的对象
if ((strstr(itemType, "Revision") == NULL) || (strstr(itemType, "Master") != NULL)
|| (strstr(itemType, "master") != NULL) || (strstr(itemType, "BOM") != NULL) || (strstr(itemType, "bom") != NULL) || (strstr(itemType, "Bom") != NULL))
{
DOFREE(itemType);
continue;
}
//更改发布状态
tag_t release_stat = NULLTAG;
RELSTAT_create_release_status("ML8_STBOMFB", &release_stat);
RELSTAT_add_release_status(release_stat, 1, &attachments[i], true);
DOFREE(itemType);
}
POM_AM__set_application_bypass(FALSE);
}
messageUserValues.clear();
vector<char *>().swap(messageUserValues);
MEM_free(attachments);
tc_root_file = NULL;
pref_vals = NULL;
if (ifail == 1)
{
EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, strResult.c_str());
}
auto stopTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stopTime - startTime);
std::cout << "ML_TCMSendMesBom用时:" << duration.count() / 1000 << std::endl;
printf("=========================TCM BOM下发到SAP End===================\n");
return ifail;
}
void getTCMMesItemJson(int index, tag_t topLine, tag_t BomLine, map<string, string> typePropertyType, char parameters[100000])
{
//获取订单BOM属性
char * prodOrderNo = NULL;//生产订单号
char * ERPCode = NULL;//ERP班组编码
tag_t revisions = NULLTAG;
AOM_ask_value_tag(topLine, "bl_line_object", &revisions);
//获取版本下的订单BOM IMAN_METarget
int tagCount = 0;
tag_t * tags = NULL;
AOM_ask_value_tags(revisions, "IMAN_METarget", &tagCount, &tags);
printf("订单BOM数量%d Start===================\n", tagCount);
if (tagCount > 0)
{
AOM_ask_value_string(tags[0], "ml8_ProdOrderNo", &prodOrderNo);
AOM_ask_value_string(tags[0], "ml8_ERPCode", &ERPCode);
char orderNo[64] = "";
if (strlen(prodOrderNo) == 7)
{
strcat(orderNo, "00000");
strcat(orderNo, prodOrderNo);
//prodOrderNo = orderNo;
}
else if (strlen(prodOrderNo) == 10)
{
strcat(orderNo, "00");
strcat(orderNo, prodOrderNo);
//prodOrderNo = orderNo;
}
getTCMMesBomLineProperty(topLine, BomLine, orderNo, ERPCode, typePropertyType, parameters);
}
DOFREE(tags);
DOFREE(prodOrderNo);
DOFREE(ERPCode);
}
void getTCMMesBomLineProperty(tag_t topLine, tag_t BomLine, char * prodOrderNo, char * ERPCode, map<string, string> typePropertyType, char parameters[100000])
{
//获取产线工艺信息
int count = 0;
tag_t *children_line = NULLTAG;
BOM_line_ask_all_child_lines(topLine, &count, &children_line);
printf("产线工艺数量%d Start===================\n", count);
for (int i = 0; i < count; i++)
{
//获取班组工艺
int classProcessCount = 0;
tag_t * classProcess_Bomline = NULLTAG;
BOM_line_ask_all_child_lines(children_line[i], &classProcessCount, &classProcess_Bomline);
printf("班组工艺数量%d Start===================\n", classProcessCount);
for (int kk = 0; kk < classProcessCount; kk++)
{
//获取工位工艺信息
int procLineCount = 0;
tag_t * procLine_Bomline = NULLTAG;
BOM_line_ask_all_child_lines(classProcess_Bomline[kk], &procLineCount, &procLine_Bomline);
printf("工位工艺数量%d Start===================\n", procLineCount);
for (int k = 0; k < procLineCount; k++)
{
//获取工位工艺下的工步信息
int stepCount = 0;
tag_t * step_Bomline = NULLTAG;
BOM_line_ask_all_child_lines(procLine_Bomline[k], &stepCount, &step_Bomline);
printf("工步数量%d Start===================\n", stepCount);
//先遍历获取工位编号和名称
char * itemId = NULL;
char * objectName = NULL;
for (int j = 0; j < stepCount; j++)
{
char *itemType = NULL;
AOM_ask_value_string(step_Bomline[j], "fnd0bl_line_object_type", &itemType);
printf("工步类型%s Start===================\n", itemType);
if (strcmp(itemType, "ML8_StationRevision") == 0)
{
AOM_ask_value_string(step_Bomline[j], "bl_item_item_id", &itemId);
AOM_ask_value_string(step_Bomline[j], "bl_rev_object_name", &objectName);
DOFREE(itemType);
break;
}
DOFREE(itemType);
}
printf("工位编号%s Start===================\n", itemId);
printf("工位名称 rev %s Start===================\n", objectName);
for (int j = 0; j < stepCount; j++)
{
//获取工步下的物料信息
int childrenCount = 0;
tag_t * children_Bomline = NULLTAG;
BOM_line_ask_all_child_lines(step_Bomline[j], &childrenCount, &children_Bomline);
printf("物料数量%d Start===================\n", childrenCount);
for (int kk = 0; kk < childrenCount; kk++)
{
tag_t revisions = NULLTAG;
AOM_ask_value_tag(children_Bomline[kk], "bl_line_object", &revisions);
char * materiaItemType = NULL;
AOM_ask_value_string(children_Bomline[kk], "fnd0bl_line_object_type", &materiaItemType);
char * materialNo = NULL;
char * quantity = NULL;
char * units = NULL;
AOM_ask_value_string(children_Bomline[kk], "bl_quantity", &quantity);
AOM_ask_value_string(children_Bomline[kk], "bl_item_uom_tag", &units);
printf("单位%s Start===================\n", units);
//转换单位大小写
if (units == NULL || strcmp(units, "") == 0 || strcmp(units, "Eac") == 0 || strcmp(units, "Each.") == 0 || strcmp(units, "每个") == 0)
{
units = "EA";
}
else
{
string unitsString = units;
transform(unitsString.begin(), unitsString.end(), unitsString.begin(), toupper);
printf("单位%s Start===================\n", unitsString);
char unitsChar[5] = "";
strcat(unitsChar, unitsString.c_str());
units = unitsChar;
}
printf("单位%s Start===================\n", units);
printf("物料类型%s Start===================\n", materiaItemType);
if (strcmp(materiaItemType, "ML8_PartRevision") == 0)
{
char * materiaItemId = NULL;
AOM_ask_value_string(children_Bomline[kk], "bl_item_item_id", &materiaItemId);
if (strstr(materiaItemId, "-") != NULL)
{
//按照,进行拆分。拆分条件
int valueCount = 0;
char ** valueChar = new char *[64];
//分割字符串
split(materiaItemId, "-", valueChar, &valueCount);
materialNo = valueChar[0];
}
else
{
materialNo = materiaItemId;
}
DOFREE(materiaItemId);
}
else
{
AOM_ask_value_string(revisions, "item_id", &materialNo);
}
if (strlen(materialNo) == 9)
{
char itemId[18] = "";
strcat(itemId, "000000000");
strcat(itemId, materialNo);
materialNo = itemId;
}
else if (strlen(materialNo) == 11)
{
char itemId[18] = "";
strcat(itemId, "0000000");
strcat(itemId, materialNo);
materialNo = itemId;
}
//拼接json字符串
if (isMesBomStart)
{
strcat(parameters, "{");
isMesBomStart = FALSE;
}
else
{
strcat(parameters, ",{");
}
strcat(parameters, "\"");
strcat(parameters, "LineCode");
strcat(parameters, "\"");
strcat(parameters, ":");
strcat(parameters, "\"");
strcat(parameters, ERPCode);
strcat(parameters, "\",");
strcat(parameters, "\"");
strcat(parameters, "PrdOrderCode");
strcat(parameters, "\"");
strcat(parameters, ":");
strcat(parameters, "\"");
strcat(parameters, prodOrderNo);
strcat(parameters, "\",");
strcat(parameters, "\"");
strcat(parameters, "StationCode");
strcat(parameters, "\"");
strcat(parameters, ":");
strcat(parameters, "\"");
strcat(parameters, itemId);
strcat(parameters, "\",");
strcat(parameters, "\"");
strcat(parameters, "StationName");
strcat(parameters, "\"");
strcat(parameters, ":");
strcat(parameters, "\"");
strcat(parameters, objectName);
strcat(parameters, "\",");
strcat(parameters, "\"");
strcat(parameters, "MaterialCode");
strcat(parameters, "\"");
strcat(parameters, ":");
strcat(parameters, "\"");
strcat(parameters, materialNo);
strcat(parameters, "\",");
strcat(parameters, "\"");
strcat(parameters, "QUOTA");
strcat(parameters, "\"");
strcat(parameters, ":");
strcat(parameters, "\"");
strcat(parameters, quantity);
strcat(parameters, "\",");
strcat(parameters, "\"");
strcat(parameters, "Unit");
strcat(parameters, "\"");
strcat(parameters, ":");
strcat(parameters, "\"");
strcat(parameters, units);
strcat(parameters, "\"");
strcat(parameters, "}");
DOFREE(materiaItemType);
DOFREE(materialNo);
DOFREE(quantity);
DOFREE(units);
}
DOFREE(children_Bomline);
}
DOFREE(step_Bomline);
DOFREE(objectName);
DOFREE(itemId);
}
DOFREE(procLine_Bomline);
}
DOFREE(classProcess_Bomline);
}
DOFREE(children_line);
}