lixiaojun
2023-04-12 fc6b7c9852f18e42fb9bccaf0cc22fbe5389d179
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 AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IStation.Untity;
 
namespace IStation.Service
{
    public partial class Signal
    {
        //Entity to Model
        private Model.Signal Entity2Model(Entity.Signal entity)
        {
            if (entity == null)
                return default;
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Entity.Signal, Model.Signal>()
            .ForMember(d => d.Flags, opt => opt.MapFrom(src => FlagsHelper.ToList(src.Flags)))
            .ForMember(d => d.ConvertParas, opt => opt.MapFrom(src => Model.Monitor.ConvertParameters.ToModel(src.ConvertParas))) 
            .ForMember(d => d.FilterParas, opt => opt.MapFrom(src => Model.Monitor.FilterParameters.ToModel(src.FilterParas)))
            .ForMember(d => d.CorrectParas, opt => opt.MapFrom(src => Model.Monitor.CorrectParameters.ToModel(src.CorrectParas)))
            ).CreateMapper();
            var model = mapper.Map<Entity.Signal, Model.Signal>(entity);
            return model;
        }
        //Entities to Models
        private List<Model.Signal> Entity2Models(List<Entity.Signal> entities)
        {
            if (entities == null || entities.Count() < 1)
                return default;
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Entity.Signal, Model.Signal>()
            .ForMember(d => d.Flags, opt => opt.MapFrom(src => FlagsHelper.ToList(src.Flags)))
            .ForMember(d => d.ConvertParas, opt => opt.MapFrom(src => Model.Monitor.ConvertParameters.ToModel(src.ConvertParas)))
            .ForMember(d => d.FilterParas, opt => opt.MapFrom(src => Model.Monitor.FilterParameters.ToModel(src.FilterParas)))
            .ForMember(d => d.CorrectParas, opt => opt.MapFrom(src => Model.Monitor.CorrectParameters.ToModel(src.CorrectParas)))
            ).CreateMapper();
            var models = mapper.Map<List<Entity.Signal>, List<Model.Signal>>(entities);
            return models;
        }
 
        //Model to Entity
        private Entity.Signal Model2Entity(Model.Signal model)
        {
            if (model == null)
                return default;
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Model.Signal, Entity.Signal>()
            .ForMember(d => d.Flags, opt => opt.MapFrom(src => FlagsHelper.ToString(src.Flags)))
            .ForMember(d => d.ConvertParas, opt => opt.MapFrom(src => src.ConvertParas == null ? null : src.ConvertParas.ToJson()))
            .ForMember(d => d.FilterParas, opt => opt.MapFrom(src => src.FilterParas==null?null:src.FilterParas.ToJson()))
            .ForMember(d => d.CorrectParas, opt => opt.MapFrom(src => src.CorrectParas==null?null:src.CorrectParas.ToJson()))
            ).CreateMapper();
            var entity = mapper.Map<Model.Signal, Entity.Signal>(model);
            return entity;
        }
 
        //Models to Entities
        private List<Entity.Signal> Model2Entities(List<Model.Signal> models)
        {
            if (models == null || models.Count < 1)
                return default;
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Model.Signal, Entity.Signal>()
            .ForMember(d => d.Flags, opt => opt.MapFrom(src => FlagsHelper.ToString(src.Flags)))
            .ForMember(d => d.ConvertParas, opt => opt.MapFrom(src => src.ConvertParas == null ? null : src.ConvertParas.ToJson()))
            .ForMember(d => d.FilterParas, opt => opt.MapFrom(src => src.FilterParas == null ? null : src.FilterParas.ToJson()))
            .ForMember(d => d.CorrectParas, opt => opt.MapFrom(src => src.CorrectParas == null ? null : src.CorrectParas.ToJson()))
            ).CreateMapper();
            var entities = mapper.Map<List<Model.Signal>, List<Entity.Signal>>(models);
            return entities;
        }
 
        //Model to Entity
        private void Model2Entity(Model.Signal model, Entity.Signal entity)
        {
            if (model == null || entity == null)
                return;
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Model.Signal, Entity.Signal>()
            .ForMember(d => d.Flags, opt => opt.MapFrom(src => FlagsHelper.ToString(src.Flags)))
            .ForMember(d => d.ConvertParas, opt => opt.MapFrom(src => src.ConvertParas == null ? null : src.ConvertParas.ToJson()))
            .ForMember(d => d.FilterParas, opt => opt.MapFrom(src => src.FilterParas == null ? null : src.FilterParas.ToJson()))
            .ForMember(d => d.CorrectParas, opt => opt.MapFrom(src => src.CorrectParas == null ? null : src.CorrectParas.ToJson()))
            ).CreateMapper();
            mapper.Map(model, entity);
        }
 
    }
}