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 PrintLabelService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
public PrintLabelService(SqlSugarRepository rep)
{
_rep = rep;
}
///
/// 分页查询标签打印
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task> Page(PrintLabelInput input)
{
var query = _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.Unit.Contains(input.SearchKey.Trim())
|| u.PackageUnit.Contains(input.SearchKey.Trim())
)
.WhereIF(input.TemplateId>0, u => u.TemplateId == input.TemplateId)
.WhereIF(input.Count>0, u => u.Count == input.Count)
.WhereIF(!string.IsNullOrWhiteSpace(input.Unit), u => u.Unit.Contains(input.Unit.Trim()))
.WhereIF(input.LabelCount>0, u => u.LabelCount == input.LabelCount)
.WhereIF(input.PackageCount>0, u => u.PackageCount == input.PackageCount)
.WhereIF(!string.IsNullOrWhiteSpace(input.PackageUnit), u => u.PackageUnit.Contains(input.PackageUnit.Trim()))
.WhereIF(input.Batch>0, u => u.Batch == input.Batch)
.Select();
if(input.ProductDateRange != null && input.ProductDateRange.Count >0)
{
DateTime? start= input.ProductDateRange[0];
query = query.WhereIF(start.HasValue, u => u.ProductDate > start);
if (input.ProductDateRange.Count >1 && input.ProductDateRange[1].HasValue)
{
var end = input.ProductDateRange[1].Value.AddDays(1);
query = query.Where(u => u.ProductDate < end);
}
}
if(input.EndDateRange != null && input.EndDateRange.Count >0)
{
DateTime? start= input.EndDateRange[0];
query = query.WhereIF(start.HasValue, u => u.EndDate > start);
if (input.EndDateRange.Count >1 && input.EndDateRange[1].HasValue)
{
var end = input.EndDateRange[1].Value.AddDays(1);
query = query.Where(u => u.EndDate < end);
}
}
return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
}
///
/// 增加标签打印
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task Add(AddPrintLabelInput input)
{
var entity = input.Adapt();
await _rep.InsertAsync(entity);
return entity.Id;
}
///
/// 删除标签打印
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeletePrintLabelInput 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(UpdatePrintLabelInput input)
{
var entity = input.Adapt();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 获取标签打印
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task Detail([FromQuery] QueryByIdPrintLabelInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
///
/// 获取标签打印列表
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task> List()
{
return await _rep.AsQueryable().Select().ToListAsync();
}
}