lixiaojun
2022-08-22 cd280a9457eda95433896e3e9e3aa5164bdd64a0
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.Model.Monitor
{
    /// <summary>
    /// 映射转换参数
    /// </summary>
    public class MappingConvert : JsonModel<MappingConvert>, IMappingConvert
    {
        /// <summary>
        /// 
        /// </summary>
        public MappingConvert() { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="convertType"></param>
        /// <param name="convertParas"></param>
        public MappingConvert(eMappingConvertType convertType, IMappingConvert convertParas)
        {
            this.ConvertType = convertType;
            this.ConvertParas = convertParas;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rhs"></param>
        public MappingConvert(MappingConvert rhs)
        {
            this.ConvertType = rhs.ConvertType;
            this.ConvertParas = rhs.ConvertParas;
        }
 
        /// <summary>
        /// 
        /// </summary>
        public eMappingConvertType ConvertType { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IMappingConvert ConvertParas { get; set; }
 
        /// <summary>
        /// 转换
        /// </summary>
        public double Convert(double value, out bool succeed)
        {
            succeed = false;
            if (ConvertParas == null)
                return value;
            return ConvertParas.Convert(value, out succeed);
        }
 
        /// <summary>
        /// 是否满足条件
        /// </summary>
        public bool Meet(double value)
        {
            if (ConvertParas == null)
                return default;
            return ConvertParas.Meet(value);
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public new static MappingConvert ToModel(string json)
        {
            if (string.IsNullOrEmpty(json))
                return default;
            try
            {
                var dict = JsonHelper.Json2Object<Dictionary<string, object>>(json);
                return ToModel(dict);
            }
            catch
            {
                return default;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dict"></param>
        /// <returns></returns>
        public static MappingConvert ToModel(Dictionary<string, object> dict)
        {
            if (dict == null || dict.Count < 1)
                return default;
            try
            {
                var convertTpe = (eMappingConvertType)System.Convert.ToInt32(dict["ConvertType"]);
                IMappingConvert convertParas = null;
                switch (convertTpe)
                {
                    case eMappingConvertType.Single:
                        convertParas = JsonHelper.Json2Object<SingleMappingConvert>(JsonHelper.Object2Json(dict["ConvertParas"]));
                        break;
                    case eMappingConvertType.Compare:
                        convertParas = JsonHelper.Json2Object<CompareMappingConvert>(JsonHelper.Object2Json(dict["ConvertParas"]));
                        break;
                    case eMappingConvertType.Range:
                        convertParas = JsonHelper.Json2Object<RangeMappingConvert>(JsonHelper.Object2Json(dict["ConvertParas"]));
                        break;
                    default: break;
                }
                return new MappingConvert(convertTpe, convertParas);
            }
            catch
            {
                return default;
            }
        }
 
    }
}