lixiaojun
2024-04-03 671ae51982c4e6671ed23efbffb7c3dd65e3f280
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
namespace Yw.Service
{
    /// <summary>
    /// 系统单位辅助类
    /// </summary>
    public static class SysUnitHelper
    {
        private static readonly Lazy<SysUnitLang> _langService = new(() => new SysUnitLang());
        private static readonly Lazy<SysUnitType> _typeService = new(() => new SysUnitType());
        private static readonly Lazy<SysUnitValue> _valueService = new(() => new SysUnitValue());
        private static readonly Lazy<SysUnitName> _nameService = new(() => new SysUnitName());
        private static readonly Lazy<SysUnitTransfer> _transferService = new(() => new SysUnitTransfer());
 
        /// <summary>
        /// 获取单位名称
        /// </summary>
        public static string GetUnitName(string type, string unit, string lang = Lang.En)
        {
            var unitLang = _langService.Value.GetByCode(lang);
            if (unitLang == null)
            {
                return string.Empty;
            }
            var unitType = _typeService.Value.GetByCode(type);
            if (unitType == null)
            {
                return string.Empty;
            }
            var unitValue = _valueService.Value.GetByCode(unitType.ID, unit);
            if (unitValue == null)
            {
                return string.Empty;
            }
            var unitName = _nameService.Value.GetByValueIDAndLangID(unitValue.ID, unitLang.ID);
            if (unitName == null)
            {
                return unitValue.Name;
            }
            return unitName.Name;
        }
 
        /// <summary>
        /// 获取单位名称列表
        /// </summary>
        public static List<string> GetUnitNameList(string type, string lang = Lang.En)
        {
            var list = new List<string>();
            var unitLang = _langService.Value.GetByCode(lang);
            if (unitLang == null)
            {
                return list;
            }
            var unitType = _typeService.Value.GetByCode(type);
            if (unitType == null)
            {
                return list;
            }
            var unitValueList = _valueService.Value.GetByTypeID(unitType.ID);
            if (unitValueList == null || unitValueList.Count < 1)
            {
                return list;
            }
            foreach (var unitValue in unitValueList)
            {
                var unitName = _nameService.Value.GetByValueIDAndLangID(unitValue.ID, unitLang.ID);
                if (unitName == null)
                {
                    list.Add(unitValue.Name);
                }
                else
                {
                    list.Add(unitName.Name);
                }
            }
            return list;
        }
 
        /// <summary>
        /// 获取单位字典
        /// </summary>
        /// <param name="type">单位类型Code值</param>
        /// <param name="lang">单位语言-默认en</param>
        /// <returns>单位值与单位名称构成的字典</returns>
        public static Dictionary<string, string> GetUnitDict(string type, string lang = Lang.En)
        {
            var dict = new Dictionary<string, string>();
            var unitLang = _langService.Value.GetByCode(lang);
            if (unitLang == null)
            {
                return dict;
            }
            var unitType = _typeService.Value.GetByCode(type);
            if (unitType == null)
            {
                return dict;
            }
            var unitValueList = _valueService.Value.GetByTypeID(unitType.ID);
            if (unitValueList == null || unitValueList.Count < 1)
            {
                return dict;
            }
            foreach (var unitValue in unitValueList)
            {
                var unitName = _nameService.Value.GetByValueIDAndLangID(unitValue.ID, unitLang.ID);
                if (unitName == null)
                {
                    dict.Add(unitValue.Code, unitValue.Name);
                }
                else
                {
                    dict.Add(unitValue.Code, unitName.Name);
                }
            }
            return dict;
        }
 
        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="fromValue">原始值</param>
        /// <param name="type">单位类型</param>
        /// <param name="fromUnit">来源单位值</param>
        /// <param name="toUnit">目标单位值</param>
        /// <returns></returns>
        public static double Transfer(this double fromValue, string type, string fromUnit, string toUnit)
        {
            var unitType = _typeService.Value.GetByCode(type);
            if (unitType == null)
            {
                return fromValue;
            }
            var fromUnitValue = _valueService.Value.GetByCode(unitType.ID, fromUnit);
            if (fromUnitValue == null)
            {
                return fromValue;
            }
            var toUnitValue = _valueService.Value.GetByCode(unitType.ID, toUnit);
            if (toUnitValue == null)
            {
                return fromValue;
            }
            var unitTransfer = _transferService.Value.GetByFromToValueID(unitType.ID, fromUnitValue.ID, toUnitValue.ID);
            if (unitTransfer == null)
            {
                return fromValue;
            }
            return unitTransfer.Transfer(fromValue);
        }
 
 
 
    }
}