BaseSimService.cs
15.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
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
using Hh.Mes.Common.Infrastructure;
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Hh.Mes.Pojo.System;
using NPOI.POIFS.FileSystem;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Crypto;
using System.Text;
using NPOI.OpenXmlFormats.Dml;
namespace Hh.Mes.Service.Base
{
public class BaseSimService : RepositorySqlSugar<base_sim>
{
public dynamic Load(PageReq pageReq, base_sim entity)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var result = new Response();
var expression = LinqWhere(entity);
//先组合查询表达式
var query = Context.Queryable<base_sim>().Where(expression);
//Exel为ture就不分页,因为导出的话是全部导出
if (pageReq != null)
{
int total = 0;
var list = query.ToOffsetPage(pageReq.page, pageReq.limit, ref total);
result.Result = HandleSimInfo(list);
result.Count = total;
}
else
{
result.Result = HandleSimInfo(query.ToList());
result.Count = result.Result.Count;
}
return result;
}, catchRetrunValue: "list");
}
public dynamic Ins(base_sim entity)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
var key = Guid.NewGuid();
entity.simKey = key;
entity.simId = entity.simNumber;
entity.createBy = sysWebUser.Account;
entity.createTime = DateTime.Now;
entity.getCardDate = null;
entity.debugDate = null;
//新增sim卡信息
Context.Insertable(entity).AddQueue();
var res = Context.SaveQueues();
if (res < 1) response.Message = SystemVariable.dataActionError;
return response;
});
}
public dynamic Upd(base_sim entity)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
//判断如果关联了设备,禁止修改卡号
var historySimInfo = Context.Queryable<base_sim_equipment_rel>().First(x => x.simNumber == entity.simNumber);
if (historySimInfo != null && entity.simNumber != historySimInfo.simNumber)
{
response.Code = 500;
response.Status = false;
response.Message = $"当前sim卡已经关联了设备,禁止修改卡号【{historySimInfo.simNumber}】,如果要修改请先解绑sim卡关联的设备!";
return response;
}
entity.updateBy = sysWebUser.Account;
entity.updateTime = DateTime.Now;
Context.Updateable(entity).AddQueue();//修改sim卡信息
var res = Context.SaveQueues();
if (res < 0) response.Message = SystemVariable.dataActionError;
return response;
});
}
public dynamic DelByIds(int[] ids)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
List<int> vpnRelIds = new List<int>();//存sim-vpn关联表的id
List<int> gatewayRelIds = new List<int>();//存sim-gateway关联表的id;
foreach (var id in ids)
{
//判断如果关联了设备,禁止删除
var simInfo = Context.Queryable<base_sim>().Where(x => x.id == id).First();
var isEqRel = Context.Queryable<base_sim_equipment_rel>().Any(x => x.simNumber == simInfo.simNumber);
if (isEqRel)
{
response.Code = 500;
response.Status = false;
response.Message = "当前sim卡已经关联了设备,禁止删除SIM卡,如果要删除请先解绑与sim卡关联的设备!";
return response;
}
//删除vpn,判断是否关联了sim卡, 删除sim卡时同时删除vpn、网关关联
var simkey = simInfo.simKey;
var vpnRel = Context.Queryable<base_sim_vpn_rel>().Where(x => x.simKey == simkey);
if (vpnRel.Any()) vpnRelIds.Add(vpnRel.First().id);
var gatewayRel = Context.Queryable<base_sim_gateway_rel>().Where(x => x.simKey == simkey);
if (gatewayRel.Any()) gatewayRelIds.Add(gatewayRel.First().id);
}
try
{
Context.Ado.BeginTran();
Context.Deleteable<base_sim>().In(ids).ExecuteCommand();
Context.Deleteable<base_sim_vpn_rel>().In(vpnRelIds).ExecuteCommand();
Context.Deleteable<base_sim_gateway_rel>().In(gatewayRelIds).ExecuteCommand();
Context.Ado.CommitTran();
}
catch (Exception ex)
{
Context.Ado.RollbackTran();
Log4NetHelper.Instance.Error($"删除sim卡操作失败:" + ex.Message);
}
return response;
});
}
public Response ExportData(base_sim entity)
{
return Load(null, entity);
}
public Expression<Func<base_sim, bool>> LinqWhere(base_sim model)
{
try
{
var exp = Expressionable.Create<base_sim>();
//数据过滤条件
//if (!string.IsNullOrWhiteSpace(model.XXX)) exp.And(x => x.XXX.Contains(model.XXX));
if (!string.IsNullOrWhiteSpace(model.simNumber))
{
exp.And(x => x.simNumber.Contains(model.simNumber));
}
if (!string.IsNullOrWhiteSpace(model.simIP))
{
exp.And(x => x.simIP.Contains(model.simIP));
}
if (!string.IsNullOrWhiteSpace(model.suppliersInfo))
{
exp.And(x => x.suppliersInfo.Contains(model.suppliersInfo));
}
return exp.ToExpression();//拼接表达式
}
catch (Exception ex)
{
throw new Exception($"{ex.Message}");
}
}
public dynamic BindEquipment(string simNumber, bool checkeds, string equipmentCode, string projectKeys)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
string[] strArray = equipmentCode.Split(new char[] { ',' });
var simBindCount = Context.Queryable<base_sim_equipment_rel>().Where(x => x.simNumber == simNumber).ToList().Count;
if (strArray != null && strArray.Length > 0)
{
var query = Context.Queryable<base_sim_equipment_rel>().Where(x => x.simNumber == simNumber && x.projectKey == new Guid(projectKeys));
foreach (var item in strArray)
{
if (checkeds)
{
var isHave = query.Any(x=>x.equipmentCode == item);
if (isHave) continue;
//添加
if (simBindCount < 250 && !isHave)
{
base_sim_equipment_rel bser = new base_sim_equipment_rel();
bser.simNumber = simNumber;
bser.equipmentCode = item;
bser.projectKey = new Guid(projectKeys);
Context.Insertable(bser).AddQueue();
response.Status = Context.SaveQueues() > 0;
if (!response.Status)
{
response.Message = "添加SIM卡关联设备数据失败!";
return response;
}
}
else
{
response.Message = "不可重复绑定!一张SIM卡只允许绑定250个设备,请选择其它SIM卡绑定";
return response;
}
}
else
{
Context.Deleteable<base_sim_equipment_rel>(x => x.simNumber == simNumber && x.projectKey == new Guid(projectKeys) && x.equipmentCode == item).AddQueue();
response.Status = Context.SaveQueues() > 0;
if (!response.Status)
{
response.Message = "删除sim卡与设备绑定关系数据失败!";
return response;
}
}
}
}
response.Message = "业务数据操作成功,稍后刷新列表数据!";
return response;
});
}
/// <summary>
/// 领卡操作 更新字段 领卡日期、调试日期、网关客户信息
/// 领卡操作 的时候 绑定 网关、vpn 信息操作
/// </summary>
/// <param name="ids"></param>
/// <param name="getCardDate"></param>
/// <param name="debugDate"></param>
/// <param name="client"></param>
/// <returns></returns>
public dynamic UseSim(base_sim entity)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
var e_simKey = entity.simKey;
var e_vpnKey = entity.vpnKey;
var e_gatewayKey = entity.gatewayKey;
if (e_vpnKey == Guid.Empty || e_gatewayKey == Guid.Empty)
{
response.Code = 500;
response.Status = false;
response.Message = $"vpn/网关不能为空";
return response;
}
var simInfo = Context.Queryable<base_sim>().First(sim => sim.id == entity.id);
simInfo.getCardDate = entity.getCardDate;
simInfo.debugDate = entity.debugDate;
simInfo.getCardUser = entity.getCardUser;
simInfo.gatewayClient = entity.gatewayClient;
simInfo.updateBy = sysWebUser.Account;
simInfo.updateTime = DateTime.Now;
base_sim_vpn_rel sim_Vpn_Rel = new base_sim_vpn_rel() { simKey = e_simKey, vpnKey = e_vpnKey };
base_sim_gateway_rel sim_Gateway_Rel = new base_sim_gateway_rel() { simKey = e_simKey, gatewayKey = e_gatewayKey };
//vpn关联
//添加之前判断有没有绑定其他sim卡
var isHaveVpn = Context.Queryable<base_sim_vpn_rel>().Where(x => x.simKey != e_simKey && x.vpnKey == e_vpnKey);
if (isHaveVpn.Any())
{
response.Code = 500;
response.Status = false;
response.Message = $"该VPN已经绑定了SIM卡{isHaveVpn.First().simKey},请选择其它的VPN !";
return response;
}
var isHaveVpnSim = Context.Queryable<base_sim_vpn_rel>().Where(x => x.simKey == e_simKey);
if (isHaveVpnSim.Any())
{
//修改
sim_Vpn_Rel = isHaveVpnSim.First();
sim_Vpn_Rel.vpnKey = e_vpnKey;
Context.Updateable(sim_Vpn_Rel).AddQueue();
}
else
{
//添加
Context.Insertable(sim_Vpn_Rel).AddQueue();
}
//绑定网关
//添加之前判断有没有绑定其他sim卡
var isHaveGate = Context.Queryable<base_sim_gateway_rel>().Where(x => x.simKey != e_simKey && x.gatewayKey == e_gatewayKey);
if (isHaveGate.Any())
{
response.Code = 500;
response.Status = false;
response.Message = $"该网关已经绑定了SIM卡{isHaveGate.First().simKey},请选择其它的网关 !";
return response;
}
var isHaveGateSim = Context.Queryable<base_sim_gateway_rel>().Where(x => x.simKey == e_simKey);
if (isHaveGateSim.Any())
{
//修改
sim_Gateway_Rel = isHaveGateSim.First();
sim_Gateway_Rel.gatewayKey = e_gatewayKey;
Context.Updateable(sim_Gateway_Rel).AddQueue();
}
else
{
//添加
Context.Insertable(sim_Gateway_Rel).AddQueue();
simInfo.simRemark += $" {DateTime.Now.ToString("yyyy-MM-dd")}已领用";
}
Context.Updateable(simInfo).AddQueue();
var res = Context.SaveQueues();
if (res < 0) response.Message = SystemVariable.dataActionError;
return response;
});
}
/// <summary>
/// 对返回多条sim信息,
/// </summary>
/// <param name="simList"></param>
/// <returns></returns>
public List<base_sim> HandleSimInfo(List<base_sim> simList)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
if (simList != null)
{
var all = Context.Queryable<base_sim, base_sim_vpn_rel, base_vpn, base_sim_gateway_rel, base_gateway>((s, sv, v, sg, g) =>
new JoinQueryInfos(JoinType.Left, s.simKey == sv.simKey,
JoinType.Left, sv.vpnKey == v.vpnKey,
JoinType.Left, s.simKey == sg.simKey,
JoinType.Left, g.gatewayKey == sg.gatewayKey))
.Select((s, sv, v, sg, g) => new
{
s.simKey,
v.vpnNumber,
g.gatewaySerialNumber,
v.vpnKey,
g.gatewayKey
})
.ToList();
foreach (var sim in simList)
{
var query = all.Where(x => x.simKey == sim.simKey).First();
sim.gatewaySerialNumber = query.gatewaySerialNumber;
sim.vpnNumber = query.vpnNumber;
sim.gatewayKey = query.gatewayKey;
sim.vpnKey = query.vpnKey;
if (query.gatewayKey != Guid.Empty && query.vpnKey != Guid.Empty) sim.isUse = true;
}
}
return simList;
});
}
}
}