ImportMethod_ProcessRoute.cs
12.8 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
using Hh.Mes.Common;
using Hh.Mes.Common.config;
using Hh.Mes.Common.DataTableTo;
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.WebEntity;
using Hh.Mes.POJO.WebEntity.bus;
using Hh.Mes.Service.Repository;
using MySqlX.XDevAPI.Common;
using MySqlX.XDevAPI.Relational;
using NPOI.OpenXmlFormats.Dml.Diagram;
using NPOI.POIFS.FileSystem;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using static Hh.Mes.Common.Exel.NpoiExcelImportHelper;
namespace Hh.Mes.Service.WebService.Planned
{
/// <summary>
/// 定义导入方法加上特性
/// dicDtSource 添加需要保存到数据源的数据源
/// 注意:只改对应业务的dt 数据【公共方法里面的读取的数据源不要修改、其他地方可能需要用到】
/// </summary>
public partial class ImportMethod
{
#region 工艺路线表 【Action里面的 base_process_route_head 】 是数据库表名 ,方法名没有特殊要求 ,注意不要删除sysDicDtSource里面的key
[Action("base_process_route_head")]
public Response BaseProcessRouteHeadMethod(Dictionary<string, SysDtAllCls> sysDicDtSource, dynamic user, DateTime nowTime)
{
#region init
var response = new Response();
//【修改项1】
var excelFileNameKeys = ExcelName.base_process_route_head.ToString();
response = ImportOhter.ImportMethodBefore(sysDicDtSource, response, excelFileNameKeys);
if (response.Code != 200)
{
response.Message = $"【{TableName.excelFileNameBaseProcessRouteHead}】" + response.Message;
return response;
}
//公共方法数据源,需要判断的数据源单独建一个
DataTable excelDtSourceDt = response.Result.data;
DataTable titleVerifySourceDt = response.Result.title;
#endregion
#region 创建dt、添加保存到数据库的dicDtSource 【修改项2】
var tableName = TableName.tableNameBaseProcessRouteHead;
var eqSaveDt = GetTableStructure(tableName);
if (eqSaveDt.Columns.Contains("Id")) eqSaveDt.Columns.Remove("Id");
var temp = new SysDtAllCls()
{
dtData = eqSaveDt
};
var dtSourceNameKey = tableName + TableName.tableSuffix;
sysDicDtSource.Add(dtSourceNameKey, temp);
#endregion
#region 去重 【修改项3 根据实际业务】
var tempExcelDtSourceDt = excelDtSourceDt.Distinct("processCode");
#endregion
///////////////////////////////////////////////////////////////////
#region 工艺路线明细
#region init
var response2 = new Response();
//【修改项1】
var excelFileNameKeys2 = ExcelName.base_process_route_detail.ToString();
response2 = ImportOhter.ImportMethodBefore(sysDicDtSource, response2, excelFileNameKeys2);
if (response2.Code != 200)
{
response2.Message = $"【{TableName.excelFileNameBaseProcessRouteDetail}】" + response2.Message;
return response2;
}
//公共方法数据源,需要判断的数据源单独建一个
DataTable excelDtSourceDt2 = response2.Result.data;
DataTable titleVerifySourceDt2 = response2.Result.title;
#endregion
#region 创建dt、添加保存到数据库的dicDtSource 【修改项2】
var tableName2 = TableName.tableNameBaseProcessRouteDetail;
var eqSaveDt2 = GetTableStructure(tableName2);
if (eqSaveDt2.Columns.Contains("Id")) eqSaveDt2.Columns.Remove("Id");
var temp2 = new SysDtAllCls()
{
dtData = eqSaveDt2
};
var dtSourceNameKey2 = tableName2 + TableName.tableSuffix;
sysDicDtSource.Add(dtSourceNameKey2, temp2);
#endregion
#region 去重 【修改项3 根据实际业务】
var tempExcelDtSourceDt_detail = excelDtSourceDt2.Distinct("oprSequenceCode");
#endregion
#endregion
#region 赋值 理论上是通用的,修改excel文件名 【修改项4】
var columns = tempExcelDtSourceDt.Columns;
var detail_columns = tempExcelDtSourceDt_detail.Columns;
for (int i = 0; i < tempExcelDtSourceDt.Rows.Count; i++)
{
//主表赋值
var dr = eqSaveDt.NewRow();
for (int c = 0; c < columns.Count; c++)
{
var colName = columns[c].ColumnName;
dynamic colNameVal = tempExcelDtSourceDt.Rows[i][colName].ToString();
response = ImportOhter.SetDataTableDataBefore(response, i, colName, colNameVal, titleVerifySourceDt, TableName.excelFileNameBaseProcessRouteHead);
if (response.Code != 200) return response;
colNameVal = ImportOhter.SetDataTableData(colNameVal, eqSaveDt.Columns[colName].DataType.Name);
dr[colName] = colNameVal;
if (colName == "productCode") productCodes = colNameVal;
}
dr["keys"] = Guid.NewGuid().ToString();
dr["createBy"] = user.Account;
dr["createTime"] = nowTime;
dr["productCode"] = "";
dr["productHeaderCode"] = "";
eqSaveDt.Rows.Add(dr);
//明细表赋值
for (int j = 0; j < tempExcelDtSourceDt_detail.Rows.Count; j++)
{
var dr2 = eqSaveDt2.NewRow();
for (int c = 0; c < detail_columns.Count; c++)
{
var colName = detail_columns[c].ColumnName;
dynamic colNameVal = tempExcelDtSourceDt_detail.Rows[j][colName].ToString();
response2 = ImportOhter.SetDataTableDataBefore(response2, j, colName, colNameVal, titleVerifySourceDt2, TableName.excelFileNameBaseProcessRouteDetail);
if (response2.Code != 200) return response2;
colNameVal = ImportOhter.SetDataTableData(colNameVal, eqSaveDt2.Columns[colName].DataType.Name);
dr2[colName] = colNameVal;
}
dr2["headkeys"] = dr["keys"];
dr2["bodyKeys"] = Guid.NewGuid().ToString();
dr2["createBy"] = user.Account;
dr2["createTime"] = nowTime;
eqSaveDt2.Rows.Add(dr2);
}
}
#endregion
#region 逻辑判断
#endregion
response = SaveBaseProcessRouteAfter(sysDicDtSource, user);//有关联的一系列表赋值
return response;
}
#endregion
#region Save BaseProcessRoute Success After
/// <summary>
/// 工艺路线表保存之后
/// </summary>
/// <param name="baseProcessRoute"></param>
public Response SaveBaseProcessRouteAfter(Dictionary<string, SysDtAllCls> sysDicDtSource, dynamic sysWebUser)
{
Response response = new Response();
response = ImportOhter.ImportMethodBefore(sysDicDtSource, response, ExcelName.base_process_route_head.ToString());
string user = sysWebUser?.Account;
try
{
#region AddBaseProcessProductRel => productCode 还是需要这个字段逗号隔开,用于生成 base_process_product_rel表;base_product_header不存在新增,存在跳过;
DataTable dt = sysDicDtSource["base_process_route_head_source"].dtData;
//产品保存到base_process_product_rel表
for (int i = 0; i < dt.Rows.Count; i++)
{
string[] products = productCodes?.ToString().Split(',').Distinct().ToArray();
var dr = dt.Rows[i];
if (products != null)
{
var bpprList = new List<base_process_product_rel> { };
foreach (var p in products)
{
var baseProcessProductRel = new base_process_product_rel()
{
keys = Guid.NewGuid(),
processCode = dr["processCode"].ToString(),
processName = dr["processName"].ToString(),
productCode = p,
createBy = user,
createTime = DateTime.Now
};
var isHave = Context.Queryable<base_process_product_rel>().Where(x => x.processCode == dr["processCode"].ToString() && x.productCode == p).ToList();
if (isHave.Count <= 0)//同时满足(相同工艺相同产品)条件;不存在新增,存在跳过
{
bpprList.Add(baseProcessProductRel);
}
}
sysDicDtSource.Add("base_process_product_rel" + TableName.tableSuffix, new SysDtAllCls() { dtData = DataTableHelp.ListToDt(bpprList) });
var bphList = new List<base_product_header> { };
foreach (var p in products)
{
//保存 base_product_header 不存在新增、存在跳过
//var ProcessHeadKeys = Context.Queryable<base_process_route_head>().Where(x => x.processCode == dr["processCode"].ToString())?.First();
var baseProductHeader = new base_product_header()
{
keys = Guid.NewGuid(),
productCode = p,
productName = "",
processCode = dr["processCode"].ToString(),
createTime = DateTime.Now,
createBy = user,
processHeadKeys = Guid.Parse(dt.Rows[i]["keys"].ToString()),
};
var isSave = Context.Queryable<base_product_header>().Where(x => x.processCode == dr["processCode"].ToString() && x.productCode == p).ToList();
if (isSave.Count <= 0)
{
bphList.Add(baseProductHeader);
}
}
var dtData = DataTableHelp.ListToDt(bphList);
if (dtData.Columns.Contains("productCodeOrProductName")) dtData.Columns.Remove("productCodeOrProductName");
if (dtData.Columns.Contains("Exel")) dtData.Columns.Remove("Exel");
sysDicDtSource.Add("base_product_header" + TableName.tableSuffix, new SysDtAllCls() { dtData = dtData });
}
}
#endregion
#region AddBaseWorkCenter => workCenterCode、workCenterName:保存到 base_work_center 表,不存在新增、存在跳过
DataTable dt_detail = sysDicDtSource["base_process_route_detail_source"].dtData;
var bwcList = new List<base_work_center> { };
for (int i = 0; i < dt_detail.Rows.Count; i++)
{
var dr = dt_detail.Rows[i];
var baseWorkCenter = new base_work_center()
{
key = Guid.NewGuid(),
workCenterCode = dr["workCenterCode"].ToString(),
workCenterName = dr["workCenterName"].ToString(),
createBy = user,
createTime = DateTime.Now,
};
var isHave = Context.Queryable<base_work_center>().Where(x => x.workCenterCode == dr["workCenterCode"].ToString()).ToList();
if (isHave.Count <= 0)//不存在新增,存在跳过
{
bwcList.Add(baseWorkCenter);
}
}
sysDicDtSource.Add("base_work_center" + TableName.tableSuffix, new SysDtAllCls() { dtData = DataTableHelp.ListToDt(bwcList) });
}
catch (Exception ex)
{
response.ResponseErr(ex.Message);
return response;
}
#endregion
return response;
}
#endregion
}
}