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.Dto.Erp.Customer;
using WMS.Web.Core.Dto.Erp.Org;
using WMS.Web.Core.Dto.Erp;
using WMS.Web.Core.Dto.ProductInventory;
using WMS.Web.Domain.Entitys;
using WMS.Web.Domain.Infrastructure;
using WMS.Web.Domain.IService.Public;
using WMS.Web.Domain.Values;
using WMS.Web.Repositories.Configuration;
using WMS.Web.Core.Dto.OutStockTask;
using WMS.Web.Domain.Values.Single;
using WMS.Web.Core;
using NPOI.SS.Formula.Functions;
using WMS.Web.Domain.Mappers;
using WMS.Web.Core.Help;
using WMS.Web.Core.Dto.SingleData;
namespace WMS.Web.Repositories
{
///
/// 成品仓即时库存
///
public class ProductInventoryRepositories : IAllFielRepositories, IProductInventoryRepositories
{
private readonly IMapper _mapper;
private readonly IServiceProvider _serviceProvider;
private readonly ILoginRepositories _loginRepositories;
private readonly IBasicsRepositories _basicsRepositories;
private readonly RepositoryDbContext _context;
private readonly IErpService _erpService;
private readonly ISingleDataService _singleDataService;
private readonly IErpBasicDataExtendService _erpBasicDataExtendService;
public ProductInventoryRepositories(RepositoryDbContext context,
IMapper mapper,
IErpService erpService,
IBasicsRepositories basicsRepositories,
ILoginRepositories loginRepositories,
IServiceProvider serviceProvider,
ISingleDataService singleDataService,
IErpBasicDataExtendService erpBasicDataExtendService)
{
_erpService = erpService;
_context = context;
_mapper = mapper;
_basicsRepositories = basicsRepositories;
_serviceProvider = serviceProvider;
_loginRepositories = loginRepositories;
_singleDataService = singleDataService;
_erpBasicDataExtendService = erpBasicDataExtendService;
}
public async Task AddRange(List entitys, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
if (entitys != null && entitys.Count != 0)
{
await _context.ProductInventory.AddRangeAsync(entitys);
await _context.SaveChangesAsync();
}
if (_transaction != null)
_transaction.Commit();
return true;
}
catch
{
if (_transaction != null)
_transaction.Rollback();
return false;
}
}
public async Task Delete(ProductInventoryType type, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
var list = await _context.ProductInventory.Where(w => w.Type == type).ToListAsync();
_context.RemoveRange(list);
if (_transaction != null)
_transaction.Commit();
return true;
}
catch
{
if (_transaction != null)
_transaction.Rollback();
return false;
}
}
public async Task<(List list, int total, List details)> GetListAsync(ProductInventoryQueryRequest dto, int companyId = 0)
{
//因为要开放给别的系统用 固定死
companyId = 1;
//if (companyId == 0)
// companyId = _loginRepositories.CompanyId;
#region erp基础资料
List mNumber = new List();
var materials_result = await _erpService.BillQueryForMaterial();
List materials = new List();
if (materials_result.IsSuccess)
materials = materials_result.Data.ToList();
//物料集合;模糊查询后的物料集合
if (!string.IsNullOrEmpty(dto.MaterialNumber))
{
if (materials != null)
mNumber = materials.Where(w => w.MaterialNumber.Contains(dto.MaterialNumber)
|| w.MaterialName.Contains(dto.MaterialNumber)
|| w.Specifications.Contains(dto.MaterialNumber)
).Select(s => s.MaterialNumber).ToList();
}
//取组织
var org_result = await _erpService.BillQueryForOrg();
List orgs = new List();
if (org_result.IsSuccess)
orgs = org_result.Data.ToList();
var p_stocks = await _basicsRepositories.GetUcStockByHeadOfficeAsync("", 1);
#endregion
var query = _context.ProductInventory
.OrderByDescending(o => o.Id)
.Where(adv => 1 == 1 && (adv.Qty > 0 || adv.BeforeQty > 0));
if (!string.IsNullOrEmpty(dto.MaterialNumber))
query = query.Where(w => mNumber.Contains(w.MaterialNumber));
if (!string.IsNullOrEmpty(dto.Customer))
query = query.Where(w => EF.Functions.Like(w.Customer, "%" + dto.Customer + "%"));
if (!string.IsNullOrEmpty(dto.StockCode))
{
var split = dto.StockCode.Split("_$");
query = query.Where(w => w.StockCode == split[0] && w.OrgCode == split[1]);
}
//组装
int total = await query.CountAsync();
decimal qty = await query.SumAsync(s => s.Qty);
decimal beforeQty = await query.SumAsync(s => s.BeforeQty);
List details = new List();
details.Add(qty);
details.Add(beforeQty);
var list = await query.Select(s => new ProductInventoryQueryResponse()
{
#region dto组装
Id = s.Id,
Type = s.Type.GetRemark(),
Stock = _erpBasicDataExtendService.GetProductInventoryStockName(p_stocks, s.StockCode),
Org = _erpBasicDataExtendService.GetOrgName(orgs, s.OrgCode),
MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.MaterialNumber),
MaterialNumber = s.MaterialNumber,
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.MaterialNumber),
Batch = s.Batch,
Customer = s.Customer,
Qty = s.Qty,
BeforeQty = s.BeforeQty,
FProductLines1FName=_erpBasicDataExtendService.GetMaterialFProductLines1FName(materials, s.MaterialNumber),
FProductCATs1FName = _erpBasicDataExtendService.GetMaterialFProductCATs1FName(materials, s.MaterialNumber),
FMaterialGroupFName = _erpBasicDataExtendService.GetMaterialFMaterialGroupFName(materials, s.MaterialNumber),
F_Product = _erpBasicDataExtendService.GetMaterialF_Product(materials, s.MaterialNumber),
#endregion
}).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
return (list, total, details);
}
public async Task<(object obj, int total)> GetListField(ProductInventoryQueryRequest dto, int companyId)
{
var (list, count, qty) = await GetListAsync(dto, companyId);
return (list, count);
}
///
/// 批量修改
///
///
///
///
public async Task EditEntityList(List entitys, bool isTransaction = true)
{
IDbContextTransaction _transaction = null;
if (isTransaction)
_transaction = _context.Database.BeginTransaction();
try
{
List list = entitys.Select(s => s.Id).ToList();
var res = await _context.ProductInventory
.Where(f => list.Contains(f.Id)).ToListAsync();
_mapper.ToMapList(entitys, res);
await _context.SaveChangesAsync();
if (_transaction != null)
_transaction.Commit();
}
catch (Exception ex)
{
if (_transaction != null)
_transaction.Rollback();
return false;
}
return true;
}
///
/// 获取数据
///
///
///
public async Task> GetEntityList(ProductInventoryType type)
{
var res = await _context.ProductInventory
.Where(f => f.Type == type)
.ToListAsync();
return res.Clone();
}
}
}