using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Znyc.Admin.Commons.Core.Dtos;
using Znyc.Admin.Commons.Entitys;
using Znyc.Admin.Commons.Extensions;
using Znyc.Admin.Commons.Mapping;
using Znyc.Admin.Commons.Services;
using Znyc.Admin.Security.Dtos;
using Znyc.Admin.Security.Entitys;
using Znyc.Admin.Security.IRepositories;
using Znyc.Admin.Security.IServices;
namespace Znyc.Admin.Security.Services
{
///
/// 组织机构
///
public class OrganizeService : BaseService, IOrganizeService
{
private readonly IOrganizeRepository _repository;
///
///
///
///
///
public OrganizeService(IOrganizeRepository repository) : base(repository)
{
_repository = repository;
}
///
/// 获取组织机构适用于Vue 树形列表
///
///
public async Task> GetAllOrganizeTreeTable()
{
List reslist = new List();
IEnumerable elist = await _repository.GetAllAsync();
List list = elist.OrderBy(t => t.SortCode).ToList();
List oneMenuList = list.FindAll(t => t.ParentId == 0);
foreach (Organize item in oneMenuList)
{
OrganizeOutputDto menuTreeTableOutputDto = new OrganizeOutputDto();
menuTreeTableOutputDto = item.MapTo();
menuTreeTableOutputDto.Children = GetSubOrganizes(list, item.Id).ToList();
reslist.Add(menuTreeTableOutputDto);
}
return reslist;
}
///
/// 获取子集,递归调用
///
///
/// 父级Id
///
private List GetSubOrganizes(List data, long ParentId)
{
List list = new List();
OrganizeOutputDto OrganizeOutputDto = new OrganizeOutputDto();
List ChilList = data.FindAll(t => t.ParentId == ParentId);
foreach (Organize entity in ChilList)
{
OrganizeOutputDto = entity.MapTo();
OrganizeOutputDto.Children = GetSubOrganizes(data, entity.Id).OrderBy(t => t.SortCode)
.MapTo();
list.Add(OrganizeOutputDto);
}
return list;
}
///
/// 获取根节点组织
///
/// 组织Id
///
public Organize GetRootOrganize(long id)
{
return _repository.Get(id);
}
///
/// 按条件批量删除
///
/// 主键Id集合
/// 事务对象
///
public CommonResult DeleteBatchWhere(DeletesInputDto idsInfo, IDbTransaction trans = null)
{
CommonResult result = new CommonResult();
string where = string.Empty;
for (long i = 0; i < idsInfo.Ids.Length; i++)
{
if (idsInfo.Ids[0] != null)
{
@where = string.Format("ParentId='{0}'", idsInfo.Ids[0]);
IEnumerable list = _repository.GetListWhere(@where);
if (list.Count() > 0)
{
result.ErrMsg = "功能存在子集数据,不能删除";
return result;
}
}
}
where = "id in ('" + idsInfo.Ids.Join(",").Trim(',').Replace(",", "','") + "')";
bool bl = repository.DeleteBatchWhere(where);
if (bl)
{
result.ErrCode = "0";
}
return result;
}
///
/// 按条件批量删除
///
/// 主键Id集合
/// 事务对象
///
public async Task DeleteBatchWhereAsync(DeletesInputDto idsInfo, IDbTransaction trans = null)
{
CommonResult result = new CommonResult();
string where = string.Empty;
for (long i = 0; i < idsInfo.Ids.Length; i++)
{
if (idsInfo.Ids[0].ToString().Length > 0)
{
@where = string.Format("ParentId='{0}'", idsInfo.Ids[0]);
IEnumerable list = _repository.GetListWhere(@where);
if (list.Count() > 0)
{
result.ErrMsg = "该机构存在子集数据,不能删除";
return result;
}
}
}
where = "id in ('" + idsInfo.Ids.Join(",").Trim(',').Replace(",", "','") + "')";
bool bl = await repository.DeleteBatchWhereAsync(where);
if (bl)
{
result.ErrCode = "0";
}
return result;
}
}
}