ningshuxia
2025-03-24 f7d479db638f3d9e3aeb05ec82dc2818f14bf903
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
using AutoMapper;
using System.Collections.Generic;
using System.Linq;
 
namespace PBS.Service
{
    /// <summary>
    ///
    /// </summary>
    public partial class ElecPrice
    {
        //Entity to GetModel
        private static Model.ElecPrice Entity2Model(Entity.ElecPrice entity)
        {
            if (entity == null)
                return default;
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Entity.ElecPrice, Model.ElecPrice>()
            .ForMember(d => d.Settings, opt => opt.MapFrom(src => Model.ElecPriceSetting.ToModel(src.Settings)))
            ).CreateMapper();
            var model = mapper.Map<Entity.ElecPrice, Model.ElecPrice>(entity);
            return model;
        }
 
        //Entities to GetModels
        private static List<Model.ElecPrice> Entity2Models(List<Entity.ElecPrice> entities)
        {
            if (entities == null || entities.Count() < 1)
                return default;
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Entity.ElecPrice, Model.ElecPrice>()
            .ForMember(d => d.Settings, opt => opt.MapFrom(src => Model.ElecPriceSetting.ToModel(src.Settings)))
            ).CreateMapper();
            var models = mapper.Map<List<Entity.ElecPrice>, List<Model.ElecPrice>>(entities);
            return models;
        }
 
        //Model to Entity
        private static Entity.ElecPrice Model2Entity(Model.ElecPrice model)
        {
            if (model == null)
                return default;
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Model.ElecPrice, Entity.ElecPrice>()
             .ForMember(d => d.Settings, opt => opt.MapFrom(src => src.Settings == null ? null : src.Settings.ToJson()))
            ).CreateMapper();
            var entity = mapper.Map<Model.ElecPrice, Entity.ElecPrice>(model);
            return entity;
        }
 
        //Models to Entities
        private static List<Entity.ElecPrice> Model2Entities(List<Model.ElecPrice> models)
        {
            if (models == null || models.Count < 1)
                return default;
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Model.ElecPrice, Entity.ElecPrice>()
             .ForMember(d => d.Settings, opt => opt.MapFrom(src => src.Settings == null ? null : src.Settings.ToJson()))
            ).CreateMapper();
            var entities = mapper.Map<List<Model.ElecPrice>, List<Entity.ElecPrice>>(models);
            return entities;
        }
 
        //Model to Entity
        private static void Model2Entity(Model.ElecPrice model, Entity.ElecPrice entity)
        {
            if (model == null || entity == null)
                return;
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Model.ElecPrice, Entity.ElecPrice>()
             .ForMember(d => d.Settings, opt => opt.MapFrom(src => src.Settings == null ? null : src.Settings.ToJson()))
             ).CreateMapper();
            mapper.Map(model, entity);
        }
    }
}