lixiaojun
2022-08-12 424d2d20a24b76850b73fae8ecc9bc98505be4f5
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IStation.RedisCache;
 
namespace IStation.Service
{
    /// <summary>
    /// 年记录
    /// </summary>
    public partial class MonitorYearRecord
    {
        #region 最近一条记录
 
        /// <summary>
        /// 获取最近一条记录
        /// </summary>
        public Model.MonitorYearRecordPure GetLastRecord(long CorpID, long MonitorPointID, long SignalID)
        {
            var redisHelper = new MonitorRecordCacheHelper();
            var record = redisHelper.GetLastYearRecord(CorpID, MonitorPointID, SignalID);
            return record;
        }
 
        /// <summary>
        /// 获取最近一条记录
        /// </summary>
        public List<Model.MonitorYearRecordPure> GetLastRecord(long CorpID, long MonitorPointID, IEnumerable<long> SignalIds)
        {
            if (SignalIds == null || SignalIds.Count() < 1)
                return default;
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastYearRecord(CorpID, MonitorPointID, SignalIds);
            return records;
        }
 
        /// <summary>
        /// 获取最近一条记录
        /// </summary>
        public List<Model.MonitorYearRecordPure> GetLastRecord(long CorpID, Dictionary<long, long> dict)
        {
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastYearRecord(CorpID, dict);
            return records;
        }
 
        /// <summary>
        /// 获取最近一条记录
        /// </summary>
        public List<Model.MonitorYearRecordPure> GetLastRecord(long CorpID, long MonitorPointID)
        {
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastYearRecord(CorpID, MonitorPointID);
            return records;
        }
 
        /// <summary>
        /// 获取最近一条记录
        /// </summary>
        public List<Model.MonitorYearRecordPure> GetLastRecord(long CorpID, IEnumerable<long> MonitorPointIds)
        {
            if (MonitorPointIds == null || MonitorPointIds.Count() < 1)
                return default;
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastYearRecord(CorpID, MonitorPointIds);
            return records;
        }
 
        /// <summary>
        /// 获取最近一条正常记录
        /// </summary>
        public Model.MonitorYearRecordPure GetLastNormalRecord(long CorpID, long MonitorPointID, long SignalID)
        {
            var redisHelper = new MonitorRecordCacheHelper();
            var record = redisHelper.GetLastNormalYearRecord(CorpID, MonitorPointID, SignalID);
            return record;
        }
 
        /// <summary>
        /// 获取最近一条正常记录
        /// </summary>
        public List<Model.MonitorYearRecordPure> GetLastNormalRecord(long CorpID, Dictionary<long, long> dict)
        {
            if (dict == null || dict.Count < 1)
                return default;
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastNormalYearRecord(CorpID, dict);
            return records;
        }
 
        /// <summary>
        /// 获取最近一条正常记录
        /// </summary>
        public List<Model.MonitorYearRecordPure> GetLastNormalRecord(long CorpID, long MonitorPointID)
        {
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastNormalYearRecord(CorpID, MonitorPointID);
            return records;
        }
 
        /// <summary>
        /// 获取最近一条正常记录
        /// </summary>
        public List<Model.MonitorYearRecordPure> GetLastNormalRecord(long CorpID, IEnumerable<long> MonitorPointIds)
        {
            if (MonitorPointIds == null || MonitorPointIds.Count() < 1)
                return default;
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastNormalYearRecord(CorpID, MonitorPointIds);
            return records;
        }
 
        #endregion
 
        #region 历史记录
 
        /// <summary>
        /// 通过 MonitorPointID 获取
        /// </summary>
        public List<Model.MonitorYearRecord> GetByMonitorPointID(long CorpID, long MonitorPointID)
        {
            var dal = new DAL.MonitorYearRecord();
            var entity_list = dal.GetByMonitorPointID(CorpID, MonitorPointID);
            var model_list = Entity2Models(entity_list);
            return model_list?.OrderBy(x=>x.DataYear).ThenBy(x=>x.DataTime).ToList();
        }
 
        /// <summary>
        /// 通过 MonitorPointID 获取某年的数据
        /// </summary>
        public List<Model.MonitorYearRecord> GetByMonitorPointIDOfYear(long CorpID, long MonitorPointID, int Year)
        {
            var dal = new DAL.MonitorYearRecord();
            var entity_list = dal.GetByMonitorPointIDOfYear(CorpID, MonitorPointID, Year);
            var model_list = Entity2Models(entity_list);
            return model_list?.OrderBy(x => x.DataYear).ThenBy(x => x.DataTime).ToList();
        }
 
        /// <summary>
        /// 通过 MonitorPointID 获取年区间内的数据
        /// </summary>
        public List<Model.MonitorYearRecord> GetByMonitorPointIDOfYearRange(long CorpID, long MonitorPointID, int StartYear, int EndYear)
        {
            var dal = new DAL.MonitorYearRecord();
            var entity_list = dal.GetByMonitorPointIDOfYearRange(CorpID, MonitorPointID, StartYear, EndYear);
            var model_list = Entity2Models(entity_list);
            return model_list?.OrderBy(x => x.DataYear).ThenBy(x => x.DataTime).ToList();
        }
 
        /// <summary>
        /// 通过 SignalID 获取某年的数据
        /// </summary>
        public Model.MonitorYearRecord GetBySignalIDOfYear(long CorpID, long MonitorPointID, long SignalID, int Year)
        {
            var dal = new DAL.MonitorYearRecord();
            var entity = dal.GetBySignalIDOfYear(CorpID, MonitorPointID,SignalID, Year);
            var model = Entity2Model(entity);
            return model;
        }
 
