using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Syc.Basic.Web.Dtos; using Syc.Basic.Web.Dtos.Common; using Syc.Basic.Web.Dtos.Dict; using Syc.Basic.Web.WMS.Dtos.Organization; using Syc.Basic.Web.WMS.Entitys; using Syc.Basic.Web.WMS.IServices; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; using System.Text; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; namespace Syc.Basic.Web.Service { public class SystemService : ApiService { private readonly IRepository dictRepository; private readonly IRepository dictItemsRepository; public SystemService(IRepository dictRepository, IRepository dictItemsRepository) { this.dictRepository = dictRepository; this.dictItemsRepository = dictItemsRepository; } #region 数据字典 /// /// 搜索数据字典 /// /// [HttpGet] public async Task> SearchDictAsync(SearchDictInput input) { var query = await dictRepository.GetQueryableAsync(); var pageResult = query.WhereIf(!input.title.IsNullOrWhiteSpace(), e => input.title.Contains(e.Title)) .PageResult(input.Page, input.PageSize); var items = ObjectMapper.Map(pageResult.Queryable.ToList(), new List()); return new PageOutput() { PageIndex = input.Page, Data = items, PageSize = input.PageSize, Total = pageResult.RowCount }; } /// /// 编辑数据字典 /// /// public async Task EditDictAsync(DictionaryInfoDto input) { if (await dictRepository.AnyAsync(e => e.Key == input.key && e.Code != input.code)) throw Oops.Oh($"Key:{input.key} 已存在,请勿重复使用"); var dict = await dictRepository.FirstOrDefaultAsync(e => e.Code == input.code); if (dict is null) throw Oops.Oh($"未找到 {input.code} 的数据"); dict.Title = input.title; dict.Key = input.key; dict.Description = input.desc; dict.Status = input.status; await dictRepository.UpdateAsync(dict); } /// /// 添加数据字典 /// /// /// public async Task AddDictAsync(DictionaryInfoDto input) { if (await dictRepository.AnyAsync(e => e.Key == input.key)) throw Oops.Oh($"Key:{input.key} 已存在,请勿重复使用"); var dict = ObjectMapper.Map(input, new DataDictionary()); dict.Createtime = DateTime.Now; dict.Status = input.status; dict.Code = Guid.NewGuid().ToString("N"); await dictRepository.InsertAsync(dict); } /// /// 删除数据字典 /// /// /// /// public async Task DelDictAsync(ByCodesDeleteInput input) { var dicts = await dictRepository.GetListAsync(e => input.code.Contains(e.Code)); await dictRepository.DeleteManyAsync(dicts); } /// /// 根据字典编号获取字典项 /// /// public async Task> GetDictItemsAsync(GetDictItemsInput input) { var dict = (await dictRepository.WithDetailsAsync(e => e.DictionaryItems)).FirstOrDefault(e => e.Code == input.code); if (dict is null) throw Oops.Oh($"未在数据字典中找到: {input.code} "); var dictItems = dict.DictionaryItems.Skip(input.SkipCount).Take(input.PageSize); var resultItems = ObjectMapper.Map(dictItems, new List()); return new PageOutput() { Data = resultItems, PageSize = input.PageSize, SkipCount = input.SkipCount, PageIndex = input.Page, Total = dictItems.Count() }; } /// /// 编辑字典项 /// /// /// public async Task EditDictItemAsync(DictItemDto input) { var entity = await dictItemsRepository.FirstOrDefaultAsync(e => e.DictionaryCode == input.dictCode && e.Id == input.Id); if (entity is null) throw Oops.Oh("字典项不存在或已被删除"); entity.Value = input.value; entity.SubValue = input.subValue; entity.Key = input.key; entity.Status = input.status; await dictItemsRepository.UpdateAsync(entity); } /// /// 添加字典项 /// /// /// public async Task AddDictItemAsync(DictItemDto input) { if (await dictItemsRepository.AnyAsync(e => e.DictionaryCode == input.dictCode && e.Key == input.key)) throw Oops.Oh("字典中已存在相同字段的项"); var dictItem = ObjectMapper.Map(input, new DictionaryItems()); dictItem.Createtime = DateTime.Now; dictItem.Status = true; await dictItemsRepository.InsertAsync(dictItem); } public async Task DelDictItemAsync(ByIdsDeleteInput input) { var items = await dictItemsRepository.GetListAsync(e => input.Ids.Contains(e.Id)); await dictItemsRepository.DeleteManyAsync(items); } /// /// 获取字典项 /// /// [AllowAnonymous] public async Task> GetItemAsync(Abp.Application.Contracts.ByCodeInput input) { var dictEntity = await dictRepository.FirstOrDefaultAsync(e => e.Key == input.Code); if (dictEntity is null) throw Oops.Oh("未找到字典项"); var entitys = await dictItemsRepository.GetListAsync(e => e.DictionaryCode == dictEntity.Code); return entitys.Select(e => new KeyValuePair(e.Key, e.Value)).ToDictionary(e => e.Key, e => e.Value); } #endregion 数据字典 } }