ningshuxia
2022-10-18 785a19197cb01f01fd137be86e789002dcaaa0d2
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.Model.Monitor
{
    /// <summary>
    /// 系数转换
    /// </summary>
    public class RatioConvert : JsonModel<RatioConvert>, IRatioConvert
    {
        /// <summary>
        /// 
        /// </summary>
        public RatioConvert() { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="convertType"></param>
        /// <param name="convertParas"></param>
        public RatioConvert(eRatioConvertType convertType, IRatioConvert convertParas)
        {
            this.ConvertType = convertType;
            this.ConvertParas = convertParas;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rhs"></param>
        public RatioConvert(RatioConvert rhs)
        {
            this.ConvertType = rhs.ConvertType;
            this.ConvertParas = rhs.ConvertParas;
        }
 
        /// <summary>
        /// 转换类型
        /// </summary>
        public eRatioConvertType ConvertType { get; set; }
 
        /// <summary>
        /// 转换参数
        /// </summary>
        public IRatioConvert ConvertParas { get; set; }
 
        /// <summary>
        /// 转换
        /// </summary>
        public double Convert(double value, out bool succeed)
        {
            return ConvertParas.Convert(value, out succeed);
        }
 
        /// <summary>
        /// 转换
        /// </summary>
        public double Convert(double value)
        {
            return ConvertParas.Convert(value);
        }
 
        /// <summary>
        /// 获取表达式
        /// </summary>
        public string GetExpression()
        {
            return ConvertParas.GetExpression();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public new static RatioConvert 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 RatioConvert ToModel(Dictionary<string, object> dict)
        {
            if (dict == null || dict.Count < 1)
                return default;
            try
            {
                var convertType = (eRatioConvertType)System.Convert.ToInt32(dict["ConvertType"]);
                IRatioConvert convertParas = null;
                switch (convertType)
                {
                    case eRatioConvertType.Unit:
                        convertParas = JsonHelper.Json2Object<UnitRatioConvert>(JsonHelper.Object2Json(dict["ConvertParas"]));
                        break;
                    case eRatioConvertType.Once:
                        convertParas = JsonHelper.Json2Object<OnceRatioConvert>(JsonHelper.Object2Json(dict["ConvertParas"]));
                        break;
                    case eRatioConvertType.Twice:
                        convertParas = JsonHelper.Json2Object<TwiceRatioConvert>(JsonHelper.Object2Json(dict["ConvertParas"]));
                        break;
                    default: break;
                }
                return new RatioConvert(convertType, convertParas);
            }
            catch
            {
                return default;
            }
        }
 
 
    }
}