namespace Yw.Service { /// /// 系统单位辅助类 /// public static class SysUnitHelper { private static readonly Lazy _langService = new(() => new SysUnitLang()); private static readonly Lazy _typeService = new(() => new SysUnitType()); private static readonly Lazy _valueService = new(() => new SysUnitValue()); private static readonly Lazy _nameService = new(() => new SysUnitName()); private static readonly Lazy _transferService = new(() => new SysUnitTransfer()); /// /// 获取单位名称 /// 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; } /// /// 获取单位名称列表 /// public static List GetUnitNameList(string type, string lang = Lang.En) { var list = new List(); 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; } /// /// 获取单位字典 /// /// 单位类型Code值 /// 单位语言-默认en /// 单位值与单位名称构成的字典 public static Dictionary GetUnitDict(string type, string lang = Lang.En) { var dict = new Dictionary(); 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; } /// /// 转换 /// /// 原始值 /// 单位类型 /// 来源单位值 /// 目标单位值 /// 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); } } }