Files
WMS-Api/src/WMS.Web.Repositories/InStockRepositories.cs
2023-10-30 18:12:20 +08:00

143 lines
5.2 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.Internal.Results;
using WMS.Web.Domain.Entitys;
using WMS.Web.Domain.Infrastructure;
using WMS.Web.Domain.IService.Public;
using WMS.Web.Domain.Values.Single;
using WMS.Web.Repositories.Configuration;
namespace WMS.Web.Repositories
{
/// <summary>
/// wms入库单-仓储
/// </summary>
public class InStockRepositories: IInStockRepositories
{
private readonly IMapper _mapper;
private readonly IServiceProvider _serviceProvider;
private readonly ILoginRepositories _loginRepositories;
private readonly RepositoryDbContext _context;
private readonly ISingleDataService _singleDataService;
public InStockRepositories(RepositoryDbContext context,
IMapper mapper,
ILoginRepositories loginRepositories,
IServiceProvider serviceProvider,
ISingleDataService singleDataService)
{
_context = context;
_mapper = mapper;
_serviceProvider = serviceProvider;
_loginRepositories = loginRepositories;
_singleDataService = singleDataService;
}
/// <summary>
/// 列表-分页
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<ResultPagedList<InStockQueryResponse>> GetPagedList(InStockQueryRequest dto)
{
var query=_context.InStockDetails
.GroupJoin(_context.Instock, 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.detail.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.CreateBeginDate != null)
query = query.Where(w => w.order.CreateTime >= dto.CreateBeginDate.Value);
if (dto.CreateEndDate != null)
query = query.Where(w => w.order.CreateTime <= dto.CreateEndDate.Value);
var response = new ResultPagedList<InStockQueryResponse>();
int total = await query.CountAsync();
response.TotalCount = total;
var list = await query.Select(s => new InStockQueryResponse()
{
Id=s.order.Id,
DetailsId=s.detail.Id,
BillNo=s.order.BillNo,
Type=s.order.Type.GetRemark(),
SourceBillNo=s.detail.SourceBillNo,
Supplier="",
Org="",
MaterialName="",
MaterialNumber="",
Specifications="",
Stock= _singleDataService.GetSingleData(SingleAction.Stocks, _loginRepositories.CompanyId, s.detail.StockId),
Qty =s.detail.Qty,
Creator = _singleDataService.GetSingleData(SingleAction.Staffs, _loginRepositories.CompanyId, s.order.CreatorId),
CreateTime =s.order.CreateTime,
SuccessSync=s.order.SuccessSync
}).OrderByDescending(x => x.Id).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
response.Data = list;
return response;
}
/// <summary>
/// 新增
/// </summary>
/// <param name="entity"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<InStock> Add(InStock entity, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
await _context.Instock.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;
}
}
}
}