BaseEquipmentService.cs
14.2 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
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.Expressions;
using Hh.Mes.Pojo.System;
using Microsoft.AspNetCore.Http;
using System.IO;
using System.Text;
namespace Hh.Mes.Service.Equipment
{
public class BaseEquipmentService : RepositorySqlSugar<base_equipment>
{
public dynamic Load(PageReq pageReq, base_equipment entity)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var result = new Response();
//先组合查询表达式
var currentTime = DateTime.Now;
var expression = LinqWhereEquipment(entity);
string currentUser = sysWebUser.Account;
var query = Context.Queryable<base_project, base_equipment, base_sim_equipment_rel, daq_equipment_status_record>
((p, eq, sim, eqStatus) =>
new JoinQueryInfos(JoinType.Right, p.keys == eq.projectKeys,
JoinType.Left, eq.equipmentCode == sim.equipmentCode,
JoinType.Left, eq.equipmentCode == eqStatus.equipmentCode)
).Where(expression)
.OrderBy((p, eq, sim, eqStatus) => p.projectCode, OrderByType.Asc)
.OrderBy((p, eq, sim, eqStatus) => eq.equipmentTypeCode, OrderByType.Asc)
.OrderBy((p, eq, sim, eqStatus) => eq.equipmentCode, OrderByType.Asc)
.Select((p, eq, sim, eqStatus) => new
{
eq.id,
eq.projectKeys,
p.projectName,
eq.factoryCode,
sim.simNumber,
eq.equipmentCode,
eq.equipmentName,
eq.equipmentTypeCode,
eq.destinationArea,
eq.isMaintain,
eq.ip,
remark = SqlFunc.Subqueryable<daq_equipment_alarm_record>().Where(c => c.equipmentCode == eq.equipmentCode && c.isHandled == false).Any() ? "有未处理报警记录" : null,
eq.equipmentMaintainRuleCode,
eq.isEnable,
eq.startTime,
eq.otherCode,
eq.imgSrc,
eqStatus.status,
eqOnLineUpdateTime = eqStatus.updateTime,
eqOnLineNowTime = currentTime,
eq.createBy,
eq.createTime,
eq.updateBy,
eq.updateTime,
});
//Exel false 分页
if (!entity.Exel)
{
int total = 0;
result.Result = query.ToOffsetPage(pageReq.page, pageReq.limit, ref total);
result.Count = total;
}
else
{
result.Result = query.ToList();
result.Count = result.Result.Count;
}
return result;
}, catchRetrunValue: "list");
}
public dynamic Ins(base_equipment entity)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
entity.createBy = sysWebUser.Account;
entity.createTime = DateTime.Now;
entity.isMaintain = false;
var pro = Context.Queryable<base_project>().First(x => x.keys == entity.projectKeys);
pro.eqCount = GetProjectEqCount(pro.keys) + 1;
Context.Updateable<base_project>(pro)
.UpdateColumns(it => new { it.eqCount })
.Where(x => x.keys == entity.projectKeys)
.AddQueue();
Context.Insertable(entity).AddQueue();
response.Status = Context.SaveQueues() > 0;
if (!response.Status) response.Message = SystemVariable.dataActionError;
return response;
});
}
public dynamic Upd(base_equipment entity)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
entity.updateBy = sysWebUser.Account;
entity.updateTime = DateTime.Now;
var pro = Context.Queryable<base_project>().First(x => x.keys == entity.projectKeys);
pro.eqCount = GetProjectEqCount(pro.keys);
Context.Updateable<base_project>(pro)
.UpdateColumns(it => new { it.eqCount })
.Where(x => x.keys == entity.projectKeys)
.AddQueue();
//同步修改daq_equipment表设备名称
var isHave = Context.Queryable<daq_equipment>().Any(x => x.code == entity.equipmentCode);
if (isHave)
{
Context.Updateable<daq_equipment>()
.SetColumns(x=>x.name==entity.equipmentName)
.SetColumns(x=>x.updated==DateTime.Now)
.SetColumns(x=>x.updatedBy==entity.updateBy)
.Where(x => x.code == entity.equipmentCode)
.AddQueue();
}
Context.Updateable(entity).AddQueue();
response.Status = Context.SaveQueues()>0;
if (!response.Status) response.Message = SystemVariable.dataActionError;
return response;
});
}
/// <summary>
/// 设备删除
/// </summary>
public dynamic DelByIds(int[] ids)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
var id = ids[0];
var projectKeys = Context.Queryable<base_equipment>().First(x => x.id == id).projectKeys;
var pro = Context.Queryable<base_project>().First(x => x.keys == projectKeys);
pro.eqCount = GetProjectEqCount(pro.keys) - 1;
Context.Updateable<base_project>(pro)
.UpdateColumns(it => new { it.eqCount })
.Where(x => x.id == pro.id)
.AddQueue();
Context.Deleteable<base_equipment>(t => t.id == id).AddQueue();
response.Status = Context.SaveQueues() > 0;
return response;
});
}
public Response ExportData(base_equipment entity)
{
return Load(null, entity);
}
private Expression<Func<sys_user_project_rel, bool>> GetExpression(string currentUser)
{
var exp = Expressionable.Create<sys_user_project_rel>();
if (currentUser != SystemVariable.DefaultCreated)
{
//非管理员,查询登陆用户绑定客户管理的项目
exp.And(x => x.userAccount == currentUser);
}
return exp.ToExpression();
}
public Expression<Func<base_project, base_equipment, base_sim_equipment_rel, daq_equipment_status_record, bool>> LinqWhereEquipment(base_equipment model)
{
try
{
var exp = Expressionable.Create<base_project, base_equipment, base_sim_equipment_rel, daq_equipment_status_record>();
if (!string.IsNullOrWhiteSpace(model.equipmentCode))
{
exp.And((p, eq, sim, eqStatus) => eq.equipmentCode.Contains(model.equipmentCode));
}
if (!string.IsNullOrWhiteSpace(model.equipmentName))
{
exp.And((p, eq, sim, eqStatus) => eq.equipmentName.Contains(model.equipmentName));
}
if (model.projectKeys != Guid.Empty)
{
exp.And((p, eq, sim, eqStatus) => eq.projectKeys == model.projectKeys);
}
else
{
string user = sysWebUser.Account;
if (user != SystemVariable.DefaultCreated)
{
var projectRoleKeys = GetProjectRoleKeys(user);
exp.And((p, eq, sim, eqStatus) => SqlFunc.Subqueryable<sys_role_projects_rel>().Where(c => projectRoleKeys.Contains(c.project_roles_key) && c.project_key == p.keys).Any());
}
}
exp.And((p, eq, sim, eqStatus) => !SystemVariable.IotNotContainDevice.Contains(eq.equipmentTypeCode));
return exp.ToExpression();//拼接表达式
}
catch (Exception ex)
{
throw new Exception($"{ex.Message}");
}
}
/// <summary>
/// 获取项目下面所有的设备
/// </summary>
/// <returns></returns>
public int GetProjectEqCount(Guid projectKeys)
{
var count = Context.Queryable<base_equipment>().Count(x => x.projectKeys == projectKeys
&& !SystemVariable.IotNotContainDevice.Contains(x.equipmentTypeCode));
return count;
}
/// <summary>
/// 设备表存在报警记录更新、不存在不处理
/// </summary>
public void UpdateRemarkWithAlarmStatus()
{
ExceptionsHelp.Instance.ExecuteVoidFunc(() =>
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine($@" UPDATE base_equipment
SET remark = CASE
WHEN EXISTS (
SELECT 1
FROM daq_equipment_alarm_record with (nolock)
WHERE daq_equipment_alarm_record.EquipmentCode = base_equipment.EquipmentCode and IsHandled=0
) THEN '有未处理报警记录'
ELSE ''
END;");
base.Context.Ado.ExecuteCommand(stringBuilder.ToString());
});
}
#region 设备上传更新图片
public dynamic UpdateEquipmentImage(int equipmentId, IFormFile file)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
// 检查文件上传
if (file == null) return response.ResponseError("请选择需要上传的文件");
// 检查文件类型
var allowedFileTypes = new List<string> { "image/jpeg", "image/gif", "image/png" };
if (!allowedFileTypes.Contains(file.ContentType))
{
return response.ResponseError("不支持此文件格式!图片文件仅支持[jpeg、png、gif]文件");
}
// 创建目标文件夹路径
var uploadEquipmentPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images", "equipment");
if (!Directory.Exists(uploadEquipmentPath))
{
Directory.CreateDirectory(uploadEquipmentPath);
}
// 生成新文件名
var suffix = Path.GetExtension(file.FileName);
var fileName = Path.GetFileNameWithoutExtension(file.FileName);
var newFileName = $"{fileName}_{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}{suffix}";
// 构建完整文件路径
var filePath = Path.Combine(uploadEquipmentPath, newFileName);
// 保存文件
using (var fs = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(fs);
fs.Flush();
}
var equipment = new base_equipment()
{
imgSrc = newFileName,
updateTime = DateTime.Now,
updateBy = sysWebUser.Account
};
var result = Context.Updateable(equipment)
.UpdateColumns(it => new { it.imgSrc, it.updateBy, it.updateTime })
.Where(x => x.id == equipmentId)
.ExecuteCommand();
if (result < 0) response.Message = SystemVariable.dataActionError;
return response;
});
}
#endregion
}
}