ImportMethod.cs
8.77 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
using Hh.Mes.Common;
using Hh.Mes.Common.config;
using Hh.Mes.Common.log;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using System;
using System.Collections.Generic;
using System.Data;
using static Hh.Mes.Common.Exel.NpoiExcelImportHelper;
namespace Hh.Mes.Service.WebService.Planned
{
/// <summary>
/// 定义导入方法加上特性
/// dicDtSource 添加需要保存到数据源的数据源
/// 注意:只改对应业务的dt 数据【公共方法里面的读取的数据源不要修改、其他地方可能需要用到】
/// </summary>
public partial class ImportMethod : RepositorySqlSugar<base_material>
{
public string materialDtKey { get; set; }
private string productCodes { get; set; }
public ImportMethod()
{
}
#region 用户表 【Action里面的 sys_user 】 是数据库表名 ,方法名没有特殊要求 ,注意不要删除sysDicDtSource里面的key
[Action("sys_user")]
public Response SysUserMethod(Dictionary<string, SysDtAllCls> sysDicDtSource, dynamic user, DateTime nowTime)
{
#region init
var response = new Response();
//【修改项1】
var excelFileNameKeys = ExcelName.sys_user.ToString();
response = ImportOhter.ImportMethodBefore(sysDicDtSource, response, excelFileNameKeys);
if (response.Code != 200)
{
response.Message = $"【{TableName.excelFileNameSysUser}】" + response.Message;
return response;
}
//公共方法数据源,需要判断的数据源单独建一个
DataTable excelDtSourceDt = response.Result.data;
DataTable titleVerifySourceDt = response.Result.title;
#endregion
#region 创建dt、添加保存到数据库的dicDtSource 【修改项2】
var tableName = TableName.tableNameSysUser;
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("account");
#endregion
#region 赋值 理论上是通用的,修改excel文件名 【修改项4】
var columns = tempExcelDtSourceDt.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.excelFileNameSysUser);
if (response.Code != 200) return response;
colNameVal = ImportOhter.SetDataTableData(colNameVal, eqSaveDt.Columns[colName].DataType.Name);
dr[colName] = colNameVal;
}
dr["password"] = Encryption.Encrypt(ConfigRead.GetInstance.GetAppsetConnection().ResetPwd);
dr["status"] = 1;
dr["createBy"] = user.Account;
dr["createTime"] = nowTime;
eqSaveDt.Rows.Add(dr);
}
#endregion
#region 逻辑判断
//response.ResponseError("");
//return response;
#endregion
return response;
}
#endregion
#region 物料表 【Action里面的 base_material 】 是数据库表名 ,方法名没有特殊要求 ,注意不要删除sysDicDtSource里面的key
[Action("base_material")]
public Response BaseMaterialMethod(Dictionary<string, SysDtAllCls> sysDicDtSource, dynamic user, DateTime nowTime)
{
#region init
var response = new Response();
//【修改项1】
var excelFileNameKeys = ExcelName.base_material.ToString();
response = ImportOhter.ImportMethodBefore(sysDicDtSource, response, excelFileNameKeys);
if (response.Code != 200)
{
response.Message = $"【{TableName.excelFileNameBaseMaterial}】" + response.Message;
return response;
}
//公共方法数据源,需要判断的数据源单独建一个
DataTable excelDtSourceDt = response.Result.data;
DataTable titleVerifySourceDt = response.Result.title;
#endregion
#region 创建dt、添加保存到数据库的dicDtSource 【修改项2】
var tableName = TableName.tableNameBaseMaterial;
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("materialCode");
#endregion
#region 赋值 理论上是通用的,修改excel文件名 【修改项4】
var columns = tempExcelDtSourceDt.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.excelFileNameBaseMaterial);
if (response.Code != 200) return response;
colNameVal = ImportOhter.SetDataTableData(colNameVal, eqSaveDt.Columns[colName].DataType.Name);
dr[colName] = colNameVal;
}
dr["keys"] = Guid.NewGuid().ToString();
dr["createBy"] = user.Account;
dr["createTime"] = nowTime;
eqSaveDt.Rows.Add(dr);
}
#endregion
#region 逻辑判断
//response.ResponseError("");
//return response;
#endregion
return response;
}
#endregion
#region Save SysUser Success After
/// <summary>
/// SysUser表保存成功之后
/// </summary>
/// <param name="dateTime"></param>
public void SaveSuccessAfterSysUesr(DateTime dateTime, dynamic sysWebUser)
{
UpdateSysRelevance(sysWebUser.Account);
}
/// <summary>
/// 更新部门关联的用户
/// </summary>
private void UpdateSysRelevance(string user)
{
// UserOrg:用户机构关联KEY
var relKey = "UserOrg";
// 26:是“工艺管线预制车间”==>“巨驰”的部门ID
var deptId = 26;
var sql = $@"insert into sys_relevance (relKey,firstId,secondId,createBy)
select UserOrg='{relKey}' ,
t1.id ,
secondId={deptId},
createBy='{user}'
from sys_user t1 with (nolock)
left join (select distinct firstId from sys_relevance) t2 on t1.id=t2.firstId
where t2.firstId is null";
var result = base.Context.Ado.ExecuteCommand(sql);
if (result <= 0)
{
Log4NetHelper.Instance.Error($"【ImportMethod-UpdateSysRelevance 执行更新部门关联的用户失败 sql】" + sql);
}
}
#endregion
#region 公共方法
/// <summary>
/// 获取表结构 没有数据源,并设置dt的表名 ,去掉id列
/// </summary>
public DataTable GetTableStructure(string tableName)
{
var sql = $"select * from {tableName} where 1!=1";
var dt = base.Context.Ado.GetDataTable(sql);
dt.TableName = tableName;
//var columnsId = "id";
// if (dt.Columns.Contains(columnsId)) dt.Columns.Remove(columnsId);
return dt;
}
#endregion
}
}