        #endregion
 
        #region Insert
 
        /// <summary>
        /// 插入一条
        /// </summary>
        public bool Insert(Model.MonitorYearRecordPure model)
        {
            if (model == null)
                return default;
            var dal = new DAL.MonitorYearRecord();
            var entity = Model2Entity(new Model.MonitorYearRecord(model));
            var id = dal.Insert(entity);
            return id > 0;
        }
 
        /// <summary>
        /// 插入多条
        /// </summary>
        public bool Inserts(List<Model.MonitorYearRecordPure> list)
        {
            if (list == null || list.Count() < 1)
                return default;
            var dal = new DAL.MonitorYearRecord();
            var entity_list = Model2Entities(list.Select(x => new Model.MonitorYearRecord(x)).ToList()).ToList();
            var bol = dal.Inserts(entity_list);
            return bol;
        }
 
        /// <summary>
        /// 插入最近一条记录
        /// </summary>
        public bool InsertLastRecord(Model.MonitorYearRecordPure model)
        {
            if (model == null)
                return default;
            var dal = new DAL.MonitorYearRecord();
            var entity = Model2Entity(new Model.MonitorYearRecord(model));
            var bol = dal.Insert(entity)>0;
            if (bol)
            {
                var redisHelper = new MonitorRecordCacheHelper();
                redisHelper.SetLastYearRecord(model);
            }
            return bol;
        }
 
        /// <summary>
        /// 插入最近多条记录
        /// </summary>
        public bool InsertsLastRecord(List<Model.MonitorYearRecordPure> list)
        {
            if (list == null || list.Count() < 1)
                return default;
            var dal = new DAL.MonitorYearRecord();
            var entity_list = Model2Entities(list.Select(x => new Model.MonitorYearRecord(x)).ToList());
            var bol = dal.Inserts(entity_list);
            if (bol)
            {
                var redisHelper = new MonitorRecordCacheHelper();
                redisHelper.SetLastYearRecord(list);
            }
            return bol;
        }
 
        /// <summary>
        /// 补录一条
        /// </summary>
        public bool InsertSupplement(Model.MonitorYearRecordPure model)
        {
            if (model == null)
                return default;
            var dal = new DAL.MonitorYearRecord();
            var entity = Model2Entity(new Model.MonitorYearRecord(model));
            var id = dal.Insert(entity);
            return id>0;
        }
 
        /// <summary>
        /// 补录多条
        /// </summary>
        public bool InsertsSupplement(List<Model.MonitorYearRecordPure> list)
        {
            if (list == null || list.Count() < 1)
                return default;
            var dal = new DAL.MonitorYearRecord();
            var entity_list = Model2Entities(list.Select(x=>new Model.MonitorYearRecord(x)).ToList()).ToList();
            var bol = dal.Inserts(entity_list);
            return bol;
        }
 
        /// <summary>
        /// 重录一条
        /// </summary>
        public bool InsertAgain(Model.MonitorYearRecordPure model)
        {
            if (model == null)
                return false;
            var dal = new DAL.MonitorYearRecord();
            var result = dal.DeleteBySignalIDOfYear(model.CorpID, model.MonitorPointID, model.SignalID, model.DataYear);
            if (!result)
            {
                LogHelper.Info($"重录一条监测年记录中,CorpID:{model.CorpID},MonitorPointID:{model.MonitorPointID},SignalID:{model.SignalID},DataYear:{model.DataYear},未检索到已存在记录!");
            }
            var entity = Model2Entity(new Model.MonitorYearRecord(model));
            var id = dal.Insert(entity);
            return id > 0;
        }
 
        /// <summary>
        /// 重录多条
        /// </summary>
        public bool InsertsAgain(List<Model.MonitorYearRecordPure> list)
        {
            if (list == null || list.Count < 1)
                return false;
            var corpIds = list.Select(x => x.CorpID).Distinct().ToList();
            if (corpIds.Count > 1)
            {
                return default;
            }
            var groupList = list.GroupBy(x => new { x.CorpID, x.MonitorPointID, x.SignalID }).ToList();
            var dal = new DAL.MonitorYearRecord();
            foreach (var group in groupList)
            {
                var startYear = group.Min(x => x.DataYear);
                var endYear = group.Max(x => x.DataYear);
                var result = dal.DeleteBySignalIDOfYearRange(group.Key.CorpID, group.Key.MonitorPointID, group.Key.SignalID, startYear, endYear);
                if (!result)
                {
                    LogHelper.Info($"重录多条监测年记录中,CorpID:{group.Key.CorpID},MonitorPointID:{group.Key.MonitorPointID},SignalID:{group.Key.SignalID},StartYear:{startYear}, EndYear:{endYear},未检索到已存在记录!");
                }
            }
            var entityList = Model2Entities(list.Select(x => new Model.MonitorYearRecord(x)).ToList());
            var bol = dal.Inserts(entityList);
            return bol;
        }
 
        #endregion
 
        #region Update
 
        /// <summary>
        /// 更新一条
        /// </summary>
        public bool Update(Model.MonitorYearRecord model)
        {
            if (model == null)
                return default;
            var dal = new DAL.MonitorYearRecord();
            var entity = Model2Entity(model);
            var bol = dal.Update(entity);
            return bol;
        }
 
        /// <summary>
        /// 批量更新
        /// </summary>
        public bool Updates(List<Model.MonitorYearRecord> list)
        {
            if (list == null || list.Count < 1)
                return default;
            var dal = new DAL.MonitorYearRecord();
            var entityList = Model2Entities(list);
            var bol = dal.Updates(entityList);
            return bol;
        }
 
 
        #endregion
    }
}