using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using GDZZ.Core.Entity;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GDZZ.Core.Service;
///
/// 字典值服务
///
[ApiDescriptionSettings(Name = "DictData", Order = 100)]
public class SysDictDataService : ISysDictDataService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _sysDictDataRep; // 字典类型表仓储
public SysDictDataService(SqlSugarRepository sysDictDataRep)
{
_sysDictDataRep = sysDictDataRep;
}
///
/// 分页查询字典值
///
///
///
[HttpGet("/sysDictData/page")]
public async Task 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.Value.Contains(input.Value.Trim()))
.Where(u => u.Status != CommonStatus.DELETED).OrderBy(u => u.Sort)
.Select()
.ToPagedListAsync(input.PageNo, input.PageSize);
return dictDatas.XnPagedResult();
}
///
/// 获取某个字典类型下字典值列表
///
///
[HttpGet("/sysDictData/list")]
public async Task 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();
}
///
/// 增加字典值
///
///
///
[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();
await _sysDictDataRep.InsertAsync(dictData);
}
///
/// 删除字典值
///
///
///
[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);
}
///
/// 更新字典值
///
///
///
[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.Value == input.Value || u.Code == input.Code) && u.TypeId == input.TypeId && u.Id != input.Id);
if (isExist) throw Oops.Oh(ErrorCode.D3003);
var dictData = input.Adapt();
await _sysDictDataRep.AsUpdateable(dictData).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 字典值详情
///
///
///
[HttpGet("/sysDictData/detail")]
public async Task GetDictData([FromQuery] QueryDictDataInput input)
{
return await _sysDictDataRep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
///
/// 修改字典值状态
///
///
///
[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;
}
///
/// 根据字典类型Id获取字典值集合
///
///
///
[NonAction]
public async Task 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();
}
///
/// 删除字典下所有值
///
///
[NonAction]
public async Task DeleteByTypeId(long dictTypeId)
{
await _sysDictDataRep.DeleteAsync(u => u.TypeId == dictTypeId);
}
///
/// 根据字典Code返回字典值列表
///
///
///
[NonAction]
public async Task> GetDictDataByCode(string code)
{
var payment = await _sysDictDataRep.AsQueryable().InnerJoin((d, t) => d.TypeId == t.Id).Where((d, t) => t.Code == code).Select().ToListAsync();
return payment;
}
}