165 lines
5.2 KiB
C#
165 lines
5.2 KiB
C#
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
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 MaterialsRepositories : IMaterialsRepositories
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly RepositoryDbContext _context;
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
public MaterialsRepositories(RepositoryDbContext context, IServiceProvider serviceProvider,
|
|
IMapper mapper)
|
|
{
|
|
_serviceProvider = serviceProvider;
|
|
_context = context;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量添加
|
|
/// </summary>
|
|
/// <param name="entitys"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AddRange(List<Materials> entitys, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
if (entitys != null && entitys.Count != 0)
|
|
{
|
|
await _context.Materials.AddRangeAsync(entitys);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
if (_transaction != null)
|
|
_transaction.Rollback();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取集合
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<Materials>> GetEntityList(int? orgId = null)
|
|
{
|
|
|
|
var query = _context.Materials.Where(x => 1 == 1);
|
|
if (orgId.HasValue)
|
|
query = query.Where(x => x.OrgId == orgId.Value);
|
|
|
|
var res = await query.ToListAsync();
|
|
|
|
return res.Clone();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
public async Task<Materials> Get(int mid)
|
|
{
|
|
var query = _context.Materials.Where(x => 1 == 1);
|
|
|
|
query = query.Where(x => x.MaterialId == mid);
|
|
|
|
var res = await query.FirstOrDefaultAsync();
|
|
|
|
return res.Clone();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
public async Task<Materials> Get(string code, int orgId)
|
|
{
|
|
var query = _context.Materials.Where(x => x.MaterialNumber == code && x.OrgId == orgId);
|
|
|
|
var res = await query.FirstOrDefaultAsync();
|
|
|
|
return res.Clone();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取全部的物料编码
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<string>> GetAllNumbers()
|
|
{
|
|
var numbers = await _context.Materials.Select(x => x.MaterialNumber).ToListAsync();
|
|
return numbers.Clone().Distinct().ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取全部的物料编码
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<Materials>> GetEntityList(List<string> materNumbers, bool isBatchManage)
|
|
{
|
|
var entitys = await _context.Materials.Where(x => materNumbers.Contains(x.MaterialNumber) && x.IsBatchManage == isBatchManage).ToListAsync();
|
|
return entitys;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改物料
|
|
/// </summary>
|
|
/// <param name="entitys"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> UpdateRange(List<Materials> 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.Materials.AsNoTracking().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;
|
|
}
|
|
}
|
|
}
|
|
}
|