You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
154 lines
5.6 KiB
154 lines
5.6 KiB
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Znyc.Cloudcar.Admin.Commons.Core.Dtos;
|
|
using Znyc.Cloudcar.Admin.Commons.Entitys;
|
|
using Znyc.Cloudcar.Admin.Commons.Extensions;
|
|
using Znyc.Cloudcar.Admin.Commons.Mapping;
|
|
using Znyc.Cloudcar.Admin.Commons.Services;
|
|
using Znyc.Cloudcar.Admin.Security.Dtos;
|
|
using Znyc.Cloudcar.Admin.Security.Entitys;
|
|
using Znyc.Cloudcar.Admin.Security.IRepositories;
|
|
using Znyc.Cloudcar.Admin.Security.IServices;
|
|
|
|
namespace Znyc.Cloudcar.Admin.Security.Services
|
|
{
|
|
/// <summary>
|
|
/// 组织机构
|
|
/// </summary>
|
|
public class OrganizeService : BaseService<OrganizeEntity, OrganizeOutputDto, long>, IOrganizeService
|
|
{
|
|
private readonly IOrganizeRepository _repository;
|
|
|
|
/// <summary>
|
|
/// </summary>
|
|
/// <param name="repository"></param>
|
|
/// <param name="logService"></param>
|
|
public OrganizeService(IOrganizeRepository repository) : base(repository)
|
|
{
|
|
_repository = repository;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取组织机构适用于Vue 树形列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<OrganizeOutputDto>> GetAllOrganizeTreeTable()
|
|
{
|
|
List<OrganizeOutputDto> reslist = new List<OrganizeOutputDto>();
|
|
IEnumerable<OrganizeEntity> elist = await _repository.GetAllAsync();
|
|
List<OrganizeEntity> list = elist.OrderBy(t => t.SortCode).ToList();
|
|
List<OrganizeEntity> oneMenuList = list.FindAll(t => t.ParentId == 0);
|
|
foreach (OrganizeEntity item in oneMenuList)
|
|
{
|
|
OrganizeOutputDto menuTreeTableOutputDto = new OrganizeOutputDto();
|
|
menuTreeTableOutputDto = item.MapTo<OrganizeOutputDto>();
|
|
menuTreeTableOutputDto.Children = GetSubOrganizes(list, item.Id).ToList();
|
|
reslist.Add(menuTreeTableOutputDto);
|
|
}
|
|
|
|
return reslist;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取根节点组织
|
|
/// </summary>
|
|
/// <param name="id">组织Id</param>
|
|
/// <returns></returns>
|
|
public OrganizeEntity GetRootOrganize(long id)
|
|
{
|
|
return _repository.Get(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按条件批量删除
|
|
/// </summary>
|
|
/// <param name="idsInfo">主键Id集合</param>
|
|
/// <param name="trans">事务对象</param>
|
|
/// <returns></returns>
|
|
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<OrganizeEntity> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按条件批量删除
|
|
/// </summary>
|
|
/// <param name="idsInfo">主键Id集合</param>
|
|
/// <param name="trans">事务对象</param>
|
|
/// <returns></returns>
|
|
public async Task<CommonResult> 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<OrganizeEntity> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取子集,递归调用
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <param name="ParentId">父级Id</param>
|
|
/// <returns></returns>
|
|
private List<OrganizeOutputDto> GetSubOrganizes(List<OrganizeEntity> data, long ParentId)
|
|
{
|
|
List<OrganizeOutputDto> list = new List<OrganizeOutputDto>();
|
|
OrganizeOutputDto OrganizeOutputDto = new OrganizeOutputDto();
|
|
List<OrganizeEntity> ChilList = data.FindAll(t => t.ParentId == ParentId);
|
|
foreach (OrganizeEntity entity in ChilList)
|
|
{
|
|
OrganizeOutputDto = entity.MapTo<OrganizeOutputDto>();
|
|
OrganizeOutputDto.Children = GetSubOrganizes(data, entity.Id).OrderBy(t => t.SortCode)
|
|
.MapTo<OrganizeOutputDto>();
|
|
list.Add(OrganizeOutputDto);
|
|
}
|
|
|
|
return list;
|
|
}
|
|
}
|
|
}
|