tangxu
2023-04-12 ac2a648ea67669eab6de9a1864c07f6475511dd2
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
 
 
namespace IStation.Service
{
    /// <summary>
    /// 
    /// </summary>
    public class InspectRecord
    {
       
      
        #region 编辑
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CorpID"></param>
        /// <param name="ProductID"></param>
        /// <param name="recordDay"></param>
        /// <param name="EmployeeID"></param>
        /// <param name="ContentID"></param>
        /// <returns></returns>
        public Model.InspectRecordAndDetail GetOrCreateRecord(long CorpID, long ProductID, DateTime recordDay, long EmployeeID, List<long> ContentID)
        {
            IStation.DAL.InspectRecord _dal = new IStation.DAL.InspectRecord();
            var entity = _dal.GetOrCreateRecord(CorpID, ProductID, recordDay, EmployeeID, ContentID);
            if (entity == null)
                return null;
 
 
            var model_Record = Entity2Model(entity.Record);
 
            var mapper_InspectRecordDetail = new MapperConfiguration(cfg => cfg.CreateMap<Entity.InspectRecordDetail, Model.InspectRecordDetail>()
  //.ForMember(d => d.MapPosition, opt => opt.MapFrom(src => Model.Map.PointInfo.ToModel(src.MapPosition)))
  //.ForMember(d => d.GisPosition, opt => opt.MapFrom(src => Model.Gis.PointInfo.ToModel(src.GisPosition)))
  ).CreateMapper();
            var models_detail = mapper_InspectRecordDetail.Map<List<Entity.InspectRecordDetail>, List<Model.InspectRecordDetail>>(entity.Details);
 
 
            return new Model.InspectRecordAndDetail() { Record = model_Record, Details = models_detail };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Record"></param>
        /// <param name="Details"></param>
        /// <returns></returns>
        public bool UpdateRecord(
            IStation.Model.InspectRecord Record,
            List<Model.InspectRecordDetail> Details)
        {
            var mapper_InspectRecord = new MapperConfiguration(cfg => cfg.CreateMap<Model.InspectRecord, Entity.InspectRecord>()
  //.ForMember(d => d.MapPosition, opt => opt.MapFrom(src => Model.Map.PointInfo.ToModel(src.MapPosition)))
  //.ForMember(d => d.GisPosition, opt => opt.MapFrom(src => Model.Gis.PointInfo.ToModel(src.GisPosition)))
  ).CreateMapper();
            var entity_Record = mapper_InspectRecord.Map<Model.InspectRecord, Entity.InspectRecord>(Record);
 
            var mapper_InspectRecordDetail = new MapperConfiguration(cfg => cfg.CreateMap<Model.InspectRecordDetail, Entity.InspectRecordDetail>()
  //.ForMember(d => d.MapPosition, opt => opt.MapFrom(src => Model.Map.PointInfo.ToModel(src.MapPosition)))
  //.ForMember(d => d.GisPosition, opt => opt.MapFrom(src => Model.Gis.PointInfo.ToModel(src.GisPosition)))
  ).CreateMapper();
            var entitys_detail = mapper_InspectRecordDetail.Map<List<Model.InspectRecordDetail>, List<Entity.InspectRecordDetail>>(Details);
            IStation.DAL.InspectRecord _dal = new IStation.DAL.InspectRecord();
            return _dal.UpdateRecord(entity_Record, entitys_detail);
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Record"></param>
        /// <param name="details"></param>
        /// <returns></returns>
        public bool UpdateRecord(
            IStation.Model.InspectRecord Record,
            List<Model.InspectRecordDetailBase> details)
        {
            if (details == null || details.Count() == 0)
                return false;
            if (Record == null)
                return false;
 
            var mapper_InspectRecordDetail = new MapperConfiguration(cfg => cfg.CreateMap<Model.InspectRecordDetailBase, Entity.InspectRecordDetail>()
  //.ForMember(d => d.MapPosition, opt => opt.MapFrom(src => Model.Map.PointInfo.ToModel(src.MapPosition)))
  //.ForMember(d => d.GisPosition, opt => opt.MapFrom(src => Model.Gis.PointInfo.ToModel(src.GisPosition)))
  ).CreateMapper();
            var entity_details = mapper_InspectRecordDetail.Map<List<Model.InspectRecordDetailBase>, List<Entity.InspectRecordDetail>>(details);
 
            foreach (var detail in entity_details)
            {
                detail.Time = DateTime.Now;
            }
 
            var mapper_InspectRecord = new MapperConfiguration(cfg => cfg.CreateMap<Model.InspectRecord, Entity.InspectRecord>()
//.ForMember(d => d.MapPosition, opt => opt.MapFrom(src => Model.Map.PointInfo.ToModel(src.MapPosition)))
//.ForMember(d => d.GisPosition, opt => opt.MapFrom(src => Model.Gis.PointInfo.ToModel(src.GisPosition)))
).CreateMapper();
            var entity_record = mapper_InspectRecord.Map<Model.InspectRecord, Entity.InspectRecord>(Record);
            entity_record.CompleteStatus = (int)Record.CompleteStatus;
 
            IStation.DAL.InspectRecord _dal = new IStation.DAL.InspectRecord();
            return _dal.UpdateRecord(entity_record, entity_details);
        }
 
        #endregion
 
 
        /// <summary>
        /// 某日某产品的巡检记录
        /// </summary>
        /// <param name="ProductID"></param>
        /// <param name="recordDay"></param>
        /// <returns></returns>
        public Model.InspectRecordAndDetail GetByProductID(long ProductID, DateTime recordDay)
        {
            IStation.DAL.InspectRecord _dal = new IStation.DAL.InspectRecord();
            var entity = _dal.GetRecordAndDetail(ProductID, recordDay);
            if (entity == null)
                return null;
 
            var mapper_InspectRecord = new MapperConfiguration(cfg => cfg.CreateMap<Entity.InspectRecord, Model.InspectRecord>()
  //.ForMember(d => d.MapPosition, opt => opt.MapFrom(src => Model.Map.PointInfo.ToModel(src.MapPosition)))
  //.ForMember(d => d.GisPosition, opt => opt.MapFrom(src => Model.Gis.PointInfo.ToModel(src.GisPosition)))
  ).CreateMapper();
            var model_Record = mapper_InspectRecord.Map<Entity.InspectRecord, Model.InspectRecord>(entity.Record);
 
            var mapper_InspectRecordDetail = new MapperConfiguration(cfg => cfg.CreateMap<Entity.InspectRecordDetail, Model.InspectRecordDetail>()
  //.ForMember(d => d.MapPosition, opt => opt.MapFrom(src => Model.Map.PointInfo.ToModel(src.MapPosition)))
  //.ForMember(d => d.GisPosition, opt => opt.MapFrom(src => Model.Gis.PointInfo.ToModel(src.GisPosition)))
  ).CreateMapper();
            var models_detail = mapper_InspectRecordDetail.Map<List<Entity.InspectRecordDetail>, List<Model.InspectRecordDetail>>(entity.Details);
 
 
            return new Model.InspectRecordAndDetail() { Record = model_Record, Details = models_detail };
        }
 
        /// <summary>
        /// 某日的巡检记录
        /// </summary>
        /// <param name="CorpID"></param>
        /// <param name="recordDay"></param>
        /// <returns></returns>
        public List<Model.InspectRecord> GetByRecordDay(long CorpID,DateTime recordDay)
        {
            IStation.DAL.InspectRecord _dal = new IStation.DAL.InspectRecord();
            var entitys = _dal.GetByRecordDay(CorpID, recordDay);
 
            return Entity2Model(entitys);
        }
 
        /// <summary>
        /// 某巡检点的巡检记录
        /// </summary>
        /// <param name="ContentID"></param>
        /// <param name="Year"></param>
        /// <returns></returns>
        public List<Model.InspectRecordDetail> GetDetailByContentID(long ContentID, int Year )
        {
            if (Year < 2022)
                Year = 2022;
            IStation.DAL.InspectRecordDetail _dal = new IStation.DAL.InspectRecordDetail();
            var entitys = _dal.GetDetailByContentID(ContentID, Year);
            var mapper_InspectRecordDetail = new MapperConfiguration(cfg => cfg.CreateMap<Entity.InspectRecordDetail, Model.InspectRecordDetail>()
//.ForMember(d => d.MapPosition, opt => opt.MapFrom(src => Model.Map.PointInfo.ToModel(src.MapPosition)))
//.ForMember(d => d.GisPosition, opt => opt.MapFrom(src => Model.Gis.PointInfo.ToModel(src.GisPosition)))
).CreateMapper();
            return mapper_InspectRecordDetail.Map<List<Entity.InspectRecordDetail>, List<Model.InspectRecordDetail>>(entitys); 
        }
 
        /// <summary>
        /// 某巡检点的巡检记录
        /// </summary>
        /// <param name="ContentID"></param>
        /// <param name="StartDay"></param>
        /// <param name="EndDay"></param>
        /// <returns></returns>
        public List<Model.InspectRecordDetail> GetDetailByContentID(long ContentID, DateTime StartDay,DateTime EndDay)
        {
            if (EndDay <= StartDay)
                return null;
            IStation.DAL.InspectRecordDetail _dal = new IStation.DAL.InspectRecordDetail();
            var entitys = _dal.GetDetailByContentID(ContentID, StartDay, EndDay);
            var mapper_InspectRecordDetail = new MapperConfiguration(cfg => cfg.CreateMap<Entity.InspectRecordDetail, Model.InspectRecordDetail>()
//.ForMember(d => d.MapPosition, opt => opt.MapFrom(src => Model.Map.PointInfo.ToModel(src.MapPosition)))
//.ForMember(d => d.GisPosition, opt => opt.MapFrom(src => Model.Gis.PointInfo.ToModel(src.GisPosition)))
).CreateMapper();
            return mapper_InspectRecordDetail.Map<List<Entity.InspectRecordDetail>, List<Model.InspectRecordDetail>>(entitys);
        }
 
 
        /// <summary>
        /// 某巡检点的巡检记录
        /// </summary>
        /// <param name="ContentID"></param>
        /// <param name="StartDay"></param>
        /// <param name="EndDay"></param>
        /// <returns></returns>
        public List<Model.InspectRecord > GetRecordByContentID(long ContentID, DateTime StartDay, DateTime EndDay)
        {
            if (EndDay <= StartDay)
                return null;
            IStation.DAL.InspectRecord  _dal = new IStation.DAL.InspectRecord();
            var entitys = _dal.GetRecordByContentID(ContentID, StartDay, EndDay);
            var mapper_InspectRecordDetail = new MapperConfiguration(cfg => cfg.CreateMap<Entity.InspectRecord , Model.InspectRecord>()
//.ForMember(d => d.MapPosition, opt => opt.MapFrom(src => Model.Map.PointInfo.ToModel(src.MapPosition)))
//.ForMember(d => d.GisPosition, opt => opt.MapFrom(src => Model.Gis.PointInfo.ToModel(src.GisPosition)))
).CreateMapper();
            return mapper_InspectRecordDetail.Map<List<Entity.InspectRecord>, List<Model.InspectRecord>>(entitys);
        }
 
        #region 统计
 
        /// <summary>
        /// 根据月份,统计 , 某日的巡检人数 
        /// </summary>
        public List<Model.Inspect.RecordDayCountStatic> GetDayEmployeeCountByMonth(long CorpID, int Year, int Month)
        {
            var start_day = new DateTime(Year, Month, 1);
            var end_day = start_day.AddMonths(1).AddDays(-1);
            return GetDayEmployeeCountByDay(CorpID, start_day, end_day);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="CorpID"></param>
        /// <param name="start_day"></param>
        /// <param name="end_day"></param>
        /// <returns></returns>
        public List<Model.Inspect.RecordDayCountStatic> GetDayEmployeeCountByDay(long CorpID, DateTime start_day, DateTime end_day)
        {
            IStation.DAL.InspectRecord _dal = new IStation.DAL.InspectRecord();
            var entitys = _dal.GetEmployeeStaticByDay(CorpID, start_day, end_day );
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Entity.Inspect.RecordDayCountStatic, 
                Model.Inspect.RecordDayCountStatic>()
                //.ForMember(d => d.RecordDay, t => t.MapFrom(src => src.RecordDay.ToString("yyyy-MM-dd"))))
                ).CreateMapper();
            return mapper.Map<List<Entity.Inspect.RecordDayCountStatic>, List<Model.Inspect.RecordDayCountStatic>>(entitys);
        }
        /// <summary>
        /// 隐患数量统计
        /// </summary>
        /// <param name="CorpID"></param>
        /// <param name="start_day"></param>
        /// <param name="end_day"></param>
        /// <returns></returns>
        public List<Model.Inspect.ProductWorryCountStatic> GetWorryCountStaticByProduct(long CorpID, DateTime start_day, DateTime end_day)
        {
            IStation.DAL.InspectRecord _dal = new IStation.DAL.InspectRecord();
            var entitys = _dal.GetWorryCountStaticByProduct(CorpID, start_day.ToString("yyyy-MM-dd"), end_day.ToString("yyyy-MM-dd"));
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Entity.Inspect.ProductWorryCountStatic, Model.Inspect.ProductWorryCountStatic>()).CreateMapper();
            return mapper.Map<List<Entity.Inspect.ProductWorryCountStatic>, List<Model.Inspect.ProductWorryCountStatic>>(entitys);
        }
        #endregion
 
 
        #region 最后一条记录(最近记录)
        /// <summary>
        /// 获取产品最后一条记录
        /// </summary>
        /// <param name="CorpID"></param>
        /// <returns></returns>
        public List<IStation.Model.InspectRecord> GetProductLastRecordByCorpID(long CorpID)
        {
            IStation.DAL.InspectRecord _dal = new IStation.DAL.InspectRecord();
            var list = _dal.GetProductLastRecordByCorpID(CorpID);
            return Entity2Model(list);
        }
 
        /// <summary>
        /// 获取产品最后一条记录
        /// </summary>
        /// <param name="ProductID"></param>
        /// <returns></returns>
        public List<IStation.Model.InspectRecord> GetProductLastRecordByProductID(IEnumerable<long> ProductID)
        {
            if (ProductID == null || ProductID.Count() == 0)
                return null;
            IStation.DAL.InspectRecord _dal = new IStation.DAL.InspectRecord();
            var list = _dal.GetProductLastRecordByProductID(ProductID);
            return Entity2Model(list);
        }
 
        /// <summary>
        /// 获取员工最后一条记录
        /// </summary>
        /// <param name="CorpID"></param>
        /// <returns></returns>
        public List<IStation.Model.InspectRecord> GetEmployeeLastRecord( long  CorpID)
        {
            IStation.DAL.InspectRecord _dal = new IStation.DAL.InspectRecord();
            var list = _dal.GetEmployeeLastRecord(CorpID);
            return Entity2Model(list);
        }
        #endregion
 
 
        #region 分页
        /// <summary>
        /// 获取某产品的所有巡检记录(分页)
        /// </summary>
        /// <param name="CorpID"></param>
        /// <param name="ProductID"></param> 
        /// <param name="PageIndex">页码序号(从0开始)</param>
        /// <param name="PageSize">每一页的条数</param>
        /// <param name="Total"></param>
        /// <returns></returns>
        public List<IStation.Model.InspectRecord> GetPageListByProductID(int PageIndex, int PageSize, long CorpID, long ProductID, out int Total)
        {
            IStation.DAL.InspectRecord _dal = new IStation.DAL.InspectRecord();
            var entity_list = _dal.GetPageListByProductID(PageIndex, PageSize, CorpID, ProductID, out Total);
            return Entity2Model(entity_list);
        }
 
        /// <summary>
        /// 某员工的巡检记录(历史):这个同一天会有多条记录(多个产品)
        /// </summary>
        /// <param name="PageIndex"></param> 
        /// <param name="PageSize"></param>
        /// <param name="EmployeeID"></param>
        /// <returns></returns>
        public List<IStation.Model.Inspect.RecordDayEmployeeStatic> GetPageListByEmployeeID(int PageIndex, int PageSize, long EmployeeID )
        {
            IStation.DAL.InspectRecord _dal = new IStation.DAL.InspectRecord();
            var entity_list = _dal.GetPageListByEmployeeID(PageIndex, PageSize, EmployeeID );
            if (entity_list == null || entity_list.Count() == 0)
                return null;
 
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Entity.Inspect.RecordDayEmployeeStatic, Model.Inspect.RecordDayEmployeeStatic>()
).CreateMapper();
            var models = mapper.Map<List<Entity.Inspect.RecordDayEmployeeStatic>, List<Model.Inspect.RecordDayEmployeeStatic>>(entity_list);
 
            return models;
        }
 
