178 lines
6.7 KiB
C#
Raw Permalink Normal View History

2025-06-04 09:42:48 +08:00
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<DataDictionary> dictRepository;
private readonly IRepository<DictionaryItems> dictItemsRepository;
public SystemService(IRepository<DataDictionary> dictRepository, IRepository<DictionaryItems> dictItemsRepository)
{
this.dictRepository = dictRepository;
this.dictItemsRepository = dictItemsRepository;
}
#region
/// <summary>
/// 搜索数据字典
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<PageOutput<DictionaryInfoDto>> 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<DictionaryInfoDto>());
return new PageOutput<DictionaryInfoDto>()
{
PageIndex = input.Page,
Data = items,
PageSize = input.PageSize,
Total = pageResult.RowCount
};
}
/// <summary>
/// 编辑数据字典
/// </summary>
/// <returns></returns>
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);
}
/// <summary>
/// 添加数据字典
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 删除数据字典
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task DelDictAsync(ByCodesDeleteInput input)
{
var dicts = await dictRepository.GetListAsync(e => input.code.Contains(e.Code));
await dictRepository.DeleteManyAsync(dicts);
}
/// <summary>
/// 根据字典编号获取字典项
/// </summary>
/// <returns></returns>
public async Task<PageOutput<DictItemDto>> 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<DictItemDto>());
return new PageOutput<DictItemDto>()
{
Data = resultItems,
PageSize = input.PageSize,
SkipCount = input.SkipCount,
PageIndex = input.Page,
Total = dictItems.Count()
};
}
/// <summary>
/// 编辑字典项
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 添加字典项
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 获取字典项
/// </summary>
/// <returns></returns>
[AllowAnonymous]
public async Task<IDictionary<string, object>> 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<string, object>(e.Key, e.Value)).ToDictionary(e => e.Key, e => e.Value);
}
#endregion
}
}