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 ProductionTasksService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
public ProductionTasksService(SqlSugarRepository rep)
{
_rep = rep;
}
///
/// 分页查询生产任务单
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task> Page(ProductionTasksInput input)
{
var query = _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.OddNumber.Contains(input.SearchKey.Trim())
|| u.ProductType.Contains(input.SearchKey.Trim())
|| u.Name.Contains(input.SearchKey.Trim())
|| u.CodeNum.Contains(input.SearchKey.Trim())
|| u.Specifications.Contains(input.SearchKey.Trim())
|| u.ProductionLine.Contains(input.SearchKey.Trim())
|| u.Remarks.Contains(input.SearchKey.Trim())
)
.WhereIF(!string.IsNullOrWhiteSpace(input.OddNumber), u => u.OddNumber.Contains(input.OddNumber.Trim()))
.WhereIF(input.State>0, u => u.State == input.State)
.WhereIF(!string.IsNullOrWhiteSpace(input.ProductType), u => u.ProductType.Contains(input.ProductType.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeNum), u => u.CodeNum.Contains(input.CodeNum.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Specifications), u => u.Specifications.Contains(input.Specifications.Trim()))
.WhereIF(input.Batch>0, u => u.Batch == input.Batch)
.WhereIF(!string.IsNullOrWhiteSpace(input.ProductionLine), u => u.ProductionLine.Contains(input.ProductionLine.Trim()))
.Select();
if(input.PlanStartDateRange != null && input.PlanStartDateRange.Count >0)
{
DateTime? start= input.PlanStartDateRange[0];
query = query.WhereIF(start.HasValue, u => u.PlanStartDate > start);
if (input.PlanStartDateRange.Count >1 && input.PlanStartDateRange[1].HasValue)
{
var end = input.PlanStartDateRange[1].Value.AddDays(1);
query = query.Where(u => u.PlanStartDate < end);
}
}
if(input.StartDateRange != null && input.StartDateRange.Count >0)
{
DateTime? start= input.StartDateRange[0];
query = query.WhereIF(start.HasValue, u => u.StartDate > start);
if (input.StartDateRange.Count >1 && input.StartDateRange[1].HasValue)
{
var end = input.StartDateRange[1].Value.AddDays(1);
query = query.Where(u => u.StartDate < 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(AddProductionTasksInput input)
{
var entity = input.Adapt();
await _rep.InsertAsync(entity);
return entity.Id;
}
///
/// 删除生产任务单
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteProductionTasksInput 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(UpdateProductionTasksInput input)
{
var entity = input.Adapt();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 获取生产任务单
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task Detail([FromQuery] QueryByIdProductionTasksInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
///
/// 获取生产任务单列表
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task> List()
{
return await _rep.AsQueryable().Select().ToListAsync();
}
}