Files
WMS-Api/src/WMS.Web.Repositories/InStockTaskRepositories.cs
2023-11-06 12:34:39 +08:00

364 lines
13 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;
using WMS.Web.Core.Dto.InStockTask;
using WMS.Web.Core.Help;
using WMS.Web.Core.Internal.Results;
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
{
/// <summary>
/// 入库任务表-仓储
/// </summary>
public class InStockTaskRepositories : IInStockTaskRepositories
{
private readonly IMapper _mapper;
private readonly IServiceProvider _serviceProvider;
private readonly RepositoryDbContext _context;
private readonly ISingleDataService _singleDataService;
private readonly ILoginRepositories _loginRepositories;
public InStockTaskRepositories(RepositoryDbContext context,
IMapper mapper,
IServiceProvider serviceProvider,
ISingleDataService singleDataService,
ILoginRepositories loginRepositories)
{
_context = context;
_mapper = mapper;
_serviceProvider = serviceProvider;
_singleDataService = singleDataService;
_loginRepositories = loginRepositories;
}
/// <summary>
/// 列表-分页
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<ResultPagedList<InStockTaskQueryResponse>> GetPagedList(InStockTaskQueryRequest dto)
{
var query = _context.InStockTaskDetails
.GroupJoin(_context.InStockTask, detail => detail.Fid, order => order.Id, (detail, orders) => new { detail, orders })
.SelectMany(x => x.orders.DefaultIfEmpty(), (p, order) => new { p.detail, order })
.Where(adv => 1 == 1);
if (!string.IsNullOrEmpty(dto.SourceBillNo))
query = query.Where(w => EF.Functions.Like(w.order.SourceBillNo, "%" + dto.SourceBillNo + "%"));
//if (!string.IsNullOrEmpty(dto.MaterialNumber))
// query = query.Where(w => EF.Functions.Like(w.detail.BillNo, "%" + dto.BillNo + "%"));
if (dto.SupplierId.HasValue)
query = query.Where(w => w.detail.SupplierId == dto.SupplierId.Value);
if (dto.OrgId.HasValue)
query = query.Where(w => w.detail.OrgId == dto.OrgId.Value);
if (dto.StockId.HasValue)
query = query.Where(w => w.detail.StockId == dto.StockId.Value);
if (dto.Type.HasValue)
query = query.Where(w => (int)w.order.Type == dto.Type.Value);
if (dto.Status.HasValue)
query = query.Where(w => (int)w.order.Status == dto.Status.Value);
if (dto.CreateBeginDate != null)
query = query.Where(w => w.order.ShelfTime >= dto.CreateBeginDate.Value);
if (dto.CreateEndDate != null)
query = query.Where(w => w.order.ShelfTime <= dto.CreateEndDate.Value);
var response = new ResultPagedList<InStockTaskQueryResponse>();
int total = await query.CountAsync();
response.TotalCount = total;
var list = await query.Select(s => new InStockTaskQueryResponse()
{
Id = s.order.Id,
DetailsId = s.detail.Id,
BillNo = s.order.BillNo,
Type = s.order.Type.GetRemark(),
Status = s.order.Status.GetRemark(),
SourceBillNo = s.order.SourceBillNo,
Supplier = "",
Org = "",
MaterialName = "",
MaterialNumber = "",
Specifications = "",
FactoryPrice = s.detail.FactoryPrice,
Stock = "",
AccruedQty = s.detail.AccruedQty,
ReceiveQty = s.detail.ReceiveQty,
RealityQty = s.detail.RealityQty,
Receiver = "",
//ReceiveTime= s.order.ReceiveTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
ReceiveTime = s.order.ReceiveTime.HasValue ? s.order.ReceiveTime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") : "",
Shelfer = "",
//ShelfTime= s.order.ShelfTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
ShelfTime = s.order.ShelfTime.HasValue ? s.order.ShelfTime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") : "",
CreateTime = s.detail.CreateTime,
}).OrderByDescending(x => x.Id).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
response.Data = list;
return response;
}
/// <summary>
/// 详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<InStockTask> Get(int id)
{
var entity =await _context.InStockTask
.Include(x=>x.Boxs)
.Include(s => s.Details)
.FirstOrDefaultAsync(f => f.Id == id);
return entity.Clone();
}
/// <summary>
/// 实体:根据箱号
/// </summary>
/// <param name="boxBillNo"></param>
/// <returns></returns>
public async Task<InStockTask> GetBy(string boxBillNo)
{
var entity= await _context.InStockTask
.Include(s => s.Boxs.Where(b => b.BoxBillNo == boxBillNo))
.Include(s => s.Details)
.Where(w => w.Boxs.Where(b => b.BoxBillNo == boxBillNo).Count() > 0).FirstOrDefaultAsync();
return entity.Clone();
}
/// <summary>
/// 箱号是否绑定了任务实体:待入库和部分入库状态中
/// </summary>
/// <param name="boxBillNo"></param>
/// <returns></returns>
public async Task<bool> IsExist(string boxBillNo)
{
return await _context.InStockTask
.AsNoTracking()
.Include(s => s.Boxs.Where(b => b.BoxBillNo == boxBillNo))
.Include(s => s.Details)
.Where(w => (w.Status == InstockStatus.Part || w.Status == InstockStatus.Wait) && w.Boxs.Where(b => b.BoxBillNo == boxBillNo).Count() > 0).AnyAsync();
}
/// <summary>
/// 列表
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<List<InStockTask>> GetList(List<int> ids)
{
var entitys= await _context.InStockTask
.Include(s => s.Details)
.Where(f => ids.Contains(f.Id))
.ToListAsync();
return entitys.Clone();
}
/// <summary>
/// 列表-根据明细中的来源单号
/// </summary>
/// <param name="sourceBillNos"></param>
/// <returns></returns>
public async Task<List<InStockTask>> GetListBy(List<string> sourceBillNos)
{
var entitys= await _context.InStockTask
.Include(s => s.Details)
.Where(w => sourceBillNos.Contains(w.SourceBillNo))
.ToListAsync();
return entitys.Clone();
}
/// <summary>
/// 列表-根据来源单号模糊
/// </summary>
/// <param name="sourceBillNo"></param>
/// <returns></returns>
public async Task<List<InStockTaskBillNoQueryResponse>> GetListBy(string sourceBillNo, InstockType? type=null)
{
var query = _context.InStockTaskDetails
.GroupJoin(_context.InStockTask, detail => detail.Fid, order => order.Id, (detail, orders) => new { detail, orders })
.SelectMany(x => x.orders.DefaultIfEmpty(), (p, order) => new { p.detail, order })
.Where(w => EF.Functions.Like(w.order.SourceBillNo, "%" + sourceBillNo + "%"));
if(type.HasValue)
query = query.Where(w => w.order.Type == type.Value);
var list = await query.Select(s => new InStockTaskBillNoQueryResponse()
{
Id=s.order.Id,
DetailsId=s.detail.Id,
SourceBillNo=s.order.SourceBillNo,
MaterialId=s.detail.MaterialId,
StockCode=s.detail.StockCode,
StockName = _singleDataService.GetSingleData(SingleAction.Stocks, _loginRepositories.CompanyId, s.detail.StockId),
MaterialName = "",
MaterialNumber = "",
Specifications = "",
Qty=s.detail.AccruedQty,
DeliveredQty=s.detail.DeliveredQty,
Remark = s.detail.Remark
}).ToListAsync();
return list;
}
/// <summary>
/// 批量添加
/// </summary>
/// <param name="entitys"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<bool> AddRange(List<InStockTask> entitys, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
if (entitys != null && entitys.Count != 0)
{
await _context.InStockTask.AddRangeAsync(entitys);
await _context.SaveChangesAsync();
foreach (var item in entitys)
{
if (string.IsNullOrEmpty(item.BillNo))
//自动生成单据编号
item.MakeBillNo(item.Id);
}
await _context.SaveChangesAsync();
}
if (_transaction != null)
_transaction.Commit();
return true;
}
catch
{
if (_transaction != null)
_transaction.Rollback();
return false;
}
}
/// <summary>
/// 添加
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<InStockTask> Add(InStockTask entity, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
//创建单据编号
entity.MakeBillNo(entity.Id);
await _context.InStockTask.AddAsync(entity);
var res = await _context.SaveChangesAsync();
if (_transaction != null)
_transaction.Commit();
return entity;
}
catch
{
if (_transaction != null)
_transaction.Rollback();
return null;
}
}
/// <summary>
/// 批量修改
/// </summary>
/// <param name="entitys"></param>
/// <returns></returns>
public async Task<bool> UpdateRange(List<InStockTask> 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.InStockTask.Include(x=>x.Boxs).Include(x => x.Details).Where(f => list.Contains(f.Id)).ToListAsync();
_mapper.ToMapList(entitys, res);
await _context.SaveChangesAsync();
if (_transaction != null)
_transaction.Commit();
}
catch (Exception ex)
{
if (_transaction != null)
_transaction.Rollback();
return false;
}
return true;
}
}
/// <summary>
/// 修改
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<InStockTask> Update(InStockTask entity, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
var model = await _context.InStockTask
.Include(s=>s.Boxs)
.Include(s => s.Details)
.FirstOrDefaultAsync(f => f.Id == entity.Id);
if (model == null)
return null;
_mapper.Map(entity, model);
_mapper.ToMapList(entity.Boxs, model.Boxs);
_mapper.ToMapList(entity.Details, model.Details);
var result= await _context.SaveChangesAsync();
if (_transaction != null)
_transaction.Commit();
return model;
}
catch (Exception ex)
{
if (_transaction != null)
_transaction.Rollback();
return null;
}
}
}
}