using Infrastructure;
using Infrastructure.Attribute;
using SqlSugar.IOC;
using ZR.Model;
using ZR.Model.System;
using ZR.Model.System.Dto;
using ZR.ServiceCore.SqlSugar;
namespace ZR.ServiceCore.Services
{
///
/// 字典类型
///
[AppService(ServiceType = typeof(ISysDictService), ServiceLifetime = LifeTime.Transient)]
public class SysDictService : BaseService, ISysDictService
{
private ISysDictDataService DictDataService;
public SysDictService(ISysDictDataService dictDataRepository)
{
this.DictDataService = dictDataRepository;
}
public List GetAll()
{
return Queryable().ToList();
}
///
/// 查询字段类型列表
///
/// 实体模型
///
///
public PagedInfo SelectDictTypeList(SysDictType dictType, PagerInfo pager)
{
var exp = Expressionable.Create();
exp.AndIF(!string.IsNullOrEmpty(dictType.DictName), it => it.DictName.Contains(dictType.DictName));
exp.AndIF(!string.IsNullOrEmpty(dictType.Status), it => it.Status == dictType.Status);
exp.AndIF(!string.IsNullOrEmpty(dictType.DictType), it => it.DictType.Contains(dictType.DictType));
exp.AndIF(!string.IsNullOrEmpty(dictType.Type), it => it.Type.Equals(dictType.Type));
return GetPages(exp.ToExpression(), pager, f => f.DictId, OrderByType.Desc);
}
///
/// 校验字典类型称是否唯一
///
/// 字典类型
///
public string CheckDictTypeUnique(SysDictType dictType)
{
SysDictType sysDictType = GetFirst(f => f.DictType == dictType.DictType);
if (sysDictType != null && sysDictType.DictId != dictType.DictId)
{
return UserConstants.NOT_UNIQUE;
}
return UserConstants.UNIQUE;
}
///
/// 批量删除字典数据信息
///
///
///
public int DeleteDictTypeByIds(long[] dictIds)
{
int sysCount = Count(s => s.Type == "Y" && dictIds.Contains(s.DictId));
if (sysCount > 0) { throw new CustomException($"删除失败Id: 系统内置参数不能删除!"); }
foreach (var dictId in dictIds)
{
SysDictType dictType = GetFirst(x => x.DictId == dictId);
if (DictDataService.Count(f => f.DictType == dictType.DictType) > 0)
{
throw new CustomException($"{dictType.DictName}已分配,不能删除");
}
}
int count = Context.Deleteable().In(dictIds).ExecuteCommand();
//if (count > 0)
//{
// DictUtils.clearDictCache();
//}
return count;
}
///
/// 插入字典类型
///
///
///
public long InsertDictType(SysDictType sysDictType)
{
return InsertReturnBigIdentity(sysDictType);
}
///
/// 修改字典类型
///
///
///
public int UpdateDictType(SysDictType sysDictType)
{
SysDictType oldDict = GetFirst(x => x.DictId == sysDictType.DictId);
if (sysDictType.DictType != oldDict.DictType)
{
//同步修改 dict_data表里面的DictType值
DictDataService.UpdateDictDataType(oldDict.DictType, sysDictType.DictType);
}
return Context.Updateable(sysDictType).IgnoreColumns(it => new { sysDictType.Create_by }).ExecuteCommand();
}
///
/// 获取字典信息
///
///
///
public SysDictType GetInfo(long dictId)
{
return GetFirst(f => f.DictId == dictId);
}
///
/// 根据字典类型查询自定义sql
///
///
///
public List SelectDictDataByCustomSql(string dictType)
{
var dictInfo = Queryable()
.Where(f => f.DictType == dictType).First();
if (dictInfo == null || !dictInfo.CustomSql.StartsWith("select", StringComparison.OrdinalIgnoreCase))
{
return null;
}
if (dictInfo.DictType == "sql_odf_room")
{
//获取当前用户的信息
var user = JwtUtil.GetLoginUser(App.HttpContext);
if (user == null)
{
dictInfo.CustomSql += " and DeptId = 0";
}
else
{
//管理员不过滤
if (!user.RoleKeys.Any(f => f.Equals(GlobalConstant.AdminRole)))
{
foreach (var role in user.Roles.OrderBy(f => f.DataScope))
{
var dataScope = (DataPermiEnum)role.DataScope;
if (DataPermiEnum.All.Equals(dataScope))//所有权限
{
break;
}
else if (DataPermiEnum.CUSTOM.Equals(dataScope))//自定数据权限
{
}
else if (DataPermiEnum.DEPT.Equals(dataScope))//本部门数据
{
//user.DeptId
dictInfo.CustomSql += " and DeptId = " + user.DeptId;
}
else if (DataPermiEnum.DEPT_CHILD.Equals(dataScope))//本部门及以下数据
{
//SQl OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )
var db = DbScoped.SugarScope.GetConnectionScope("0");
var allChildDepts = db.Queryable().ToChildList(it => it.ParentId, user.DeptId);
var allDeptId = allChildDepts.Select(f => f.DeptId).ToList();
dictInfo.CustomSql += " and DeptId in ( " + string.Join(",", allDeptId) + ")";
}
}
}
}
}
return DictDataService.SelectDictDataByCustomSql(dictInfo);
}
}
}