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.
190 lines
6.7 KiB
190 lines
6.7 KiB
using ATS.NonCustodial.Application.Base; |
|
using ATS.NonCustodial.Application.Contracts.Interfaces.Admins.Deptcode.Input; |
|
using ATS.NonCustodial.Application.Contracts.Interfaces.Admins.Deptcode.Output; |
|
using ATS.NonCustodial.Application.Contracts.Interfaces.Admins.Role; |
|
using ATS.NonCustodial.Application.Contracts.Interfaces.Admins.Unitcode.Input; |
|
using ATS.NonCustodial.Domain.Entities.Admins; |
|
using ATS.NonCustodial.Domain.Shared.AggRootEntities.Dtos; |
|
using ATS.NonCustodial.Domain.Shared.OrmRepositories.Basic.EfCore; |
|
using ATS.NonCustodial.DynamicApi; |
|
using ATS.NonCustodial.DynamicApi.Attributes; |
|
using ATS.NonCustodial.Shared.Common.Enums; |
|
using ATS.NonCustodial.Shared.Common.UnifiedResults; |
|
using ATS.NonCustodial.Shared.Extensions; |
|
using ATS.NonCustodial.Shared.Extensions.AdvancedQuery; |
|
using AutoMapper.QueryableExtensions; |
|
using Microsoft.AspNetCore.Mvc; |
|
using Microsoft.EntityFrameworkCore; |
|
using Yitter.IdGenerator; |
|
|
|
namespace ATS.NonCustodial.Application.Impl.Admins |
|
{ |
|
/// <summary> |
|
/// 部门服务 |
|
/// </summary> |
|
/// Author:mxg |
|
/// CreatedTimed:2022-05-15 10:08 PM |
|
[DynamicApi(Area = "admin")] |
|
public class DeptcodeService : AdminAppServiceBase, IDeptcodeService, IDynamicApi |
|
{ |
|
#region Identity |
|
|
|
private readonly IEfRepository<App_Deptcode?, long> _appDeptcodeRepository; |
|
|
|
public DeptcodeService(IEfRepository<App_Deptcode?, long> appDeptcodeRepository) |
|
{ |
|
_appDeptcodeRepository = appDeptcodeRepository; |
|
} |
|
|
|
#endregion Identity |
|
|
|
/// <summary> |
|
/// 查询接口 |
|
/// </summary> |
|
/// <param name="id"></param> |
|
/// <returns></returns> |
|
public async Task<IResultOutput> GetAsync(long id) |
|
{ |
|
var rtn = await base.GetAsync<App_Deptcode, DeptcodeListOutput, long>(_appDeptcodeRepository, id); |
|
return ResultOutput.Ok(rtn); |
|
} |
|
|
|
|
|
/// <summary> |
|
/// 根据单位查询部门---列表 |
|
/// </summary> |
|
/// <param name="input"></param> |
|
/// <returns></returns> |
|
[HttpPost] |
|
public async Task<IResultOutput> GetlistAsync(DeptcodeGetPageDto input) |
|
{ |
|
var express = GetExpression(input, _appDeptcodeRepository.AsQueryable(false, true)); |
|
return ResultOutput.Ok(express); |
|
} |
|
|
|
/// <summary> |
|
/// 根据单位查询部门---树列表 |
|
/// </summary> |
|
/// <param name="input"></param> |
|
/// <returns></returns> |
|
[HttpPost] |
|
public async Task<IResultOutput> GetPageAsync(DeptcodeGetPageDto input) |
|
{ |
|
var rtnlist = new List<dynamic>();//无条件返回结果 |
|
var express = GetExpression(input, _appDeptcodeRepository.AsQueryable(false, true)); |
|
if (express.Count() == 0) return ResultOutput.Ok(rtnlist); |
|
if (!string.IsNullOrEmpty(input.name)) return ResultOutput.Ok(express);//有条件返回结果 |
|
foreach (var item in await express.Where(q => q.pid == null || q.pid == 0).ToArrayAsync()) |
|
{ |
|
rtnlist.Add(new |
|
{ |
|
Id = item.Id, |
|
pid = item.pid, |
|
DeptCode = item.DeptCode, |
|
UnitId = item.UnitId, |
|
DeptName = item.DeptName, |
|
DeptAbbr = item.DeptAbbr, |
|
Note = item.Note, |
|
children = pidlist(express.Where(q => q.pid != 0 || q.pid != null).ToList(), item.Id) |
|
}); |
|
} |
|
return ResultOutput.Ok(rtnlist); |
|
} |
|
|
|
/// <summary> |
|
/// 循环获取子级 |
|
/// </summary> |
|
/// <param name="list"></param> |
|
/// <param name="pid"></param> |
|
/// <returns></returns> |
|
public static List<dynamic> pidlist(List<App_Deptcode> list, long pid) |
|
{ |
|
var plist = new List<dynamic>(); |
|
foreach (var item in list.Where(q => q.pid == pid).ToList()) |
|
{ |
|
plist.Add(new |
|
{ |
|
Id = item.Id, |
|
pid = item.pid, |
|
DeptCode = item.DeptCode, |
|
UnitId = item.UnitId, |
|
DeptName = item.DeptName, |
|
DeptAbbr = item.DeptAbbr, |
|
Note = item.Note, |
|
children = pidlist(list, item.Id) |
|
}); |
|
} |
|
return plist; |
|
} |
|
/// <summary> |
|
/// 添加 |
|
/// </summary> |
|
/// <param name="input"></param> |
|
/// <returns></returns> |
|
public async Task<IResultOutput> AddAsync(DeptcodeAddInput input) |
|
{ |
|
var entity = Mapper.Map<App_Deptcode>(input); |
|
entity.Id = YitIdHelper.NextId(); |
|
Uow.BeginTransaction(); |
|
{ |
|
await _appDeptcodeRepository.InsertAsync(entity); |
|
} |
|
await Uow.CommitAsync(); |
|
return ResultOutput.Ok(); |
|
} |
|
|
|
/// <summary> |
|
/// 修改 |
|
/// </summary> |
|
/// <param name="input"></param> |
|
/// <returns></returns> |
|
public async Task<IResultOutput> UpdateAsync(DeptcodeUpdateInput input) |
|
{ |
|
if (!(input?.Id > 0)) return ResultOutput.NotOk(); |
|
|
|
var entity = await _appDeptcodeRepository.FindAsync((long)input.Id); |
|
|
|
if (!(entity?.Id > 0)) return ResultOutput.NotOk("编辑失败"); |
|
|
|
Mapper.Map(input, entity); |
|
await _appDeptcodeRepository.UpdateAsync(entity); |
|
return ResultOutput.Ok(); |
|
} |
|
|
|
/// <summary> |
|
/// 批量彻底删除 |
|
/// </summary> |
|
/// <param name="id"></param> |
|
/// <returns></returns> |
|
public async Task<IResultOutput> DeleteAsync(BatchIdsInput input) |
|
{ |
|
Uow.BeginTransaction(); |
|
{ |
|
//删除部门 |
|
await _appDeptcodeRepository.DeleteAsync(w => input.Ids.Contains(w.Id)); |
|
//删除子部门 |
|
await _appDeptcodeRepository.DeleteAsync(w => input.Ids.Contains((long)w.pid)); |
|
} |
|
await Uow.CommitAsync(); |
|
return ResultOutput.Ok(); |
|
} |
|
#region Private |
|
|
|
/// <summary> |
|
/// 查询条件 |
|
/// </summary> |
|
/// <param name="pageInput"></param> |
|
/// <param name="query"></param> |
|
/// <returns></returns> |
|
private IQueryable<App_Deptcode> GetExpression(DeptcodeGetPageDto pageInput, IQueryable<App_Deptcode?> query) |
|
{ |
|
var key = pageInput?.name; |
|
query = query |
|
.WhereIf(key.NotNull(), a => a.DeptName.Contains(key) || a.DeptAbbr.Contains(key)) |
|
.Where(q=>q.UnitId == pageInput.unitid); |
|
return query; |
|
} |
|
|
|
#endregion Private |
|
} |
|
} |