using JNPF.Common.Core.Manager; using JNPF.Common.Core.Manager.Files; using JNPF.Common.Enums; using JNPF.Common.Security; using JNPF.DatabaseAccessor; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using JNPF.Systems.Entitys.Dto.DictionaryData; using JNPF.Systems.Entitys.System; using JNPF.Systems.Interfaces.System; using Mapster; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using SqlSugar; namespace JNPF.Systems; /// /// 字典数据 /// 版 本:V3.2 /// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com) /// 日 期:2021-06-01. /// [ApiDescriptionSettings(Tag = "System", Name = "DictionaryData", Order = 203)] [Route("api/system/[controller]")] public class DictionaryDataService : IDictionaryDataService, IDynamicApiController, ITransient { /// /// 服务基础仓储. /// private readonly ISqlSugarRepository _repository; /// /// 字典类型服务. /// private readonly IDictionaryTypeService _dictionaryTypeService; /// /// 文件服务. /// private readonly IFileManager _fileManager; /// /// 用户管理. /// private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例. /// public DictionaryDataService( ISqlSugarRepository repository, IDictionaryTypeService dictionaryTypeService, IFileManager fileManager, IUserManager userManager) { _repository = repository; _dictionaryTypeService = dictionaryTypeService; _fileManager = fileManager; _userManager = userManager; } #region GET /// /// 获取数据字典列表. /// /// 分类id. /// 参数. /// [HttpGet("{dictionaryTypeId}")] public async Task GetList_Api(string dictionaryTypeId, [FromQuery] DictionaryDataListQuery input) { var data = await GetList(dictionaryTypeId, false); if ("1".Equals(input.isTree)) { var treeList = data.Adapt>(); if (!string.IsNullOrEmpty(input.keyword)) treeList = treeList.TreeWhere(t => t.enCode.Contains(input.keyword) || t.fullName.Contains(input.keyword), t => t.id, t => t.parentId); return new { list = treeList.ToTree() }; } else { if (!string.IsNullOrEmpty(input.keyword)) data = data.FindAll(t => t.EnCode.Contains(input.keyword) || t.FullName.Contains(input.keyword)); var treeList = data.Adapt>(); return new { list = treeList }; } } /// /// 获取所有数据字典列表(分类+内容). /// /// [HttpGet("All")] public async Task GetListAll() { var dictionaryData = await _repository.AsQueryable().Where(d => d.DeleteMark == null && d.EnabledMark == 1) .OrderBy(o => o.SortCode).OrderBy(o => o.CreatorTime, OrderByType.Desc).OrderBy(o => o.LastModifyTime, OrderByType.Desc).ToListAsync(); var dictionaryType = await _dictionaryTypeService.GetList(); var data = dictionaryType.Adapt>(); data.ForEach(dataall => { if (dataall.isTree == 1) dataall.dictionaryList = dictionaryData.FindAll(d => d.DictionaryTypeId == dataall.id).Adapt>().ToTree(); else dataall.dictionaryList = dictionaryData.FindAll(d => d.DictionaryTypeId == dataall.id).Adapt>(); }); return new { list = data }; } /// /// 获取字典分类下拉框. /// /// [HttpGet("{dictionaryTypeId}/Selector/{id}")] public async Task GetSelector(string dictionaryTypeId, string id, string isTree) { var output = new List(); var typeEntity = await _dictionaryTypeService.GetInfo(dictionaryTypeId); // 顶级节点 var dataEntity = typeEntity.Adapt(); dataEntity.id = "0"; dataEntity.parentId = "-1"; output.Add(dataEntity); if ("1".Equals(isTree)) { var dataList = (await GetList(dictionaryTypeId, false)).Adapt>(); if (!id.Equals("0")) dataList.RemoveAll(x => x.id == id); output = output.Union(dataList).ToList(); return new { list = output.ToTree("-1") }; } return new { list = output }; } /// /// 获取字典数据下拉框列表. /// /// [HttpGet("{dictionaryTypeId}/Data/Selector")] public async Task GetDataSelector(string dictionaryTypeId) { try { var isTree = (await _dictionaryTypeService.GetInfo(dictionaryTypeId)).IsTree; var datalist = await GetList(dictionaryTypeId); var treeList = datalist.Adapt>(); if (isTree == 1) { var typeEntity = await _dictionaryTypeService.GetInfo(dictionaryTypeId); // 顶级节点 var dataEntity = typeEntity.Adapt(); dataEntity.id = "0"; dataEntity.parentId = "-1"; treeList.Add(dataEntity); treeList = treeList.ToTree(); } return new { list = treeList }; } catch (Exception) { return new { list = new List() }; } } /// /// 获取数据字典信息. /// /// 主键id. /// [HttpGet("{id}/Info")] public async Task GetInfo_Api(string id) { var data = await GetInfo(id); return data.Adapt(); } /// /// 导出. /// /// /// [HttpGet("{id}/Action/Export")] public async Task ActionsExport(string id) { var output = new DictionaryDataExportInput(); await _dictionaryTypeService.GetListAllById(id, output.list); foreach (var item in output.list) { var modelList = await GetList(item.Id, false); output.modelList = output.modelList.Union(modelList).ToList(); } var jsonStr = output.ToJsonString(); return await _fileManager.Export(jsonStr, (await _dictionaryTypeService.GetInfo(id)).FullName, ExportFileType.bdd); } #endregion #region Post /// /// 添加. /// /// /// [HttpPost("")] public async Task Creater([FromBody] DictionaryDataCrInput input) { if (await _repository.IsAnyAsync(x => x.EnCode == input.enCode && x.DictionaryTypeId == input.dictionaryTypeId && x.DeleteMark == null) || await _repository.IsAnyAsync(x => x.FullName == input.fullName && x.DictionaryTypeId == input.dictionaryTypeId && x.DeleteMark == null)) throw Oops.Oh(ErrorCode.D3003); var entity = input.Adapt(); var isOk = await _repository.AsInsertable(entity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync(); if (isOk < 1) throw Oops.Oh(ErrorCode.COM1000); } /// /// 修改. /// /// /// /// [HttpPut("{id}")] public async Task Update(string id, [FromBody] DictionaryDataUpInput input) { if (await _repository.IsAnyAsync(x => x.EnCode == input.enCode && x.DictionaryTypeId == input.dictionaryTypeId && x.Id != id && x.DeleteMark == null) || await _repository.IsAnyAsync(x => x.Id != id && x.FullName == input.fullName && x.DictionaryTypeId == input.dictionaryTypeId && x.DeleteMark == null)) throw Oops.Oh(ErrorCode.D3003); var entity = input.Adapt(); var isOk = await _repository.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandHasChangeAsync(); if (!isOk) throw Oops.Oh(ErrorCode.COM1001); } /// /// 删除. /// /// /// [HttpDelete("{id}")] public async Task Delete(string id) { if (!await _repository.IsAnyAsync(x => x.Id == id && x.DeleteMark == null)) throw Oops.Oh(ErrorCode.D3004); if (await _repository.IsAnyAsync(o => o.ParentId.Equals(id) && o.DeleteMark == null)) throw Oops.Oh(ErrorCode.D3002); var isOk = await _repository.AsUpdateable().SetColumns(it => new DictionaryDataEntity() { DeleteTime = DateTime.Now, DeleteMark = 1, DeleteUserId = _userManager.UserId }).Where(x => x.Id == id).ExecuteCommandHasChangeAsync(); if (!isOk) throw Oops.Oh(ErrorCode.COM1002); } /// /// 更新字典状态. /// /// id. /// [HttpPut("{id}/Actions/State")] public async Task ActionsState(string id) { var isOk = await _repository.AsUpdateable().SetColumns(it => new DictionaryDataEntity() { EnabledMark = SqlFunc.IIF(it.EnabledMark == 0, 1, 0), LastModifyTime = DateTime.Now, LastModifyUserId = _userManager.UserId }).Where(x => x.Id == id).ExecuteCommandHasChangeAsync(); if (!isOk) throw Oops.Oh(ErrorCode.D1506); } /// /// 导入. /// /// /// [HttpPost("Action/Import")] [UnitOfWork] public async Task ActionsImport(IFormFile file) { var fileType = Path.GetExtension(file.FileName).Replace(".", string.Empty); if (!fileType.ToLower().Equals(ExportFileType.bdd.ToString())) throw Oops.Oh(ErrorCode.D3006); var josn = _fileManager.Import(file); var model = josn.ToObject(); if (model == null || model.list.Count == 0) throw Oops.Oh(ErrorCode.D3006); if (model.list.Find(x => x.ParentId == "-1") == null && !_dictionaryTypeService.IsExistParent(model.list)) throw Oops.Oh(ErrorCode.D3007); await ImportData(model); } #endregion #region PulicMethod /// /// 列表. /// /// 类别主键. /// 是否过滤启用状态. /// [NonAction] public async Task> GetList(string dictionaryTypeId, bool enabledMark = true) { var entity = await _dictionaryTypeService.GetInfo(dictionaryTypeId); return await _repository.AsQueryable().Where(d => d.DictionaryTypeId == entity.Id && d.DeleteMark == null).WhereIF(enabledMark, d => d.EnabledMark == 1) .OrderBy(o => o.SortCode).OrderBy(o => o.CreatorTime, OrderByType.Desc).OrderBy(o => o.LastModifyTime, OrderByType.Desc).ToListAsync(); } /// /// 信息. /// /// 主键值. /// [NonAction] public async Task GetInfo(string id) { return await _repository.GetFirstAsync(x => x.Id == id && x.DeleteMark == null); } #endregion #region PrivateMethod /// /// 导入数据. /// /// /// private async Task ImportData(DictionaryDataExportInput inputList) { var isOk = await _repository.AsSugarClient().Storageable(inputList.list).ExecuteCommandAsync(); var isOk1 = await _repository.AsSugarClient().Storageable(inputList.modelList).ExecuteCommandAsync(); if (isOk < 1 && isOk1 < 1) throw Oops.Oh(ErrorCode.D3008); } #endregion }