Files
WMS-Api/src/WMS.Web.Repositories/MoveBoxRecordRepositories.cs
18942506660 ed42cc20f8 修复bug
2024-03-20 14:58:23 +08:00

202 lines
8.6 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.MoveBoxRecord;
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.Values;
using WMS.Web.Domain.Values.Single;
using WMS.Web.Repositories.Configuration;
namespace WMS.Web.Repositories
{
public class MoveBoxRecordRepositories : IAllFielRepositories<MoveBoxRecordQueryRequest>, IMoveBoxRecordRepositories
{
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 IErpService _erpService;
private readonly IErpBasicDataExtendService _erpBasicDataExtendService;
public MoveBoxRecordRepositories(RepositoryDbContext context, IMapper mapper, IServiceProvider serviceProvider,
ISingleDataService singleDataService, ILoginRepositories loginRepositories, IBasicsRepositories basicsRepositories,
IErpService erpService, IErpBasicDataExtendService erpBasicDataExtendService)
{
_context = context;
_mapper = mapper;
_serviceProvider = serviceProvider;
_singleDataService = singleDataService;
_loginRepositories = loginRepositories;
_basicsRepositories = basicsRepositories;
_erpBasicDataExtendService = erpBasicDataExtendService;
_erpService = erpService;
}
/// <summary>
/// 新增
/// </summary>
/// <param name="entity"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<MoveBoxRecord> Add(MoveBoxRecord entity, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
await _context.MoveBoxRecord.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;
}
}
public async Task<bool> AddRange(List<MoveBoxRecord> entitys, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
if (entitys != null && entitys.Count != 0)
{
await _context.MoveBoxRecord.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 (Exception ex)
{
if (_transaction != null)
_transaction.Rollback();
return false;
}
}
public async Task<List<BoxDetailResponse>> GetDetailsByBoxId(int boxId)
{
var res = await _context.MoveBoxRecord
.Include(s => s.Details)
.Where(f => f.BoxId == boxId && f.Type == MoveBoxType.Down)
.OrderByDescending(o => o.Id)
.ToListAsync();
List<BoxDetailResponse> details = new List<BoxDetailResponse>();
if (res.Count() == 0) return details;
var materials_result = await _erpService.BillQueryForMaterial();
if (!materials_result.IsSuccess)
return new List<BoxDetailResponse>();
var materials = materials_result.Data.ToList();
foreach (var d in res.First().Details)
{
BoxDetailResponse detail = new BoxDetailResponse();
detail.MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, d.MaterialNumber);
detail.MaterialNumber = d.MaterialNumber;
detail.Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, d.MaterialNumber);
detail.SerialNumbers = d.SerialNumbers;
detail.Qty = d.Qty;
details.Add(detail);
}
return details;
}
/// <summary>
/// 列表
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<(List<MoveBoxRecordQueryInfoResponse> list, int total)> GetListAsync(MoveBoxRecordQueryRequest dto, int companyId = 0)
{
if (companyId == 0)
companyId = _loginRepositories.CompanyId;
List<int> ids = new List<int>();
if (!string.IsNullOrEmpty(dto.Creator))
{
var staffList = await _basicsRepositories.GetStaffListAsync(companyId);
if (staffList != null)
ids = staffList.Where(w => w.Name.Contains(dto.Creator)).Select(s => s.Id).ToList();
}
var query = _context.MoveBoxRecord
.GroupJoin(_context.Box, moveBox => moveBox.BoxId, box => box.Id, (moveBox, box) => new { moveBox, box })
.SelectMany(x => x.box.DefaultIfEmpty(), (d, box) => new { d.moveBox, box })
.OrderByDescending(o => o.moveBox.Id)
.Where(adv => 1 == 1);
if (!string.IsNullOrEmpty(dto.SrcSubStockCode))
query = query.Where(w => w.moveBox.SrcSubStockCode == dto.SrcSubStockCode);
if (!string.IsNullOrEmpty(dto.DestSubStockCode))
query = query.Where(w => w.moveBox.DestSubStockCode == dto.DestSubStockCode);
if (!string.IsNullOrEmpty(dto.Creator))
query = query.Where(w => ids.Contains(w.moveBox.CreatorId));
if (!string.IsNullOrEmpty(dto.Box))
query = query.Where(w => EF.Functions.Like(w.box.BoxBillNo, "%" + dto.Box + "%"));
if (dto.Type != null)
query = query.Where(w => w.moveBox.Type == (MoveBoxType)dto.Type);
if (dto.CreateBeginDate != null)
query = query.Where(w => w.moveBox.CreateTime >= dto.CreateBeginDate);
if (dto.CreateEndDate != null)
query = query.Where(w => w.moveBox.CreateTime <= dto.CreateEndDate);
//组装
int total = await query.CountAsync();
var list = await query.Select(s => new MoveBoxRecordQueryInfoResponse()
{
#region dto组装
BillNo = s.moveBox.BillNo,
Box = s.box.BoxBillNo,
SrcSubStock = _singleDataService.GetSingleData(SingleAction.SubStocksJoinOrgCode, companyId, s.moveBox.SrcSubStockCode + s.moveBox.StockCode + s.moveBox.OrgCode),
DestSubStock = _singleDataService.GetSingleData(SingleAction.SubStocksJoinOrgCode, companyId, s.moveBox.DestSubStockCode + s.moveBox.StockCode + s.moveBox.OrgCode),
Qty = s.moveBox.Qty,
Type = s.moveBox.Type.GetRemark(),
Creator = _singleDataService.GetSingleData(SingleAction.Staffs, companyId, s.moveBox.CreatorId),
CreateTime = s.moveBox.CreateTime.DateToStringSeconds()
#endregion
}).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
return (list, total);
}
public async Task<(object obj, int total)> GetListField(MoveBoxRecordQueryRequest dto, int companyId)
{
return await GetListAsync(dto, companyId);
}
}
}