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; 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.Values.Single; using WMS.Web.Repositories.Configuration; namespace WMS.Web.Repositories { /// /// 物料收发明细-仓储 /// public class InventoryInOutDetailsRepositories : IAllFielRepositories, IInventoryInOutDetailsRepositories { 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 InventoryInOutDetailsRepositories(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; } /// /// 列表-分页 /// /// /// /// public async Task<(List list, int total)> GetPagedList(InventoryInOutDetailsQueryRequest dto, int companyId) { //1.获取物料集合和组织集合 var materials = new List(); var materials_result = await _erpService.BillQueryForMaterial(); if (materials_result.IsSuccess) materials = materials_result.Data.ToList(); //组织集合 var orgs = new List(); var orgs_result = await _erpService.BillQueryForOrg(); if (orgs_result.IsSuccess) orgs = orgs_result.Data.ToList(); List materialNumbs = new List(); //物料集合;模糊查询后的物料集合 if (!string.IsNullOrEmpty(dto.MaterialNumber)) { if (materials.Count != 0) { materialNumbs = materials.Where(w => w.MaterialNumber.Contains(dto.MaterialNumber) || w.MaterialName.Contains(dto.MaterialNumber) || w.Specifications.Contains(dto.MaterialNumber) ).Select(x => x.MaterialNumber).ToList(); } } var query = _context.InventoryInOutDetails .Where(adv => 1 == 1); //物料ID在模糊后的物料 if (!string.IsNullOrEmpty(dto.MaterialNumber)) { query = query.Where(w => materialNumbs.Contains(w.MaterialNumber)); } if (!string.IsNullOrEmpty(dto.StockCode)) { var splitStrs = dto.StockCode.Split("_$"); query = query.Where(w => w.StockCode == splitStrs[0] && w.OrgCode == splitStrs[1]); } if (dto.OrderType.HasValue) query = query.Where(w => (int)w.OrderType == dto.OrderType.Value); if (dto.CreateBeginDate != null) query = query.Where(w => w.CreateTime.Date >= dto.CreateBeginDate.Value); if (dto.CreateEndDate != null) query = query.Where(w => w.CreateTime.Date <= dto.CreateEndDate.Value); int total = await query.CountAsync(); var list = await query.Select(s => new InventoryInOutDetailsQueryResponse() { Id = s.Id, MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.MaterialNumber), MaterialNumber = s.MaterialNumber, Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.MaterialNumber), Type =s.Type.GetRemark(), OrderType=s.OrderType.GetRemark(), OrderBillNo=s.OrderBillNo, Org = _erpBasicDataExtendService.GetOrgName(orgs, s.OrgCode), Stock = _singleDataService.GetSingleData(SingleAction.StocksJoinOrgCode, companyId, s.StockCode+s.OrgCode), Qty = s.Qty, SurplusQty=s.SurplusQty, CreateTime = s.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"), }).OrderByDescending(x => x.Id).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync(); return (list,total); } /// /// 批量添加 /// /// /// /// 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.InventoryInOutDetails.AddRangeAsync(entitys); await _context.SaveChangesAsync(); } if (_transaction != null) _transaction.Commit(); return true; } catch { if (_transaction != null) _transaction.Rollback(); return false; } } /// /// 导出 /// /// /// /// public async Task<(object obj, int total)> GetListField(InventoryInOutDetailsQueryRequest dto, int companyId) { return await GetPagedList(dto,companyId); } } }