chenhaozhe
2026-04-16 b56d9c96bc858556f51cf3ca94501ebc7ebc1f52
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using Newtonsoft.Json.Linq;
using Pub_Class;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Data.SqlClient;
using System.Web.Http;
using WebAPI.Models;
using Newtonsoft.Json;
 
namespace WebAPI.Controllers.生产管理
{
    //设备状态分布Controller
    public class Sc_HEquipStateDistributionController : ApiController
    {
        public DBUtility.ClsPub.Enum_BillStatus BillStatus;
 
        private json objJsonResult = new json();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        DataSet ds;
        //获取系统参数
        Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
 
        
 
        #region 设备分布 设备绑定的生产资源上正在生产的工单 
 
        [Route("Sc_HEquipStateDistribution/Sc_HEquipStateDistribution_Souce")]
        [HttpGet]
        public object Sc_HEquipStateDistribution_Souce(string HDeptID, string user, int OrganizationID)
        {
            try
            {
                string sql1 = string.Format("exec h_p_sc_HDeptSouceMOList {0},{1}", HDeptID, OrganizationID);
 
                ds = oCN.RunProcReturn(sql1, "h_p_sc_HDeptSouceMOList");
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 设备分布 设备绑定的生产资源上正在生产的工单 合并 稼动率和OEE
 
        [Route("Sc_HEquipStateDistribution/Sc_HEquipStateDistribution_Souce_MergeOEEReport")]
        [HttpGet]
        public object Sc_HEquipStateDistribution_Souce_MergeOEEReport(string HDeptID, string user, int OrganizationID)
        {
            try
            {
                string sql1 = string.Format("exec h_p_sc_HDeptSouceMOList_MergeOEEReport {0},{1}", HDeptID, OrganizationID);
 
                ds = oCN.RunProcReturn(sql1, "h_p_sc_HDeptSouceMOList_MergeOEEReport");
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
 
        #region 设备状态分步下推设备点检参数表
        [Route("Sc_HEquipStateDistribution/Get_EquipICMOTechParamList_Json")]
        [HttpGet]
        public object Get_EquipICMOTechParamList_Json(string sWhere, string user)
        {
            DataSet ds;
            try
            {
                List<object> columnNameList = new List<object>();
 
                Dictionary<object, object> dic = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<object, object>>(sWhere);
 
                string HEquipID = dic["HEquipID"].ToString();
                string HICMOInterID = dic["HICMOInterID"].ToString();
                string HICMOEntryID = dic["HICMOEntryID"].ToString();
 
                ds = oCN.RunProcReturn("exec h_p_Sc_HEquipStateDistribution_TechParam '" + HEquipID + "','" + HICMOInterID + "','"+ HICMOEntryID + "'", "h_p_Sc_HEquipStateDistribution_TechParam");
 
                //添加列名
                foreach (DataColumn col in ds.Tables[0].Columns)
                {
                    Type dataType = col.DataType;
                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
                    columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
                }
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                objJsonResult.list = columnNameList;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "没有返回任何记录!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 设备状态分步 产线+部门 查询 下方四个图表数据
        [Route("Sc_HEquipStateDistribution/Get_EquipStateDistribution_FourReport")]
        [HttpGet]
        public object Get_EquipStateDistribution_FourReport(string sWhere, string user)
        {
            DataSet ds;
            try
            {
                List<object> columnNameList = new List<object>();
 
                Dictionary<object, object> dic = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<object, object>>(sWhere);
 
                string HDeptID = dic["HDeptID"].ToString();
                string HSourceID = dic["HSourceID"].ToString();
                string HEquipID = dic["HEquipID"].ToString();
 
 
                if (string.IsNullOrWhiteSpace(HEquipID))
                {
                    ds = oCN.RunProcReturn("exec h_p_Sc_FourStateDistributionReport '" + HDeptID + "','" + HSourceID + "'", "h_p_Sc_FourStateDistributionReport");
                }else
                {
                    ds = oCN.RunProcReturn("exec h_p_Sc_FourStateDistributionReport '" + HDeptID + "','" + HSourceID + "'" + $",'{HEquipID}'", "h_p_Sc_FourStateDistributionReport");
                }
 
                //添加列名
                foreach (DataColumn col in ds.Tables[0].Columns)
                {
                    Type dataType = col.DataType;
                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
                    columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
                }
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds;
                objJsonResult.list = columnNameList;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "没有返回任何记录!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 设备运行切片,获取部门-设备 树状图
        private class HEquipTreeList_DAQ_Node
        {
            public int id { get; set; }
            public string title { get; set; }
            public List<HEquipTreeList_DAQ_Node> children { get; set; }
        }
 
 
        [Route("DAQ_EquipRunningSlice/GetHEquipTreeList_DAQ")]
        [HttpGet]
        public object Sc_HEquipStateDistribution_Souce(string sWhere)
        {
            try
            {
                List<HEquipTreeList_DAQ_Node> treeArr = new List<HEquipTreeList_DAQ_Node>();
                // 记录部门是否已经加入treeArr(不可重复加入)
                HashSet<String> HDeptSet = new HashSet<String>();
 
                ds = oCN.RunProcReturn($@"select hmainid HEquipID, 设备名称 HEquipName, HDeptID HDeptID, 使用部门 HDeptName from h_v_Gy_EquipFileMainList
                {sWhere}    
                order by HEquipName asc", "h_v_Gy_EquipFileMainList");
 
                foreach(DataRow row in ds.Tables[0].Rows)
                {
                    int HDeptID = int.Parse(row["HDeptID"].ToString());
                    string HDeptName = row["HDeptName"] as string;
                    int HEquipID = int.Parse(row["HEquipID"].ToString());
                    string HEquipName = row["HEquipName"] as string;
 
                    if (!HDeptSet.Contains(row["HDeptID"].ToString()))
                    {
                        // 没有部门对应的一级节点,则添加一级节点
                        treeArr.Add(new HEquipTreeList_DAQ_Node
                        {
                            id = int.Parse(row["HDeptID"].ToString()),
                            title = row["HDeptName"].ToString(),
                            children = new List<HEquipTreeList_DAQ_Node>() 
                        });
 
                        // 部门ID添加到Set,确保唯一性
                        HDeptSet.Add(row["HDeptID"].ToString());
                    }
 
                    // 部门对印的一级节点已经设置好,设置二级节点
                    HEquipTreeList_DAQ_Node Node = treeArr.First(item => item.id == HDeptID);
                    Node.children.Add(new HEquipTreeList_DAQ_Node
                    {
                        id = HEquipID,
                        title = HEquipName,
                        children = new List<HEquipTreeList_DAQ_Node>()
                    });
                }
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "查询成功";
                objJsonResult.data = JArray.FromObject(treeArr);
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.Message.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 设备运行切片 返回设备状态 图表数据 (饼图)
        [Route("DAQ_EquipRunningSlice/GetHEquipStatusReportList_DAQ")]
        [HttpGet]
        public object GetHEquipStatusReportList_DAQ(string HEquipID, string HDeptID, string HBeginDate, string HEndDate, string user)
        {
            try
            {
                List<object> columnNameList = new List<object>();
 
                string sql = "select * from h_v_Sb_EquipRunningStatusReport where 1 = 1";
                if(!string.IsNullOrWhiteSpace(HEquipID) && HEquipID != "0")
                {
                    sql += $" and HEquipID = {HEquipID}";
                }
                if(!string.IsNullOrWhiteSpace(HDeptID) && HDeptID != "0")
                {
                    sql += $" and HDeptID = {HDeptID}";
                }
                if(!string.IsNullOrWhiteSpace(HBeginDate) && !string.IsNullOrWhiteSpace(HEndDate))
                {
                    sql += $"and (所属日期 between CONVERT(DATETIME, '{HBeginDate} 00:00:00', 120) AND CONVERT(DATETIME, '{HEndDate} 23:59:59', 120))";
                }
 
                ds = oCN.RunProcReturn(sql, "h_v_Sb_EquipRunningStatusReport");
 
                //添加列名
                foreach (DataColumn col in ds.Tables[0].Columns)
                {
                    Type dataType = col.DataType;
                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
                    columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
                }
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0]; 
                objJsonResult.list = columnNameList;
                return objJsonResult;
            }
            catch (Exception ex)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "查询数据异常,请与管理员联系!" + ex.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 设备运行切片 返回设备状态 图表数据 (时序图)
        [Route("DAQ_EquipRunningSlice/GetHEquipStatusReportList2_DAQ")]
        [HttpGet]
        public object GetHEquipStatusReportList2_DAQ(string HEquipID, string HDeptID, string HBeginDate, string HEndDate, string user)
        {
            try
            {
                List<object> columnNameList = new List<object>();
                
                ds = oCN.RunProcReturn($@"exec h_p_DAQ_EquipTimeLineReport {HEquipID}, {HDeptID}, N'{HBeginDate}', N'{HEndDate}'", "h_p_DAQ_EquipTimeLineReport");
 
 
                // 初始化结果 JObject
                JObject result = new JObject();
                // 按时间对表查询出来的数据进行分组
                DataTable table = ds.Tables[0];
                var groupedData = table.Rows.Cast<DataRow>()
                .GroupBy(row =>
                {
                    // 取出时间字段并转为日期(自动忽略时分秒)
                    DateTime time = Convert.ToDateTime(row["时间"]);
                    return time.ToString("yyyy-MM-dd"); // 分组 Key
                });
 
                foreach (var group in groupedData)
                {
                    string dateKey = group.Key;
                    JArray dataArray = new JArray();
 
                    foreach (DataRow row in group)
                    {
                        JObject rowObj = new JObject();
 
                        // 把这一行的所有列都转成 ECharts可以读取的data对象
                        foreach (DataColumn col in table.Columns)
                        {
 
                            rowObj[col.ColumnName] = row[col] != DBNull.Value
                                ? JToken.FromObject(row[col])
                                : null;
                        }
 
                        dataArray.Add(rowObj);
                    }
 
                    // 加入最终结果
                    result[dateKey] = dataArray;
                }
 
                //添加列名
                foreach (DataColumn col in ds.Tables[0].Columns)
                {
                    Type dataType = col.DataType;
                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
                    columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
                }
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = result;
                objJsonResult.list = columnNameList;
                return objJsonResult;
            }
            catch (Exception ex)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "查询数据异常,请与管理员联系!" + ex.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 设备驾驶舱 根据部门查询对应的设备
 
        [Route("Sc_HEquipStateDistribution/Get_HEquipStateList")]
        [HttpGet]
        public object Get_HEquipStateList(string HDeptID, string user, int OrganizationID)
        {
            try
            {
                string sql1 = string.Format("exec h_p_sc_HDeptEquipList {0},{1}", HDeptID, OrganizationID);
 
                ds = oCN.RunProcReturn(sql1, "h_p_sc_HDeptEquipList");
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
    }
}