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);
|
}
|
|
|
|
}
|
}
|