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.Erp;
using WMS.Web.Core.Dto.Erp.Customer;
using WMS.Web.Core.Dto.SerialNumbers;
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.Mappers;
using WMS.Web.Repositories.Configuration;
namespace WMS.Web.Repositories
{
///
/// 序列号
///
public class SerialNumbersRepositories : ISerialNumbersRepositories
{
private readonly IMapper _mapper;
private readonly IServiceProvider _serviceProvider;
private readonly RepositoryDbContext _context;
private readonly IErpService _erpService;
private readonly IErpBasicDataExtendService _erpBasicDataExtendService;
public SerialNumbersRepositories(RepositoryDbContext context, IMapper mapper,
IServiceProvider serviceProvider, IErpService erpService, IErpBasicDataExtendService erpBasicDataExtendService)
{
_context = context;
_mapper = mapper;
_serviceProvider = serviceProvider;
_erpService = erpService;
_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.SerialNumbers.AddRangeAsync(entitys);
await _context.SaveChangesAsync();
}
if (_transaction != null)
_transaction.Commit();
return true;
}
catch (Exception ex)
{
if (_transaction != null)
_transaction.Rollback();
return false;
}
}
///
/// 批量修改
///
///
///
///
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.SerialNumbers
.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 Get(string serialNumber)
{
List materials = new List();
var materials_result = await _erpService.BillQueryForMaterial();
if (materials_result.IsSuccess)
materials = materials_result.Data.ToList();
List customers = new List();
var customer_result = await _erpService.BillQueryForCustomer();
if (customer_result.IsSuccess)
customers = customer_result.Data.ToList();
var s = await _context.SerialNumbers
.FirstOrDefaultAsync(f => serialNumber.Equals(f.SerialNumber));
if (s == null) return null;
SerialNumbersResponse respone = new SerialNumbersResponse()
{
MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.MaterialId),
MaterialNumber = _erpBasicDataExtendService.GetMaterialNumber(materials, s.MaterialId),
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.MaterialId),
CompleteCartonTime = s.CompleteCartonTime.DateToStringSeconds(),
InStockTime = s.InStockTime.DateToStringSeconds(),
OutStockTime = s.OutStockTime.DateToStringSeconds(),
PurchaseBillNo = s.PurchaseBillNo,
Customer = _erpBasicDataExtendService.GetCustomerName(customers, s.CustomerId),
SerialNumber = s.SerialNumber
};
if (!string.IsNullOrEmpty(s.SalBillNo))
{
Random rd = new Random();
var list = JsonConvert.DeserializeObject>(s.SalBillNo);
if (list.Count() > 0)
{
int index = rd.Next(0, list.Count);
respone.SalBillNo = list[index];
}
}
return respone;
}
///
/// 根据序列号查询
///
///
///
public async Task> GetEntityList(List serialNumbers)
{
var res = await _context.SerialNumbers
.Where(f => serialNumbers.Contains(f.SerialNumber))
.ToListAsync();
return res.Clone();
}
public async Task> GetEntityListByBoxId(int boxId)
{
var res = await _context.SerialNumbers
.Where(f => f.BoxId == boxId)
.ToListAsync();
return res.Clone();
}
///
/// 根据箱Ids查询集合
///
///
///
public async Task> GetEntityListByBoxIds(List boxIds)
{
var res = await _context.SerialNumbers
.Where(f => boxIds.Contains(f.BoxId))
.ToListAsync();
return res.Clone();
}
///
/// 根据序列号
///
///
///
public async Task GetSerialNumber(string serialNumber)
{
MaterialResponse response = new MaterialResponse();
var materials_result = await _erpService.BillQueryForMaterial();
if (!materials_result.IsSuccess)
return response;
var materials = materials_result.Data.ToList();
var material = materials.FirstOrDefault(f => f.Specifications.Equals(serialNumber));
if (material != null)
{
response.MaterialId = material.MaterialId;
response.MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, material.MaterialId);
response.MaterialNumber = _erpBasicDataExtendService.GetMaterialNumber(materials, material.MaterialId);
response.Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, material.MaterialId);
return response;
}
var entity = await _context.SerialNumbers
.GroupJoin(_context.BoxInventory, serial => serial.BoxId, box => box.BoxId, (serial, box) => new { serial, box })
.SelectMany(x => x.box.DefaultIfEmpty(), (p, box) => new { p.serial, box })
.FirstOrDefaultAsync(w => serialNumber.Equals(w.serial.SerialNumber));
if (entity == null || entity.serial == null) return null;
response.BoxId = entity.serial.BoxId;
response.SubStockId = entity.box?.SubStockId ?? 0;
response.SerialNumber = entity.serial.SerialNumber;
response.MaterialId = entity.serial.MaterialId;
response.MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, entity.serial.MaterialId);
response.MaterialNumber = _erpBasicDataExtendService.GetMaterialNumber(materials, entity.serial.MaterialId);
response.Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, entity.serial.MaterialId);
return response;
}
}
}