Files
WMS-Api/src/WMS.Web.Repositories/BoxInventoryRepositories.cs
2023-11-23 16:03:16 +08:00

263 lines
11 KiB
C#

using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMS.Web.Core.Dto.Inventory;
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.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 readonly IErpBasicDataExtendService _erpBasicDataExtendService;
public BoxInventoryRepositories(RepositoryDbContext context,
IMapper mapper,
IErpService erpService,
ILoginRepositories loginRepositories,
IServiceProvider serviceProvider,
ISingleDataService singleDataService,
IErpBasicDataExtendService erpBasicDataExtendService)
{
_context = context;
_mapper = mapper;
_erpService = erpService;
_erpBasicDataExtendService = erpBasicDataExtendService;
_serviceProvider = serviceProvider;
_loginRepositories = loginRepositories;
_singleDataService = singleDataService;
}
/// <summary>
/// 列表-分页
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<(List<BoxInventoryQueryResponse> list, int total)> GetPagedList(BoxInventoryQueryRequest dto)
{
//1.获取物料集合和组织集合
var materials_result = await _erpService.BillQueryForMaterial();
if (!materials_result.IsSuccess)
return (new List<BoxInventoryQueryResponse>(), 0);
var materials = materials_result.Data.ToList();
//物料集合;模糊查询后的物料集合
if (!string.IsNullOrEmpty(dto.MaterialNumber))
materials = materials.Where(w => EF.Functions.Like(w.MaterialNumber, "%" + dto.MaterialNumber + "%")).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);
if (!string.IsNullOrEmpty(dto.BoxBillNo))
query = query.Where(w => EF.Functions.Like(w.box.BoxBillNo, "%" + dto.BoxBillNo + "%"));
if (!string.IsNullOrEmpty(dto.StockCode))
query = query.Where(w => w.order.StockCode == dto.StockCode);
if (dto.SubStockId.HasValue)
query = query.Where(w => w.order.SubStockId == dto.SubStockId.Value);
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.MaterialId),
MaterialNumber = _erpBasicDataExtendService.GetMaterialNumber(materials, s.detail.MaterialId),
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.detail.MaterialId),
Stock = _singleDataService.GetSingleData(SingleAction.StocksJoinOrgCode, _loginRepositories.CompanyId, s.order.StockCode+s.order.OrgCode),
SubStock = _singleDataService.GetSingleData(SingleAction.SubStocks, _loginRepositories.CompanyId, s.order.SubStockId),
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_result = await _erpService.BillQueryForMaterial();
//if (!materials_result.IsSuccess)
// return new List<BoxInventoryDetailsResponse>();
//var 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)
{
//var serialNumbs = await _context.SerialNumbers.Where(x => x.BoxId == response.BoxId).ToListAsync();
response.Stock = _singleDataService.GetSingleData(SingleAction.StocksJoinOrgCode, _loginRepositories.CompanyId, response.StockCode + response.OrgCode);
response.SubStock = _singleDataService.GetSingleData(SingleAction.SubStocks, _loginRepositories.CompanyId, response.SubStockId);
response.Details = _mapper.Map<List<BoxInventoryDetailsResponse>>(entity.Details);
//if (response.Details != null && response.Details.Count != 0)
//{
// response.Details.ForEach(x =>
// {
// x.SerialNumbers = serialNumbs.Where(t => t.MaterialId == x.MaterialId).Select(t=>t.SerialNumber).ToList();
// });
//}
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();
}
/// <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.Id==id).FirstOrDefaultAsync();
}
/// <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);
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 = false)
{
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);
}
}
}