using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using iWare.Wms.Core;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;
namespace iWare.Wms.Application
{
///
/// 生产计划表服务
///
[ApiDescriptionSettings("基础数据管理", Name = "ProductionPlanInfo", Order = 100)]
[Route("api/[Controller]")]
public class ProductionPlanInfoService : IProductionPlanInfoService, IDynamicApiController, ITransient
{
private readonly IRepository _productionPlanInfoRep;
public ProductionPlanInfoService(
IRepository productionPlanInfoRep
)
{
_productionPlanInfoRep = productionPlanInfoRep;
}
///
/// 分页查询生产计划表
///
///
///
[HttpGet("page")]
public async Task> Page([FromQuery] ProductionPlanInfoSearch input)
{
var productionPlanInfos = await _productionPlanInfoRep.DetachedEntities
.Where(!string.IsNullOrEmpty(input.PlanType), u => u.PlanType == input.PlanType)
.Where(input.PlanTime != null, u => u.PlanTime == input.PlanTime)
.Where(!string.IsNullOrEmpty(input.TeamType), u => u.TeamType == input.TeamType)
.Where(input.PlanProductionNum != null, u => u.PlanProductionNum == input.PlanProductionNum)
.Where(!string.IsNullOrEmpty(input.Remarks), u => u.Remarks == input.Remarks)
.OrderByDescending(o=>o.PlanTime)
.ProjectToType()
.ToADPagedListAsync(input.PageNo, input.PageSize);
return productionPlanInfos;
}
///
/// 增加生产计划表
///
///
///
[HttpPost("add")]
public async Task Add(AddProductionPlanInfoInput input)
{
if (input==null)
{
throw Oops.Oh("传参不允许为空");
}
if (input.PlanType.Equals("1"))
{
var shiftInfo = await _productionPlanInfoRep.FirstOrDefaultAsync(x => x.PlanType == input.PlanType&&x.PlanTime.Date==input.PlanTime&&x.TeamType.Equals(input.TeamType) && x.IsDeleted == false);
if (shiftInfo != null)
{
throw Oops.Oh($"班次[{input.TeamType}]在[{input.PlanTime}]中已存在");
}
}else if (input.PlanType.Equals("2"))
{
var shiftInfo = await _productionPlanInfoRep.FirstOrDefaultAsync(x => x.PlanType == input.PlanType && x.PlanTime.Date == input.PlanTime && x.IsDeleted == false);
if (shiftInfo != null)
{
throw Oops.Oh($"[{input.PlanTime}]计划已存在");
}
}
else
{
throw Oops.Oh("参数计划类型传入不正确!");
}
var productionPlanInfo = input.Adapt();
await _productionPlanInfoRep.InsertAsync(productionPlanInfo);
}
///
/// 删除生产计划表
///
///
///
[HttpPost("delete")]
public async Task Delete(DeleteProductionPlanInfoInput input)
{
var productionPlanInfo = await _productionPlanInfoRep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _productionPlanInfoRep.DeleteAsync(productionPlanInfo);
}
///
/// 更新生产计划表
///
///
///
[HttpPost("edit")]
public async Task Update(UpdateProductionPlanInfoInput input)
{
var isExist = await _productionPlanInfoRep.AnyAsync(u => u.Id == input.Id, false);
if (!isExist) throw Oops.Oh(ErrorCode.D3000);
var productionPlanInfo = input.Adapt();
await _productionPlanInfoRep.UpdateAsync(productionPlanInfo,ignoreNullValues:true);
}
///
/// 获取生产计划表
///
///
///
[HttpGet("detail")]
public async Task Get([FromQuery] QueryeProductionPlanInfoInput input)
{
return (await _productionPlanInfoRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id)).Adapt();
}
///
/// 获取生产计划表列表
///
///
///
[HttpGet("list")]
public async Task> List([FromQuery] ProductionPlanInfoInput input)
{
return await _productionPlanInfoRep.DetachedEntities.ProjectToType().ToListAsync();
}
}
}