218 lines
8.7 KiB
C#
218 lines
8.7 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.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
|
|
{
|
|
/// <summary>
|
|
/// 序列号
|
|
/// </summary>
|
|
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<bool> AddRange(List<SerialNumbers> 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;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 批量修改
|
|
/// </summary>
|
|
/// <param name="entitys"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> EditEntityList(List<SerialNumbers> entitys, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
List<int> 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;
|
|
}
|
|
/// <summary>
|
|
/// 根据序列号搜索信息
|
|
/// </summary>
|
|
/// <param name="serialNumber"></param>
|
|
/// <returns></returns>
|
|
public async Task<SerialNumbersResponse> Get(string serialNumber)
|
|
{
|
|
List<ErpMaterialDto> materials = new List<ErpMaterialDto>();
|
|
var materials_result = await _erpService.BillQueryForMaterial();
|
|
if (materials_result.IsSuccess)
|
|
materials = materials_result.Data.ToList();
|
|
|
|
List<ErpCustomerDto> customers = new List<ErpCustomerDto>();
|
|
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<List<string>>(s.SalBillNo);
|
|
if (list.Count() > 0)
|
|
{
|
|
int index = rd.Next(0, list.Count);
|
|
respone.SalBillNo = list[index];
|
|
}
|
|
}
|
|
|
|
return respone;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据序列号查询
|
|
/// </summary>
|
|
/// <param name="serialNumbers"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<SerialNumbers>> GetEntityList(List<string> serialNumbers)
|
|
{
|
|
var res = await _context.SerialNumbers
|
|
.Where(f => serialNumbers.Contains(f.SerialNumber))
|
|
.ToListAsync();
|
|
|
|
return res.Clone();
|
|
}
|
|
|
|
public async Task<List<SerialNumbers>> GetEntityListByBoxId(int boxId)
|
|
{
|
|
var res = await _context.SerialNumbers
|
|
.Where(f => f.BoxId == boxId)
|
|
.ToListAsync();
|
|
|
|
return res.Clone();
|
|
}
|
|
/// <summary>
|
|
/// 根据箱Ids查询集合
|
|
/// </summary>
|
|
/// <param name="boxIds"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<SerialNumbers>> GetEntityListByBoxIds(List<int> boxIds)
|
|
{
|
|
var res = await _context.SerialNumbers
|
|
.Where(f => boxIds.Contains(f.BoxId))
|
|
.ToListAsync();
|
|
|
|
return res.Clone();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据序列号
|
|
/// </summary>
|
|
/// <param name="serialNumbers"></param>
|
|
/// <returns></returns>
|
|
public async Task<MaterialResponse> 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;
|
|
}
|
|
}
|
|
}
|