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; using WMS.Web.Core.Help; using WMS.Web.Domain.Entitys; using WMS.Web.Domain.Infrastructure; using WMS.Web.Domain.IService.Public; using WMS.Web.Domain.Mappers; using WMS.Web.Repositories.Configuration; namespace WMS.Web.Repositories { /// /// 老ops箱信息 /// public class BoxRepositories : IBoxRepositories { private readonly IMapper _mapper; private readonly IServiceProvider _serviceProvider; private readonly RepositoryDbContext _context; private readonly ISingleDataService _singleDataService; private readonly ILoginRepositories _loginRepositories; private readonly IBasicsRepositories _basicsRepositories; public BoxRepositories(RepositoryDbContext context, IMapper mapper, IServiceProvider serviceProvider, ISingleDataService singleDataService, ILoginRepositories loginRepositories, IBasicsRepositories basicsRepositories) { _context = context; _mapper = mapper; _serviceProvider = serviceProvider; _singleDataService = singleDataService; _loginRepositories = loginRepositories; _basicsRepositories = basicsRepositories; } public async Task Get(int id) { var entity= await _context.Box.Include(x => x.Details) .FirstOrDefaultAsync(f => f.Id.Equals(id)); return entity.Clone(); } /// /// 根据箱号查询物料信息 /// /// /// public async Task GetBox(string BoxBillNo) { var entity = await _context.Box.FirstOrDefaultAsync(f => f.BoxBillNo.Equals(BoxBillNo)); if (entity == null) return null; BoxResponse result = new BoxResponse() { Id = entity.Id, SupplierId=entity.SupplierId, BoxBillNo = entity.BoxBillNo }; result.Details = await _context.BoxDetails .GroupJoin(_context.Box, detail => detail.Fid, order => order.Id, (detail, orders) => new { detail, orders }) .SelectMany(x => x.orders.DefaultIfEmpty(), (p, order) => new { p.detail, order }) .Where(w => w.order.BoxBillNo.Equals(BoxBillNo)) .Select(s => new BoxDetailResponse() { MaterialId = s.detail.MaterialId, MaterialName = "", MaterialNumber = "", Specifications = "", SerialNumbers = s.detail.SerialNumbers, SupplierId = s.detail.SupplierId, Qty = s.detail.Qty }) .ToListAsync(); return result; } /// /// 批量修改 /// /// /// /// public async Task EditEntityList(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.Box .Include(s => s.Details) .Where(f => list.Contains(f.Id)).ToListAsync(); _mapper.ToMapList(entitys, res); _mapper.ToMapList(entitys.SelectMany(s => s.Details).ToList(), res.SelectMany(s => s.Details).ToList()); await _context.SaveChangesAsync(); if (_transaction != null) _transaction.Commit(); } catch (Exception ex) { if (_transaction != null) _transaction.Rollback(); return false; } return true; } } }