175 lines
7.8 KiB
C#
175 lines
7.8 KiB
C#
using AutoMapper;
|
||
using AutoMapper.QueryableExtensions;
|
||
using IRaCIS.Application.ExpressionExtend;
|
||
using IRaCIS.Application.Interfaces;
|
||
using IRaCIS.Application.ViewModels;
|
||
using IRaCIS.Core.Application.Contracts.RequestAndResponse;
|
||
using IRaCIS.Core.Domain.Interfaces;
|
||
using IRaCIS.Core.Domain.Models;
|
||
using IRaCIS.Infra.Data.ExpressionExtend;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Linq.Expressions;
|
||
|
||
namespace IRaCIS.Application.Services
|
||
{
|
||
public class DictionaryService : IDictionaryService
|
||
{
|
||
private readonly IDictionaryRepository _dicRepository;
|
||
private readonly IDoctorDictionaryRepository _doctorDictionaryRepository;
|
||
private readonly ITrialDictionaryRepository _trialDictionaryRepository;
|
||
private readonly IMapper _mapper;
|
||
|
||
public DictionaryService(IDictionaryRepository sysDicRepository, IDoctorDictionaryRepository doctorDictionaryRepository, ITrialDictionaryRepository trialDictionaryRepository,IMapper mapper)
|
||
{
|
||
_dicRepository = sysDicRepository;
|
||
_doctorDictionaryRepository = doctorDictionaryRepository;
|
||
_trialDictionaryRepository = trialDictionaryRepository;
|
||
_mapper = mapper;
|
||
}
|
||
|
||
/// <summary> 根据字典 Key 获取字典数据 </summary>
|
||
public DicResultDTO GetDictionaryResult(string[] searchArray)
|
||
{
|
||
var doctorViewList = _dicRepository.GetAll().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, o.Value));
|
||
projectDicResult.DicList.Add(item, tempDic);
|
||
}
|
||
return projectDicResult;
|
||
}
|
||
|
||
public DicResultDTO GetAllDictionary()
|
||
{
|
||
var list = _dicRepository.GetAll().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, o.Value));
|
||
projectDicResult.DicList.Add(type, tempDic);
|
||
}
|
||
return projectDicResult;
|
||
}
|
||
|
||
/// <summary> 根据Key,获取单个字典数组 </summary>
|
||
public PageOutput<DicViewModelDTO> GetDicSelectList(DicQueryDTO dicSearchModel)
|
||
{
|
||
Expression<Func<Dictionary, bool>> dictionaryLambda = x => true;
|
||
if (!string.IsNullOrWhiteSpace(dicSearchModel.KeyName.Trim()))
|
||
{
|
||
dictionaryLambda = dictionaryLambda.And(t => t.KeyName == dicSearchModel.KeyName.Trim());
|
||
}
|
||
var dicQueryable = _dicRepository.GetAll().Where(dictionaryLambda).ProjectTo<DicViewModelDTO>(_mapper.ConfigurationProvider);
|
||
var totalCount = dicQueryable.Count();
|
||
|
||
//处理排序字段
|
||
var propName = dicSearchModel.SortField == string.Empty ? "KeyName" : dicSearchModel.SortField;
|
||
//处理升序和降序
|
||
dicQueryable = dicSearchModel.Asc ? dicQueryable.OrderBy(propName) : dicQueryable.OrderByDescending(propName);
|
||
//分页
|
||
dicQueryable = dicQueryable
|
||
.Skip((dicSearchModel.PageIndex - 1) * dicSearchModel.PageSize)
|
||
.Take(dicSearchModel.PageSize);
|
||
var list = dicQueryable.OrderBy(t => t.ShowOrder).ToList();
|
||
return new PageOutput<DicViewModelDTO>(dicSearchModel.PageIndex, dicSearchModel.PageSize, totalCount, list);
|
||
}
|
||
|
||
/// <summary> 根据Type、Key 获取字典 树结构 </summary>
|
||
public List<DictionaryTreeNode> GetDicTree()
|
||
{
|
||
var keyNameTypeDistinctList = _dicRepository.Find(u => !(u.KeyName == "UserType" || u.KeyName == "InstitutionalType")).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>
|
||
public IResponseOutput AddOrUpdateDictionary(DicViewModelDTO viewModel)
|
||
{
|
||
if (viewModel.Id == Guid.Empty)
|
||
{
|
||
var existItem = _dicRepository.FindSingleOrDefault(dic => dic.KeyName.Equals(viewModel.KeyName) && dic.Value.Equals(viewModel.Value));
|
||
if (existItem != null)
|
||
{
|
||
return ResponseOutput.NotOk("新添加项不能与当前类别下面子项同名,请修改名称");
|
||
}
|
||
var result = _dicRepository.Add(_mapper.Map<Dictionary>(viewModel));
|
||
var success = _dicRepository.SaveChanges();
|
||
return ResponseOutput.Result(success);
|
||
}
|
||
else
|
||
{
|
||
var existItem = _dicRepository.FindSingleOrDefault(dic => dic.KeyName.Equals(viewModel.KeyName) && dic.Value.Equals(viewModel.Value));
|
||
if (existItem != null && existItem.Id != viewModel.Id)
|
||
{
|
||
return ResponseOutput.NotOk("更新后的名称不能与该类别下子项同名,请修改名称");
|
||
}
|
||
var updateItem = _dicRepository.GetAll().FirstOrDefault(t => t.Id == viewModel.Id);
|
||
_mapper.Map(viewModel, updateItem);
|
||
_dicRepository.Update(updateItem);
|
||
var success = _dicRepository.SaveChanges();
|
||
return ResponseOutput.Result(success);
|
||
}
|
||
}
|
||
|
||
/// <summary> 删除字典数据 </summary>
|
||
public IResponseOutput DeleteDictionary(Guid id)
|
||
{
|
||
if (_doctorDictionaryRepository.GetAll().Any(t => t.DictionaryId == id))
|
||
{
|
||
return ResponseOutput.NotOk("医生简历表有数据关联此字典表项");
|
||
}
|
||
|
||
if (_trialDictionaryRepository.GetAll().Any(t => t.DictionaryId == id))
|
||
{
|
||
return ResponseOutput.NotOk("项目表有数据关联此字典表项");
|
||
}
|
||
|
||
var success = _dicRepository.Delete(t => t.Id == id);
|
||
return ResponseOutput.Result(success);
|
||
}
|
||
|
||
/// <summary> 获取所有字典数据 </summary>
|
||
public IEnumerable<string> GetDicSelect()
|
||
{
|
||
return _dicRepository.GetAll().Select(t => t.KeyName).Distinct().ToList();
|
||
}
|
||
|
||
public DicViewModelDTO GetDetailById(Guid id)
|
||
{
|
||
var result = _dicRepository.FindSingleOrDefault(u => u.Id == id);
|
||
return _mapper.Map<DicViewModelDTO>(result);
|
||
}
|
||
}
|
||
}
|