WelddingPost.cs
17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using Accessibility;
using Newtonsoft.Json.Linq;
using NPOI.HPSF;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Markup;
namespace HHWelding.WeldingTask
{
public class WelddingPost
{
///<summary>
/// 构造函数
/// </summary>
List<string> list = new List<string>();
//Dictionary<string, object> keyValuePairs = new Dictionary<string,object>();
string kname = string.Empty;
string DwgPtah = string.Empty;
///<summary>
/// 管段总材料接口
/// </summary>
string method = "/api/Upstream/Mes/V1/ep3dDataPush";
/// <summary>
/// EP3D图纸接口
/// </summary>
string tethod = "/api/Upstream/Mes/V1/ep3dUploadFile";
#region MES地址
private static string _MesPost = string.Empty;
public static string MesPost
{
get
{
if (_MesPost == string.Empty)
{
string Tem = ConfigurationManager.AppSettings["MesApiKey"];
_MesPost = Tem;
}
return _MesPost;
}
set { _MesPost = value; }
}
#endregion
#region 下发图片接口
public void GetDistinctValuesFromDataGrid(DataSet dataset)
{
string dwgpath;
string _msg = string.Empty;
if (dataset != null)
{
// 用于存储所有不重复值的列表
int count = 0;
List<string> distinctValues = new List<string>();
foreach (DataTable table in dataset.Tables)
{
// 从每个DataTable中获取指定字段(这里假设字段名为 "ColumnName",需替换为实际字段名)的不重复值
var tableDistinctValues = table.AsEnumerable()
.Select(row => row.Field<string>("管线号"))
.Distinct();
// 将每个表中的不重复值添加到总的列表中
distinctValues.AddRange(tableDistinctValues);
}
// 去除总的列表中可能存在的重复值(因为不同表中可能有重复元素)
distinctValues = distinctValues.Distinct().ToList();
// 可以对获取到的不重复值进行进一步操作,比如遍历展示等
foreach (string value in distinctValues)
{
// 打开配置文件
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var appSettings = config.AppSettings.Settings;
if (appSettings["TrueUrl"].Value.Contains(value))
{
//MessageBox.Show(value+"已经上传给产线系统");
kname += value + ",";
continue;
}
// 要修改的键
string keyToModify = "TrueUrl";
// 新的值
string newValueToAppend = value;
if (appSettings[keyToModify] != null)
{
string existingValue = appSettings[keyToModify].Value;
// 组合新值和旧值
string combinedValue = existingValue + newValueToAppend;
appSettings[keyToModify].Value = combinedValue;
}
// 保存修改后的配置文件
config.Save(ConfigurationSaveMode.Modified);
// 刷新配置,使修改生效
ConfigurationManager.RefreshSection("appSettings");
string filename = value + ".dwg";
dwgpath = appSettings["UrlCsv"].Value + @"\" + filename;
string path = Regex.Replace(dwgpath, @"\s+", "").Trim();
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
// 此时fileStream就是文件流对象,可以对其进行读取操作
byte[] buffer = new byte[1024];
int bytesRead;
if (!File.Exists(path))
{
MessageBox.Show(dwgpath + "无文件请检查数据");
}
else
{
JObject job = new JObject();
job.Add("files", path);
string res = Post(MesPost + tethod, fileStream.ToString(), ref _msg);
if (res == "No files")
{
MessageBox.Show("图片上传MES失败");
}
else
{
MessageBox.Show(kname + "已经上传给产线系统");
}
count++;
}
}
}
//回传逻辑实现
if (count != 0)
{
MessageBox.Show("成功上传数据" + count + "条成功");
}
}
}
#endregion
/// <summary>
/// Post请求方法
/// </summary>
/// <param name="url"></param>
/// <param name="body"></param>
/// <param name="errText"></param>
/// <returns></returns>
#region 下发管段总材料接口
public void mThreadMtd(DataSet data)
{
JArray hobbies = new JArray();
JObject relationShip1 = new JObject();
var str = "";
string count = "";
string _msg = string.Empty;
string weldingPointNo2 = "";
string weldingPointNo1 = "";
string weldingMaterial1 = "";
string weldingMaterial2 = "";
if (data.Tables[0].Rows == null)
MessageBox.Show("表格中无数据");
foreach (DataRow dr in data.Tables[0].Rows)
{
if (count.Contains(dr["管段编号"].ToString()))
{
continue;
}
//这里循环出管段编号相同的焊口号做出拆分
var customername = from row in data.Tables[0].AsEnumerable()
where row.Field<string>("管段编号").StartsWith(dr["管段编号"].ToString())
group row by row.Field<string>("管段编号");
foreach (var group in customername)
{
foreach (var productRow in group)
{
// 这里可以访问每个产品行的其他字段,例如ProductName
string productName = productRow.Field<string>("焊点编号");
string pipeSectionNo1 = productRow.Field<string>("管段编号");
string weldingMaterial01 = productRow.Field<string>("焊接材料1");
string weldingMaterial02 = productRow.Field<string>("焊接材料2");
string pipeMaterialCode01 = productRow.Field<string>("材料代码");
if (string.IsNullOrEmpty(weldingPointNo1))
{
if (pipeMaterialCode01.Contains(weldingMaterial02))
{
weldingPointNo1 = productName;
weldingMaterial1 = weldingMaterial01;
continue;
}
else if (pipeMaterialCode01.Contains(weldingMaterial01))
{
weldingPointNo1 = productName;
weldingMaterial1 = weldingMaterial02;
continue;
}
}
else if (!string.IsNullOrEmpty(weldingPointNo1))
{
if (pipeMaterialCode01.Contains(weldingMaterial02))
{
weldingPointNo2 = productName;
weldingMaterial2 = weldingMaterial01;
continue;
}
else if (pipeMaterialCode01.Contains(weldingMaterial01))
{
weldingPointNo2 = productName;
weldingMaterial2 = weldingMaterial02;
continue;
}
}
}
}
string pipelineNo = dr["管线号"].ToString();
string pagination = dr["页码"].ToString();
string pipeSectionNo = dr["管段编号"].ToString();
string pipelineGrade = dr["管路等级"].ToString();
//string weldingMaterial1= dr["焊接材料1"].ToString();
//string weldingMaterial2 = dr["焊接材料2"].ToString();
string pipeLength = dr["管段长度"].ToString();
string pipeMaterialCode = dr["材料代码"].ToString();
string pipeNumber = dr["管段数量"].ToString();
count += pipeSectionNo + ",";
JObject job = new JObject();
job.Add("pipelineNo", pipelineNo);
job.Add("weldingPointNo1", weldingPointNo1);
job.Add("weldingPointNo2", weldingPointNo2);
job.Add("pagination", pagination);
job.Add("pipeSectionNo", pipeSectionNo);
job.Add("pipelineGrade", pipelineGrade);
job.Add("weldingMaterial1", weldingMaterial1);
job.Add("weldingMaterial2", weldingMaterial2);
job.Add("pipeLength", Convert.ToDecimal(pipeLength) * 1000);
job.Add("pipeMaterialCode", pipeMaterialCode);
job.Add("pipeNumber", pipeNumber);
job.Add("state", 0);
hobbies.Add(job);
// relationShip1.RemoveAll();
weldingPointNo1 = "";
weldingPointNo2 = "";
weldingMaterial1 = "";
weldingMaterial2 = "";
}
relationShip1.Add("details", hobbies);
str = relationShip1.ToString();
MessageBox.Show(str.ToString());
var res = Post(MesPost + method, str.ToString(), ref _msg);
if (res == "")
{
MessageBox.Show("请检查mes环境是否正常");
// job.RemoveAll();
relationShip1.RemoveAll();
}
else
{
JObject js = JObject.Parse(res);
string code = js.GetValue("Code").ToString();
string message = js.GetValue("Message").ToString();
if (code == "200")
{
MessageBox.Show("下发MES管段总材料成功");
}
else
{
MessageBox.Show("下发MES管段总材料失败原因:" + message);
}
//清空
relationShip1.RemoveAll();
// job.RemoveAll();
}
}
#endregion
#region 下发焊口接口
public void mThreadWtd(DataSet data)
{
JObject job = new JObject();
string _msg = string.Empty;
if (data.Tables[0].Rows == null)
MessageBox.Show("表格中无数据");
foreach (DataRow dr in data.Tables[0].Rows)
{
string Pipelinenumber = dr["管线号"].ToString();
string ym = dr["页码"].ToString();
string hdbh = dr["焊点编号"].ToString();
string gdbm = dr["管段编号"].ToString();
string gczj = dr["公称直径"].ToString();
string hdwj = dr["焊点外径"].ToString();
string yz = dr["英制"].ToString();
string sj = dr["吋径"].ToString();
string bhxl1 = dr["壁厚系列1"].ToString();
string bhxl2 = dr["壁厚系列2"].ToString();
string cz1 = dr["材质1"].ToString();
string cz2 = dr["材质2"].ToString();
string hdlx = dr["焊点类型"].ToString();
string hjlx = dr["焊接类型"].ToString();
string gldj = dr["管路等级"].ToString();
string rcl = dr["热处理"].ToString();
string gdjb = dr["管道级别"].ToString();
string hjcl1 = dr["焊接材料1"].ToString();
string ms1 = dr["描述1"].ToString();
string hjcl2 = dr["焊接材料2"].ToString();
string ms2 = dr["描述2"].ToString();
string hdwz = dr["焊点位置"].ToString();
job.Add("Pipelinenumber", Pipelinenumber);
job.Add("ym", ym);
job.Add("hdbh", hdbh);
job.Add("gdbm", gdbm);
job.Add("gczj", gczj);
job.Add("hdwj", hdwj);
job.Add("yz", yz);
job.Add("sj", sj);
job.Add("bhxl1", bhxl1);
job.Add("bhxl2", bhxl2);
job.Add("cz1", cz1);
job.Add("cz2", cz2);
job.Add("hdlx", hdlx);
job.Add("hjlx", hjlx);
job.Add("gldj", gldj);
job.Add("rcl", rcl);
job.Add("gdjb", gdjb);
job.Add("hjcl1", hjcl1);
job.Add("ms1", ms1);
job.Add("hjcl2", hjcl2);
job.Add("ms2", ms2);
job.Add("hdwz", hdwz);
MessageBox.Show(job.ToString());
//string res = Post(MesPost + method, job.ToString(), ref _msg);
//if (res.Length <= 0 || res.Length > 0)
//{
// MessageBox.Show("下发MES管段总材料失败");
//}
//else
//{
// JObject js = JObject.Parse(res);
// string code = js.GetValue("Code").ToString();
// string msg = js.GetValue("Msg").ToString();
// if (code == "200")
// {
// MessageBox.Show("下发MES管段总材料成功");
// }
// else
// {
// MessageBox.Show("下发MES管段总材料失败原因:" + msg);
// }
//}
job.RemoveAll();
}
}
#endregion
#region 下发切割接口
public void mThreeWeldingTask(DataSet data)
{
}
#endregion
#region POST
public string Post(string url, string body, ref string errText)
{
string jsonStr = string.Empty;
errText = string.Empty;
try
{
//创建Http请求
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.Timeout = 20000;
req.ContentType = "application/json;charset=utf-8";
//req.ContentType = "application/x-www-form-urlencoded";
var postData = Encoding.UTF8.GetBytes(body);
var reqStream = req.GetRequestStream();
reqStream.Write(postData, 0, body.Length);
reqStream.Close();
var rsp = (HttpWebResponse)req.GetResponse();
jsonStr = InStream2String(rsp.GetResponseStream(), ref errText);
rsp.Close();
return jsonStr;
}
catch (WebException ex)
{
if (ex.Response != null)
{
Stream stream = ex.Response.GetResponseStream();
//从连接错误流获取返回错误信息
String errorMsg = InStream2String(stream, ref errText);
errText = "提交失败,错误信息:" + errorMsg;
}
else { errText = ex.Message; }
return "";
}
}
#endregion
#region 读取流信息并且释放流
/// <summary>
/// 读取流信息并且释放流
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
private static string InStream2String(Stream stream, ref string errText)
{
StreamReader reader = null;
errText = string.Empty;
try
{
reader = new StreamReader(stream);
return reader.ReadToEnd();
}
catch (Exception ex)
{
errText = ex.Message;
return null;
}
finally
{
// 释放资源
if (reader != null) reader.Close();
if (stream != null) stream.Close();
}
}
#endregion
}
}