using AutoMapper;
|
using WIDESEA_Common.StockEnum;
|
using WIDESEA_Core.BaseRepository;
|
using WIDESEA_Core.BaseServices;
|
using WIDESEA_IRecordService;
|
using WIDESEA_IStockService;
|
using WIDESEA_Model.Models;
|
|
namespace WIDESEA_StockService
|
{
|
public partial class StockInfoService : ServiceBase<Dt_StockInfo, IRepository<Dt_StockInfo>>, IStockInfoService
|
{
|
private readonly IMapper _mapper;
|
|
private readonly IRecordService _recordService;
|
public IRepository<Dt_StockInfo> Repository => BaseDal;
|
private readonly IRepository<Dt_StockInfoDetail> _stockInfoDetailRepository;
|
public StockInfoService(IRepository<Dt_StockInfo> BaseDal, IMapper mapper, IRepository<Dt_StockInfoDetail> stockInfoDetailRepository, IRecordService recordService) : base(BaseDal)
|
{
|
_mapper = mapper;
|
_stockInfoDetailRepository = stockInfoDetailRepository;
|
_recordService = recordService;
|
}
|
|
/// <summary>
|
/// 根据托盘号查询库存
|
/// </summary>
|
/// <param name="palletCode"></param>
|
/// <returns></returns>
|
public Dt_StockInfo? GetStockByPalletCode(string palletCode)
|
{
|
Dt_StockInfo stockInfo = BaseDal.QueryFirst(x => x.PalletCode == palletCode);
|
if (stockInfo != null)
|
{
|
stockInfo.Details = _stockInfoDetailRepository.QueryData(x => x.StockId == stockInfo.Id);
|
}
|
return stockInfo;
|
}
|
|
|
public void AddMaterielGroup(Dt_StockInfo stockInfo)
|
{
|
decimal beforeQuantity = 0;
|
List<Dt_StockInfoDetail> details = new List<Dt_StockInfoDetail>();
|
if (stockInfo.Id == 0)
|
{
|
if (stockInfo.Details != null && stockInfo.Details.Any())
|
{
|
BaseDal.Db.InsertNav(stockInfo).Include(x => x.Details).ExecuteCommand();
|
}
|
else
|
{
|
BaseDal.AddData(stockInfo);
|
}
|
details = stockInfo.Details;
|
}
|
else
|
{
|
beforeQuantity = stockInfo.Details.Where(x => x.Id != 0).Sum(x => x.StockQuantity);
|
|
for (int i = 0; i < stockInfo.Details.Count; i++)
|
{
|
if (stockInfo.Details[i].Id == 0)
|
{
|
details.Add(_stockInfoDetailRepository.Db.Insertable(stockInfo.Details[i]).ExecuteReturnEntity());
|
}
|
|
}
|
|
}
|
stockInfo.Details = details;
|
_recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfo, stockInfo.Details, beforeQuantity, stockInfo.Details.Sum(x => x.StockQuantity) + beforeQuantity, WIDESEA_Common.StockEnum.StockChangeType.MaterielGroup);
|
}
|
|
}
|
}
|