184 lines
5.7 KiB
C#
184 lines
5.7 KiB
C#
using BaseOUDAL;
|
|
using YLErp.Models;
|
|
|
|
namespace YLErp.BLL
|
|
{
|
|
/// <summary>
|
|
/// 系统字典数据(从YLErpWeb中迁移)
|
|
/// </summary>
|
|
public class DictionaryBLL
|
|
{
|
|
public static List<SelectItem> GetList(string dictName, bool appendblank, string defaultValue = "", bool IsShowShortName = false)
|
|
{
|
|
var list = new List<SelectItem>();
|
|
|
|
if (appendblank == true)
|
|
{
|
|
list.Add(new SelectItem { Value = "", Text = @"---" });
|
|
}
|
|
|
|
using (var db = new ErpBaseContext())
|
|
{
|
|
var query = from i in db.DictionaryItems
|
|
join d in db.Dictionaries on i.DictId equals d.Id
|
|
where d.Name == dictName
|
|
orderby i.IndexNum, i.Name
|
|
select i;
|
|
|
|
list.AddRange(query.AsEnumerable().Select(c => new SelectItem
|
|
{
|
|
Value = !string.IsNullOrWhiteSpace(c.ShortName) && IsShowShortName ? c.ShortName : c.Name,
|
|
Text = c.Name,
|
|
Selected = c.Name == defaultValue
|
|
}));
|
|
|
|
return list;
|
|
}
|
|
}
|
|
|
|
public static YLDictionary GetDictionary(params string[] dictName)
|
|
{
|
|
var yldic = new YLDictionary();
|
|
|
|
if (dictName == null || !dictName.Any())
|
|
{
|
|
return yldic;
|
|
}
|
|
|
|
using (var db = new ErpBaseContext())
|
|
{
|
|
var query = from di in db.DictionaryItems
|
|
join d in db.Dictionaries on di.DictId equals d.Id
|
|
where dictName.Contains(d.Name) && d.Name != null
|
|
orderby di.IndexNum, di.Name
|
|
select new
|
|
{
|
|
dictName = d.Name,
|
|
itemName = di.Name,
|
|
di.ShortName
|
|
};
|
|
|
|
foreach (var c in query)
|
|
{
|
|
yldic.AddItem(c.dictName, c.itemName, c.ShortName);
|
|
}
|
|
}
|
|
|
|
return yldic;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据字典名称和key获取列表
|
|
/// </summary>
|
|
/// <param name="dictName">字典名称</param>
|
|
/// <param name="dictKey">字典项名称</param>
|
|
/// <returns></returns>
|
|
public static List<DictionaryItem> GetDictionaryItems(string dictName, string dictKey = null)
|
|
{
|
|
using (var basedb = new ErpBaseContext())
|
|
{
|
|
var query = from dic in basedb.Dictionaries.Where(o => o.Name == dictName)
|
|
join items in basedb.DictionaryItems on dic.Id equals items.DictId
|
|
select items;
|
|
if (!string.IsNullOrWhiteSpace(dictKey))
|
|
{
|
|
query = query.Where(O => O.Name == dictKey);
|
|
}
|
|
return query.ToList();
|
|
}
|
|
}
|
|
|
|
public static DictionaryItem GetDictionaryItemByName(string name)
|
|
{
|
|
using (var db = new ErpBaseContext())
|
|
{
|
|
return db.DictionaryItems.FirstOrDefault(x => x.Name == name);
|
|
}
|
|
}
|
|
}
|
|
|
|
public class YLDictionaryItem
|
|
{
|
|
public string Name { get; set; }
|
|
|
|
public string ShortName { get; set; }
|
|
}
|
|
|
|
public class YLDictionary
|
|
{
|
|
Dictionary<string, List<YLDictionaryItem>> _dic;
|
|
|
|
public YLDictionary AddItem(string dictName, string itemName, string itemShortName)
|
|
{
|
|
return AddItem(dictName, new YLDictionaryItem
|
|
{
|
|
Name = itemName,
|
|
ShortName = itemShortName
|
|
});
|
|
}
|
|
|
|
public YLDictionary AddItem(string dictName, YLDictionaryItem dicItem)
|
|
{
|
|
if (dictName is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(dictName));
|
|
}
|
|
|
|
List<YLDictionaryItem> list = null;
|
|
|
|
if (_dic == null)
|
|
{
|
|
_dic = new Dictionary<string, List<YLDictionaryItem>>();
|
|
}
|
|
else
|
|
{
|
|
_dic.TryGetValue(dictName, out list);
|
|
}
|
|
|
|
if (list == null)
|
|
{
|
|
_dic[dictName] = list = new List<YLDictionaryItem>();
|
|
}
|
|
|
|
list.Add(dicItem);
|
|
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取字典选项
|
|
/// </summary>
|
|
/// <param name="dictName">字典名</param>
|
|
/// <param name="appendblank">是否附件空项</param>
|
|
/// <param name="defaultValue">默认值(用于设置Selected)</param>
|
|
public List<SelectItem> GetList(string dictName, bool appendblank, string defaultValue = "")
|
|
{
|
|
var list = new List<SelectItem>();
|
|
|
|
if (appendblank == true)
|
|
{
|
|
list.Add(new SelectItem { Value = "", Text = @"---" });
|
|
}
|
|
|
|
if (_dic != null && _dic.TryGetValue(dictName, out var itemList))
|
|
{
|
|
list.AddRange(itemList.Select(n => new SelectItem
|
|
{
|
|
Value = string.IsNullOrWhiteSpace(n.ShortName) ? n.Name : n.ShortName,
|
|
Text = n.Name,
|
|
Selected = n.Name == defaultValue
|
|
}));
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否有字典项
|
|
/// </summary>
|
|
public bool HasItems(string dictName)
|
|
{
|
|
return _dic != null && _dic.TryGetValue(dictName, out var itemList) ? itemList.Any() : false;
|
|
}
|
|
}
|
|
} |