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.Dto; using WMS.Web.Core.Dto.ChangeBoxRecord; using WMS.Web.Core.Dto.Erp; 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.Values.Single; using WMS.Web.Repositories.Configuration; namespace WMS.Web.Repositories { public class ChangeBoxRecordRepositories : IAllFielRepositories, IChangeBoxRecordRepositories { private readonly IMapper _mapper; private readonly IServiceProvider _serviceProvider; private readonly RepositoryDbContext _context; private readonly ISingleDataService _singleDataService; private readonly ILoginRepositories _loginRepositories; private readonly IBasicsRepositories _basicsRepositories; private readonly IErpService _erpService; private readonly IErpBasicDataExtendService _erpBasicDataExtendService; public ChangeBoxRecordRepositories(RepositoryDbContext context, IMapper mapper, IServiceProvider serviceProvider, ISingleDataService singleDataService, ILoginRepositories loginRepositories, IBasicsRepositories basicsRepositories, IErpService erpService, IErpBasicDataExtendService erpBasicDataExtendService) { _context = context; _mapper = mapper; _serviceProvider = serviceProvider; _singleDataService = singleDataService; _loginRepositories = loginRepositories; _basicsRepositories = basicsRepositories; _erpService = erpService; _erpBasicDataExtendService = erpBasicDataExtendService; } /// /// 新增 /// /// /// /// public async Task Add(ChangeBoxRecord entity, bool isTransaction = true) { IDbContextTransaction _transaction = null; if (isTransaction) _transaction = _context.Database.BeginTransaction(); try { await _context.ChangeBoxRecord.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 AddRange(List list, bool isTransaction = true) { IDbContextTransaction _transaction = null; if (isTransaction) _transaction = _context.Database.BeginTransaction(); try { _context.ChangeBoxRecord.AddRange(list); await _context.SaveChangesAsync(); foreach (var item in list) { if (string.IsNullOrEmpty(item.BillNo)) //自动生成单据编号 item.GenerateNo(); } await _context.SaveChangesAsync(); if (_transaction != null) _transaction.Commit(); return true; } catch (Exception ex) { if (_transaction != null) _transaction.Rollback(); return false; } } /// /// 列表 /// /// /// public async Task<(List list, int total)> GetListAsync(ChangeBoxRecordQueryRequest dto, int companyId = 0) { if (companyId == 0) companyId = _loginRepositories.CompanyId; List ids = new List(); if (!string.IsNullOrEmpty(dto.Creator)) { var staffList = await _basicsRepositories.GetStaffListAsync(companyId); if (staffList != null) ids = staffList.Where(w => w.Name.Contains(dto.Creator)).Select(s => s.Id).ToList(); } var materials_result = await _erpService.BillQueryForMaterial(); List materials = new List(); if (materials_result.IsSuccess) materials = materials_result.Data.ToList(); var query = _context.ChangeBoxRecord .GroupJoin(_context.Box, changeBox => changeBox.SrcBoxId, srcBox => srcBox.Id, (changeBox, srcBox) => new { changeBox, srcBox }) .SelectMany(x => x.srcBox.DefaultIfEmpty(), (d, srcBox) => new { d.changeBox, srcBox }) .GroupJoin(_context.Box, d => d.changeBox.DestBoxId, destBox => destBox.Id, (d, destBox) => new { d.changeBox, d.srcBox, destBox }) .SelectMany(x => x.destBox.DefaultIfEmpty(), (d, destBox) => new { d.changeBox, d.srcBox, destBox }) .OrderByDescending(o => o.changeBox.Id) .Where(adv => 1 == 1); if (!string.IsNullOrEmpty(dto.Creator)) query = query.Where(w => ids.Contains(w.changeBox.CreatorId)); if (!string.IsNullOrEmpty(dto.SrcBox)) query = query.Where(w => EF.Functions.Like(w.srcBox.BoxBillNo, "%" + dto.SrcBox + "%")); if (!string.IsNullOrEmpty(dto.DestBox)) query = query.Where(w => EF.Functions.Like(w.destBox.BoxBillNo, "%" + dto.DestBox + "%")); if (dto.SrcSubStockId != null) query = query.Where(w => w.changeBox.SrcSubStockId == dto.SrcSubStockId); if (dto.DestSubStockId != null) query = query.Where(w => w.changeBox.DestSubStockId == dto.DestSubStockId); if (dto.CreateBeginDate != null) query = query.Where(w => w.changeBox.CreateTime >= dto.CreateBeginDate); if (dto.CreateEndDate != null) query = query.Where(w => w.changeBox.CreateTime <= dto.CreateEndDate); //组装 int total = await query.CountAsync(); var list = await query.Select(s => new ChangeBoxRecordQueryInfoResponse() { #region dto组装 BillNo = s.changeBox.BillNo, MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.changeBox.MaterialNumber), MaterialNumber = s.changeBox.MaterialNumber, Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.changeBox.MaterialNumber), //Stock = _singleDataService.GetSingleData(SingleAction.Stocks, _loginRepositories.CompanyId, s.changeBox.StockId), SerialNumbers = string.Join(",", s.changeBox.SerialNumbers), SrcBox = s.srcBox.BoxBillNo, DestBox = s.destBox.BoxBillNo, Qty=s.changeBox.Qty, DestBoxOrg= _singleDataService.GetSingleData(SingleAction.Orgs, companyId, s.changeBox.DestBoxOrgCode), SrcSubStock = _singleDataService.GetSingleData(SingleAction.SubStocks, companyId, s.changeBox.SrcSubStockId), DestSubStock = _singleDataService.GetSingleData(SingleAction.SubStocks, companyId, s.changeBox.DestSubStockId), Creator = _singleDataService.GetSingleData(SingleAction.Staffs, companyId, s.changeBox.CreatorId), CreateTime = s.changeBox.CreateTime.DateToStringSeconds() #endregion }).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync(); return (list, total); } public async Task<(object obj, int total)> GetListField(ChangeBoxRecordQueryRequest dto, int companyId) { return await GetListAsync(dto, companyId); } } }