using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Bowin.Common.Linq.Entity;
using Bowin.Common.Linq;
using EMIS.ViewModel;
using EMIS.ViewModel.SystemView;
using EMIS.DataLogic.SystemSetting;
using EMIS.Entities;
using Bowin.Common.Cache;
namespace EMIS.CommonLogic.SystemSetting
{
public class DictionaryItemServices : BaseServices, IDictionaryItemServices
{
public DictionaryItemDAL DictionaryItemDAL { get; set; }
///
/// 查询对应的数据字典信息View
///
///
///
///
///
///
///
///
public IGridResultSet GetDictionaryItemViewGird(ConfiguretView configuretView, string dictionaryCode, int? isEditable, int? isVisible, int pageIndex, int pageSize)
{
//数据字典元素
Expression> exp = (x => true);
if (!string.IsNullOrEmpty(dictionaryCode) && dictionaryCode != "-1")
{
//类型代码
exp = exp.And(x => x.DictionaryCode == dictionaryCode);
}
if (isEditable.HasValue)
{
//可否编辑
if (isEditable == (int)CF_GeneralPurpose.IsYes)
{
exp = exp.And(x => x.IsEditable == true);
}
if (isEditable == (int)CF_GeneralPurpose.IsNo)
{
exp = exp.And(x => x.IsEditable != true);
}
}
var query = DictionaryItemDAL.GetDictionaryItemViewQueryable(exp);
if (isVisible.HasValue)
{
//可否显示
if (isVisible == (int)CF_GeneralPurpose.IsYes)
{
query = query.Where(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
}
if (isVisible == (int)CF_GeneralPurpose.IsNo)
{
query = query.Where(x => x.RecordStatus <= (int)SYS_STATUS.UNUSABLE);
}
}
//查询条件
if (!string.IsNullOrEmpty(configuretView.ConditionValue))
{
query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim());
}
return query.OrderBy(x => x.DictionaryCode).ThenBy(x => x.DictionaryName).ThenBy(x => x.OrderNo).ToGridResultSet(pageIndex, pageSize);
}
///
/// 查询对应的数据字典信息List
///
///
///
///
///
///
public IList GetDictionaryItemViewList(ConfiguretView configuretView, string dictionaryCode, int? isEditable, int? isVisible)
{
//数据字典元素
Expression> exp = (x => true);
if (!string.IsNullOrEmpty(dictionaryCode) && dictionaryCode != "-1")
{
//类型代码
exp = exp.And(x => x.DictionaryCode == dictionaryCode);
}
if (isEditable.HasValue)
{
//可否编辑
if (isEditable == (int)CF_GeneralPurpose.IsYes)
{
exp = exp.And(x => x.IsEditable == true);
}
if (isEditable == (int)CF_GeneralPurpose.IsNo)
{
exp = exp.And(x => x.IsEditable != true);
}
}
var query = DictionaryItemDAL.GetDictionaryItemViewQueryable(exp);
if (isVisible.HasValue)
{
//可否显示
if (isVisible == (int)CF_GeneralPurpose.IsYes)
{
query = query.Where(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
}
if (isVisible == (int)CF_GeneralPurpose.IsNo)
{
query = query.Where(x => x.RecordStatus <= (int)SYS_STATUS.UNUSABLE);
}
}
//查询条件
if (!string.IsNullOrEmpty(configuretView.ConditionValue))
{
query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue.Trim());
}
return query.OrderBy(x => x.DictionaryCode).ThenBy(x => x.DictionaryName).ThenBy(x => x.OrderNo).ToList();
}
///
/// 根据数据字典元素ID查询对应的数据字典信息DictionaryItemView
///
///
///
public DictionaryItemView GetDictionaryItemView(Guid? DictionaryItemID)
{
try
{
var dictionaryItemView = DictionaryItemDAL.GetDictionaryItemViewQueryable(x => x.DictionaryItemID == DictionaryItemID).SingleOrDefault();
return dictionaryItemView;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
///
/// 编辑(新增、修改)
///
///
public void DictionaryItemEdit(DictionaryItemView dictionaryItemView)
{
try
{
//查询数据库进行验证
var dictionaryItemVerification = DictionaryItemDAL.DictionaryItemRepository.GetList(x => x.DictionaryItemID != dictionaryItemView.DictionaryItemID
&& x.DictionaryCode == dictionaryItemView.DictionaryCode && (x.Value == dictionaryItemView.Value || (x.Code == dictionaryItemView.Code && x.Name == dictionaryItemView.Name))).SingleOrDefault();
if (dictionaryItemVerification == null)
{
//数据有误验证
if (dictionaryItemView.DictionaryItemID != Guid.Empty)
{
var dictionaryItem = DictionaryItemDAL.DictionaryItemRepository.GetList(x => x.DictionaryItemID == dictionaryItemView.DictionaryItemID).SingleOrDefault();
if (dictionaryItem == null)
{
throw new Exception("数据有误,请核查");
}
else
{
//表示修改
dictionaryItem.OrderNo = (short)dictionaryItemView.OrderNo;
//dictionaryItem.Code = dictionaryItemView.Code;
dictionaryItem.Name = dictionaryItemView.Name;
//dictionaryItem.IsEditable = dictionaryItemView.IsEditable;
dictionaryItem.RecordStatus = dictionaryItemView.IsVisible == true ? (int)SYS_STATUS.USABLE : (int)SYS_STATUS.UNUSABLE;
//SetModifyStatus(dictionaryItem);
}
}
else
{
//表示新增
Sys_DictionaryItem dictionaryItem = new Sys_DictionaryItem();
dictionaryItem.DictionaryItemID = Guid.NewGuid();
dictionaryItem.DictionaryCode = dictionaryItemView.DictionaryCode;
dictionaryItem.OrderNo = (short)dictionaryItemView.OrderNo;
dictionaryItem.Code = dictionaryItemView.Code;
dictionaryItem.Name = dictionaryItemView.Name;
dictionaryItem.Value = dictionaryItemView.Value;
dictionaryItem.IsEditable = true;
dictionaryItem.RecordStatus = dictionaryItemView.IsVisible == true ? (int)SYS_STATUS.USABLE : (int)SYS_STATUS.UNUSABLE;
//SetNewStatus(dictionaryItem, dictionaryItemView.IsVisible == true ? (int)SYS_STATUS.USABLE : (int)SYS_STATUS.UNUSABLE);
UnitOfWork.Add(dictionaryItem);
}
}
else
{
throw new Exception("已存在相同的数据字典,请核查");
}
//事务提交
UnitOfWork.Commit();
//清除数据缓存
var key = "Dictionary_" + dictionaryItemView.DictionaryCode;
CacheHelper.Remove(key);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
///
/// 删除(只可删除可编辑的信息)
///
///
///
public bool DictionaryItemDelete(IList dictionaryItemIDList)
{
try
{
//查询对应的数据字典信息List
var dictionaryItemList = DictionaryItemDAL.DictionaryItemRepository.GetList(x => dictionaryItemIDList.Contains(x.DictionaryItemID)).ToList();
if (dictionaryItemList.Any(x => x.IsEditable != true))
{
throw new Exception("不能对不可编辑的信息进行删除");
}
UnitOfWork.Delete(x => dictionaryItemIDList.Contains(x.DictionaryItemID));
UnitOfWork.Commit();
foreach (var dictionaryItem in dictionaryItemList)
{
//清除数据缓存
var key = "Dictionary_" + dictionaryItem.DictionaryCode;
CacheHelper.Remove(key);
}
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
///
/// 数据字典下拉列表
///
///
///
///
///
///
public IGridResultSet GetDictionaryViewComboGrid(ConfiguretView configuretView, string dictionaryCode, int pageIndex, int pageSize)
{
var query = DictionaryItemDAL.GetDictionaryItemViewQueryable(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
if (!string.IsNullOrEmpty(dictionaryCode))
{
query = query.Where(x => x.DictionaryCode == dictionaryCode);
}
if (!string.IsNullOrEmpty(configuretView.ConditionValue))
{
query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
}
return query.OrderBy(x => x.DictionaryCode).ThenBy(x => x.DictionaryName).ThenBy(x => x.Value).ToGridResultSet(pageIndex, pageSize);
}
///
/// 数据字典下拉列表(市)
///
///
///
///
///
///
public IGridResultSet GetCityComboGrid(ConfiguretView configuretView, int pageIndex, int pageSize, string proviceCode = null)
{
//所属地区
var dictionaryCode = typeof(CF_Region).Name;
//后缀"00"表示省份
var query = DictionaryItemDAL.GetDictionaryItemViewQueryable(x => x.DictionaryCode == dictionaryCode && x.Code.EndsWith("00") && !x.Code.EndsWith("0000") && x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
if (!string.IsNullOrEmpty(proviceCode))
{
query = query.Where(x => x.Code.StartsWith(proviceCode));
}
if (!string.IsNullOrEmpty(configuretView.ConditionValue))
{
query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
}
return query.OrderBy(x => x.DictionaryCode).ThenBy(x => x.DictionaryName).ThenBy(x => x.Value).ToGridResultSet(pageIndex, pageSize);
}
///
/// 数据字典下拉列表(县、区)
///
///
///
///
///
///
public IGridResultSet GetDistrictComboGrid(ConfiguretView configuretView, int pageIndex, int pageSize, string cityCode = null)
{
//所属地区
var dictionaryCode = typeof(CF_Region).Name;
//后缀"00"表示省份,中间两位表示县、区
var query = DictionaryItemDAL.GetDictionaryItemViewQueryable(x => x.DictionaryCode == dictionaryCode && !x.Code.EndsWith("00") && !x.Code.EndsWith("0000") && x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
if (!string.IsNullOrEmpty(cityCode))
{
query = query.Where(x => x.Code.StartsWith(cityCode.Substring(0,4)));
}
if (!string.IsNullOrEmpty(configuretView.ConditionValue))
{
query = query.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
}
return query.OrderBy(x => x.DictionaryCode).ThenBy(x => x.DictionaryName).ThenBy(x => x.Value).ToGridResultSet(pageIndex, pageSize);
}
}
}