DigitalFactory/Admin.NET/Admin.NET.Application/Service/PrintLabel/PrintLabelService.cs

134 lines
4.6 KiB
C#
Raw Normal View History

2024-03-20 02:37:36 +00:00
using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
/// <summary>
/// 标签打印服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class PrintLabelService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<PrintLabel> _rep;
public PrintLabelService(SqlSugarRepository<PrintLabel> rep)
{
_rep = rep;
}
/// <summary>
/// 分页查询标签打印
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<PrintLabelOutput>> 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<PrintLabelOutput>();
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);
}
/// <summary>
/// 增加标签打印
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task<long> Add(AddPrintLabelInput input)
{
var entity = input.Adapt<PrintLabel>();
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 删除标签打印
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[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); //真删除
}
/// <summary>
/// 更新标签打印
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Update")]
public async Task Update(UpdatePrintLabelInput input)
{
var entity = input.Adapt<PrintLabel>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 获取标签打印
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<PrintLabel> Detail([FromQuery] QueryByIdPrintLabelInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
/// <summary>
/// 获取标签打印列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task<List<PrintLabelOutput>> List()
{
return await _rep.AsQueryable().Select<PrintLabelOutput>().ToListAsync();
}
}