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.OutStock; 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.Mappers; using WMS.Web.Domain.Values; using WMS.Web.Domain.Values.Single; using WMS.Web.Repositories.Configuration; namespace WMS.Web.Repositories { public class OutStockRepositories : IOutStockRepositories { 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 OutStockRepositories(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(OutStock entity, bool isTransaction = true) { IDbContextTransaction _transaction = null; if (isTransaction) _transaction = _context.Database.BeginTransaction(); try { await _context.OutStock.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<(List list, int total)> GetListAsync(OutStockQueryRequest 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.OutStockDetails .GroupJoin(_context.OutStock, detail => detail.Fid, order => order.Id, (detail, orders) => new { detail, orders }) .SelectMany(x => x.orders.DefaultIfEmpty(), (p, order) => new { p.detail, order }) .OrderByDescending(o => o.order.Id) .Where(adv => 1 == 1); //if (!string.IsNullOrEmpty(dto.ReceiptCustomer)) //if (!string.IsNullOrEmpty(dto.MaterialNumber)) if (ids.Count() > 0) query = query.Where(w => ids.Contains(w.order.CreatorId)); if (!string.IsNullOrEmpty(dto.SourceBillNo)) query = query.Where(w => EF.Functions.Like(w.detail.SourceBillNo, "%" + dto.SourceBillNo + "%")); if (dto.Type != null) query = query.Where(w => w.order.Type == (OrderType)dto.Type); if (dto.SuccessSync != null) query = query.Where(w => w.order.SuccessSync == dto.SuccessSync); if (dto.DeliveryOrgId != null) query = query.Where(w => w.detail.DeliveryOrgId == dto.DeliveryOrgId); if (dto.StockId != null) query = query.Where(w => w.detail.StockId == dto.StockId); if (dto.CreateBeginDate != null) query = query.Where(w => w.order.CreateTime >= dto.CreateBeginDate); if (dto.CreateEndDate != null) query = query.Where(w => w.order.CreateTime <= dto.CreateEndDate); //组装 int total = await query.CountAsync(); var list = await query.Select(s => new OutStockQueryInfoResponse() { #region dto组装 Id = s.order.Id, BillNo = s.order.BillNo, Status = s.order.Status.GetRemark(), Type = s.order.Type.GetRemark(), Creator = _singleDataService.GetSingleData(SingleAction.Staffs, _loginRepositories.CompanyId, s.order.CreatorId), CreateTime = s.order.CreateTime.DateToStringSeconds(), SuccessSync = s.order.SuccessSync, Stock = _singleDataService.GetSingleData(SingleAction.Stocks, _loginRepositories.CompanyId, s.detail.StockId), SourceBillNo = s.detail.SourceBillNo, SaleBillNo = s.detail.SaleBillNo, DeliveryOrg = "", ReceiptCustomer = _singleDataService.GetSingleData(SingleAction.Customers, _loginRepositories.CompanyId, s.detail.ReceiptCustomerId), MaterialName = "", MaterialNumber = "", Specifications = "", Qty = s.detail.Qty #endregion }).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync(); return (list, total); } /// /// 批量修改 /// /// /// /// public async Task EditEntityList(List entitys, bool isTransaction = true) { IDbContextTransaction _transaction = null; if (isTransaction) _transaction = _context.Database.BeginTransaction(); try { List list = entitys.Select(s => s.Id).ToList(); var res = await _context.OutStock .Include(s => s.Details) .Where(f => list.Contains(f.Id)).ToListAsync(); _mapper.ToMapList(entitys, res); _mapper.ToMapList(entitys.SelectMany(s => s.Details).ToList(), res.SelectMany(s => s.Details).ToList()); await _context.SaveChangesAsync(); if (_transaction != null) _transaction.Commit(); } catch (Exception ex) { if (_transaction != null) _transaction.Rollback(); return false; } return true; } /// /// 根据单据头id获取数据 /// /// /// public async Task> GetEntityList(List ids) { var res = await _context.OutStock .Include(s => s.Details) .Where(f => ids.Contains(f.Id)) .ToListAsync(); return res.Clone(); } /// /// 编辑 /// /// /// /// public async Task Edit(OutStock entity, bool isTransaction = true) { IDbContextTransaction _transaction = null; if (isTransaction) _transaction = _context.Database.BeginTransaction(); try { var res = await _context.OutStock .Include(s => s.Details) .FirstOrDefaultAsync(f => f.Id == entity.Id); if (res == null) return null; _mapper.Map(entity, res); _mapper.ToMapList(entity.Details, res.Details); await _context.SaveChangesAsync(); if (_transaction != null) _transaction.Commit(); return res; } catch (Exception) { if (_transaction != null) _transaction.Rollback(); return null; } } /// /// 根据任务单Id获取 /// /// /// public async Task GetByTaskId(int taskId) { var res = await _context.OutStock .Include(s => s.Details) .FirstOrDefaultAsync(f => taskId == f.TaskId); return res.Clone(); } } }