Files
WMS-Api/src/WMS.Web.Repositories/OutStockTaskRepositories.cs
2023-11-06 14:56:31 +08:00

328 lines
12 KiB
C#

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.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.Domain.Values;
using WMS.Web.Domain.Values.Single;
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;
private readonly ILoginRepositories _loginRepositories;
public OutStockTaskRepositories(RepositoryDbContext context, IMapper mapper, IServiceProvider serviceProvider,
ISingleDataService singleDataService, ILoginRepositories loginRepositories)
{
_context = context;
_mapper = mapper;
_serviceProvider = serviceProvider;
_singleDataService = singleDataService;
_loginRepositories = loginRepositories;
}
/// <summary>
/// 新增
/// </summary>
/// <param name="entity"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<OutStockTask> 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;
}
}
/// <summary>
/// 批量删除(合并后删除原数据)
/// </summary>
/// <param name="ids"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<bool> DeleteEntityList(List<int> 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;
}
}
/// <summary>
/// 批量修改
/// </summary>
/// <param name="entitys"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<bool> EditEntityList(List<OutStockTask> entitys, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
List<int> 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;
}
/// <summary>
/// 根据Id获取实体
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<OutStockTask> Get(int id)
{
var res = await _context.OutStockTask
.Include(s => s.Details)
.FirstOrDefaultAsync(f => id == f.Id);
return res.Clone();
}
/// <summary>
/// 根据单据头id获取数据
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
public async Task<List<OutStockTask>> GetEntityList(List<int> ids)
{
var res = await _context.OutStockTask
.Include(s => s.Details)
.Where(f => ids.Contains(f.Id))
.ToListAsync();
return res.Clone();
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="entity"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<OutStockTask> Edit(OutStockTask entity, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
var res = await _context.OutStockTask
.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;
}
}
/// <summary>
/// 列表
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<(List<OutStockTaskQueryInfoResponse> 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 (!string.IsNullOrEmpty(dto.ReceiptCustomer))
//if (!string.IsNullOrEmpty(dto.MaterialNumber))
if (dto.Ids.Count() > 0)
query = query.Where(w => dto.Ids.Contains(w.detail.Id));
if (!string.IsNullOrEmpty(dto.SourceBillNo))
query = query.Where(w => EF.Functions.Like(w.order.SourceBillNo, "%" + dto.SourceBillNo + "%"));
if (dto.Type != null)
query = query.Where(w => w.order.Type == (OutStockType)dto.Type);
if (dto.Status != null)
query = query.Where(w => w.order.Status == (OutStockStatus)dto.Status);
if (dto.DeliveryOrgId != null)
query = query.Where(w => w.order.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.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 = s.order.Id,
DetailId=s.detail.Id,
BillNo =s.order.BillNo,
Status = s.order.Status.GetRemark(),
Type = s.order.Type.GetRemark(),
CreateTime = s.order.OperateTime.DateToStringSeconds(),
Stock = _singleDataService.GetSingleData(SingleAction.Stocks, _loginRepositories.CompanyId, s.detail.StockId),
SourceBillNo = s.order.SourceBillNo,
SaleBillNo = s.detail.SaleBillNo,
DeliveryOrg = "",
ReceiptCustomer = _singleDataService.GetSingleData(SingleAction.Customers, _loginRepositories.CompanyId, s.order.ReceiptCustomerId),
MaterialName = "",
MaterialNumber = "",
Specifications = ""
#endregion
}).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
return (list, total);
}
/// <summary>
/// 根据订单号获取订单信息
/// </summary>
/// <param name="billNo"></param>
/// <returns></returns>
public async Task<GetOutStockTaskByNoResponse> GetOutStockTaskByNo(string billNo)
{
var entity = await _context.OutStockTask.Include(x => x.Details).FirstOrDefaultAsync(f => billNo.Equals(f.BillNo));
var response = _mapper.Map<GetOutStockTaskByNoResponse>(entity);
//获取物料信息 显示物料三件套
var mIds = entity.Details.Select(s => s.MaterialId).ToList();
return response;
}
/// <summary>
/// 根据来源单号搜索
/// </summary>
/// <param name="sourceBillNos"></param>
/// <returns></returns>
public async Task<List<OutStockTask>> GetListBySourceBillNo(List<string> sourceBillNos)
{
var entitys = await _context.OutStockTask
.Include(s => s.Details)
.Where(w => sourceBillNos.Contains(w.SourceBillNo))
.ToListAsync();
return entitys.Clone();
}
/// <summary>
/// 批量添加
/// </summary>
/// <param name="entitys"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<bool> AddRange(List<OutStockTask> entitys, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
if (entitys != null && entitys.Count != 0)
{
await _context.OutStockTask.AddRangeAsync(entitys);
await _context.SaveChangesAsync();
foreach (var item in entitys)
{
if (string.IsNullOrEmpty(item.BillNo))
//自动生成单据编号
item.GenerateNo();
}
await _context.SaveChangesAsync();
}
if (_transaction != null)
_transaction.Commit();
return true;
}
catch
{
if (_transaction != null)
_transaction.Rollback();
return false;
}
}
}
}