205 lines
8.3 KiB
C#
205 lines
8.3 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.InStock;
|
|
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.Values.Single;
|
|
using WMS.Web.Repositories.Configuration;
|
|
|
|
namespace WMS.Web.Repositories
|
|
{
|
|
/// <summary>
|
|
/// wms入库单-仓储
|
|
/// </summary>
|
|
public class InStockRepositories: IAllFielRepositories<InStockQueryRequest>, IInStockRepositories
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IServiceProvider _serviceProvider;
|
|
private readonly ILoginRepositories _loginRepositories;
|
|
private readonly RepositoryDbContext _context;
|
|
private readonly ISingleDataService _singleDataService;
|
|
private readonly IErpService _erpService;
|
|
private readonly IErpBasicDataExtendService _erpBasicDataExtendService;
|
|
|
|
|
|
public InStockRepositories(RepositoryDbContext context,
|
|
IMapper mapper,
|
|
IErpService erpService,
|
|
ILoginRepositories loginRepositories,
|
|
IServiceProvider serviceProvider,
|
|
ISingleDataService singleDataService,
|
|
IErpBasicDataExtendService erpBasicDataExtendService)
|
|
{
|
|
_context = context;
|
|
_mapper = mapper;
|
|
_erpService = erpService;
|
|
_serviceProvider = serviceProvider;
|
|
_loginRepositories = loginRepositories;
|
|
_singleDataService = singleDataService;
|
|
_erpBasicDataExtendService = erpBasicDataExtendService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 列表-分页
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
public async Task<(List<InStockQueryResponse> list, int total)> GetPagedList(InStockQueryRequest dto)
|
|
{
|
|
//1.获取物料集合和组织集合和供应商的集合
|
|
var materials_result = await _erpService.BillQueryForMaterial();
|
|
if (!materials_result.IsSuccess)
|
|
return (new List<InStockQueryResponse>(), 0);
|
|
var materials = materials_result.Data.ToList();
|
|
|
|
//组织集合
|
|
var orgs_result = await _erpService.BillQueryForOrg();
|
|
if (!orgs_result.IsSuccess)
|
|
return (new List<InStockQueryResponse>(), 0);
|
|
var orgs = orgs_result.Data.ToList();
|
|
|
|
//供应商集合
|
|
var suppliers_result = await _erpService.BillQueryForSupplier();
|
|
if (!suppliers_result.IsSuccess)
|
|
return (new List<InStockQueryResponse>(), 0);
|
|
var suppliers = suppliers_result.Data.ToList();
|
|
|
|
//物料集合;模糊查询后的物料集合
|
|
if (!string.IsNullOrEmpty(dto.MaterialNumber))
|
|
materials = materials.Where(w => EF.Functions.Like(w.MaterialNumber, "%" + dto.MaterialNumber + "%")).ToList();
|
|
|
|
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 + "%"));
|
|
|
|
//物料ID在模糊后的物料
|
|
if (materials != null && materials.Count != 0 && !string.IsNullOrEmpty(dto.MaterialNumber))
|
|
{
|
|
var mids = materials.Select(x => x.MaterialId).ToList();
|
|
query = query.Where(w => mids.Contains(w.detail.MaterialId));
|
|
}
|
|
|
|
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 (!string.IsNullOrEmpty(dto.StockCode))
|
|
query = query.Where(w => w.detail.StockCode == dto.StockCode);
|
|
|
|
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);
|
|
|
|
|
|
int total = await query.CountAsync();
|
|
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= _erpBasicDataExtendService.GetSupplierName(suppliers, s.detail.SupplierId),
|
|
Org = _erpBasicDataExtendService.GetOrgName(orgs, s.detail.OrgId),
|
|
MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.detail.MaterialId),
|
|
MaterialNumber = _erpBasicDataExtendService.GetMaterialNumber(materials, s.detail.MaterialId),
|
|
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.detail.MaterialId),
|
|
Stock = _singleDataService.GetSingleData(SingleAction.Stocks, _loginRepositories.CompanyId, s.detail.StockCode),
|
|
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();
|
|
|
|
return (list, total);
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 导出
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <param name="companyId"></param>
|
|
/// <returns></returns>
|
|
public async Task<(object obj, int total)> GetListField(InStockQueryRequest dto, int companyId)
|
|
{
|
|
return await GetPagedList(dto);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取详情
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<InStockInfoResponse> GetInfo(int id)
|
|
{
|
|
var res = await _context.Instock
|
|
.FirstOrDefaultAsync(f => id == f.Id);
|
|
if (res == null) return null;
|
|
var response = new InStockInfoResponse()
|
|
{
|
|
CreateTime = res.CreateTime.DateToStringSeconds(),
|
|
SyncTime = res.SyncTime.DateToStringSeconds(),
|
|
Creator = _singleDataService.GetSingleData(SingleAction.Staffs, _loginRepositories.CompanyId, res.CreatorId),
|
|
Operate = _singleDataService.GetSingleData(SingleAction.Staffs, _loginRepositories.CompanyId, res.OperateId)
|
|
};
|
|
return response;
|
|
}
|
|
}
|
|
}
|