using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.Extras.iWare.Wms.Util.LowCode.Front.Code;
using Furion.FriendlyException;
using iWare.Wms.Core;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SharpYaml;
using System.Linq.Dynamic.Core;
namespace iWare.Wms.Application
{
///
/// 工件出库信息服务
///
//[Route("api")]
[ApiDescriptionSettings("自己的业务", Name = "WorkPieceOutbound", Order = 100)]
[Route("api/[Controller]")]
[DisableOpLog]
public class WorkPieceOutboundService : IWorkPieceOutboundService, IDynamicApiController, ITransient
{
private readonly IRepository _workPieceOutboundRep;
private readonly IRepository _workPieceInfoRep;
private readonly IRepository _workPieceOutboundRecordRep;
public WorkPieceOutboundService(
IRepository workPieceOutboundRecordRep,
IRepository workPieceInfoRep,
IRepository workPieceOutboundRep
)
{
_workPieceOutboundRecordRep = workPieceOutboundRecordRep;
_workPieceInfoRep = workPieceInfoRep;
_workPieceOutboundRep = workPieceOutboundRep;
}
///
/// 分页查询工件出库信息
///
///
///
[HttpGet("page")]
public async Task> Page([FromQuery] WorkPieceOutboundSearch input)
{
var workPieceOutbounds = await _workPieceOutboundRep.DetachedEntities
.Where(!string.IsNullOrEmpty(input.WorkPieceID), u => u.WorkPieceID.Contains(input.WorkPieceID))
.Where(!string.IsNullOrEmpty(input.OP80NewCode), u => u.OP80NewCode.Contains(input.OP80NewCode))
.Where(!string.IsNullOrEmpty(input.WorkPieceOutboundUserName), u => u.WorkPieceOutboundUserName.Contains(input.WorkPieceOutboundUserName))
.Where(!string.IsNullOrEmpty(input.StartTimeBeginTime.ToString()), u => u.WorkPieceOutboundTime >= input.StartTimeBeginTime)
.Where(!string.IsNullOrEmpty(input.StartTimeEndTime.ToString()), u => u.WorkPieceOutboundTime <= input.StartTimeEndTime)
.OrderBy(PageInputOrder.OrderBuilder(input))
.ProjectToType()
.ToADPagedListAsync(input.PageNo, input.PageSize);
return workPieceOutbounds;
}
///
/// 分页查询工件出库历史信息
///
///
///
[HttpGet("pageForRecord")]
public async Task> PageForRecord([FromQuery] WorkPieceOutboundSearch input)
{
var workPieceOutbounds = await _workPieceOutboundRecordRep.DetachedEntities
.Where(!string.IsNullOrEmpty(input.WorkPieceID), u => u.WorkPieceID.Contains(input.WorkPieceID))
.Where(!string.IsNullOrEmpty(input.OP80NewCode), u => u.OP80NewCode.Contains(input.OP80NewCode))
.Where(!string.IsNullOrEmpty(input.WorkPieceOutboundUserName), u => u.CreatedUserName.Contains(input.WorkPieceOutboundUserName))
.Where(!string.IsNullOrEmpty(input.StartTimeBeginTime.ToString()), u => u.CreatedTime >= input.StartTimeBeginTime)
.Where(!string.IsNullOrEmpty(input.StartTimeEndTime.ToString()), u => u.CreatedTime <= input.StartTimeEndTime)
.OrderBy(PageInputOrder.OrderBuilder(input))
.ProjectToType()
.ToADPagedListAsync(input.PageNo, input.PageSize);
return workPieceOutbounds;
}
///
/// 增加工件出库信息
///
///
///
[HttpPost("add")]
public async Task Add(AddWorkPieceOutboundInput input)
{
var workPieceOutbound = input.Adapt();
await _workPieceOutboundRep.InsertAsync(workPieceOutbound);
}
///
/// 删除工件出库信息
///
///
///
[HttpPost("delete")]
public async Task Delete(DeleteWorkPieceOutboundInput input)
{
var workPieceOutbound = await _workPieceOutboundRep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _workPieceOutboundRep.DeleteAsync(workPieceOutbound);
}
///
/// 更新工件出库信息
///
///
///
[HttpPost("edit")]
public async Task Update(UpdateWorkPieceOutboundInput input)
{
var isExist = await _workPieceOutboundRep.AnyAsync(u => u.Id == input.Id, false);
if (!isExist) throw Oops.Oh(ErrorCode.D3000);
var workPieceOutbound = input.Adapt();
await _workPieceOutboundRep.UpdateAsync(workPieceOutbound, ignoreNullValues: true);
}
///
/// 获取工件出库信息
///
///
///
[HttpGet("detail")]
public async Task Get([FromQuery] QueryeWorkPieceOutboundInput input)
{
return (await _workPieceOutboundRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id)).Adapt();
}
///
/// 获取工件出库信息列表
///
///
///
[HttpGet("list")]
public async Task> List([FromQuery] WorkPieceOutboundInput input)
{
return await _workPieceOutboundRep.DetachedEntities.ProjectToType().ToListAsync();
}
///
/// 撤销工件出库信息
///
///
///
[HttpPost("revoke")]
[UnitOfWork]
public async Task Revoke(List input)
{
foreach (var item in input)
{
var workPieceOutbound = await _workPieceOutboundRep.FirstOrDefaultAsync(u => u.Id == item.Id);
if (workPieceOutbound == null)
{
throw Oops.Oh("工件入库信息不存在");
}
//workPieceOutbound.IsDeleted = true;
//workPieceOutbound.Remark = workPieceOutbound.Remark ?? "" + "撤销入库";
//await _workPieceOutboundRep.UpdateAsync(workPieceOutbound);
await _workPieceOutboundRep.DeleteAsync(workPieceOutbound);
var outBoundLog = new WorkPieceOutboundRecord
{
OperationType = OutboundOperationType.撤销入库.ToString(),
WorkPieceID = workPieceOutbound?.WorkPieceID,
OP80NewCode = workPieceOutbound?.OP80NewCode,
CreatedUserId = CurrentUserInfo.UserId,
CreatedUserName = CurrentUserInfo.Name,
// CarNo = itemModel.CarNo,
Remark = "撤销入库",
};
await _workPieceOutboundRecordRep.InsertAsync(outBoundLog);
var workPiece = await _workPieceInfoRep.FirstOrDefaultAsync(u => u.WorkPieceID == workPieceOutbound.WorkPieceID);
if (workPiece != null)
{
workPiece.OutPerson = CurrentUserInfo.Name;
workPiece.OutRemark = "撤销入库";
workPiece.OutTime = DateTime.Now;
workPiece.IsOut = false;
await _workPieceInfoRep.UpdateAsync(workPiece);
}
}
}
}
}