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 ProductCodeConfigurationService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository _rep;
    public ProductCodeConfigurationService(SqlSugarRepository rep)
    {
        _rep = rep;
    }
    /// 
    /// 分页查询产码配置
    /// 
    /// 
    /// 
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task> Page(ProductCodeConfigurationInput input)
    {
        var query = _rep.AsQueryable().Where(a => !a.IsDelete)
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.CodeType.Contains(input.SearchKey.Trim())
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.CodeType), u => u.CodeType.Contains(input.CodeType.Trim()))
            .WhereIF(input.CodeTypeId>0, u => u.CodeTypeId == input.CodeTypeId)
            .WhereIF(input.CodeLength>0, u => u.CodeLength == input.CodeLength)
            .WhereIF(input.InternalCodeLength>0, u => u.InternalCodeLength == input.InternalCodeLength)
            .Select();
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }
    /// 
    /// 增加产码配置
    /// 
    /// 
    /// 
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(AddProductCodeConfigurationInput input)
    {
        var entity = input.Adapt();
        await _rep.InsertAsync(entity);
        return entity.Id;
    }
    /// 
    /// 删除产码配置
    /// 
    /// 
    /// 
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteProductCodeConfigurationInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
        //await _rep.DeleteAsync(entity);   //真删除
    }
    /// 
    /// 更新产码配置
    /// 
    /// 
    /// 
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateProductCodeConfigurationInput input)
    {
        var entity = input.Adapt();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }
    /// 
    /// 获取产码配置
    /// 
    /// 
    /// 
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task Detail([FromQuery] QueryByIdProductCodeConfigurationInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }
    /// 
    /// 获取产码配置列表
    /// 
    /// 
    /// 
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task> List()
    {
        return await _rep.AsQueryable().Where(a => !a.IsDelete).Select().ToListAsync();
    }
}