312 lines
13 KiB
C#
312 lines
13 KiB
C#
using IRaCIS.Core.Infrastructure.ExpressionExtend;
|
||
using IRaCIS.Application.Interfaces;
|
||
using IRaCIS.Application.Contracts;
|
||
using IRaCIS.Core.Infra.EFCore;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Panda.DynamicWebApi.Attributes;
|
||
|
||
namespace IRaCIS.Application.Services
|
||
{
|
||
/// <summary>
|
||
/// 数据字典-基础数据维护
|
||
/// </summary>
|
||
|
||
[ApiExplorerSettings(GroupName = "Common")]
|
||
public class DictionaryService : BaseService, IDictionaryService
|
||
{
|
||
private readonly IRepository<Dictionary> _dicRepository;
|
||
private readonly IRepository<DoctorDictionary> _doctorDictionaryRepository;
|
||
private readonly IRepository<TrialDictionary> _trialDictionaryRepository;
|
||
private readonly IRepository<Doctor> _doctorRepository;
|
||
private readonly IRepository<Trial> _trialRepository;
|
||
|
||
public DictionaryService(IRepository<Dictionary> sysDicRepository, IRepository<DoctorDictionary> doctorDictionaryRepository, IRepository<TrialDictionary> trialDictionaryRepository,
|
||
IRepository<Doctor> doctorRepository, IRepository<Trial> trialRepository)
|
||
{
|
||
_dicRepository = sysDicRepository;
|
||
_doctorDictionaryRepository = doctorDictionaryRepository;
|
||
_trialDictionaryRepository = trialDictionaryRepository;
|
||
_doctorRepository = doctorRepository;
|
||
_trialRepository = trialRepository;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// New 查询条件 IsConfig 代表是字典类型配置项 否就是我们普通的项 和普通项的子项
|
||
/// </summary>
|
||
/// <param name="basicDicQuery"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public async Task<PageOutput<BasicDicView>> GetBasicDicList(BasicDicQuery basicDicQuery)
|
||
{
|
||
|
||
var systemBasicDataQueryable = _dicRepository.Where(t => t.ParentId == null)
|
||
.WhereIf(!string.IsNullOrEmpty(basicDicQuery.Code), t => t.Code.Contains(basicDicQuery.Code!))
|
||
.WhereIf(!string.IsNullOrEmpty(basicDicQuery.KeyName), t => t.KeyName.Contains(basicDicQuery.KeyName!))
|
||
.WhereIf(basicDicQuery.ConfigTypeId != null, t => t.ConfigTypeId == basicDicQuery.ConfigTypeId!)
|
||
.WhereIf(basicDicQuery.IsConfig != null, t => t.IsConfig == basicDicQuery.IsConfig)
|
||
|
||
.ProjectTo<BasicDicView>(_mapper.ConfigurationProvider);
|
||
|
||
return await systemBasicDataQueryable.ToPagedListAsync(basicDicQuery.PageIndex, basicDicQuery.PageSize, String.IsNullOrEmpty(basicDicQuery.SortField) ? "Code" : basicDicQuery.SortField, basicDicQuery.Asc);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// New
|
||
/// </summary>
|
||
/// <param name="addOrEditBasic"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public async Task<IResponseOutput> AddOrUpdateBasicDic(AddOrEditBasicDic addOrEditBasic)
|
||
{
|
||
|
||
var entity = await _repository.InsertOrUpdateAsync<Dictionary, AddOrEditBasicDic>(addOrEditBasic, true);
|
||
|
||
return ResponseOutput.Ok(entity.Id.ToString());
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// New
|
||
/// </summary>
|
||
/// <param name="parentId"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("{parentId:guid}")]
|
||
public async Task<List<BasicDicView>> GetChildList(Guid parentId)
|
||
{
|
||
return await _dicRepository.Where(t => t.ParentId == parentId)
|
||
.OrderBy(t => t.ShowOrder).ProjectTo<BasicDicView>(_mapper.ConfigurationProvider).ToListAsync();
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 传递父亲 code 字符串 数组 返回多个下拉框数据
|
||
/// </summary>
|
||
/// <param name="searchArray"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public async Task<Dictionary<string, List<BasicDicSelect>>> GetBasicDataSelect(string[] searchArray)
|
||
{
|
||
|
||
var searchList = await _dicRepository.Where(t => searchArray.Contains(t.Parent.Code) && t.ParentId != null && t.IsEnable).ProjectTo<BasicDicSelect>(_mapper.ConfigurationProvider).ToListAsync();
|
||
|
||
return searchList.GroupBy(t => t.ParentCode).ToDictionary(g => g.Key, g => g.ToList());
|
||
|
||
}
|
||
|
||
public async Task<List<BasicDicSelect>> GetBasicDataSelect(string searchKey)
|
||
{
|
||
var searchList = await _dicRepository.Where(t => t.Parent.Code== searchKey && t.ParentId != null && t.IsEnable).ProjectTo<BasicDicSelect>(_mapper.ConfigurationProvider).ToListAsync();
|
||
|
||
return searchList;
|
||
}
|
||
|
||
|
||
|
||
#region old 待废弃
|
||
|
||
/// <summary>
|
||
/// 获取项目多选字典
|
||
/// </summary>
|
||
/// <param name="searchArray">Title、Department、Rank、Position、ReadingType、Subspeciality Sponsor CROCompany ReadingStandard ReviewMode ReviewType ProjectState</param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public DicResultDTO GetDictionary(string[] searchArray)
|
||
{
|
||
var doctorViewList = _dicRepository.ProjectTo<DicViewModelDTO>(_mapper.ConfigurationProvider).OrderBy(t => t.KeyName)
|
||
.ThenBy(t => t.ShowOrder).ToList();
|
||
|
||
var projectDicResult = new DicResultDTO();
|
||
foreach (var searchItem in searchArray)
|
||
{
|
||
var item = searchItem.Trim();
|
||
var tempDic = new Dictionary<Guid, string>();
|
||
doctorViewList.Where(o => o.KeyName == item).ToList().ForEach(o => tempDic.Add(o.Id!.Value, o.Value));
|
||
projectDicResult.DicList.Add(item, tempDic);
|
||
}
|
||
return projectDicResult;
|
||
}
|
||
|
||
|
||
public DicResultDTO GetAllDictionary()
|
||
{
|
||
var list = _dicRepository.ProjectTo<DicViewModelDTO>(_mapper.ConfigurationProvider).OrderBy(t => t.KeyName)
|
||
.ThenBy(t => t.ShowOrder).ToList();
|
||
|
||
var types = list.Select(u => u.KeyName).Distinct();
|
||
|
||
var projectDicResult = new DicResultDTO();
|
||
foreach (var type in types)
|
||
{
|
||
var tempDic = new Dictionary<Guid, string>();
|
||
|
||
//list.Where(o => o.KeyName == type).ToList().ForEach(o => tempDic.Add(o.Id, string.IsNullOrEmpty(o.ValueCN)?o.Value: o.Value + " / " + o.ValueCN));
|
||
list.Where(o => o.KeyName == type).ToList().ForEach(o => tempDic.Add(o.Id!.Value, o.Value));
|
||
|
||
projectDicResult.DicList.Add(type, tempDic);
|
||
}
|
||
|
||
// //用户类型从字典表 移到另外的表了,现在为了前端不变,在这里获取,给出数据
|
||
|
||
//var userTypes= _userTypeRoleRepository.GetAll().OrderBy(t => t.Order).Select(t => new {t.Id, t.UserType}).ToList();
|
||
//var userTypeDic = new Dictionary<Guid, string>();
|
||
//userTypes.ForEach(o => userTypeDic.Add(o.Id, o.UserType));
|
||
|
||
// projectDicResult.DicList.Add("UserType", userTypeDic);
|
||
|
||
|
||
return projectDicResult;
|
||
}
|
||
|
||
/// <summary> 根据Key,获取单个字典数组 </summary>
|
||
[HttpPost]
|
||
public PageOutput<DicViewModelDTO> getDictionarySelectList(DicQueryDTO dicSearchModel)
|
||
{
|
||
|
||
|
||
var dicQueryable = _dicRepository
|
||
.WhereIf(!string.IsNullOrEmpty(dicSearchModel.KeyName), t => t.KeyName == dicSearchModel.KeyName && t.Value != "")
|
||
.ProjectTo<DicViewModelDTO>(_mapper.ConfigurationProvider);
|
||
|
||
var pageList = dicQueryable.ToPagedList(dicSearchModel.PageIndex, dicSearchModel.PageSize, dicSearchModel.SortField, dicSearchModel.Asc);
|
||
|
||
return pageList;
|
||
|
||
}
|
||
|
||
/// <summary> 根据Type、Key 获取字典 树结构 </summary>
|
||
public List<DictionaryTreeNode> GetDicTree()
|
||
{
|
||
var keyNameTypeDistinctList = _dicRepository.Where(t => t.ParentId != null)
|
||
.ProjectTo<KeyNameType>(_mapper.ConfigurationProvider).Distinct().ToList();
|
||
|
||
var treeNodeList = new List<DictionaryTreeNode>();
|
||
var group = keyNameTypeDistinctList.GroupBy(t => t.Type);
|
||
foreach (var groupItem in group)
|
||
{
|
||
var node = new DictionaryTreeNode()
|
||
{
|
||
Id = Guid.NewGuid(),
|
||
KeyName = groupItem.Key,
|
||
Type = groupItem.Key,
|
||
Children = keyNameTypeDistinctList.Where(t => t.Type == groupItem.Key).Select(t =>
|
||
new DictionaryTreeNode()
|
||
{
|
||
Id = Guid.NewGuid(),
|
||
KeyName = t.KeyName,
|
||
Type = t.Type,
|
||
Children = new List<DictionaryTreeNode>()
|
||
}).ToList()
|
||
};
|
||
treeNodeList.Add(node);
|
||
}
|
||
return treeNodeList;
|
||
}
|
||
|
||
|
||
/// <summary> 添加或更新字典数据 </summary>
|
||
//[HttpPost]
|
||
//public IResponseOutput AddOrUpdateDictionary(AddOrUpdateDicDTO viewModel)
|
||
//{
|
||
// #region 封装前
|
||
// //if (viewModel.Id == null)
|
||
// //{
|
||
// // var existItem = _dicRepository.FirstOrDefault(dic => dic.KeyName.Equals(viewModel.KeyName) && dic.Value.Equals(viewModel.Value));
|
||
// // if (existItem != null)
|
||
// // {
|
||
// // return ResponseOutput.NotOk("The added item has the same name as a sub-item of current categpry. Please modify the name.");
|
||
// // }
|
||
// // var result = _dicRepository.Add(_mapper.Map<Dictionary>(viewModel));
|
||
// // var success = _dicRepository.SaveChanges();
|
||
// // return ResponseOutput.Result(success);
|
||
// //}
|
||
// //else
|
||
// //{
|
||
// // var existItem = _dicRepository.FirstOrDefault(dic => dic.KeyName.Equals(viewModel.KeyName) && dic.Value.Equals(viewModel.Value));
|
||
// // if (existItem != null && existItem.Id != viewModel.Id)
|
||
// // {
|
||
// // return ResponseOutput.NotOk("The updated item has the same name as a sub-item of current categpry. Please modify the name.");
|
||
// // }
|
||
// // var updateItem = _dicRepository.FirstOrDefault(t => t.Id == viewModel.Id);
|
||
// // _mapper.Map(viewModel, updateItem);
|
||
// // var success = _dicRepository.SaveChanges();
|
||
// // return ResponseOutput.Result(success);
|
||
// //}
|
||
// #endregion
|
||
|
||
// var exp = new EntityVerifyExp<Dictionary>()
|
||
// {
|
||
// VerifyExp = dic => dic.KeyName.Equals(viewModel.KeyName) && dic.Value.Equals(viewModel.Value),
|
||
// VerifyMsg = "The item has the same name as a sub-item of current categpry"
|
||
// };
|
||
|
||
// //var entity = _dicRepository.UseMapper(_mapper).InsertOrUpdate(viewModel, true, exp);
|
||
|
||
// return ResponseOutput.Ok(entity.Id);
|
||
|
||
//}
|
||
|
||
|
||
/// <summary> 删除字典数据 </summary>
|
||
[HttpDelete("{id:guid}")]
|
||
public async Task<IResponseOutput> DeleteDictionary(Guid id)
|
||
{
|
||
if ((await _doctorDictionaryRepository.AnyAsync(t => t.DictionaryId == id)) ||
|
||
(await _doctorRepository.AnyAsync(t => t.SpecialityId == id|| t.PositionId == id|| t.DepartmentId == id|| t.RankId == id))
|
||
|
||
)
|
||
{
|
||
return ResponseOutput.NotOk("This item is referenced by content of the reviewer's resume.");
|
||
}
|
||
|
||
if (await _trialDictionaryRepository.AnyAsync(t => t.DictionaryId == id) ||
|
||
await _trialRepository.AnyAsync(t => t.ReviewModeId == id))
|
||
{
|
||
return ResponseOutput.NotOk("This item is referenced by content of the trial infomation.");
|
||
}
|
||
|
||
var success = await _dicRepository.DeleteFromQueryAsync(t => t.Id == id);
|
||
return ResponseOutput.Result(success);
|
||
}
|
||
|
||
/// <summary> 获取所有字典数据 </summary>
|
||
public async Task<IEnumerable<string>> getDictionarySelect()
|
||
{
|
||
return await _dicRepository.Select(t => t.KeyName).Distinct().ToListAsync();
|
||
}
|
||
|
||
//[Obsolete]
|
||
[NonDynamicMethod]
|
||
public DicViewModelDTO GetDetailById(Guid id)
|
||
{
|
||
var result = _dicRepository.ProjectTo<DicViewModelDTO>(_mapper.ConfigurationProvider).FirstOrDefault(u => u.Id == id).IfNullThrowException();
|
||
return result;
|
||
}
|
||
|
||
|
||
|
||
public TrialDicSelect GetGenerateTrialCodeDic()
|
||
{
|
||
var list = _dicRepository.Where(t => t.KeyName == "Phase" || t.KeyName == "IndicationType" || t.KeyName == "DeclarationType").ProjectTo<TrialDictionaryView>(_mapper.ConfigurationProvider).ToList();
|
||
|
||
return new TrialDicSelect()
|
||
{
|
||
Phase = list.Where(t => t.KeyName == "Phase").OrderBy(t => t.ShowOrder).ToArray(),
|
||
IndicationType = list.Where(t => t.KeyName == "IndicationType").OrderBy(t => t.ShowOrder).ToArray(),
|
||
DeclarationType = list.Where(t => t.KeyName == "DeclarationType").OrderBy(t => t.ShowOrder).ToArray()
|
||
};
|
||
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
}
|