Blame view

sys/Hh.Mes.Service/WebService/Equipment/EquipmentVisualService.cs 14.1 KB
1
2
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
-  
唐召明 authored
3
using Hh.Mes.POJO.EnumEntitys;
4
using Hh.Mes.POJO.Response;
-  
唐召明 authored
5
using Hh.Mes.POJO.ViewModel.Equipment;
6
7
8
9
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
唐召明 authored
10
using System.Data;
-  
唐召明 authored
11
using System.Linq;
12
13
14
15
16
17
18
using System.Text;

namespace Hh.Mes.Service.WebService.Equipment
{
    /// <summary>
    /// 设备可视化参数
    /// </summary>
赖素文 authored
19
    public class EquipmentVisualService : RepositorySqlSugar<base_equipment>
20
21
    {
        /// <summary>
22
        /// 设备健康表现、 操作手册、未处理设备报警记录
23
        /// </summary>
赖素文 authored
24
        public Response GetEquipmentFileList(base_equipment model)
25
26
        {
            var result = new Response();
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

            //开机时间以及当天各状态时间及占比(通用方法,考虑报表接口共用)
            var res = GetEquipmentHealth(model, DateTime.Today, true);
            var alarmRecords = res.alarmRecords;
            var alarmTotal = res.vm.AlarmTotal;

            result.Result = new
            {
                equipmentHealth = res.vm,
                fileList = res.dt.Tables[0],
                eqInfo = res.dt.Tables[1],
                alarmList = alarmRecords,
                alarmTotal,
            };
            result.Count = 1;
            return result;
        }

        public (EquipmentHealthVM vm, List<daq_equipment_alarm_record> alarmRecords, dynamic dt) GetEquipmentHealth(base_equipment model, DateTime day, bool isSelectAlarmList = false)
        {
47
48
            var stringBuilder = new StringBuilder();
赖素文 authored
49
50
51
52
53
            stringBuilder.AppendLine($@" select * from sys_File
                                         where targetId=@targetId and targetTableName='base_equipment'
                                         union all
                                         select * from sys_File
                                         where targetId=@targetId2 and targetTableName='base_equipment_type';
54
55
56
                                        SELECT  t.EquipmentCode,
                                                ISNULL(DATEDIFF(second, t1.UpdateTime, GETDATE()), -1) AS seconds,
57
                                                t1.Status,                                               
58
59
60
61
62
63
64
65
66
67
68
	                                            t.ip,

	                                            simNumber=   (
                                                    SELECT STUFF((
                                                        SELECT ',' + CAST(rel.simNumber AS VARCHAR(4000))
                                                        FROM base_sim_equipment_rel rel
                                                        WHERE rel.EquipmentCode = t.EquipmentCode
                                                        FOR XML PATH('')
                                                    ), 1, 1, '')
                                                )  
                                         FROM base_equipment t
69
                                         LEFT JOIN daq_equipment_status_record t1 WITH(NOLOCK) ON t1.EquipmentCode = t.otherCode
70
                                         WHERE t.EquipmentCode=@EquipmentCode");
-  
唐召明 authored
71
72

            var dt = Context.Ado.GetDataSetAll(stringBuilder.ToString(), new List<SugarParameter>(){
赖素文 authored
73
74
75
                      new SugarParameter("@targetId", model.equipmentCode),
                      new SugarParameter("@targetId2", model.equipmentTypeCode),
                      new SugarParameter("@EquipmentCode", model.equipmentCode),
76
                      new SugarParameter("@OtherEquipmentCode", model.otherCode),
赖素文 authored
77
            });
唐召明 authored
78
            //今天的记录
79
            var startTime = day;
80
            var records = Context.Queryable<daq_equipment_status_record_history>().Where(x => x.equipmentCode == model.otherCode && x.createTime >= startTime).Select(x => new
-  
唐召明 authored
81
82
            {
                x.id,
83
84
                x.statusDuration,
                x.status
-  
唐召明 authored
85
86
            }).ToList();
87
            var currentMonthStartTime = startTime.AddDays(1 - startTime.Day);
88
            //设备饼图当天 空闲时间
89
            var freeTimeSeconds = records.Where(x => x.status == EquipmentStatus.Free.ToString()).Sum(x => x.statusDuration);
90
            //设备饼图当天 运行时间
91
            var runTimeSeconds = records.Where(x => x.status == EquipmentStatus.Running.ToString()).Sum(x => x.statusDuration);
HuXiYu authored
92
93
94
95


            //设备饼图当天 总故障时间
            var alarmTimeSeconds = Context.Queryable<daq_equipment_alarm_record>()
96
                                          .Where(x => x.equipmentCode == model.otherCode && x.createTime >= startTime)
HuXiYu authored
97
                                          .Select(x => new
98
99
100
101
                                          {
                                              x.id,
                                              x.errorduration
                                          })
HuXiYu authored
102
                                          .ToList()
103
                                          .Sum(x => x.errorduration);
HuXiYu authored
104
105
            //设备饼图当天 总时间
106
107
108
            var dayTime = DateTime.Now;
            if (!startTime.Date.Equals(dayTime.Date)) dayTime = DateTime.Parse(startTime.ToString("yyyy-MM-dd") + " 23:59:59");
            var currentTotalSecondss = (dayTime - startTime).TotalSeconds;
-  
唐召明 authored
109
110
            //设备饼图当天 离线时间
111
            var offlineTimeTicks = currentTotalSecondss - (alarmTimeSeconds + freeTimeSeconds + runTimeSeconds);
-  
唐召明 authored
112
113
114
115
116
117
118
119
            if (offlineTimeTicks < 0)
            {
                offlineTimeTicks = 0;
            }
            var equipmentHealth = new EquipmentHealthVM
            {
                EquipmentStatus = new EquipmentStatusDto
                {
120
                    TodayPowerTime = TimeSpan.FromSeconds(records.Sum(x => x.statusDuration)).TotalSeconds,
HuXiYu authored
121
                    CurrentMonthPowerTime = TimeSpan.FromSeconds(Context.Queryable<daq_equipment_status_record_history>()
122
                                                                        .Where(x => x.equipmentCode == model.otherCode && x.createTime >= currentMonthStartTime)
123
                                                                        .Sum(x => x.statusDuration)).TotalSeconds,
124
125
126
127
128
                    RunTime = runTimeSeconds,
                    FreeTime = freeTimeSeconds,
                    AlarmTime = alarmTimeSeconds,
                    OfflineTime = offlineTimeTicks,
129
130
131
132
133
134

                    RunProportion = Math.Round(runTimeSeconds * 100d / currentTotalSecondss, 1),
                    FreeProportion = Math.Round(freeTimeSeconds * 100d / currentTotalSecondss, 1),
                    AlarmProportion = Math.Round(alarmTimeSeconds * 100d / currentTotalSecondss, 1),
                    OfflineProportion = Math.Round(offlineTimeTicks * 100d / currentTotalSecondss, 1),
-  
唐召明 authored
135
136
                }
            };
137
138
            //当前设备未处理的报警总数
139
            var alarmQuery = Context.Queryable<daq_equipment_alarm_record>().Where(x => x.equipmentCode == model.otherCode && !x.isHandled);
140
            var alarmTotal = alarmQuery.Count();
141
142
            var alarmRecords = new List<daq_equipment_alarm_record>();
            if (isSelectAlarmList)//设备可视化展示的数据
-  
唐召明 authored
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
                alarmRecords = alarmQuery.OrderBy(x => x.createTime, OrderByType.Desc).Take(200).ToList();
            }

            //保养提醒
            var maintainCount = Context.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.equipmentCode == model.otherCode && x.status != 20).Count();

            equipmentHealth.AlarmTotal = alarmTotal;
            equipmentHealth.MaintainTotal = maintainCount;
            equipmentHealth.EquipmentCode = model.equipmentCode;
            equipmentHealth.EquipmentName = model.equipmentName;
            equipmentHealth.ProjectName = Context.Queryable<base_project>().Where(x => x.keys == model.projectKeys).Select(x => x.projectName).First();
            equipmentHealth.IP = model.ip;

            //设备状态赋值
            var statusText = "在线";
            var status = dt.Tables[1].Rows[0]["status"].ToString();
            int seconds = (int)dt.Tables[1].Rows[0]["seconds"];
            if (status == "Failure" || status == "Error")
            {
                statusText = "故障";
            }
            if (seconds > 300 || seconds == -1)
            {
                statusText = "离线";
            }
            else
            {
                statusText = "空闲";
            }
            equipmentHealth.Status = statusText;
            return (equipmentHealth, alarmRecords, dt);
175
        }
-  
唐召明 authored
176
177
178
179
        /// <summary>
        /// 设备保养信息tab 概述总览数据、部件列表
        /// </summary>
唐召明 authored
180
181
182
183
184
        public Response GetEquipmentMaintainList(base_equipment model)
        {
            var result = new Response();
            var stringBuilder = new StringBuilder();
            //设备保养概述总表关联部件基础信息表
185
186
187
188
189
190
            stringBuilder.AppendLine(@" SELECT  t.EquipmentCode,
                                                t.LastMaintainTime,
                                                t.SumAmount,
                                                t1.Status,
                                                ISNULL(DATEDIFF(second, t1.UpdateTime, GETDATE()), -1) AS seconds,
                                                t.MaintainNumber,
191
                                                t.ToBeMaintainedNumber,                                               
192
193
	                                            t3.ip,
                                                t3.imgSrc
194
195
196
                                            FROM
                                            bus_equipment_maintain_record_top t
                                            LEFT JOIN daq_equipment_status_record t1 WITH(NOLOCK) ON t1.EquipmentCode = t.EquipmentCode
197
198
199
                                            LEFT JOIN base_equipment t3 WITH(NOLOCK) ON t3.equipmentCode = t.EquipmentCode
                                        WHERE t.EquipmentCode=@equipmentCode;
200
                                        select  p.*
201
202
203
                                                from bus_equipment_maintain_record_head p 
                                        where p.EquipmentCode=@equipmentCode ");
            var dt = base.Context.Ado.GetDataSetAll(stringBuilder.ToString(), new List<SugarParameter>(){
唐召明 authored
204
205
                      new SugarParameter("@equipmentCode", model.equipmentCode),
            });
206
207
208
209
210
211
            if (dt == null || dt.Tables[0].Rows.Count == 0) return result.ResponseError(SystemVariable.queryNotData);
            result.Result = new
            {
                main = dt.Tables[0],
                eqmaintainHeadList = dt.Tables[1]
            };
唐召明 authored
212
213
214
215
            return result;
        }

        /// <summary>
216
        ///  设备保养信息tab 已保养设备部件 未保养设备部件
唐召明 authored
217
218
219
220
221
222
223
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Response GetEquipmentMaintainRecordList(string eqCode, string partCode)
        {
            var result = new Response();
            var stringBuilder = new StringBuilder();
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
            stringBuilder.AppendLine(@"SELECT h.EquipmentCode,
                                              h.PartCode,
                                              h.PartName,
                                              h.LastMaintainTime,
                                              h.NextMaintainTime,
                                              d.MaintainTime,
                                              d.Maintainer,
                                              d.MaintainContent,
                                              d.MaintainPrice,
                                              d.PushStatus,
                                              d.Remark,

                                              url=STUFF(( SELECT ',' + f.url  FROM sys_File f
                                                           WHERE targetTableName='bus_equipment_maintain_record_detail' and f.targetId = d.id  
                                                          FOR XML PATH('')   
		                                              ), 1, 1, '') 
                                        FROM dbo.bus_equipment_maintain_record_detail d LEFT
                                        JOIN dbo.bus_equipment_maintain_record_head h
                                        ON h.HeadKeys=d.HeadKeys
                                        WHERE h.EquipmentCode=@equipmentCode  AND d.Status=@status");
唐召明 authored
244
245
246
247
248
249
250
251
252
253
            if (!string.IsNullOrEmpty(partCode))
            {
                //非空 附加部件条件
                stringBuilder.AppendLine(" AND h.PartCode=@partCode");
            }

            //已保养
            var dt = base.Context.Ado.GetDataTable(stringBuilder.ToString(), new List<SugarParameter>(){
                      new SugarParameter("@equipmentCode", eqCode),
                      new SugarParameter("@partCode", partCode),
254
                      new SugarParameter("@status", (int)EnumMaintainStatus.Maintained),
唐召明 authored
255
256
257
258
259
260
            });

            //待保养
            var dt_tobe = base.Context.Ado.GetDataTable(stringBuilder.ToString(), new List<SugarParameter>(){
                      new SugarParameter("@equipmentCode", eqCode),
                      new SugarParameter("@partCode", partCode),
261
                      new SugarParameter("@status", (int)EnumMaintainStatus.ToBeMaintain),
唐召明 authored
262
263
264
265
266
267
268
269
270
            });
            result.Result = new
            {
                maintainList = dt,
                tobemaintainList = dt_tobe
            };
            return result;
        }
-  
唐召明 authored
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287

        /// <summary>
        /// 
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="comparisonValue">比较值</param>
        /// <returns></returns>
        private long ConvertToTicks(DateTime startTime, DateTime endTime, DateTime comparisonValue)
        {
            var tmep = startTime;
            if (startTime < comparisonValue)
            {
                tmep = comparisonValue;
            }
            return (endTime - tmep).Ticks;
        }
288
289
    }
}