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.Dto.OutStockTask; 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.Repositories.Configuration; namespace WMS.Web.Repositories { public class OutStockTaskRepositories : IOutStockTaskRepositories { private readonly IMapper _mapper; private readonly IServiceProvider _serviceProvider; private readonly RepositoryDbContext _context; private readonly ISingleDataService _singleDataService; public OutStockTaskRepositories(RepositoryDbContext context, IMapper mapper, IServiceProvider serviceProvider, ISingleDataService singleDataService) { _context = context; _mapper = mapper; _serviceProvider = serviceProvider; _singleDataService = singleDataService; } /// /// 新增 /// /// /// /// public async Task Add(OutStockTask entity, bool isTransaction = true) { IDbContextTransaction _transaction = null; if (isTransaction) _transaction = _context.Database.BeginTransaction(); try { await _context.OutStockTask.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 DeleteEntityList(List ids, bool isTransaction = true) { IDbContextTransaction _transaction = null; if (isTransaction) _transaction = _context.Database.BeginTransaction(); try { var res_list = await _context.OutStockTask .Include(s => s.Details) .Where(f => ids.Contains(f.Id)) .ToListAsync(); _context.OutStockTask.RemoveRange(res_list); await _context.SaveChangesAsync(); if (_transaction != null) _transaction.Commit(); return true; } catch (Exception ex) { if (_transaction != null) _transaction.Rollback(); return false; } } /// /// 批量修改 /// /// /// /// 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.OutStockTask .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.OutStockTask .Include(s => s.Details) .Where(f => ids.Contains(f.Id)) .ToListAsync(); return res.Clone(); } /// /// 列表 /// /// /// public async Task<(List list, int total)> GetListAsync(OutStockTaskQueryRequest dto) { var query = _context.OutStockTaskDetails .GroupJoin(_context.OutStockTask, 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 (dto.CreateBeginDate != null) query = query.Where(w => w.order.OperateTime >= dto.CreateBeginDate); if (dto.CreateEndDate != null) query = query.Where(w => w.order.OperateTime <= dto.CreateEndDate); //组装 int total = await query.CountAsync(); var list = await query.Select(s => new OutStockTaskQueryInfoResponse() { #region dto组装 Id = 0, DetailId=0, BillNo =s.order.BillNo, Status = "", Type = "", CreateTime = s.order.OperateTime.DateToStringSeconds(), Stock = "", SourceBillNo = "", SaleBillNo = "", DeliveryOrg = "", ReceiptCustomer = "", MaterialName = "", MaterialNumber = "", Specifications = "" #endregion }).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync(); return (list, total); } } }