using Admin.NET.Core.Helper.ExcelHelper;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using iWare.Wms.Core;
using iWare.Wms.Core.Enum;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;
namespace iWare.Wms.Application
{
///
/// 库位信息
///
[ApiDescriptionSettings("基础信息", Name = "WmsPlace", Order = 100)]
[Route("api/[Controller]")]
public class WmsPlaceService : ControllerBase, IWmsPlaceService, IDynamicApiController, ITransient
{
private readonly IRepository _wmsPlaceRep;
private readonly IRepository _wmsAreaRep;
private readonly IRepository _v_empty_location;
public WmsPlaceService(
IRepository wmsAreaRep,
IRepository wmsPlaceRep,
IRepository v_empty_location
)
{
_wmsAreaRep = wmsAreaRep;
_wmsPlaceRep = wmsPlaceRep;
_v_empty_location = v_empty_location;
}
///
/// 分页查询库位信息
///
///
///
[HttpGet("page")]
public async Task> Page([FromQuery] WmsPlaceSearch input)
{
var wmsPlaces = await _wmsPlaceRep.DetachedEntities
.Where(!string.IsNullOrEmpty(input.Placecode), u => EF.Functions.Like(u.PlaceCode, $"%{input.Placecode.Trim()}%"))
.Where(input.Placestatus != null, u => u.PlaceStatus == input.Placestatus)
.Where(input.Areaid > 0, u => u.AreaId == input.Areaid)
.Where(input.Rowno != null, u => u.RowNo == input.Rowno)
.Where(input.Columnno != null, u => u.ColumnNo == input.Columnno)
.Where(input.Layerno != null, u => u.LayerNo == input.Layerno)
.Where(input.Aisle != null, u => u.Aisle == input.Aisle)
.Where(input.Islock != null, u => u.Islock == input.Islock)
//.OrderBy(PageInputOrder.OrderBuilder(input))
.OrderBy(u => u.PlaceCode)
.ProjectToType()
.ToADPagedListAsync(input.PageNo, input.PageSize);
return wmsPlaces;
}
///
/// 增加库位信息
///
///
///
[HttpPost("add")]
public async Task Add([FromBody] AddWmsPlaceInput input)
{
var wmsPlace = input.Adapt();
for (int t = 1; t <= wmsPlace.RowNo; t++) //循环排
{
for (int i = 1; i <= wmsPlace.ColumnNo; i++) //循环列
{
for (int j = 1; j <= wmsPlace.LayerNo; j++) //循环层
{
var model = new WmsPlace();
model.PlaceStatus = Core.Enum.PlaceStatus.KONGXIAN;
model.AreaId = wmsPlace.AreaId;
model.RowNo = t;
model.ColumnNo = i;
model.LayerNo = j;
model.Aisle = wmsPlace.Aisle;
model.Islock = YesOrNot.N;
model.Length = new decimal(1.2);
model.Width = new decimal(1.5);
model.Height = new decimal(1.8);
model.MaxWeight = new decimal(2.8);
model.HeightLevel = Core.Enum.Heightlevel.DI;
model.Priority = 0;
model.PlaceCode = String.Format("{0}-{1}-{2}-{3}", wmsPlace.Aisle, t.ToString().PadLeft(2, '0'), i.ToString().PadLeft(2, '0'), j.ToString().PadLeft(2, '0'));
model.DeepcellNo = t == 1 ? (int)DeepcellNoEnum.远伸 : (int)DeepcellNoEnum.近伸;
var isExit = await _wmsPlaceRep.AnyAsync(n => n.PlaceCode == model.PlaceCode);
if (!isExit) await _wmsPlaceRep.InsertAsync(model);
}
}
}
}
///
/// 删除库位信息
///
///
///
[HttpPost("delete")]
public async Task Delete([FromBody] DeleteWmsPlaceInput input)
{
var wmsPlace = await _wmsPlaceRep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _wmsPlaceRep.DeleteAsync(wmsPlace);
}
///
/// 更新库位信息
///
///
///
[HttpPost("edit")]
public async Task Update([FromBody] UpdateWmsPlaceInput input)
{
var isExist = await _wmsPlaceRep.AnyAsync(u => u.Id == input.Id, false);
if (!isExist) throw Oops.Oh(ErrorCode.D3000);
var wmsPlace = input.Adapt();
await _wmsPlaceRep.UpdateAsync(wmsPlace, ignoreNullValues: true);
}
///
/// 获取库位信息
///
///
///
[HttpGet("detail")]
public async Task Get([FromQuery] QueryeWmsPlaceInput input)
{
return (await _wmsPlaceRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id)).Adapt();
}
///
/// 获取库位信息列表
///
///
///
[HttpGet("list")]
public async Task> List([FromQuery] WmsPlaceInput input)
{
return await _wmsPlaceRep.DetachedEntities.ProjectToType().ToListAsync();
}
///
/// 获取WmsArea列表
///
///
[HttpGet("fkWmsArea")]
public async Task FkWmsAreaList()
{
var list = await _wmsAreaRep.DetachedEntities.ToListAsync();
return list.Select(e => new { Code = e.Id, Name = e.AreaName });
}
///
/// 获取空库位信息列表
///
///
///
[HttpGet("emptylist")]
public async Task EmptyList([FromQuery] WmsPlaceInput input)
{
return await _v_empty_location.DetachedEntities
.Where(!string.IsNullOrEmpty(input.Placecode), u => u.PlaceCode == input.Placecode)
.Where(!string.IsNullOrEmpty(input.ContainerTypeCode), u => u.WareLocationTypeCode == input.ContainerTypeCode)
.Select(u => new { locationno = u.PlaceCode, locationname = u.DeepcellNo == Core.Enum.DeepcellNoEnum.近伸 ? "近伸" : "远伸" }).ToListAsync();
}
///
/// 导出Excel
///
/// 筛选条件
///
[HttpPost("download")]
[AllowAnonymous]
public async Task Download([FromBody] WmsPlaceSearch input)
{
var dataRecords = await _wmsPlaceRep.DetachedEntities
.Where(!string.IsNullOrEmpty(input.Placecode), u => EF.Functions.Like(u.PlaceCode, $"%{input.Placecode.Trim()}%"))
.Where(input.Placestatus != null, u => u.PlaceStatus == input.Placestatus)
.Where(input.Areaid > 0, u => u.AreaId == input.Areaid)
.Where(input.Rowno != null, u => u.RowNo == input.Rowno)
.Where(input.Columnno != null, u => u.ColumnNo == input.Columnno)
.Where(input.Layerno != null, u => u.LayerNo == input.Layerno)
.Where(input.Aisle != null, u => u.Aisle == input.Aisle)
.Where(input.Islock != null, u => u.Islock == input.Islock)
.OrderBy(u => u.PlaceCode)
.ProjectToType().ToListAsync();
var excelBaseResult = new Excel2003Result(dataRecords, "库位表格" + DateTime.Now.ToString("yyyyMMdd"), false, "生产记录");
return File(excelBaseResult.GetExcelStream(), "application/vnd.ms-excel");
}
}
}