schangxiang@126.com
2025-09-23 ea04aef49691444ed8ac1e7e7b94feabcbb5326f
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
using iWareCommon.Utils;
using iWareLog.ORM;
using iWareLog.Report.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace iWareLog.Report.Service
{
    public class AlertService
    {
        private static object Lock = new object();
 
        private static AlertService Instance = null;
 
        /// <summary>
        /// 获取单例的方法
        /// </summary>
        /// <returns>单例实体</returns>
        public static AlertService GetInstance()
        {
 
            if (Instance == null)
            {
                lock (Lock)
                {
                    if (Instance == null)
                    {
                        Instance = new AlertService();
                    }
                }
            }
            return Instance;
        }
 
 
 
        /// <summary>
        /// 查询时间内故障出现次数,总时间,以设备分组
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public List<AlertHelper> GetAlertByMachine(DateTime start, DateTime end)
        {
            using (DbModelLog context = new DbModelLog())
            {
                try
                {
                    string sql = "SELECT  name,COUNT(equipid) as devalertcount, sum(DATEDIFF(SECOND,createtime,finishtime)) as datediffs FROM DEVAlert  WHERE createtime >= @p0 and createtime < @p1 group by name";
                    List<AlertHelper> data = context.Database.SqlQuery<AlertHelper>(sql, start, end).ToList();
                    return data;
                }
                catch (Exception ex)
                {
                    LogTextHelper.WriteLine("AlertService", "GetAlertByMachine", ex.ToString());
                    return null;
                }
            }
        }
 
        /// <summary>
        /// 查询时间内故障出现次数,总时间,以故障代码分组
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public List<AlertHelper> GetAlertByAlertCode(DateTime start, DateTime end)
        {
            using (DbModelLog context = new DbModelLog())
            {
                try
                {
                    string sql = "SELECT alertcode, COUNT(alertcode) as alertcount, sum(DATEDIFF(SECOND,createtime,finishtime)) as datediffs FROM DEVAlert  WHERE createtime >= @p0 and createtime < @p1 group by alertcode";
                    List<AlertHelper> data = context.Database.SqlQuery<AlertHelper>(sql, start, end).ToList();
                    return data;
                }
                catch (Exception ex)
                {
                    LogTextHelper.WriteLine("AlertService", "GetAlertByAlertCode", ex.ToString());
                    return null;
                }
            }
        }
 
    }
}