478 lines
22 KiB
C#
478 lines
22 KiB
C#
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using WMS.Web.Core.Dto.Erp;
|
|
using WMS.Web.Core.Dto.Erp.Org;
|
|
using WMS.Web.Core.Dto.Inventory;
|
|
using WMS.Web.Core.Dto.Login;
|
|
using WMS.Web.Core.Internal.Results;
|
|
using WMS.Web.Domain.Entitys;
|
|
using WMS.Web.Domain.Infrastructure;
|
|
using WMS.Web.Domain.IService.Public;
|
|
using WMS.Web.Domain.Mappers;
|
|
using WMS.Web.Domain.Values;
|
|
using WMS.Web.Domain.Values.Single;
|
|
using WMS.Web.Repositories.Configuration;
|
|
|
|
namespace WMS.Web.Repositories
|
|
{
|
|
/// <summary>
|
|
/// 箱库存-仓储
|
|
/// </summary>
|
|
public class BoxInventoryRepositories : IAllFielRepositories<BoxInventoryQueryRequest>, IBoxInventoryRepositories
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IServiceProvider _serviceProvider;
|
|
private readonly ILoginRepositories _loginRepositories;
|
|
private readonly RepositoryDbContext _context;
|
|
private readonly ISingleDataService _singleDataService;
|
|
private readonly IErpService _erpService;
|
|
private ILogger<BoxInventoryRepositories> _logger;
|
|
private readonly IErpBasicDataExtendService _erpBasicDataExtendService;
|
|
private readonly IBasicsRepositories _basicsRepositories;
|
|
|
|
public BoxInventoryRepositories(RepositoryDbContext context,
|
|
IMapper mapper,
|
|
ILogger<BoxInventoryRepositories> logger,
|
|
IErpService erpService,
|
|
ILoginRepositories loginRepositories,
|
|
IServiceProvider serviceProvider,
|
|
ISingleDataService singleDataService,
|
|
IBasicsRepositories basicsRepositories,
|
|
IErpBasicDataExtendService erpBasicDataExtendService)
|
|
{
|
|
_context = context;
|
|
_logger = logger;
|
|
_mapper = mapper;
|
|
_erpService = erpService;
|
|
_erpBasicDataExtendService = erpBasicDataExtendService;
|
|
_serviceProvider = serviceProvider;
|
|
_loginRepositories = loginRepositories;
|
|
_singleDataService = singleDataService;
|
|
_basicsRepositories = basicsRepositories;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 列表-分页
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <param name="companyId"></param>
|
|
/// <returns></returns>
|
|
public async Task<(List<BoxInventoryQueryResponse> list, int total)> GetPagedList(BoxInventoryQueryRequest dto, int companyId)
|
|
{
|
|
//1.获取物料集合和组织集合
|
|
var materials = new List<ErpMaterialDto>();
|
|
var materials_result = await _erpService.BillQueryForMaterial();
|
|
if (materials_result.IsSuccess)
|
|
materials = materials_result.Data.ToList();
|
|
|
|
List<string> materialNumbs = new List<string>();
|
|
//物料集合;模糊查询后的物料集合
|
|
if (!string.IsNullOrEmpty(dto.MaterialNumber))
|
|
{
|
|
if (materials.Count != 0)
|
|
{
|
|
materialNumbs = materials.Where(w => w.MaterialNumber.Contains(dto.MaterialNumber)
|
|
|| w.MaterialName.Contains(dto.MaterialNumber)
|
|
|| w.Specifications.Contains(dto.MaterialNumber)
|
|
).Select(x => x.MaterialNumber).ToList();
|
|
}
|
|
}
|
|
|
|
//组织集合
|
|
var orgs = new List<ErpOrgDto>();
|
|
var orgs_result = await _erpService.BillQueryForOrg();
|
|
if (orgs_result.IsSuccess)
|
|
orgs = orgs_result.Data.ToList();
|
|
|
|
var query = _context.BoxInventoryDetails
|
|
.GroupJoin(_context.BoxInventory, detail => detail.Fid, order => order.Id, (detail, orders) => new { detail, orders })
|
|
.SelectMany(x => x.orders.DefaultIfEmpty(), (p, order) => new { p.detail, order })
|
|
.GroupJoin(_context.Box, p => p.order.BoxId, t => t.Id, (p, ts) => new { p.detail, p.order, ts })
|
|
.SelectMany(x => x.ts.DefaultIfEmpty(), (p, box) => new { p.detail, p.order, box })
|
|
.Where(adv => 1 == 1 && adv.detail.Qty != 0);
|
|
|
|
if (!string.IsNullOrEmpty(dto.BoxBillNo))
|
|
query = query.Where(w => EF.Functions.Like(w.box.BoxBillNo, "%" + dto.BoxBillNo + "%"));
|
|
|
|
if (!string.IsNullOrEmpty(dto.StockCode))
|
|
{
|
|
var splitStrs = dto.StockCode.Split("_$");
|
|
query = query.Where(w => w.order.StockCode == splitStrs[0] && w.order.OrgCode == splitStrs[1]);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(dto.SubStockCode))
|
|
query = query.Where(w => w.order.SubStockCode == dto.SubStockCode);
|
|
|
|
//物料ID在模糊后的物料
|
|
if (!string.IsNullOrEmpty(dto.MaterialNumber))
|
|
{
|
|
query = query.Where(w => materialNumbs.Contains(w.detail.MaterialNumber));
|
|
}
|
|
|
|
int total = await query.CountAsync();
|
|
var list = await query.Select(s => new BoxInventoryQueryResponse()
|
|
{
|
|
Id = s.order.Id,
|
|
DetailsId = s.detail.Id,
|
|
BoxBillNo = s.box.BoxBillNo,
|
|
MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.detail.MaterialNumber),
|
|
MaterialNumber = s.detail.MaterialNumber,
|
|
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.detail.MaterialNumber),
|
|
Stock = _singleDataService.GetSingleData(SingleAction.StocksJoinOrgCode, companyId, s.order.StockCode + s.order.OrgCode),
|
|
SubStock = _singleDataService.GetSingleData(SingleAction.SubStocksJoinOrgCode, companyId, s.order.SubStockCode + s.order.StockCode + s.order.OrgCode),
|
|
SerialNumbers = (string.Join(",", s.detail.SerialNumbers)).TrimEnd(','),
|
|
Org = _erpBasicDataExtendService.GetOrgName(orgs, s.order.OrgCode),
|
|
Qty = s.detail.Qty,
|
|
}).OrderByDescending(x => x.Id).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
|
|
|
|
return (list, total);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 明细集合-根据箱号
|
|
/// </summary>
|
|
/// <param name="boxBillNo"></param>
|
|
/// <returns></returns>
|
|
public async Task<BoxInventoryResponse> GetInfoBy(string boxBillNo)
|
|
{
|
|
//1.获取物料集合和组织集合
|
|
var materials = new List<ErpMaterialDto>();
|
|
var materials_result = await _erpService.BillQueryForMaterial();
|
|
if (materials_result.IsSuccess)
|
|
materials = materials_result.Data.ToList();
|
|
|
|
|
|
var entity = await _context.BoxInventory.Include(x => x.Details)
|
|
.GroupJoin(_context.Box, t => t.BoxId, box => box.Id, (boxinvent, ts) => new { boxinvent, ts })
|
|
.SelectMany(x => x.ts.DefaultIfEmpty(), (p, box) => new { p.boxinvent, box })
|
|
.Where(x => 1 == 1 && x.box.BoxBillNo == boxBillNo).Select(x => x.boxinvent).FirstOrDefaultAsync();
|
|
|
|
var response = _mapper.Map<BoxInventoryResponse>(entity);
|
|
if (response != null)
|
|
{
|
|
response.Stock = _singleDataService.GetSingleData(SingleAction.StocksJoinOrgCode, _loginRepositories.CompanyId, response.StockCode + response.OrgCode);
|
|
response.SubStock = _singleDataService.GetSingleData(SingleAction.SubStocksJoinOrgCode, _loginRepositories.CompanyId, response.SubStockCode + response.StockCode + response.OrgCode);
|
|
response.Details = _mapper.Map<List<BoxInventoryDetailsResponse>>(entity.Details);
|
|
response.Details.ForEach(x =>
|
|
{
|
|
x.MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, x.MaterialNumber);
|
|
x.MaterialNumber = x.MaterialNumber;
|
|
x.Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, x.MaterialNumber);
|
|
});
|
|
response.TotalQty = response.Details.Sum(x => x.Qty);
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 列表-根据箱ids
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<BoxInventory>> GetList(List<int> ids)
|
|
{
|
|
return await _context.BoxInventory.Include(x => x.Details).Where(x => ids.Contains(x.BoxId)).ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> IsExist(int boxId)
|
|
{
|
|
return await _context.BoxInventory.Where(x => x.BoxId == boxId).AnyAsync();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 实体-根据箱id
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
public async Task<BoxInventory> Get(int id)
|
|
{
|
|
return await _context.BoxInventory.Include(x => x.Details).Where(x => x.BoxId == id).FirstOrDefaultAsync();
|
|
}
|
|
/// <summary>
|
|
/// 添加及时库存
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<BoxInventory> Add(BoxInventory entity, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
await _context.BoxInventory.AddAsync(entity);
|
|
await _context.SaveChangesAsync();
|
|
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
return entity;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_transaction != null)
|
|
_transaction.Rollback();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量添加
|
|
/// </summary>
|
|
/// <param name="entitys"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AddRange(List<BoxInventory> entitys, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
if (entitys != null && entitys.Count != 0)
|
|
{
|
|
await _context.BoxInventory.AddRangeAsync(entitys);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
if (_transaction != null)
|
|
_transaction.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量修改
|
|
/// </summary>
|
|
/// <param name="entitys"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> UpdateRange(List<BoxInventory> entitys, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
{
|
|
try
|
|
{
|
|
List<int> list = entitys.Select(s => s.Id).ToList();
|
|
var res = await _context.BoxInventory.Include(x => x.Details).Where(f => list.Contains(f.Id)).ToListAsync();
|
|
_mapper.Map(entitys, res);
|
|
var tt = await _context.SaveChangesAsync();
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_transaction != null)
|
|
_transaction.Rollback();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量删除
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DeleteRange(List<int> ids, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var list = await _context.BoxInventory.Include(x => x.Details)
|
|
.Where(f => ids.Contains(f.Id)).ToListAsync();
|
|
_context.BoxInventory.RemoveRange(list);
|
|
await _context.SaveChangesAsync();
|
|
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_transaction != null)
|
|
_transaction.Rollback();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 导出
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <param name="companyId"></param>
|
|
/// <returns></returns>
|
|
public async Task<(object obj, int total)> GetListField(BoxInventoryQueryRequest dto, int companyId)
|
|
{
|
|
return await GetPagedList(dto, companyId);
|
|
}
|
|
/// <summary>
|
|
/// PDA库存查询-物料
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <param name="companyId"></param>
|
|
/// <returns></returns>
|
|
public async Task<GetPagedListBoxByMaterialResponse> GetPagedListBoxByMaterial(GetPagedListBoxByMaterialRequest dto, int companyId)
|
|
{
|
|
//1.获取物料集合和组织集合
|
|
var materials = new List<ErpMaterialDto>();
|
|
var materials_result = await _erpService.BillQueryForMaterial();
|
|
if (materials_result.IsSuccess)
|
|
materials = materials_result.Data.ToList();
|
|
|
|
ErpMaterialDto material = null;
|
|
//物料集合;模糊查询后的物料集合
|
|
if (!string.IsNullOrEmpty(dto.MaterialNumber))
|
|
{
|
|
if (materials.Count != 0)
|
|
{
|
|
material = materials.FirstOrDefault(w => w.MaterialNumber.Equals(dto.MaterialNumber)
|
|
//|| w.MaterialName.Equals(dto.MaterialNumber)
|
|
|| w.Specifications.Equals(dto.MaterialNumber)
|
|
|| w.BarCode.Equals(dto.MaterialNumber)
|
|
);
|
|
if (material == null) return new GetPagedListBoxByMaterialResponse();
|
|
}
|
|
}
|
|
|
|
var query = _context.BoxInventoryDetails
|
|
.GroupJoin(_context.BoxInventory, detail => detail.Fid, order => order.Id, (detail, orders) => new { detail, orders })
|
|
.SelectMany(x => x.orders.DefaultIfEmpty(), (p, order) => new { p.detail, order })
|
|
.GroupJoin(_context.Box, p => p.order.BoxId, t => t.Id, (p, ts) => new { p.detail, p.order, ts })
|
|
.SelectMany(x => x.ts.DefaultIfEmpty(), (p, box) => new { p.detail, p.order, box })
|
|
.Where(adv => 1 == 1 && adv.detail.MaterialNumber.Equals(material.MaterialNumber));
|
|
|
|
if (!string.IsNullOrEmpty(dto.StockCode))
|
|
{
|
|
var splitStrs = dto.StockCode.Split("_$");
|
|
query = query.Where(w => w.order.StockCode == splitStrs[0] && w.order.OrgCode == splitStrs[1]);
|
|
}
|
|
GetPagedListBoxByMaterialResponse response = new GetPagedListBoxByMaterialResponse();
|
|
response.Specifications = material.Specifications;
|
|
response.MaterialNumber = material.MaterialNumber;
|
|
response.MaterialName = material.MaterialName;
|
|
|
|
response.TotalCount = await query.CountAsync();
|
|
response.TotalQty = await query.SumAsync(s => s.detail.Qty);
|
|
response.Details = await query.Select(s => new GetPagedListBoxByMaterialDetailsResponse()
|
|
{
|
|
BoxBillNo = s.box.BoxBillNo,
|
|
SubStock = _singleDataService.GetSingleData(SingleAction.SubStocksJoinOrgCode, companyId, s.order.SubStockCode + s.order.StockCode + s.order.OrgCode),
|
|
Qty = s.detail.Qty,
|
|
}).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
|
|
|
|
response.Details = response.Details.Where(w => w.Qty > 0).ToList();
|
|
return response;
|
|
}
|
|
/// <summary>
|
|
/// PDA库存查询-箱号
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <param name="companyId"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<GetPagedListBoxByBoxResponse> GetPagedListBoxByBox(GetPagedListBoxByBoxRequest dto, int companyId)
|
|
{
|
|
//1.获取物料集合和组织集合
|
|
var materials = new List<ErpMaterialDto>();
|
|
var materials_result = await _erpService.BillQueryForMaterial();
|
|
if (materials_result.IsSuccess)
|
|
materials = materials_result.Data.ToList();
|
|
|
|
var query = _context.BoxInventoryDetails
|
|
.GroupJoin(_context.BoxInventory, detail => detail.Fid, order => order.Id, (detail, orders) => new { detail, orders })
|
|
.SelectMany(x => x.orders.DefaultIfEmpty(), (p, order) => new { p.detail, order })
|
|
.GroupJoin(_context.Box, p => p.order.BoxId, t => t.Id, (p, ts) => new { p.detail, p.order, ts })
|
|
.SelectMany(x => x.ts.DefaultIfEmpty(), (p, box) => new { p.detail, p.order, box })
|
|
.Where(adv => 1 == 1 && adv.box.BoxBillNo.Equals(dto.BoxBillNo));
|
|
|
|
if (!string.IsNullOrEmpty(dto.StockCode))
|
|
{
|
|
var splitStrs = dto.StockCode.Split("_$");
|
|
query = query.Where(w => w.order.StockCode == splitStrs[0] && w.order.OrgCode == splitStrs[1]);
|
|
}
|
|
GetPagedListBoxByBoxResponse response = new GetPagedListBoxByBoxResponse();
|
|
response.BoxBillNo = dto.BoxBillNo;
|
|
|
|
response.TotalCount = await query.CountAsync();
|
|
response.TotalQty = await query.SumAsync(s => s.detail.Qty);
|
|
response.Details = await query.Select(s => new GetPagedListBoxByBoxDetailsResponse()
|
|
{
|
|
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.detail.MaterialNumber),
|
|
SubStock = _singleDataService.GetSingleData(SingleAction.SubStocksJoinOrgCode, companyId, s.order.SubStockCode + s.order.StockCode + s.order.OrgCode),
|
|
Qty = s.detail.Qty,
|
|
}).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
|
|
|
|
if (response.Details.Count > 0) response.SubStock = response.Details.First().SubStock;
|
|
response.Details = response.Details.Where(w => w.Qty > 0).ToList();
|
|
return response;
|
|
}
|
|
/// <summary>
|
|
/// PDA库存查询-仓位
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <param name="companyId"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<GetPagedListBoxBySubStockResponse> GetPagedListBoxBySubStock(GetPagedListBoxBySubStockRequest dto, int companyId)
|
|
{
|
|
//1.获取物料集合和组织集合
|
|
var materials = new List<ErpMaterialDto>();
|
|
var materials_result = await _erpService.BillQueryForMaterial();
|
|
if (materials_result.IsSuccess)
|
|
materials = materials_result.Data.ToList();
|
|
|
|
var r = await _basicsRepositories.GetSubUcStockAsync(ManagementSystemCode.GLXT0004.ToString(), dto.SubStock, companyId);
|
|
var subStock = r.FirstOrDefault(f => f.Name.Equals(dto.SubStock));
|
|
if (subStock == null) return new GetPagedListBoxBySubStockResponse();
|
|
|
|
var query = _context.BoxInventoryDetails
|
|
.GroupJoin(_context.BoxInventory, detail => detail.Fid, order => order.Id, (detail, orders) => new { detail, orders })
|
|
.SelectMany(x => x.orders.DefaultIfEmpty(), (p, order) => new { p.detail, order })
|
|
.GroupJoin(_context.Box, p => p.order.BoxId, t => t.Id, (p, ts) => new { p.detail, p.order, ts })
|
|
.SelectMany(x => x.ts.DefaultIfEmpty(), (p, box) => new { p.detail, p.order, box })
|
|
.Where(adv => 1 == 1 && adv.order.SubStockCode.Equals(subStock.Code));
|
|
|
|
if (!string.IsNullOrEmpty(dto.StockCode))
|
|
{
|
|
var splitStrs = dto.StockCode.Split("_$");
|
|
query = query.Where(w => w.order.StockCode == splitStrs[0] && w.order.OrgCode == splitStrs[1]);
|
|
}
|
|
GetPagedListBoxBySubStockResponse response = new GetPagedListBoxBySubStockResponse();
|
|
|
|
response.TotalCount = await query.CountAsync();
|
|
response.TotalQty = await query.SumAsync(s => s.detail.Qty);
|
|
response.Details = await query.Select(s => new GetPagedListBoxBySubStockDetailsResponse()
|
|
{
|
|
BoxBillNo = s.box.BoxBillNo,
|
|
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.detail.MaterialNumber),
|
|
SubStock = _singleDataService.GetSingleData(SingleAction.SubStocksJoinOrgCode, companyId, s.order.SubStockCode + s.order.StockCode + s.order.OrgCode),
|
|
Qty = s.detail.Qty,
|
|
}).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
|
|
|
|
if (response.Details.Count > 0) response.SubStock = response.Details.First().SubStock;
|
|
response.Details = response.Details.Where(w => w.Qty > 0).ToList();
|
|
return response;
|
|
}
|
|
}
|
|
}
|