using JNPF.Common.Enums;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
using JNPF.VisualData.Entity;
using JNPF.VisualData.Entitys.Dto.ScreenCategory;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Yitter.IdGenerator;
namespace JNPF.VisualData;
///
/// 业务实现:大屏.
///
[ApiDescriptionSettings(Tag = "BladeVisual", Name = "category", Order = 160)]
[Route("api/blade-visual/[controller]")]
public class ScreenCategoryService : IDynamicApiController, ITransient
{
///
/// 服务基础仓储.
///
private readonly ISqlSugarRepository _visualCategoryRepository;
///
/// 初始化一个类型的新实例.
///
public ScreenCategoryService(ISqlSugarRepository visualCategoryRepository)
{
_visualCategoryRepository = visualCategoryRepository;
}
#region Get
///
/// 获取大屏分类分页列表.
///
///
///
[HttpGet("page")]
public async Task GetPagetList([FromQuery] ScreenCategoryListQueryInput input)
{
SqlSugarPagedList? data = await _visualCategoryRepository.AsQueryable().Where(v => v.IsDeleted == 0).Select(v => new ScreenCategoryListOutput { id = v.Id, categoryKey = v.CategoryKey, categoryValue = v.CategoryValue, isDeleted = v.IsDeleted }).ToPagedListAsync(input.current, input.size);
return new { current = data.pagination.CurrentPage, pages = data.pagination.Total / data.pagination.PageSize, records = data.list, size = data.pagination.PageSize, total = data.pagination.Total };
}
///
/// 获取大屏分类列表.
///
///
[HttpGet("list")]
public async Task GetList([FromQuery] ScreenCategoryListQueryInput input)
{
SqlSugarPagedList? list = await _visualCategoryRepository.AsQueryable().Where(v => v.IsDeleted == 0)
.Select(v => new ScreenCategoryListOutput
{
id = v.Id,
categoryKey = v.CategoryKey,
categoryValue = v.CategoryValue,
isDeleted = v.IsDeleted
}).ToPagedListAsync(input.current, input.size);
return list.list;
}
///
/// 详情.
///
///
///
[HttpGet("detail")]
public async Task GetInfo(string id)
{
VisualCategoryEntity? entity = await _visualCategoryRepository.AsQueryable().FirstAsync(v => v.Id == id);
return entity.Adapt();
}
#endregion
#region Post
///
/// 新增.
///
///
///
[HttpPost("save")]
public async Task Create([FromBody] ScreenCategoryCrInput input)
{
bool isExist = await _visualCategoryRepository.IsAnyAsync(v => v.CategoryValue == input.categoryValue && v.IsDeleted == 0);
if (isExist) throw Oops.Oh(ErrorCode.D2200);
VisualCategoryEntity? entity = input.Adapt();
entity.IsDeleted = 0;
entity.Id = YitIdHelper.NextId().ToString();
int isOk = await _visualCategoryRepository.AsInsertable(entity).ExecuteCommandAsync();
if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1000);
}
///
/// 修改.
///
///
///
[HttpPost("update")]
public async Task Update([FromBody] ScreenCategoryUpInput input)
{
bool isExist = await _visualCategoryRepository.IsAnyAsync(v => v.CategoryValue == input.categoryValue && v.Id != input.Id && v.IsDeleted == 0);
if (isExist) throw Oops.Oh(ErrorCode.D2200);
VisualCategoryEntity? entity = input.Adapt();
int isOk = await _visualCategoryRepository.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1001);
}
///
/// 删除.
///
///
///
[HttpPost("remove")]
public async Task Delete(string ids)
{
List? entity = await _visualCategoryRepository.AsQueryable().In(v => v.Id, ids.Split(',').ToArray()).Where(v => v.IsDeleted == 0).ToListAsync();
_ = entity ?? throw Oops.Oh(ErrorCode.COM1005);
int isOk = await _visualCategoryRepository.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.Delete()).ExecuteCommandAsync();
if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1002);
}
#endregion
}