using Learun.Cache.Base;
using Learun.Cache.Factory;
using Learun.Util;
using System;
using System.Collections.Generic;
namespace Learun.Application.Base.SystemModule
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.03.08
/// 描 述:数据字典管理
///
public class DataItemBLL : DataItemIBLL
{
#region 属性
private DataItemService dataItemService = new DataItemService();
#endregion
#region 缓存定义
private ICache cache = CacheFactory.CaChe();
private string cacheKeyClassify = "learun_adms_dataItem_classify";// 字典分类
private string cacheKeyDetail = "learun_adms_dataItem_detail_"; // 字典分类明显+分类编码
#endregion
#region 数据字典分类
///
/// 分类列表
///
///
public List GetClassifyList()
{
try
{
List list = cache.Read>(cacheKeyClassify, CacheId.dataItem);
if (list == null)
{
list = (List)dataItemService.GetClassifyList();
cache.Write>(cacheKeyClassify, list, CacheId.dataItem);
}
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 分类列表
///
/// 关键词(名称/编码)
/// 是否只取有效
///
public List GetClassifyList(string keyword, bool enabledMark = true)
{
try
{
List list = GetClassifyList();
if (enabledMark)
{
list = list.FindAll(t => t.F_EnabledMark.Equals(1));
}
if (!string.IsNullOrEmpty(keyword))
{
list = list.FindAll(t => t.F_ItemName.Contains(keyword) || t.F_ItemCode.Contains(keyword));
}
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取分类树形数据
///
///
public List GetClassifyTree()
{
try
{
List classifyList = GetClassifyList();
List treeList = new List();
foreach (var item in classifyList)
{
TreeModel node = new TreeModel();
node.id = item.F_ItemId;
node.text = item.F_ItemName;
node.value = item.F_ItemCode;
node.showcheck = false;
node.checkstate = 0;
node.isexpand = true;
node.parentId = item.F_ParentId;
treeList.Add(node);
}
return treeList.ToTree();
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 判断分类编号是否重复
///
/// 主键
/// 编码
///
public bool ExistItemCode(string keyValue, string itemCode) {
try
{
bool res = false;
List list = GetClassifyList();
if (string.IsNullOrEmpty(keyValue))
{
res = list.FindAll(t => t.F_ItemCode.Equals(itemCode)).Count <= 0;
}
else
{
res = list.FindAll(t => t.F_ItemCode.Equals(itemCode) && !t.F_ItemId.Equals(keyValue)).Count <= 0;
}
return res;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 判断分类名称是否重复
///
/// 主键
/// 名称
///
public bool ExistItemName(string keyValue, string itemName)
{
try
{
bool res = false;
List list = GetClassifyList();
if (string.IsNullOrEmpty(keyValue))
{
res = list.FindAll(t => t.F_ItemName.Equals(itemName)).Count <= 0;
}
else
{
res = list.FindAll(t => t.F_ItemName.Equals(itemName) && !t.F_ItemId.Equals(keyValue)).Count <= 0;
}
return res;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 保存分类数据实体
///
/// 主键
/// 实体
public void SaveClassifyEntity(string keyValue, DataItemEntity entity) {
try
{
dataItemService.SaveClassifyEntity(keyValue, entity);
cache.Remove(cacheKeyClassify, CacheId.dataItem);
cache.Remove(cacheKeyDetail + "dic", CacheId.dataItem);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 虚拟删除分类数据
///
/// 主键
public void VirtualDeleteClassify(string keyValue)
{
try
{
dataItemService.VirtualDeleteClassify(keyValue);
cache.Remove(cacheKeyClassify, CacheId.dataItem);
cache.Remove(cacheKeyDetail + "dic", CacheId.dataItem);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 通过编号获取字典分类实体
///
/// 编码
///
public DataItemEntity GetClassifyEntityByCode(string itemCode)
{
try
{
List list = GetClassifyList();
return list.Find(t => t.F_ItemCode.Equals(itemCode));
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
#region 字典明细
///
/// 获取数据字典明显
///
/// 分类编码
///
public List GetDetailList(string itemCode)
{
try
{
List list = cache.Read>(cacheKeyDetail + itemCode, CacheId.dataItem);
if (list?.Count==0 || list==null)
{
list = (List)dataItemService.GetDetailList(itemCode);
cache.Write>(cacheKeyDetail + itemCode, list, CacheId.dataItem);
}
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
public IEnumerable GetAllDetailList()
{
try
{
return dataItemService.GetAllDetailList();
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取数据字典详细映射数据
///
///
public Dictionary> GetModelMap()
{
try
{
Dictionary> dic = cache.Read>>(cacheKeyDetail + "dic", CacheId.dataItem);
if (dic == null) {
dic = new Dictionary>();
var list = GetClassifyList();
foreach (var item in list) {
var detailList = GetDetailList(item.F_ItemCode);
if (!dic.ContainsKey(item.F_ItemCode)) {
dic.Add(item.F_ItemCode,new Dictionary());
}
foreach (var detailItem in detailList) {
dic[item.F_ItemCode].Add(detailItem.F_ItemDetailId, new DataItemModel()
{
parentId = detailItem.F_ParentId,
text = detailItem.F_ItemName,
value = detailItem.F_ItemValue
});
}
}
cache.Write(cacheKeyDetail + "dic", dic, CacheId.dataItem);
}
return dic;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取数据字典明显
///
/// 分类编码
/// 关键词(名称/值)
///
public List GetDetailList(string itemCode, string keyword)
{
try
{
List list = GetDetailList(itemCode);
if (!string.IsNullOrEmpty(keyword)) {
list = list.FindAll(t => t.F_ItemName.Contains(keyword) || t.F_ItemValue.Contains(keyword));
}
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取数据字典明显
///
/// 分类编号
/// 父级主键
///
public List GetDetailListByParentId(string itemCode, string parentId)
{
try
{
List list = GetDetailList(itemCode);
if (!string.IsNullOrEmpty(parentId))
{
list = list.FindAll(t => t.F_ParentId.ContainsEx(parentId));
}
else
{
list = list.FindAll(t => t.F_ParentId.ContainsEx("0"));
}
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取字典明细树形数据
///
/// 分类编号
///
public List GetDetailTree(string itemCode)
{
try
{
List list = GetDetailList(itemCode);
List treeList = new List();
foreach (var item in list) {
TreeModel node = new TreeModel();
node.id = item.F_ItemDetailId;
node.text = item.F_ItemName;
node.value = item.F_ItemValue;
node.showcheck = false;
node.checkstate = 0;
node.isexpand = true;
node.parentId = item.F_ParentId == null ? "0" : item.F_ParentId;
treeList.Add(node);
}
return treeList.ToTree();
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 项目值不能重复
///
/// 主键
/// 项目值
/// 分类编码
///
public bool ExistDetailItemValue(string keyValue, string itemValue, string itemCode)
{
try
{
bool res = false;
List list = GetDetailList(itemCode);
if (string.IsNullOrEmpty(keyValue))
{
res = list.FindAll(t => t.F_ItemValue.Equals(itemValue)).Count <= 0;
}
else
{
res = list.FindAll(t => t.F_ItemValue.Equals(itemValue) && !t.F_ItemDetailId.Equals(keyValue)).Count <= 0;
}
return res;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 项目名不能重复
///
/// 主键
/// 项目名
/// 分类编码
///
public bool ExistDetailItemName(string keyValue, string itemName, string itemCode)
{
try
{
bool res = false;
List list = GetDetailList(itemCode);
if (string.IsNullOrEmpty(keyValue))
{
res = list.FindAll(t => t.F_ItemName.Equals(itemName)).Count <= 0;
}
else
{
res = list.FindAll(t => t.F_ItemName.Equals(itemName) && !t.F_ItemDetailId.Equals(keyValue)).Count <= 0;
}
return res;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 保存明细数据实体
///
/// 主键
/// 实体
public void SaveDetailEntity(string keyValue, DataItemDetailEntity entity)
{
try
{
List list = GetClassifyList();
string itemId = entity.F_ItemId;
DataItemEntity classifyEntity = list.Find(t => t.F_ItemId.Equals(itemId));
if (classifyEntity.F_IsTree != 1 || string.IsNullOrEmpty(entity.F_ParentId))
{
entity.F_ParentId = "0";
}
dataItemService.SaveDetailEntity(keyValue, entity);
cache.Remove(cacheKeyDetail + "dic", CacheId.dataItem);
cache.Remove(cacheKeyDetail + classifyEntity.F_ItemCode, CacheId.dataItem);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 虚拟删除明细数据
///
/// 主键
public void VirtualDeleteDetail(string keyValue)
{
try
{
dataItemService.VirtualDeleteDetail(keyValue);
DataItemDetailEntity entity = dataItemService.GetDetailEntity(keyValue);
List list = GetClassifyList();
string itemId = entity.F_ItemId;
DataItemEntity classifyEntity = list.Find(t => t.F_ItemId.Equals(itemId));
cache.Remove(cacheKeyDetail + "dic", CacheId.dataItem);
cache.Remove(cacheKeyDetail + classifyEntity.F_ItemCode, CacheId.dataItem);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
}
}