244 lines
8.9 KiB
C#
244 lines
8.9 KiB
C#
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
|
using NPOI.OpenXmlFormats.Spreadsheet;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using WMS.Web.Core.Dto;
|
|
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
|
|
{
|
|
/// <summary>
|
|
/// 老ops箱信息
|
|
/// </summary>
|
|
public class BoxRepositories : IBoxRepositories
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IServiceProvider _serviceProvider;
|
|
private readonly RepositoryDbContext _context;
|
|
private readonly ISingleDataService _singleDataService;
|
|
private readonly ILoginRepositories _loginRepositories;
|
|
private readonly IBasicsRepositories _basicsRepositories;
|
|
private readonly IErpBasicDataExtendService _erpBasicDataExtendService;
|
|
private readonly IErpService _erpService;
|
|
public BoxRepositories(RepositoryDbContext context, IMapper mapper, IServiceProvider serviceProvider,
|
|
ISingleDataService singleDataService, ILoginRepositories loginRepositories,
|
|
IBasicsRepositories basicsRepositories, IErpBasicDataExtendService erpBasicDataExtendService,
|
|
IErpService erpServic)
|
|
{
|
|
_context = context;
|
|
_mapper = mapper;
|
|
_serviceProvider = serviceProvider;
|
|
_singleDataService = singleDataService;
|
|
_loginRepositories = loginRepositories;
|
|
_basicsRepositories = basicsRepositories;
|
|
_erpBasicDataExtendService = erpBasicDataExtendService;
|
|
_erpService = erpServic;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<Box> Add(Box entity, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
await _context.Box.AddAsync(entity);
|
|
await _context.SaveChangesAsync();
|
|
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
return entity;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_transaction != null)
|
|
_transaction.Rollback();
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
public async Task<Box> Get(int id)
|
|
{
|
|
var entity = await _context.Box.Include(x => x.Details)
|
|
.FirstOrDefaultAsync(f => f.Id.Equals(id));
|
|
|
|
return entity.Clone();
|
|
}
|
|
/// <summary>
|
|
/// 根据箱号查询物料信息
|
|
/// </summary>
|
|
/// <param name="BoxBillNo"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<BoxResponse>> GetBox(List<string> BoxBillNos)
|
|
{
|
|
var list = await _context.Box.Include(x => x.Details).Where(f => BoxBillNos.Contains(f.BoxBillNo.ToLower())).ToListAsync();
|
|
var resList = _mapper.Map<List<BoxResponse>>(list);
|
|
|
|
var materials_result = await _erpService.BillQueryForMaterial();
|
|
var materials = materials_result.Data.ToList();
|
|
|
|
var ids = list.Select(s => s.Id).ToList();
|
|
//获取所有序列号
|
|
var serialList = await _context.SerialNumbers.Where(f => ids.Contains(f.BoxId)).ToListAsync();
|
|
|
|
//获取物料信息 显示物料三件套
|
|
var mIds = list.SelectMany(s => s.Details).Select(s => s.MaterialNumber).ToList();
|
|
foreach (var r in resList)
|
|
{
|
|
foreach (var detail in r.Details)
|
|
{
|
|
detail.MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, detail.MaterialNumber);
|
|
detail.MaterialNumber = detail.MaterialNumber;
|
|
detail.Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, detail.MaterialNumber);
|
|
detail.SerialNumbers = serialList.Where(w => w.BoxId == r.Id && w.MaterialNumber == detail.MaterialNumber).Select(s => s.SerialNumber).ToList();
|
|
}
|
|
r.TotalQty = r.Details.Sum(s => s.Qty);
|
|
}
|
|
return resList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量修改
|
|
/// </summary>
|
|
/// <param name="entitys"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> EditEntityList(List<Box> 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.Box
|
|
.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;
|
|
|
|
}
|
|
|
|
public async Task<Box> GetByNo(string billNo)
|
|
{
|
|
var entity = await _context.Box.Include(x => x.Details)
|
|
.FirstOrDefaultAsync(f => f.BoxBillNo.Equals(billNo));
|
|
|
|
return entity.Clone();
|
|
}
|
|
public async Task<List<Box>> GetEntityListByNos(List<string> billNos)
|
|
{
|
|
var entity = await _context.Box.Include(x => x.Details)
|
|
.Where(f => billNos.Contains(f.BoxBillNo)).ToListAsync();
|
|
|
|
return entity.Clone();
|
|
}
|
|
//根据箱号搜索 用来比对确定是否箱号信息是否存在
|
|
public async Task<List<string>> GetByNos(List<string> billNos)
|
|
{
|
|
return await _context.Box
|
|
.Where(w => billNos.Contains(w.BoxBillNo)).Select(s => s.BoxBillNo).ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> AddRange(List<Box> entitys, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
if (entitys != null && entitys.Count != 0)
|
|
{
|
|
await _context.Box.AddRangeAsync(entitys);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
if (_transaction != null)
|
|
_transaction.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取箱集合
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<Box>> GetEntityList(List<int> ids)
|
|
{
|
|
var res = await _context.Box
|
|
.Include(s => s.Details)
|
|
.Where(f => ids.Contains(f.Id))
|
|
.ToListAsync();
|
|
|
|
return res.Clone();
|
|
}
|
|
/// <summary>
|
|
/// 批量删除
|
|
/// </summary>
|
|
/// <param name="billNos"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<bool> DeleteEntityList(List<int> boxIds, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var res = await _context.Box
|
|
.Include(s => s.Details)
|
|
.Where(f => boxIds.Contains(f.Id)).ToListAsync();
|
|
|
|
_context.Box.RemoveRange(res);
|
|
await _context.SaveChangesAsync();
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_transaction != null)
|
|
_transaction.Rollback();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|