You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Core/Service/Dict/SysDictDataService.cs

242 lines
8.0 KiB
C#

2 years ago

using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Myshipping.Core.Entity;
2 years ago
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2 years ago
using Myshipping.Core.Service.Dict.Dto;
2 years ago
namespace Myshipping.Core.Service;
2 years ago
/// <summary>
/// 字典值服务
/// </summary>
[ApiDescriptionSettings(Name = "DictData", Order = 100)]
public class SysDictDataService : ISysDictDataService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<SysDictData> _sysDictDataRep; // 字典类型表仓储
2 years ago
private readonly ISysCacheService _cache;
public SysDictDataService(SqlSugarRepository<SysDictData> sysDictDataRep, ISysCacheService cache)
2 years ago
{
_sysDictDataRep = sysDictDataRep;
2 years ago
_cache = cache;
2 years ago
}
/// <summary>
/// 分页查询字典值
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/sysDictData/page")]
public async Task<dynamic> QueryDictDataPageList([FromQuery] DictDataInput input)
{
var code = !string.IsNullOrEmpty(input.Code?.Trim());
var value = !string.IsNullOrEmpty(input.Value?.Trim());
var dictDatas = await _sysDictDataRep
.Where(u => u.TypeId == input.TypeId)
.WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code.Contains(input.Code.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Value), u => u.Code.Contains(input.Value.Trim()))
.Where(u => u.Status != CommonStatus.DELETED).OrderBy(u => u.Sort)
.Select<DictDataOutput>()
.ToPagedListAsync(input.PageNo, input.PageSize);
return dictDatas.XnPagedResult();
}
/// <summary>
/// 获取某个字典类型下字典值列表
/// </summary>
/// <returns></returns>
[HttpGet("/sysDictData/list")]
public async Task<dynamic> GetDictDataList([FromQuery] QueryDictDataListInput input)
{
return await _sysDictDataRep.AsQueryable()
.WhereIF(input.TypeId > 0, u => u.TypeId == input.TypeId)
.Where(u => u.Status != CommonStatus.DELETED).OrderBy(u => u.Sort).ToListAsync();
}
/// <summary>
/// 增加字典值
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/sysDictData/add")]
public async Task AddDictData(AddDictDataInput input)
{
var isExist = await _sysDictDataRep.AnyAsync(u => (u.Code == input.Code || u.Value == input.Value) && u.TypeId == input.TypeId);
if (isExist) throw Oops.Oh(ErrorCode.D3003);
var dictData = input.Adapt<SysDictData>();
await _sysDictDataRep.InsertAsync(dictData);
2 years ago
2 years ago
await CacheData();
2 years ago
2 years ago
}
/// <summary>
/// 删除字典值
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/sysDictData/delete")]
public async Task DeleteDictData(DeleteDictDataInput input)
{
var dictData = await _sysDictDataRep.FirstOrDefaultAsync(u => u.Id == input.Id);
if (dictData == null) throw Oops.Oh(ErrorCode.D3004);
await _sysDictDataRep.DeleteAsync(dictData);
2 years ago
2 years ago
await CacheData();
2 years ago
}
/// <summary>
/// 更新字典值
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/sysDictData/edit")]
public async Task UpdateDictData(UpdateDictDataInput input)
{
var isExist = await _sysDictDataRep.AnyAsync(u => u.Id == input.Id);
if (!isExist) throw Oops.Oh(ErrorCode.D3004);
// 排除自己并且判断与其他是否相同
isExist = await _sysDictDataRep.AnyAsync(u => (u.Code == input.Code) && u.TypeId == input.TypeId && u.Id != input.Id);
2 years ago
if (isExist) throw Oops.Oh(ErrorCode.D3003);
var dictData = input.Adapt<SysDictData>();
await _sysDictDataRep.AsUpdateable(dictData).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
2 years ago
2 years ago
await CacheData();
2 years ago
}
/// <summary>
/// 字典值详情
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/sysDictData/detail")]
public async Task<dynamic> GetDictData([FromQuery] QueryDictDataInput input)
{
return await _sysDictDataRep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
/// <summary>
/// 修改字典值状态
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/sysDictData/changeStatus")]
public async Task ChangeDictDataStatus(UpdateDictDataInput input)
{
var dictData = await _sysDictDataRep.FirstOrDefaultAsync(u => u.Id == input.Id);
if (dictData == null) throw Oops.Oh(ErrorCode.D3004);
if (!Enum.IsDefined(typeof(CommonStatus), input.Status))
throw Oops.Oh(ErrorCode.D3005);
dictData.Status = input.Status;
2 years ago
2 years ago
await CacheData();
2 years ago
}
/// <summary>
/// 根据字典类型Id获取字典值集合
/// </summary>
/// <param name="dictTypeId"></param>
/// <returns></returns>
[NonAction]
public async Task<dynamic> GetDictDataListByDictTypeId(long dictTypeId)
{
return await _sysDictDataRep.Where(u => u.TypeId == dictTypeId)
.Where(u => u.Status != CommonStatus.DELETED).OrderBy(u => u.Sort)
.Select(u => new
{
u.Code,
u.Value
}).ToListAsync();
}
/// <summary>
/// 删除字典下所有值
/// </summary>
/// <param name="dictTypeId"></param>
[NonAction]
public async Task DeleteByTypeId(long dictTypeId)
{
await _sysDictDataRep.DeleteAsync(u => u.TypeId == dictTypeId);
2 years ago
2 years ago
await CacheData();
}
/// <summary>
/// 根据字典Code返回字典值列表
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
[NonAction]
public async Task<List<SysDictData>> GetDictDataByCode(string code)
{
var payment = await _sysDictDataRep.AsQueryable().InnerJoin<SysDictType>((d, t) => d.TypeId == t.Id).Where((d, t) => t.Code == code).Select<SysDictData>().ToListAsync();
return payment;
}
/// <summary>
/// 缓存数据
/// </summary>
/// <returns></returns>
/// <exception cref="System.NotImplementedException"></exception>
[NonAction]
2 years ago
public async Task CacheData(bool falg=false)
2 years ago
{
2 years ago
if (falg)
2 years ago
{
2 years ago
if (!_cache.Exists(CommonConst.CACHE_KEY_COMMON_DB_DICT_DATA))
{
var list = await _sysDictDataRep.AsQueryable().InnerJoin<SysDictType>((d, t) => d.TypeId == t.Id).Filter(null, true).Where((d, t) => t.Status == CommonStatus.ENABLE && d.Status == CommonStatus.ENABLE).Select((d, t) => new DictDataDto
{
Id = d.Id,
TypeName = t.Name,
TypeCode = t.Code,
Value = d.Value,
Code = d.Code,
Sort = d.Sort,
Remark = d.Remark,
IsFront = t.IsFront
}).ToListAsync();
await _cache.SetAllDictData(list);
}
} else {
var list = await _sysDictDataRep.AsQueryable().InnerJoin<SysDictType>((d, t) => d.TypeId == t.Id).Filter(null, true).Where((d, t) => t.Status == CommonStatus.ENABLE && d.Status == CommonStatus.ENABLE).Select((d, t) => new DictDataDto
{
Id = d.Id,
TypeName = t.Name,
TypeCode = t.Code,
Value = d.Value,
Code = d.Code,
Sort = d.Sort,
Remark = d.Remark,
IsFront = t.IsFront
}).ToListAsync();
await _cache.SetAllDictData(list);
}
2 years ago
2 years ago
}
2 years ago
2 years ago
}