using AutoMapper; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage; using Newtonsoft.Json; 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; 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 BackRecordRepositories : IAllFielRepositories, IBackRecordRepositories { private readonly IMapper _mapper; private readonly IServiceProvider _serviceProvider; private readonly ILoginRepositories _loginRepositories; private readonly IBasicsRepositories _basicsRepositories; private readonly RepositoryDbContext _context; private readonly IErpService _erpService; private readonly ISingleDataService _singleDataService; private readonly IErpBasicDataExtendService _erpBasicDataExtendService; public BackRecordRepositories(RepositoryDbContext context, IMapper mapper, IErpService erpService, IBasicsRepositories basicsRepositories, ILoginRepositories loginRepositories, IServiceProvider serviceProvider, ISingleDataService singleDataService, IErpBasicDataExtendService erpBasicDataExtendService) { _erpService = erpService; _context = context; _mapper = mapper; _basicsRepositories = basicsRepositories; _serviceProvider = serviceProvider; _loginRepositories = loginRepositories; _singleDataService = singleDataService; _erpBasicDataExtendService = erpBasicDataExtendService; } /// /// 列表-分页 /// /// /// public async Task<(List list,int total)> GetPagedList(BackRecordQueryRequest dto) { //1.获取物料集合和组织集合和供应商的集合 var materials_result = await _erpService.BillQueryForMaterial(); if (!materials_result.IsSuccess) return (new List(), 0); var materials = materials_result.Data.ToList(); //组织集合 var orgs_result = await _erpService.BillQueryForOrg(); if (!orgs_result.IsSuccess) return (new List(), 0); var orgs = orgs_result.Data.ToList(); List ids = new List(); if (!string.IsNullOrEmpty(dto.Creator)) { var staffList = await _basicsRepositories.GetStaffListAsync(_loginRepositories.CompanyId); ids = staffList.Where(w =>w.Name.Contains(dto.Creator)).Select(s => s.Id).ToList(); } var query = _context.BackRecordDetails .GroupJoin(_context.BackRecord, 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.detail.BoxId, ts => ts.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 (dto.SubStockId.HasValue) query = query.Where(w => w.detail.SubStockId == dto.SubStockId.Value); if(dto.Type.HasValue) query = query.Where(w => (int)w.order.Type == dto.Type.Value); if (ids.Count() > 0) query = query.Where(w => ids.Contains(w.order.CreatorId)); if (dto.CreateBeginDate != null) query = query.Where(w => w.order.CreateTime >= dto.CreateBeginDate.Value); if (dto.CreateEndDate != null) query = query.Where(w => w.order.CreateTime <= dto.CreateEndDate.Value); int total = await query.CountAsync(); var list = await query.Select(s => new BackRecordQueryResponse() { Id = s.order.Id, DetailsId = s.detail.Id, BillNo=s.order.BillNo, BoxBillNo = s.box.BoxBillNo, Type = s.order.Type.GetRemark(), Creator = _singleDataService.GetSingleData(SingleAction.Staffs, _loginRepositories.CompanyId, s.order.CreatorId), CreateTime =s.order.CreateTime, MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.detail.MaterialId), MaterialNumber = _erpBasicDataExtendService.GetMaterialNumber(materials, s.detail.MaterialId), Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.detail.MaterialId), Qty = s.detail.Qty, Org= _erpBasicDataExtendService.GetOrgName(orgs, s.detail.OrgCode), Stock = _singleDataService.GetSingleData(SingleAction.StocksJoinOrgCode, _loginRepositories.CompanyId, s.detail.StockCode + s.detail.OrgCode), SubStock = _singleDataService.GetSingleData(SingleAction.SubStocks, _loginRepositories.CompanyId, s.detail.SubStockId), SerialNumbers = JsonConvert.SerializeObject(s.detail.SerialNumbers), }).OrderByDescending(x => x.Id).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync(); return (list,total); } /// /// 新增 /// /// /// /// public async Task Add(BackRecord entity, bool isTransaction = true) { IDbContextTransaction _transaction = null; if (isTransaction) _transaction = _context.Database.BeginTransaction(); try { await _context.BackRecord.AddAsync(entity); await _context.SaveChangesAsync(); if (string.IsNullOrEmpty(entity.BillNo)) { entity.GenerateNo(); await _context.SaveChangesAsync(); } if (_transaction != null) _transaction.Commit(); return entity; } catch (Exception ex) { if (_transaction != null) _transaction.Rollback(); return null; } } /// /// 导出 /// /// /// /// public async Task<(object obj, int total)> GetListField(BackRecordQueryRequest dto, int companyId) { return await GetPagedList(dto); } } }