Files
WMS-Api/src/WMS.Web.Repositories/MoveBoxRecordRepositories.cs
18942506660 70cb97a29e 修复bug
2023-12-18 16:00:08 +08:00

167 lines
6.8 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.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;
public MoveBoxRecordRepositories(RepositoryDbContext context, IMapper mapper, IServiceProvider serviceProvider,
ISingleDataService singleDataService, ILoginRepositories loginRepositories, IBasicsRepositories basicsRepositories)
{
_context = context;
_mapper = mapper;
_serviceProvider = serviceProvider;
_singleDataService = singleDataService;
_loginRepositories = loginRepositories;
_basicsRepositories = basicsRepositories;
}
/// <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
{
if (_transaction != null)
_transaction.Rollback();
return false;
}
}
/// <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(_loginRepositories.CompanyId);
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 (dto.SrcSubStockId != null)
query = query.Where(w => w.moveBox.SrcSubStockId == dto.SrcSubStockId);
if (dto.DestSubStockId != null)
query = query.Where(w => w.moveBox.DestSubStockId == dto.DestSubStockId);
if (ids.Count() > 0)
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.SubStocks, companyId, s.moveBox.SrcSubStockId),
DestSubStock = _singleDataService.GetSingleData(SingleAction.SubStocks, companyId, s.moveBox.DestSubStockId),
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);
}
}
}