|
|
|
|
using ATS.NonCustodial.Application.Base;
|
|
|
|
|
using ATS.NonCustodial.Application.Contracts.Interfaces.Admins.AppDictionaries;
|
|
|
|
|
using ATS.NonCustodial.Application.Contracts.Interfaces.Admins.AppDictionaries.Input;
|
|
|
|
|
using ATS.NonCustodial.Application.Contracts.Interfaces.Admins.AppDictionaries.Output;
|
|
|
|
|
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.UnifiedResults;
|
|
|
|
|
using ATS.NonCustodial.Shared.Extensions;
|
|
|
|
|
using ATS.NonCustodial.Shared.Extensions.AdvancedQuery;
|
|
|
|
|
using AutoMapper.QueryableExtensions;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace ATS.NonCustodial.Application.Impl.Admins
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 数据字典服务
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// Author:mxg
|
|
|
|
|
/// CreatedTimed:2022-05-15 10:08 PM
|
|
|
|
|
[DynamicApi(Area = "admin")]
|
|
|
|
|
public class AppDictionaryService : AdminAppServiceBase, IAppDictionaryService, IDynamicApi
|
|
|
|
|
{
|
|
|
|
|
#region Identity
|
|
|
|
|
|
|
|
|
|
private readonly IEfRepository<AppDictionaryType?, long> _appDictionaryTypeRepository;
|
|
|
|
|
private readonly IEfRepository<AppDictionary?, long> _appDictionaryRepository;
|
|
|
|
|
|
|
|
|
|
public AppDictionaryService(IEfRepository<AppDictionaryType?, long> appDictionaryTypeRepository, IEfRepository<AppDictionary?, long> appDictionaryRepository)
|
|
|
|
|
{
|
|
|
|
|
_appDictionaryTypeRepository = appDictionaryTypeRepository;
|
|
|
|
|
_appDictionaryRepository = appDictionaryRepository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion Identity
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询字典明细(字典类型及其明细)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<IResultOutput> GetAsync(long id)
|
|
|
|
|
{
|
|
|
|
|
//字典类型
|
|
|
|
|
var dictType = await base.GetWithDataAsync<AppDictionaryType, DictionaryAggGetOutput, long>(_appDictionaryTypeRepository, id);
|
|
|
|
|
|
|
|
|
|
//字典值
|
|
|
|
|
var dictData = await base.GetListWithDataAsync<AppDictionary, DictionaryGetOutput, long>(_appDictionaryRepository, w => w.DictionaryTypeId == id);
|
|
|
|
|
|
|
|
|
|
dictType.Dictionaries = dictData;
|
|
|
|
|
|
|
|
|
|
//返回
|
|
|
|
|
return ResultOutput.Ok(dictType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 分页查询(字典类型)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IResultOutput> GetPageAsync(DictionaryTypeGetPageInput input)
|
|
|
|
|
{
|
|
|
|
|
var express = GetExpression(input, _appDictionaryTypeRepository.AsQueryable(false, true));
|
|
|
|
|
var rtn = await base.GetPageAsync<AppDictionaryType, DictionaryTypeGetPageInput, DictionaryTypeListOutput>(input, express);
|
|
|
|
|
return ResultOutput.Ok(rtn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 分页查询(字典)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IResultOutput> GetDictionaryPageAsync(DictionaryGetPageInput input)
|
|
|
|
|
{
|
|
|
|
|
var express = GetExpression(input, _appDictionaryRepository.AsQueryable(false, true));
|
|
|
|
|
var rtn = await base.GetPageAsync<AppDictionary, DictionaryGetPageInput, DictionaryListOutput>(input, express);
|
|
|
|
|
return ResultOutput.Ok(rtn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取字典列表(非分页)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="code">字典类型编码</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<IResultOutput> GetListAsync(string? code)
|
|
|
|
|
{
|
|
|
|
|
var rtn = await GetListNoApiAsync(code);
|
|
|
|
|
foreach (var item in rtn)
|
|
|
|
|
{
|
|
|
|
|
if(item ==null) continue;
|
|
|
|
|
// 修复:添加 null 检查并转换为 List
|
|
|
|
|
if (item.Dictionaries != null)
|
|
|
|
|
{
|
|
|
|
|
item.Dictionaries = item.Dictionaries
|
|
|
|
|
.Where(wd => wd.DataStatus == 0)
|
|
|
|
|
.ToList(); // 添加 ToList() 转换
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//返回
|
|
|
|
|
return ResultOutput.Ok(rtn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取字典列表(非分页)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="code">字典类型编码</param>
|
|
|
|
|
[NonAction]
|
|
|
|
|
public async Task<List<DictionaryAggGetOutput>> GetListNoApiAsync(string? code)
|
|
|
|
|
{
|
|
|
|
|
//字典类型
|
|
|
|
|
var dictionaryTypes = await _appDictionaryTypeRepository.AsQueryable(false, true)
|
|
|
|
|
.WhereIf(code.IsNotNullOrWhiteSpace(), w => w.Code.Contains(code))
|
|
|
|
|
.ProjectTo<DictionaryAggGetOutput>(Mapper.ConfigurationProvider)
|
|
|
|
|
.ToListAsync()
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
//字典值
|
|
|
|
|
var dictTypeIds = dictionaryTypes.Select(w => w.Id).ToList();
|
|
|
|
|
var dictData = await base.GetListWithDataAsync<AppDictionary, DictionaryGetOutput, long>(_appDictionaryRepository, w => dictTypeIds.Contains(w.DictionaryTypeId));
|
|
|
|
|
|
|
|
|
|
foreach (var dictionaryType in dictionaryTypes)
|
|
|
|
|
{
|
|
|
|
|
dictionaryType.Dictionaries = dictData.Where(w => w.DictionaryTypeId == dictionaryType.Id).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dictionaryTypes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据字典Id获取字典信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<DictionaryGetOutput> GetDicByDicId(long id) => await base.GetAsync<AppDictionary, DictionaryGetOutput, long>(_appDictionaryRepository, id);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<List<DictionaryGetOutput>> GetDicData()
|
|
|
|
|
{
|
|
|
|
|
return await _appDictionaryRepository.AsQueryable(false, true)
|
|
|
|
|
.ProjectTo<DictionaryGetOutput>(Mapper.ConfigurationProvider)
|
|
|
|
|
.OrderByDescending(r => r.CreatedTime)
|
|
|
|
|
.ToListAsync()
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 创建或者修改字典及其字典值
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IResultOutput> CreateOrModifyDictionary(CreateOrModifyAggDictionaryInput input)
|
|
|
|
|
{
|
|
|
|
|
var dictType = input.DictionaryTypeInput;
|
|
|
|
|
var dictValues = input.DictionaryInput;
|
|
|
|
|
|
|
|
|
|
//[1]校验非法数据
|
|
|
|
|
var valid = await Valid(dictType, dictValues);
|
|
|
|
|
if (!valid.Success) return valid;
|
|
|
|
|
|
|
|
|
|
//[2]处理逻辑(字典类型)
|
|
|
|
|
var dictionaryId = dictType!.Id;
|
|
|
|
|
if (dictType.Id > 0) await base.UpdateAsync(dictType, _appDictionaryTypeRepository);
|
|
|
|
|
else dictionaryId = (await base.AddWithDataAsync(dictType, _appDictionaryTypeRepository))?.Id;
|
|
|
|
|
|
|
|
|
|
//[3]处理逻辑(字典值)
|
|
|
|
|
var addDictData = dictValues
|
|
|
|
|
.Where(w => w.Id == 0)
|
|
|
|
|
.Select(w =>
|
|
|
|
|
{
|
|
|
|
|
w.DictionaryTypeId = dictionaryId!.Value;
|
|
|
|
|
return w;
|
|
|
|
|
}).ToList();
|
|
|
|
|
if (addDictData.Count > 0) await base.AddAsync(addDictData, _appDictionaryRepository);
|
|
|
|
|
|
|
|
|
|
var updateDictData = dictValues.Where(w => w.Id > 0).ToList();
|
|
|
|
|
|
|
|
|
|
if (updateDictData.Any()) await base.UpdateAsync(updateDictData, _appDictionaryRepository);
|
|
|
|
|
//返回结果
|
|
|
|
|
return ResultOutput.Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 批量修改状态(如果禁用了字典类型,那么其下面所有的字典值都要禁用)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IResultOutput> BatchChangeDictionaryTypeStatus(BatchChangeStatusInput input)
|
|
|
|
|
{
|
|
|
|
|
if (!input.Ids.Any()) return ResultOutput.NotOk("参数错误");
|
|
|
|
|
|
|
|
|
|
//字典类型
|
|
|
|
|
var dictType = await _appDictionaryTypeRepository.AsQueryable(false, true)
|
|
|
|
|
.Where(w => input.Ids.Contains(w.Id))
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
var dictData = await _appDictionaryRepository.AsQueryable(false, true)
|
|
|
|
|
.Where(w => input.Ids.Contains(w.DictionaryTypeId))
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
//批量更新
|
|
|
|
|
await _appDictionaryTypeRepository.UpdateAsync(
|
|
|
|
|
dictType.Select(w =>
|
|
|
|
|
{
|
|
|
|
|
w.DataStatus = input.DataStatus;
|
|
|
|
|
return w;
|
|
|
|
|
})
|
|
|
|
|
,
|
|
|
|
|
UpdatingProps<AppDictionaryType>(x => x.DataStatus)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
//批量更新
|
|
|
|
|
await _appDictionaryRepository.UpdateAsync(
|
|
|
|
|
dictData.Select(w =>
|
|
|
|
|
{
|
|
|
|
|
w.DataStatus = input.DataStatus;
|
|
|
|
|
return w;
|
|
|
|
|
}),
|
|
|
|
|
UpdatingProps<AppDictionary>(x => x.DataStatus)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
//返回结果
|
|
|
|
|
return ResultOutput.Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 批量修改状态批量修改数据字典状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IResultOutput> BatchChangeDictionaryStatus(BatchChangeStatusInput input) => await base.BatchChangeStatus(input, _appDictionaryRepository);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 批量删除数据字典类型
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
public async Task<IResultOutput> BatchDeleteDictionaryType(BatchIdsInput input)
|
|
|
|
|
{
|
|
|
|
|
await _appDictionaryTypeRepository.DeleteAsync(w => input.Ids.Contains(w.Id));
|
|
|
|
|
|
|
|
|
|
await _appDictionaryRepository.DeleteAsync(w => input.Ids.Contains(w.DictionaryTypeId));
|
|
|
|
|
|
|
|
|
|
return ResultOutput.Result(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 批量删除数据字典
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
public async Task<IResultOutput> BatchDeleteDictionary(BatchIdsInput input)
|
|
|
|
|
{
|
|
|
|
|
await _appDictionaryRepository.DeleteAsync(w => input.Ids.Contains(w.Id));
|
|
|
|
|
|
|
|
|
|
return ResultOutput.Result(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据Ids获取字典
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<List<DictionaryGetOutput>> GetDicByDicIds(BatchIdsInput input) => await base.GetListWithDataAsync<AppDictionary, DictionaryGetOutput, long>(_appDictionaryRepository, w => input.Ids.Contains(w.Id));
|
|
|
|
|
|
|
|
|
|
#region Private
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询条件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pageInput"></param>
|
|
|
|
|
/// <param name="query"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private IQueryable<AppDictionaryType> GetExpression(DictionaryTypeGetPageInput pageInput, IQueryable<AppDictionaryType?> query)
|
|
|
|
|
{
|
|
|
|
|
query = query
|
|
|
|
|
.WhereIf(pageInput.KeyWord.NotNull(), a => a.Name.Contains(pageInput.KeyWord) || a.Code.Contains(pageInput.KeyWord))
|
|
|
|
|
.WhereIf(pageInput.StatusSearch?.Content != null, w => w.DataStatus == pageInput.StatusSearch!.Content);
|
|
|
|
|
|
|
|
|
|
var express = base.GetExpression<AppDictionaryType, DictionaryTypeGetPageInput, long>(pageInput, query);
|
|
|
|
|
|
|
|
|
|
return express;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询字典条件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pageInput"></param>
|
|
|
|
|
/// <param name="query"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private IQueryable<AppDictionary> GetExpression(DictionaryGetPageInput pageInput, IQueryable<AppDictionary?> query)
|
|
|
|
|
{
|
|
|
|
|
query = query
|
|
|
|
|
.WhereIf(pageInput.KeyWord.NotNull(), a => a.Name.Contains(pageInput.KeyWord) || a.Code.Contains(pageInput.KeyWord))
|
|
|
|
|
.WhereIf(pageInput.StatusSearch?.Content != null, w => w.DataStatus == pageInput.StatusSearch!.Content)
|
|
|
|
|
.WhereIf(pageInput.DictionaryTypeId != null, w => w.DictionaryTypeId == pageInput.DictionaryTypeId);
|
|
|
|
|
|
|
|
|
|
var express = base.GetExpression<AppDictionary, DictionaryGetPageInput, long>(pageInput, query);
|
|
|
|
|
|
|
|
|
|
return express;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 新增验证方法
|
|
|
|
|
/// </summary>
|
|
|
|
|
private async Task<IResultOutput> Valid(CreateOrModifyDictionaryTypeInput? dictType, List<CreateOrModifyDictionaryInput>? dictValues)
|
|
|
|
|
{
|
|
|
|
|
//校验字典类型
|
|
|
|
|
var dictTypeValid = await base.Valid<AppDictionaryType, CreateOrModifyDictionaryTypeInput, long>(dictType, _appDictionaryTypeRepository, w =>
|
|
|
|
|
{
|
|
|
|
|
w = w.AndNotNull(w => w.Code.Equals(dictType.Code), dictType.Code.IsNotNullOrEmpty());
|
|
|
|
|
|
|
|
|
|
return (w, $"当前字典类型:{dictType.Code}已存在");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!dictTypeValid.Success) return dictTypeValid;
|
|
|
|
|
|
|
|
|
|
//字典值编码不能重复
|
|
|
|
|
var dictRepeatList = dictValues!.GroupBy(w => w.Code).Where(w => w.Count() > 1);
|
|
|
|
|
foreach (var dr in dictRepeatList)
|
|
|
|
|
{
|
|
|
|
|
return ResultOutput.NotOk($"字典编码:{dr.Key}不能重复!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//校验字典值
|
|
|
|
|
foreach (var dictValue in dictValues)
|
|
|
|
|
{
|
|
|
|
|
//校验字典类型
|
|
|
|
|
var dictValueValid = await base.Valid<AppDictionary, CreateOrModifyDictionaryInput, long>(dictValue, _appDictionaryRepository, w =>
|
|
|
|
|
{
|
|
|
|
|
w = w.AndNotNull(w => w.Code.Equals(dictValue.Code) && w.DictionaryTypeId == dictType.Id, dictValue.Code.IsNotNullOrEmpty());
|
|
|
|
|
|
|
|
|
|
return (w, $"当前字典值:{dictValue.Code}已存在");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!dictValueValid.Success) return dictValueValid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultOutput.Ok(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion Private
|
|
|
|
|
}
|
|
|
|
|
}
|