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 { /// /// 组织机构 /// 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 (OrganizeEntity item in oneMenuList) { OrganizeOutputDto menuTreeTableOutputDto = new OrganizeOutputDto(); menuTreeTableOutputDto = item.MapTo(); menuTreeTableOutputDto.Children = GetSubOrganizes(list, item.Id).ToList(); reslist.Add(menuTreeTableOutputDto); } return reslist; } /// /// 获取根节点组织 /// /// 组织Id /// public OrganizeEntity 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; } /// /// 获取子集,递归调用 /// /// /// 父级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 (OrganizeEntity entity in ChilList) { OrganizeOutputDto = entity.MapTo(); OrganizeOutputDto.Children = GetSubOrganizes(data, entity.Id).OrderBy(t => t.SortCode) .MapTo(); list.Add(OrganizeOutputDto); } return list; } } }