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 { /// /// 箱库存-仓储 /// public class BoxInventoryRepositories: IAllFielRepositories, 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; } /// /// 列表-分页 /// /// /// public async Task<(List list, int total)> GetPagedList(BoxInventoryQueryRequest dto) { //1.获取物料集合和组织集合 var materials_result = await _erpService.BillQueryForMaterial(); if (!materials_result.IsSuccess) return (new List(), 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); } /// /// 明细集合-根据箱号 /// /// /// public async Task GetInfoBy(string boxBillNo) { ////1.获取物料集合和组织集合 //var materials_result = await _erpService.BillQueryForMaterial(); //if (!materials_result.IsSuccess) // return new List(); //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(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>(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; } /// /// 列表-根据箱ids /// /// /// public async Task> GetList(List ids) { return await _context.BoxInventory.Include(x => x.Details).Where(x => ids.Contains(x.BoxId)).ToListAsync(); } /// /// 实体-根据箱id /// /// /// public async Task Get(int id) { return await _context.BoxInventory.Include(x => x.Details).Where(x => x.Id==id).FirstOrDefaultAsync(); } /// /// 批量添加 /// /// /// /// public async Task AddRange(List 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; } } /// /// 批量修改 /// /// /// public async Task UpdateRange(List entitys, bool isTransaction = true) { IDbContextTransaction _transaction = null; if (isTransaction) _transaction = _context.Database.BeginTransaction(); { try { List 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; } } /// /// 批量删除 /// /// /// public async Task DeleteRange(List 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; } /// /// 导出 /// /// /// /// public async Task<(object obj, int total)> GetListField(BoxInventoryQueryRequest dto, int companyId) { return await GetPagedList(dto); } } }