BaseSimService.cs
12.6 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
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;
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();
entity.createBy = sysWebUser.Account;
entity.createTime = DateTime.Now;
entity.getCardDate = null;
entity.debugDate = null;
//新增sim卡信息
Context.Insertable(entity).AddQueue();
//新增sim卡与vpn配置关联关系
if (!string.IsNullOrEmpty(entity.accountNumber))
{
var vpnList = entity.accountNumber.Split(',', StringSplitOptions.RemoveEmptyEntries);
foreach (var vpn in vpnList)
{
base_sim_vpn_rel sv = new base_sim_vpn_rel()
{
simNumber = entity.simNumber,
accountNumber = vpn,
};
Context.Insertable(sv).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, base_sim_equipment_rel>((x, y) => x.simNumber == y.simNumber)
.Where((x, y) => x.id == entity.id)
.Select((x, y) => new { x.id, x.simNumber })
.First();
if (historySimInfo != null && entity.simNumber != historySimInfo.simNumber)
{
response.Code = 500;
response.Status = false;
response.Message = $"当前sim卡已经关联了设备,禁止修改卡号【{historySimInfo.simNumber}】,如果要修改请先解绑sim卡关联的设备!";
return response;
}
//判断如果关联了VPN,禁止修改卡号;
var vpnSimRel = Context.Queryable<base_sim, base_sim_vpn_rel>((x, y) => x.simNumber == y.simNumber)
.Where((x, y) => x.id == entity.id)
.Select((x, y) => new { x.id, x.simNumber })
.First();
if (vpnSimRel != null && entity.simNumber != vpnSimRel.simNumber)
{
response.Code = 500;
response.Status = false;
response.Message = $"当前sim卡已经关联了VPN,禁止修改卡号【{vpnSimRel.simNumber}】,如果要修改请先解绑sim卡配置的vpn账号!";
return response;
}
entity.updateBy = sysWebUser.Account;
entity.updateTime = DateTime.Now;
//如果accountNumber为空,删除关联
//删除旧关联
if (vpnSimRel != null)
{
Context.Deleteable<base_sim_vpn_rel>().Where(x => x.simNumber == entity.simNumber).AddQueue();
}
//添加vpn关联
if (!string.IsNullOrEmpty(entity.accountNumber))
{
var vpnList = entity.accountNumber.Split(',', StringSplitOptions.RemoveEmptyEntries);
foreach (var vpn in vpnList)
{
base_sim_vpn_rel sv = new base_sim_vpn_rel()
{
simNumber = entity.simNumber,
accountNumber = vpn,
};
Context.Insertable(sv).AddQueue();
}
}
Context.Updateable(entity).AddQueue();//修改sim卡信息
var res = Context.SaveQueues();
if (res < 0) response.Message = SystemVariable.dataActionError;
return response;
});
}
public dynamic DelByIds(string[] ids)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
//判断如果关联了设备,禁止删除
var simNumber = ids[0];
var isSimInfo = Context.Queryable<base_sim_equipment_rel>().Any(x => x.simNumber == simNumber);
if (isSimInfo)
{
response.Code = 500;
response.Status = false;
response.Message = "当前sim卡已经关联了设备,禁止删除SIM卡,如果要删除请先解绑与sim卡关联的设备!";
return response;
}
//如果关联了VPN,禁止删除
var isVpnRel = Context.Queryable<base_sim_vpn_rel>().Any(x => x.simNumber == simNumber);
if (isVpnRel)
{
response.Code = 500;
response.Status = false;
response.Message = "当前sim卡已经关联了VPN,禁止删除SIM卡,如果要删除请先解绑sim卡关联的VPN账号!";
return response;
}
Context.Deleteable<base_sim>(t => ids.Contains(t.simNumber)).ExecuteCommand();
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.ip))
{
exp.And(x => x.ip.Contains(model.ip));
}
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)
{
foreach (var item in strArray)
{
if (checkeds)
{
var isHave = Context.Queryable<base_sim_equipment_rel>()
.Any(x => x.simNumber == simNumber && x.projectKey == new Guid(projectKeys) && 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;
});
}
public dynamic UseSim(int[] ids,DateTime getCardDate,DateTime debugDate)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
if (ids != null)
{
foreach(var i in ids)
{
var simInfo = Context.Queryable<base_sim>().First(sim => sim.id == i);
simInfo.getCardDate=getCardDate;
simInfo.debugDate=debugDate;
simInfo.updateBy = sysWebUser.Account;
simInfo.updateTime = DateTime.Now;
Context.Updateable<base_sim>(simInfo).AddQueue();
}
var res=Context.SaveQueues();
if (res < 0) response.Message = SystemVariable.dataActionError;
}
return response;
});
}
public List<base_sim> HandleSimInfo(List<base_sim> simList)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
if (simList != null)
{
foreach (var sim in simList)
{
var sim_Vpn_Rels = Context.Queryable<base_sim_vpn_rel>().Where(x => x.simNumber == sim.simNumber).Select(x => x.accountNumber).ToList();
sim.accountNumber = string.Join(",", sim_Vpn_Rels.ToArray());
}
}
return simList;
});
}
}
}