        #endregion
 
        private List<Model.InspectRecord> Entity2Model(List<Entity.InspectRecord> entities)
        {
            if (entities == null || entities.Count() < 1)
                return default;
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Entity.InspectRecord, Model.InspectRecord>()
            //.ForMember(d => d.RecordDay , opt => opt.MapFrom(src => Model.RecordDay.ToModel(src.MapPosition)))
            ).CreateMapper();
            var models = mapper.Map<List<Entity.InspectRecord>, List<Model.InspectRecord>>(entities);
            return models;
        }
        private static Entity.InspectRecord Model2Entity(Model.InspectRecord model)
        {
            if (model == null)
                return default;
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Model.InspectRecord, Entity.InspectRecord>()
              //.ForMember(d => d.RecordDay, opt => opt.MapFrom(src => src.RecordDay))
              //.ForMember(d => d.GisPosition, opt => opt.MapFrom(src => Model.Gis.PointInfo.ToModel(src.GisPosition)))
              ).CreateMapper();
            var entity = mapper.Map<Model.InspectRecord, Entity.InspectRecord>(model);
            //entity.LogType = (int)model.LogType;
            //entity.SoftType = (int)model.SoftType;
            //entity.UserType = (int)model.UserType;
            entity.RecordDay = DateTime.Parse( model.RecordDay );
            return entity;
        }
 
        private Model.InspectRecord Entity2Model(Entity.InspectRecord entity)
        {
            if (entity == null)
                return default;
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Entity.InspectRecord, Model.InspectRecord>()
            ).CreateMapper();
            var model = mapper.Map<Entity.InspectRecord, Model.InspectRecord>(entity);
            return model;
        }
    }
}