using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
/// 
/// 码元素服务
/// 
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class CodeElementService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository _rep;
    public CodeElementService(SqlSugarRepository rep)
    {
        _rep = rep;
    }
    /// 
    /// 分页查询码元素
    /// 
    /// 
    /// 
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task> Page(CodeElementInput input)
    {
        var query = _rep.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.CodeName.Contains(input.SearchKey.Trim())
                || u.ExportFormatExample.Contains(input.SearchKey.Trim())
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.CodeName), u => u.CodeName.Contains(input.CodeName.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.ExportFormatExample), u => u.ExportFormatExample.Contains(input.ExportFormatExample.Trim()))
            .Select();
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }
    /// 
    /// 增加码元素
    /// 
    /// 
    /// 
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(AddCodeElementInput input)
    {
        var entity = input.Adapt();
        await _rep.InsertAsync(entity);
        return entity.Id;
    }
    /// 
    /// 删除码元素
    /// 
    /// 
    /// 
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteCodeElementInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        //var entity = await _rep.GetFirstAsync(u => u.CodeLength == input.CodeLength) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        //await _rep.FakeDeleteAsync(entity);   //假删除
        await _rep.DeleteAsync(entity);   //真删除
    }
    /// 
    /// 更新码元素
    /// 
    /// 
    /// 
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateCodeElementInput input)
    {
        var entity = input.Adapt();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }
    /// 
    /// 获取码元素
    /// 
    /// 
    /// 
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task Detail([FromQuery] QueryByIdCodeElementInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
        //return await _rep.GetFirstAsync(u => u.CodeLength == input.CodeLength);
    }
    /// 
    /// 获取码元素
    /// 
    /// 
    /// 
    [HttpGet]
    [ApiDescriptionSettings(Name = "ElementByName")]
    public async Task GetElementByName([FromQuery] string input,long? unitGroupId)
    {
        return await _rep.GetFirstAsync(u => u.UnitGroupId == unitGroupId && u.CodeName == input);
    }
    /// 
    /// 获取码元素列表
    /// 
    /// 
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task> List()
    {
        return await _rep.AsQueryable().Where(a => !a.IsDelete).Select().ToListAsync();
    }
    /// 
    /// 获取码包配置列表
    /// 
    /// 
    [HttpGet]
    [ApiDescriptionSettings(Name = "ListByGroupId")]
    public async Task> ListByGroupId(long? unitGroupId)
    {
        var list = await _rep.AsQueryable().Where(a => a.UnitGroupId == unitGroupId && !a.IsDelete).Select().ToListAsync();
        return list;
    }
}