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.Erp; using WMS.Web.Core.Dto.Erp.Org; 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.Mappers; 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, int companyId) { //1.获取物料集合和组织集合 var materials = new List(); var materials_result = await _erpService.BillQueryForMaterial(); if (materials_result.IsSuccess) materials = materials_result.Data.ToList(); //物料集合;模糊查询后的物料集合 if (!string.IsNullOrEmpty(dto.MaterialNumber)) materials = materials.Where(w => w.MaterialNumber.Contains(dto.MaterialNumber)).ToList(); //组织集合 var orgs = new List(); 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 (dto.SubStockId.HasValue) query = query.Where(w => w.order.SubStockId == dto.SubStockId.Value); //物料ID在模糊后的物料 if (!string.IsNullOrEmpty(dto.MaterialNumber)) { if (materials != null && materials.Count != 0) { var mids = materials.Select(x => x.MaterialId).ToList(); query = query.Where(w => mids.Contains(w.detail.MaterialId)); } else query = query.Where(w => w.detail.MaterialId == 0); } 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, companyId, s.order.StockCode+s.order.OrgCode), SubStock = _singleDataService.GetSingleData(SingleAction.SubStocks, companyId, s.order.SubStockId), 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); } /// /// 明细集合-根据箱号 /// /// /// public async Task GetInfoBy(string boxBillNo) { //1.获取物料集合和组织集合 var materials = new List(); 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(entity); if (response != null) { 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); response.Details.ForEach(x => { x.MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, x.MaterialId); x.MaterialNumber = _erpBasicDataExtendService.GetMaterialNumber(materials, x.MaterialId); x.Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, x.MaterialId); }); 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.BoxId== 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); //_mapper.ToMapList(entitys.SelectMany(x=>x.Details).ToList(), res.SelectMany(x=>x.Details).ToList()); var tt= 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,companyId); } } }