Files
WMS-Api/src/WMS.Web.Repositories/InventoryDetailsRepositories.cs
2023-11-25 09:51:50 +08:00

196 lines
7.8 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 InventoryDetailsRepositories: IAllFielRepositories<InventoryDetailsQueryRequest>, IInventoryDetailsRepositories
{
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 InventoryDetailsRepositories(RepositoryDbContext context,
IMapper mapper,
IErpService erpService,
ILoginRepositories loginRepositories,
IServiceProvider serviceProvider,
ISingleDataService singleDataService,
IErpBasicDataExtendService erpBasicDataExtendService)
{
_context = context;
_mapper = mapper;
_erpService = erpService;
_serviceProvider = serviceProvider;
_loginRepositories = loginRepositories;
_singleDataService = singleDataService;
_erpBasicDataExtendService = erpBasicDataExtendService;
}
/// <summary>
/// 列表-分页
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<(List<InventoryDetailsQueryResponse> list,int total)> GetPagedList(InventoryDetailsQueryRequest dto)
{
//1.获取物料集合和组织集合
var materials_result = await _erpService.BillQueryForMaterial();
if (!materials_result.IsSuccess)
return (new List<InventoryDetailsQueryResponse>(), 0);
var materials = materials_result.Data.ToList();
//物料集合;模糊查询后的物料集合
if (!string.IsNullOrEmpty(dto.MaterialNumber))
materials = materials.Where(w =>w.MaterialNumber.Contains(dto.MaterialNumber)).ToList();
//组织集合
var orgs_result = await _erpService.BillQueryForOrg();
if (!orgs_result.IsSuccess)
return (new List<InventoryDetailsQueryResponse>(), 0);
var orgs = orgs_result.Data.ToList();
//
var query = _context.InventoryDetails
.Where(adv => 1 == 1);
//物料ID在模糊后的物料
if (materials != null && materials.Count != 0 && !string.IsNullOrEmpty(dto.MaterialNumber))
{
var mids = materials.Select(x => x.MaterialId).ToList();
query = query.Where(w => mids.Contains(w.MaterialId));
}
if (!string.IsNullOrEmpty(dto.StockCode))
query = query.Where(w => w.StockCode == dto.StockCode);
if (dto.Qty.HasValue)
query = query.Where(w => w.Qty == dto.Qty);
int total = await query.CountAsync();
var list = await query.Select(s => new InventoryDetailsQueryResponse()
{
Id=s.Id,
MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.MaterialId),
MaterialNumber = _erpBasicDataExtendService.GetMaterialNumber(materials, s.MaterialId),
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.MaterialId),
Org= _erpBasicDataExtendService.GetOrgName(orgs, s.OrgCode),
Stock = _singleDataService.GetSingleData(SingleAction.StocksJoinOrgCode, _loginRepositories.CompanyId, s.StockCode + s.OrgCode),
Qty = s.Qty,
SubStock = _singleDataService.GetSingleData(SingleAction.SubStocks, _loginRepositories.CompanyId, s.SubStockId),
Unit = _erpBasicDataExtendService.GetMaterialUnitName(materials, s.MaterialId),
}).OrderByDescending(x => x.Id).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
return (list,total);
}
/// <summary>
/// 批量添加
/// </summary>
/// <param name="entitys"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<bool> AddRange(List<InventoryDetails> entitys, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
if (entitys != null && entitys.Count != 0)
{
await _context.InventoryDetails.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<InventoryDetails> 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.InventoryDetails.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="dto"></param>
/// <param name="companyId"></param>
/// <returns></returns>
public async Task<(object obj, int total)> GetListField(InventoryDetailsQueryRequest dto, int companyId)
{
return await GetPagedList(dto);
}
/// <summary>
/// 列表-根据物料和仓库和仓位
/// </summary>
/// <param name="materialIds"></param>
/// <param name="stockCodes"></param>
/// <param name="subStockIds"></param>
/// <param name="orgCodes"></param>
/// <returns></returns>
public async Task<List<InventoryDetails>> GetListBy(List<int> materialIds, List<string> stockCodes, List<int> subStockIds, List<string> orgCodes)
{
return await _context.InventoryDetails.Where(x => materialIds.Contains(x.MaterialId) &&
stockCodes.Contains(x.StockCode) &&
orgCodes.Contains(x.OrgCode) &&
subStockIds.Contains(x.SubStockId) ).ToListAsync();
}
}
}