wangzelong
2022-10-24 aa63fe28851c03c57d2df8c6be9deb32a86dc4e0
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
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 MonitorMonthRecord
    {
 
 
        #region 最近一条记录
 
        /// <summary>
        /// 获取最近一条记录
        /// </summary>
        public Model.MonitorMonthRecordPure GetLastRecord(long CorpID, long MonitorPointID, long SignalID)
        {
            var redisHelper = new MonitorRecordCacheHelper();
            var record = redisHelper.GetLastMonthRecord(CorpID, MonitorPointID, SignalID);
            return record;
        }
 
        /// <summary>
        /// 获取最近一条记录
        /// </summary>
        public List<Model.MonitorMonthRecordPure> GetLastRecord(long CorpID, long MonitorPointID, IEnumerable<long> SignalIds)
        {
            if (SignalIds == null || SignalIds.Count() < 1)
                return default;
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastMonthRecord(CorpID, MonitorPointID, SignalIds);
            return records;
        }
 
        /// <summary>
        /// 获取最近一条记录
        /// </summary>
        public List<Model.MonitorMonthRecordPure> GetLastRecord(long CorpID, Dictionary<long, long> dict)
        {
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastMonthRecord(CorpID, dict);
            return records;
        }
 
        /// <summary>
        /// 获取最近一条记录
        /// </summary>
        public List<Model.MonitorMonthRecordPure> GetLastRecord(long CorpID, long MonitorPointID)
        {
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastMonthRecord(CorpID, MonitorPointID);
            return records;
        }
 
        /// <summary>
        /// 获取最近一条记录
        /// </summary>
        public List<Model.MonitorMonthRecordPure> GetLastRecord(long CorpID, IEnumerable<long> MonitorPointIds)
        {
            if (MonitorPointIds == null || MonitorPointIds.Count() < 1)
                return default;
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastMonthRecord(CorpID, MonitorPointIds);
            return records;
        }
 
        /// <summary>
        /// 获取最近一条正常记录
        /// </summary>
        public Model.MonitorMonthRecordPure GetLastNormalRecord(long CorpID, long MonitorPointID, long SignalID)
        {
            var redisHelper = new MonitorRecordCacheHelper();
            var record = redisHelper.GetLastNormalMonthRecord(CorpID, MonitorPointID, SignalID);
            return record;
        }
 
        /// <summary>
        /// 获取最近一条正常记录
        /// </summary>
        public List<Model.MonitorMonthRecordPure> GetLastNormalRecord(long CorpID, Dictionary<long, long> dict)
        {
            if (dict == null || dict.Count < 1)
                return default;
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastNormalMonthRecord(CorpID, dict);
            return records;
        }
 
        /// <summary>
        /// 获取最近一条正常记录
        /// </summary>
        public List<Model.MonitorMonthRecordPure> GetLastNormalRecord(long CorpID, long MonitorPointID)
        {
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastNormalMonthRecord(CorpID, MonitorPointID);
            return records;
        }
 
        /// <summary>
        /// 获取最近一条正常记录
        /// </summary>
        public List<Model.MonitorMonthRecordPure> GetLastNormalRecord(long CorpID, IEnumerable<long> MonitorPointIds)
        {
            if (MonitorPointIds == null || MonitorPointIds.Count() < 1)
                return default;
            var redisHelper = new MonitorRecordCacheHelper();
            var records = redisHelper.GetLastNormalMonthRecord(CorpID, MonitorPointIds);
            return records;
        }
        #endregion
 
        #region 历史记录
 
        /// <summary>
        /// 通过 MonitorPointID 获取
        /// </summary>
        public List<Model.MonitorMonthRecord> GetByMonitorPointID(long CorpID, long MonitorPointID)
        {
            var dal = new DAL.MonitorMonthRecord();
            var entity_list = dal.GetByMonitorPointID(CorpID,MonitorPointID);
            var model_list = Entity2Models(entity_list);
            return model_list?.OrderBy(x => x.DataYear).ThenBy(x => x.DataMonth).ThenBy(x => x.DataTime).ToList();
        }
 
        /// <summary>
        /// 通过 MonitorPointID 获取某年的数据
        /// </summary>
        public List<Model.MonitorMonthRecord> GetByMonitorPointIDOfYear(long CorpID, long MonitorPointID, int Year)
        {
            var dal = new DAL.MonitorMonthRecord();
            var entity_list = dal.GetByMonitorPointIDOfYear(CorpID, MonitorPointID, Year);
            var model_list = Entity2Models(entity_list);
            return model_list?.OrderBy(x=>x.DataYear).ThenBy(x=>x.DataMonth).ThenBy(x=>x.DataTime).ToList();
        }
 
        /// <summary>
        /// 通过 MonitorPointID 获取某月的数据
        /// </summary>
        public List<Model.MonitorMonthRecord> GetByMonitorPointIDOfMonth(long CorpID, long MonitorPointID, int Year, int Month)
        {
            var dal = new DAL.MonitorMonthRecord();
            var entity_list = dal.GetByMonitorPointIDOfMonth(CorpID, MonitorPointID, Year, Month);
            var model_list = Entity2Models(entity_list);
            return model_list?.OrderBy(x => x.DataYear).ThenBy(x => x.DataMonth).ThenBy(x => x.DataTime).ToList();
        }
 
        /// <summary>
        /// 通过 MonitorPointID 获取年区间内的数据
        /// </summary>
        public List<Model.MonitorMonthRecord> GetByMonitorPointIDOfYearRange(long CorpID, long MonitorPointID, int StartYear, int EndYear)
        {
            var dal = new DAL.MonitorMonthRecord();
            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.DataMonth).ThenBy(x => x.DataTime).ToList();
        }
 
        /// <summary>
        /// 通过 MonitorPointID 获取月区间内的数据
        /// </summary>
        public List<Model.MonitorMonthRecord> GetByMonitorPointIDOfMonthRange(long CorpID, long MonitorPointID, int StartYear, int StartMonth, int EndYear, int EndMonth)
        {
            var dal = new DAL.MonitorMonthRecord();
            var entity_list = dal.GetByMonitorPointIDOfMonthRange(CorpID, MonitorPointID, StartYear, StartMonth, EndYear, EndMonth);
            var model_list = Entity2Models(entity_list);
            return model_list?.OrderBy(x => x.DataYear).ThenBy(x => x.DataMonth).ThenBy(x => x.DataTime).ToList();
        }
 
        /// <summary>
        /// 通过 SignalID 获取某月的数据
        /// </summary>
        public Model.MonitorMonthRecord GetBySignalIDOfMonth(long CorpID, long MonitorPointID, long SignalID, int DataYear, int DataMonth)
        {
            var dal = new DAL.MonitorMonthRecord();
            var entity = dal.GetBySignalIDOfMonth(CorpID, MonitorPointID, SignalID, DataYear, DataMonth);
            var model = Entity2Model(entity);
            return model;
        }
 
        /// <summary>
        /// 通过 SignalID 获取月区间内的数据
        /// </summary>
        public List<Model.MonitorMonthRecord> GetBySignalIDOfMonthRange(long CorpID, long MonitorPointID, long SignalID, int StartYear, int StartMonth, int EndYear, int EndMonth)
        {
            var dal = new DAL.MonitorMonthRecord();
            var entityList = dal.GetBySignalIDOfMonthRange(CorpID, MonitorPointID, SignalID, StartYear, StartMonth, EndYear, EndMonth);
            var modelList = Entity2Models(entityList);
            return modelList;
        }
 
 
        /// <summary>
        /// 通过 SignalID 获取月区间内的基础内容数据
        /// </summary>
        public List<Model.MonitorBasicRecordContent> GetBasicContentBySignalIDOfMonthRange(long CorpID, long MonitorPointID,long SignalID, int StartYear, int StartMonth, int EndYear, int EndMonth)
        {
            var dal = new DAL.MonitorMonthRecord();
            var entity_list = dal.GetBySignalIDOfMonthRange(CorpID, MonitorPointID, SignalID, StartYear, StartMonth, EndYear, EndMonth);
            var model_list = Entity2BasicContents(entity_list); 
            return model_list;
        }
 
        #endregion
 
        #region Insert
 
        /// <summary>
        /// 插入一条
        /// </summary>
        public bool Insert(Model.MonitorMonthRecordPure model)
        {
            if (model == null)
                return default;
            var dal = new DAL.MonitorMonthRecord();
            var entity = Model2Entity(new Model.MonitorMonthRecord(model));
            var id = dal.Insert(entity);
            return id > 0;
        }
 
        /// <summary>
        ///  插入多条
        /// </summary>
        public bool Inserts(List<Model.MonitorMonthRecordPure> list)
        {
            if (list == null || list.Count() < 1)
                return default;
            var dal = new DAL.MonitorMonthRecord();
            var entity_list = Model2Entities(list.Select(x => new Model.MonitorMonthRecord(x)).ToList()).ToList();
            var bol = dal.Inserts(entity_list);
            return bol;
        }
 
        /// <summary>
        /// 插入最近一条记录
        /// </summary>
        public bool InsertLastRecord(Model.MonitorMonthRecordPure model)
        {
            if (model == null)
                return default;
            var dal = new DAL.MonitorMonthRecord();
            var entity = Model2Entity(new Model.MonitorMonthRecord(model));
            var bol = dal.Insert(entity) > 0;
            if (bol)
            {
                var redisHelper = new MonitorRecordCacheHelper();
                redisHelper.SetLastMonthRecord(model);
            }
            return bol;
        }
 
        /// <summary>
        /// 插入最近多条记录
        /// </summary>
        public bool InsertsLastRecord(List<Model.MonitorMonthRecordPure> list)
        {
            if (list == null || list.Count() < 1)
                return default;
            var dal = new DAL.MonitorMonthRecord();
            var entity_list = Model2Entities(list.Select(x => new Model.MonitorMonthRecord(x)).ToList());
            var bol = dal.Inserts(entity_list);
            if (bol)
            {
                var redisHelper = new MonitorRecordCacheHelper();
                redisHelper.SetLastMonthRecord(list);
            }
            return bol;
        }
 
        /// <summary>
        /// 补录一条
        /// </summary>
        public bool InsertSupplement(Model.MonitorMonthRecordPure model)
        {
            if (model == null)
                return default;
            var dal = new DAL.MonitorMonthRecord();
            var entity = Model2Entity(new Model.MonitorMonthRecord(model));
            var id = dal.Insert(entity);
            return id>0;
        }
 
        /// <summary>
        ///  补录多条
        /// </summary>
        public bool InsertsSupplement(List<Model.MonitorMonthRecordPure> list)
        {
            if (list == null || list.Count() < 1)
                return default;
            var dal = new DAL.MonitorMonthRecord();
            var entity_list = Model2Entities(list.Select(x=>new Model.MonitorMonthRecord(x)).ToList()).ToList();
            var bol = dal.Inserts(entity_list);
            return bol;
        }
 
        /// <summary>
        /// 重录一条
        /// </summary>
        public bool InsertAgain(Model.MonitorMonthRecordPure model)
        {
            if (model == null)
                return false;
            var dal = new DAL.MonitorMonthRecord();
            var result = dal.DeleteBySignalIDOfMonth(model.CorpID, model.MonitorPointID, model.SignalID, model.DataYear,model.DataMonth);
            if (!result)
            {
                LogHelper.Info($"重录一条监测月记录中,CorpID:{model.CorpID},MonitorPointID:{model.MonitorPointID},SignalID:{model.SignalID},DataYear:{model.DataYear},DataMonth:{model.DataMonth},未检索到已存在记录!");
            }
            var entity = Model2Entity(new Model.MonitorMonthRecord(model));
            var id = dal.Insert(entity);
            return id > 0;
        }
 
        /// <summary>
        /// 重录多条
        /// </summary>
        public bool InsertsAgain(List<Model.MonitorMonthRecordPure> 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.MonitorMonthRecord();
            foreach (var group in groupList)
            {
                var orderList = group.OrderBy(x => x.DataYear).ThenBy(x => x.DataMonth).ToList();
                var first = orderList.First();
                var startYear = first.DataYear;
                var startMonth=first.DataMonth;
                var last = orderList.Last();
                var endYear = last.DataYear;
                var endMonth=last.DataMonth;    
                var result = dal.DeleteBySignalIDOfMonthRange(group.Key.CorpID, group.Key.MonitorPointID, group.Key.SignalID, startYear,startMonth,endYear,endMonth);
                if (!result)
                {
                    LogHelper.Info($"重录多条监测月记录中,CorpID:{group.Key.CorpID},MonitorPointID:{group.Key.MonitorPointID},SignalID:{group.Key.SignalID},StartYear:{startYear},StartMonth:{startMonth},EndYear:{endYear},EndMonth:{endMonth},未检索到已存在记录!");
                }
            }
            var entityList = Model2Entities(list.Select(x => new Model.MonitorMonthRecord(x)).ToList());
            var bol = dal.Inserts(entityList);
            return bol;
        }
 
 
        #endregion
 
        #region Update
 
        /// <summary>
        /// 更新一条
        /// </summary>
        public bool Update(Model.MonitorMonthRecord model)
        {
            if (model == null)
                return default;
            var dal = new DAL.MonitorMonthRecord();
            var entity = Model2Entity(model);
            var bol = dal.Update(entity);
            return bol;
        }
 
        /// <summary>
        /// 批量更新
        /// </summary>
        public bool Updates(List<Model.MonitorMonthRecord> list)
        {
            if (list == null || list.Count < 1)
                return default;
            var dal = new DAL.MonitorMonthRecord();
            var entityList = Model2Entities(list);
            var bol = dal.Updates(entityList);
            return bol;
        }
 
 
        #endregion
    }
}