using BaseOUDAL;
using YLErp.Models;
namespace YLErp.BLL
{
///
/// 系统字典数据(从YLErpWeb中迁移)
///
public class DictionaryBLL
{
public static List GetList(string dictName, bool appendblank, string defaultValue = "", bool IsShowShortName = false)
{
var list = new List();
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;
}
///
/// 根据字典名称和key获取列表
///
/// 字典名称
/// 字典项名称
///
public static List 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> _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 list = null;
if (_dic == null)
{
_dic = new Dictionary>();
}
else
{
_dic.TryGetValue(dictName, out list);
}
if (list == null)
{
_dic[dictName] = list = new List();
}
list.Add(dicItem);
return this;
}
///
/// 获取字典选项
///
/// 字典名
/// 是否附件空项
/// 默认值(用于设置Selected)
public List GetList(string dictName, bool appendblank, string defaultValue = "")
{
var list = new List();
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;
}
///
/// 是否有字典项
///
public bool HasItems(string dictName)
{
return _dic != null && _dic.TryGetValue(dictName, out var itemList) ? itemList.Any() : false;
}
}
}