Files
WMS-Api/src/WMS.Web.Repositories/ChangeBoxRecordRepositories.cs
18942506660 3baab2cea1 调整列表
2023-12-18 14:39:17 +08:00

183 lines
8.1 KiB
C#

using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Newtonsoft.Json;
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.Dto.ChangeBoxRecord;
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.Single;
using WMS.Web.Repositories.Configuration;
namespace WMS.Web.Repositories
{
public class ChangeBoxRecordRepositories : IAllFielRepositories<ChangeBoxRecordQueryRequest>, IChangeBoxRecordRepositories
{
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 ChangeBoxRecordRepositories(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;
_erpService = erpService;
_erpBasicDataExtendService = erpBasicDataExtendService;
}
/// <summary>
/// 新增
/// </summary>
/// <param name="entity"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<ChangeBoxRecord> Add(ChangeBoxRecord entity, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
await _context.ChangeBoxRecord.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="entity"></param>
/// <param name="isTransaction"></param>
/// <returns></returns>
public async Task<bool> AddRange(List<ChangeBoxRecord> list, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
_context.ChangeBoxRecord.AddRange(list);
await _context.SaveChangesAsync();
foreach (var item in list)
{
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;
}
}
/// <summary>
/// 列表
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<(List<ChangeBoxRecordQueryInfoResponse> list, int total)> GetListAsync(ChangeBoxRecordQueryRequest dto)
{
List<int> ids = new List<int>();
if (!string.IsNullOrEmpty(dto.Creator))
{
var staffList = await _basicsRepositories.GetStaffListAsync(_loginRepositories.CompanyId);
ids = staffList.Where(w => EF.Functions.Like(w.Name, "%" + dto.Creator + "%")).Select(s => s.Id).ToList();
}
var materials_result = await _erpService.BillQueryForMaterial();
var materials = materials_result.Data.ToList();
var query = _context.ChangeBoxRecord
.GroupJoin(_context.Box, changeBox => changeBox.SrcBoxId, srcBox => srcBox.Id, (changeBox, srcBox) => new { changeBox, srcBox })
.SelectMany(x => x.srcBox.DefaultIfEmpty(), (d, srcBox) => new { d.changeBox, srcBox })
.GroupJoin(_context.Box, d => d.changeBox.DestBoxId, destBox => destBox.Id, (d, destBox) => new { d.changeBox, d.srcBox, destBox })
.SelectMany(x => x.destBox.DefaultIfEmpty(), (d, destBox) => new { d.changeBox, d.srcBox, destBox })
.OrderByDescending(o => o.changeBox.Id)
.Where(adv => 1 == 1);
if (ids.Count() > 0)
query = query.Where(w => ids.Contains(w.changeBox.CreatorId));
if (!string.IsNullOrEmpty(dto.SrcBox))
query = query.Where(w => EF.Functions.Like(w.srcBox.BoxBillNo, "%" + dto.SrcBox + "%"));
if (!string.IsNullOrEmpty(dto.DestBox))
query = query.Where(w => EF.Functions.Like(w.destBox.BoxBillNo, "%" + dto.DestBox + "%"));
if (dto.SrcSubStockId != null)
query = query.Where(w => w.changeBox.SrcSubStockId == dto.SrcSubStockId);
if (dto.DestSubStockId != null)
query = query.Where(w => w.changeBox.DestSubStockId == dto.DestSubStockId);
if (dto.CreateBeginDate != null)
query = query.Where(w => w.changeBox.CreateTime >= dto.CreateBeginDate);
if (dto.CreateEndDate != null)
query = query.Where(w => w.changeBox.CreateTime <= dto.CreateEndDate);
//组装
int total = await query.CountAsync();
var list = await query.Select(s => new ChangeBoxRecordQueryInfoResponse()
{
#region dto组装
BillNo=s.changeBox.BillNo,
MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.changeBox.MaterialId),
MaterialNumber = _erpBasicDataExtendService.GetMaterialNumber(materials, s.changeBox.MaterialId),
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.changeBox.MaterialId),
//Stock = _singleDataService.GetSingleData(SingleAction.Stocks, _loginRepositories.CompanyId, s.changeBox.StockId),
SerialNumbers = string.Join(",", s.changeBox.SerialNumbers),
SrcBox = s.srcBox.BoxBillNo,
DestBox = s.destBox.BoxBillNo,
SrcSubStock = _singleDataService.GetSingleData(SingleAction.SubStocks, _loginRepositories.CompanyId, s.changeBox.SrcSubStockId),
DestSubStock = _singleDataService.GetSingleData(SingleAction.SubStocks, _loginRepositories.CompanyId, s.changeBox.DestSubStockId),
Creator = _singleDataService.GetSingleData(SingleAction.Staffs, _loginRepositories.CompanyId, s.changeBox.CreatorId),
CreateTime = s.changeBox.CreateTime.DateToStringSeconds()
#endregion
}).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
return (list, total);
}
public async Task<(object obj, int total)> GetListField(ChangeBoxRecordQueryRequest dto, int companyId)
{
return await GetListAsync(dto);
}
}
}