using AutoMapper; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WMS.Web.Core.Help; using WMS.Web.Domain.Entitys; using WMS.Web.Domain.Infrastructure; using WMS.Web.Domain.IService.Public; using WMS.Web.Repositories.Configuration; namespace WMS.Web.Repositories { /// /// 物料仓储 /// public class MaterialsRepositories : IMaterialsRepositories { private readonly IMapper _mapper; private readonly RepositoryDbContext _context; private readonly IServiceProvider _serviceProvider; public MaterialsRepositories(RepositoryDbContext context, IServiceProvider serviceProvider, IMapper mapper) { _serviceProvider = serviceProvider; _context = context; _mapper = mapper; } /// /// 批量添加 /// /// /// /// 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.Materials.AddRangeAsync(entitys); await _context.SaveChangesAsync(); } if (_transaction != null) _transaction.Commit(); return true; } catch { if (_transaction != null) _transaction.Rollback(); return false; } } /// /// 获取集合 /// /// /// public async Task> GetEntityList(IServiceProvider serPro,int? orgId = null ) { using (var context = serPro.GetRequiredService()) { var query = context.Materials.Where(x => 1 == 1); if (orgId.HasValue) query = query.Where(x => x.OrgId == orgId.Value); var res = await query.ToListAsync(); return res.Clone(); } } /// /// 获取集合 /// /// /// public async Task> GetEntityList(int? orgId = null) { var query = _context.Materials.Where(x => 1 == 1); if (orgId.HasValue) query = query.Where(x => x.OrgId == orgId.Value); var res = await query.ToListAsync(); return res.Clone(); } /// /// 获取 /// /// /// public async Task Get(int mid) { var query = _context.Materials.Where(x => 1 == 1); query = query.Where(x => x.MaterialId == mid); var res = await query.FirstOrDefaultAsync(); return res.Clone(); } public Materials GetNew(int mid) { var query = _context.Materials.Where(x => 1 == 1); query = query.Where(x => x.MaterialId == mid); var res = query.FirstOrDefault(); return res.Clone(); } /// /// 获取 /// /// /// public async Task Get(string code, int orgId) { var query = _context.Materials.Where(x => x.MaterialNumber == code && x.OrgId == orgId); var res = await query.FirstOrDefaultAsync(); return res.Clone(); } } }