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.MoveBoxRecord; 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; using WMS.Web.Domain.Values.Single; using WMS.Web.Repositories.Configuration; namespace WMS.Web.Repositories { public class MoveBoxRecordRepositories : IMoveBoxRecordRepositories { 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 MoveBoxRecordRepositories(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 Add(MoveBoxRecord entity, bool isTransaction = true) { IDbContextTransaction _transaction = null; if (isTransaction) _transaction = _context.Database.BeginTransaction(); try { await _context.MoveBoxRecord.AddAsync(entity); await _context.SaveChangesAsync(); //if (string.IsNullOrEmpty(entity.OutSourcFeedNo)) //{ // 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<(List list, int total)> GetListAsync(MoveBoxRecordQueryRequest dto) { List ids = new List(); if (!string.IsNullOrEmpty(dto.Creator)) { var staffList = await _basicsRepositories.GetStaffListAsync(_loginRepositories.CompanyId); ids = staffList.Where(w => EF.Functions.Like(w.Name, "%" + dto.Creator + "%")).Select(s => s.Id).ToList(); } var query = _context.MoveBoxRecord .GroupJoin(_context.Box, moveBox => moveBox.BoxId, box => box.Id, (moveBox, box) => new { moveBox, box }) .SelectMany(x => x.box.DefaultIfEmpty(), (d, box) => new { d.moveBox, box }) .OrderByDescending(o => o.moveBox.Id) .Where(adv => 1 == 1); if (ids.Count() > 0) query = query.Where(w => ids.Contains(w.moveBox.CreatorId)); if (!string.IsNullOrEmpty(dto.Box)) query = query.Where(w => EF.Functions.Like(w.box.BoxBillNo, "%" + dto.Box + "%")); if (dto.Type != null) query = query.Where(w => w.moveBox.Type == (MoveBoxType)dto.Type); if (dto.CreateBeginDate != null) query = query.Where(w => w.moveBox.CreateTime >= dto.CreateBeginDate); if (dto.CreateEndDate != null) query = query.Where(w => w.moveBox.CreateTime <= dto.CreateEndDate); //组装 int total = await query.CountAsync(); var list = await query.Select(s => new MoveBoxRecordQueryInfoResponse() { #region dto组装 Box = s.box.BoxBillNo, SrcSubStock = _singleDataService.GetSingleData(SingleAction.SubStocks, _loginRepositories.CompanyId, s.moveBox.SrcSubStockCode), DestSubStock = _singleDataService.GetSingleData(SingleAction.SubStocks, _loginRepositories.CompanyId, s.moveBox.DestSubStockCode), Qty = s.moveBox.Qty, Type = s.moveBox.Type.GetRemark(), Creator = _singleDataService.GetSingleData(SingleAction.Staffs, _loginRepositories.CompanyId, s.moveBox.CreatorId), CreateTime = s.moveBox.CreateTime.DateToStringSeconds() #endregion }).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync(); return (list, total); } } }