384 lines
17 KiB
C#
384 lines
17 KiB
C#
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 BarCode.Web.Core.Dto;
|
|
using BarCode.Web.Core.Help;
|
|
using BarCode.Web.Domain.Entitys;
|
|
using BarCode.Web.Domain.Infrastructure;
|
|
using BarCode.Web.Domain.IService.Public;
|
|
using BarCode.Web.Domain.Mappers;
|
|
using BarCode.Web.Repositories.Configuration;
|
|
using BarCode.Web.Core.Dto.Box;
|
|
using BarCode.Web.Core.Dto.Erp;
|
|
using BarCode.Web.Core.Dto.Erp.Supplier;
|
|
using BarCode.Web.Core.Dto.Erp.Org;
|
|
using BarCode.Web.Core.Dto.SerialNumbers;
|
|
using BarCode.Web.Domain.Values.Single;
|
|
using BarCode.Web.Core;
|
|
using BarCode.Web.Domain.Values;
|
|
using NPOI.OpenXmlFormats.Wordprocessing;
|
|
using System.Runtime.CompilerServices;
|
|
using System.ComponentModel.Design;
|
|
using BarCode.Web.Core.Dto.Login;
|
|
using BarCode.Web.Core.Dto.SingleData;
|
|
using BarCode.Web.Core.Internal.Results;
|
|
|
|
namespace BarCode.Web.Repositories
|
|
{
|
|
/// <summary>
|
|
/// 老ops箱信息
|
|
/// </summary>
|
|
public class BoxRepositories : IBoxRepositories
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly IServiceProvider _serviceProvider;
|
|
private readonly RepositoryDbContext _context;
|
|
private readonly ISingleDataService _singleDataService;
|
|
private readonly ILoginRepositories _loginRepositories;
|
|
private readonly IBasicsRepositories _basicsRepositories;
|
|
private readonly IErpBasicDataExtendService _erpBasicDataExtendService;
|
|
private readonly IErpService _erpService;
|
|
public BoxRepositories(RepositoryDbContext context, IMapper mapper, IServiceProvider serviceProvider,
|
|
ISingleDataService singleDataService, ILoginRepositories loginRepositories,
|
|
IBasicsRepositories basicsRepositories, IErpBasicDataExtendService erpBasicDataExtendService,
|
|
IErpService erpServic)
|
|
{
|
|
_context = context;
|
|
_mapper = mapper;
|
|
_serviceProvider = serviceProvider;
|
|
_singleDataService = singleDataService;
|
|
_loginRepositories = loginRepositories;
|
|
_basicsRepositories = basicsRepositories;
|
|
_erpBasicDataExtendService = erpBasicDataExtendService;
|
|
_erpService = erpServic;
|
|
}
|
|
public async Task<Box?> Get(int id)
|
|
{
|
|
return await _context.Box.AsNoTracking()
|
|
.Include(x => x.Details)
|
|
.FirstOrDefaultAsync(f => f.Id.Equals(id));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量修改
|
|
/// </summary>
|
|
/// <param name="entitys"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> EditEntityList(List<Box> 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.Box
|
|
.Include(s => s.Details)
|
|
.Where(f => list.Contains(f.Id)).ToListAsync();
|
|
|
|
_mapper.ToMapList(entitys, res);
|
|
//_mapper.ToMapList(entitys.SelectMany(s => s.Details).ToList(), res.SelectMany(s => s.Details).ToList());
|
|
|
|
await _context.SaveChangesAsync();
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (_transaction != null)
|
|
_transaction.Rollback();
|
|
return false;
|
|
}
|
|
return true;
|
|
|
|
}
|
|
|
|
public async Task<Box?> GetByNo(string billNo)
|
|
{
|
|
return await _context.Box.AsNoTracking()
|
|
.Include(x => x.Details)
|
|
.FirstOrDefaultAsync(f => f.BoxBillNo.Equals(billNo));
|
|
}
|
|
public async Task<List<Box>> GetEntityListByNos(List<string> billNos)
|
|
{
|
|
return await _context.Box.AsNoTracking()
|
|
.Include(x => x.Details)
|
|
.Where(f => billNos.Contains(f.BoxBillNo)).ToListAsync();
|
|
}
|
|
//根据箱号搜索 用来比对确定是否箱号信息是否存在
|
|
public async Task<List<string>> GetByNos(List<string> billNos)
|
|
{
|
|
return await _context.Box.AsNoTracking()
|
|
.Where(w => billNos.Contains(w.BoxBillNo)).Select(s => s.BoxBillNo).ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> AddRange(List<Box> entitys, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
if (entitys != null && entitys.Count != 0)
|
|
{
|
|
await _context.Box.AddRangeAsync(entitys);
|
|
await _context.SaveChangesAsync();
|
|
|
|
foreach (var e in entitys)
|
|
{
|
|
e.GenerateNo();
|
|
}
|
|
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<Box>> GetEntityList(List<int> ids)
|
|
{
|
|
return await _context.Box.AsNoTracking()
|
|
.Include(s => s.Details)
|
|
.Where(f => ids.Contains(f.Id))
|
|
.ToListAsync();
|
|
}
|
|
/// <summary>
|
|
/// 列表
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <param name="companyId"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<(List<BoxInfoResponse> list, int total)> GetListAsync(BoxQueryRequest dto, LoginInDto loginInfo)
|
|
{
|
|
int companyId = loginInfo.UserInfo.CompanyId;
|
|
List<string> mNumber = new List<string>();
|
|
var materials_result = await _erpService.BillQueryForMaterial();
|
|
List<ErpMaterialDto> materials = new List<ErpMaterialDto>();
|
|
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 supplier_result = await _erpService.BillQueryForSupplier();
|
|
//List<ErpSupplierDto> suppliers = new List<ErpSupplierDto>();
|
|
//if (supplier_result.IsSuccess)
|
|
// suppliers = supplier_result.Data.ToList();
|
|
////取组织
|
|
//var org_result = await _erpService.BillQueryForOrg();
|
|
//List<ErpOrgDto> orgs = new List<ErpOrgDto>();
|
|
//if (org_result.IsSuccess)
|
|
// orgs = org_result.Data.ToList();
|
|
var result = await _singleDataService.GetSingleData<ResultList<SysOrgResponse>, SingleDataRequest, SysConfigAction>(
|
|
new SingleDataRequest(loginInfo.UserInfo.CompanyId), SysConfigAction.GetOrgByCompany, SingleControllerType.SysConfig);
|
|
List<SysOrgResponse> orgs = new List<SysOrgResponse>();
|
|
if (result.IsSuccess)
|
|
orgs = result.Data.ToList();
|
|
|
|
List<int> cr_ids = new List<int>();
|
|
if (!string.IsNullOrEmpty(dto.CreateUser))
|
|
{
|
|
cr_ids = await _basicsRepositories.GetUserIdsAsync(dto.CreateUser, loginInfo.UserInfo.CompanyId);
|
|
}
|
|
List<int> s_ids = new List<int>();
|
|
if (dto.SerialNumbers != null && dto.SerialNumbers.Count() > 0)
|
|
{
|
|
var details = await GetDetailIdBySerialNumbers(dto.SerialNumbers);
|
|
s_ids = details.Select(s => s.Id).ToList();
|
|
}
|
|
var query = _context.Box
|
|
.GroupJoin(_context.BoxDetails, box => box.Id, detail => detail.Fid, (box, detail) => new { box, detail })
|
|
.SelectMany(x => x.detail.DefaultIfEmpty(), (p, detail) => new { p.box, detail })
|
|
.OrderByDescending(o => o.box.Id)
|
|
.Where(f => 1 == 1);
|
|
|
|
if (loginInfo.UserInfo.IsAdmin != true && !string.IsNullOrEmpty(dto.OrgCode))
|
|
{
|
|
var rec_type = dto.OrgCode.Substring(0, 1);
|
|
var rec_code = dto.OrgCode.Substring(2, dto.OrgCode.Length - 2);
|
|
|
|
if (rec_type.Equals("s"))//供应商
|
|
query = query.Where(w => rec_code == w.box.SupplierCode);
|
|
else//查其他单据(组织)
|
|
query = query.Where(w => rec_code == w.box.OrgCode);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(dto.SupplierOrOrg))
|
|
{
|
|
var rec_type = dto.SupplierOrOrg.Substring(0, 1);
|
|
var rec_code = dto.SupplierOrOrg.Substring(2, dto.SupplierOrOrg.Length - 2);
|
|
|
|
if (rec_type.Equals("s"))//供应商
|
|
query = query.Where(w => rec_code == w.box.SupplierCode);
|
|
else//查其他单据(组织)
|
|
query = query.Where(w => rec_code == w.box.OrgCode);
|
|
}
|
|
if (!string.IsNullOrEmpty(dto.MaterialNumber))
|
|
query = query.Where(w => mNumber.Contains(w.detail.MaterialNumber));
|
|
if (!string.IsNullOrEmpty(dto.BoxBeginNo))
|
|
{
|
|
//V01.05.00: 只输入一个框则只查输入值,不做区间查询
|
|
var id = Convert.ToInt32(dto.BoxBeginNo.ToUpper().Replace("CTN", ""));
|
|
if (!string.IsNullOrEmpty(dto.BoxEndNo))
|
|
query = query.Where(w => w.box.Id >= id);
|
|
else
|
|
query = query.Where(w => w.box.Id == id);
|
|
}
|
|
if (!string.IsNullOrEmpty(dto.BoxEndNo))
|
|
{
|
|
//V01.05.00: 只输入一个框则只查输入值,不做区间查询
|
|
var id = Convert.ToInt32(dto.BoxEndNo.ToUpper().Replace("CTN", ""));
|
|
if (!string.IsNullOrEmpty(dto.BoxBeginNo))
|
|
query = query.Where(w => w.box.Id <= id);
|
|
else
|
|
query = query.Where(w => w.box.Id == id);
|
|
}
|
|
if (dto.Status != null)
|
|
query = query.Where(w => w.box.Status == (BoxStatus)dto.Status);
|
|
if (dto.CreateBeginDate != null)
|
|
query = query.Where(w => w.box.CreateTime >= dto.CreateBeginDate);
|
|
if (dto.CreateEndDate != null)
|
|
{
|
|
DateTime dt_end = ((DateTime)dto.CreateEndDate).AddDays(1);
|
|
query = query.Where(w => w.box.CreateTime <= dt_end);
|
|
}
|
|
if (dto.CartonBeginDate != null)
|
|
query = query.Where(w => w.box.CartonEndTime >= dto.CartonBeginDate);
|
|
if (dto.CartonEndDate != null)
|
|
{
|
|
DateTime dt_end = ((DateTime)dto.CartonEndDate).AddDays(1);
|
|
query = query.Where(w => w.box.CartonEndTime <= dt_end);
|
|
}
|
|
|
|
if (dto.SerialNumbers != null && dto.SerialNumbers.Count() > 0)
|
|
query = query.Where(w => s_ids.Contains(w.detail.Id));
|
|
if (dto.BoxPrintStatus != null)
|
|
query = query.Where(w => dto.BoxPrintStatus == true ? w.box.PrintNumber > 0 : w.box.PrintNumber <= 0);
|
|
if (!string.IsNullOrEmpty(dto.CreateUser))
|
|
query = query.Where(w => cr_ids.Contains(w.box.CreatorId));
|
|
|
|
int total = await query.CountAsync();
|
|
var list = await query.Select(s => new BoxInfoResponse()
|
|
{
|
|
Id = s.box.Id,
|
|
DetailId = s.detail == null ? 0 : s.detail.Id,
|
|
BoxBillNo = s.box.BoxBillNo,
|
|
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.detail == null ? "" : s.detail.MaterialNumber),
|
|
MaterialNumber = s.detail == null ? "" : s.detail.MaterialNumber,
|
|
MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.detail == null ? "" : s.detail.MaterialNumber),
|
|
BarCode = _erpBasicDataExtendService.GetMaterialBarCode(materials, s.detail == null ? "" : s.detail.MaterialNumber),
|
|
SerialNumbers = s.detail == null ? "" : string.Join(",", s.detail.SerialNumbers),
|
|
Qty = s.detail == null ? 0 : s.detail.Qty,
|
|
Status = s.box.Status.GetRemark(),
|
|
CartonBeginTime = s.box.CartonBeginTime.DateToStringSeconds(),
|
|
CartonEndTime = s.box.CartonEndTime.DateToStringSeconds(),
|
|
CartonUser = _singleDataService.GetSingleData(SingleAction.Users, companyId, s.box.CartonUserId),
|
|
Creator = _singleDataService.GetSingleData(SingleAction.Users, companyId, s.box.CreatorId),
|
|
CreateTime = s.box.CreateTime.DateToStringSeconds(),
|
|
PrintNumber = s.box.PrintNumber,
|
|
SupplierOrOrg = string.IsNullOrEmpty(s.box.SupplierCode) ? _erpBasicDataExtendService.GetSingleOrgName(orgs, s.box.OrgCode)
|
|
: _singleDataService.GetSingleData(SingleAction.Suppliers, companyId, s.box.SupplierCode)//供应商取单点的
|
|
}).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
|
|
return (list, total);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据序列号获取明细
|
|
/// </summary>
|
|
/// <param name="serialNumbers"></param>
|
|
/// <returns></returns>
|
|
private Task<List<BoxDetails>> GetDetailIdBySerialNumbers(List<string> serialNumbers)
|
|
{
|
|
if (serialNumbers.Count() == 0) Task.FromResult(new List<BoxDetails>());
|
|
string str = $"select * from t_barcode_box_details where ";
|
|
for (int i = 0; i < serialNumbers.Count(); i++)
|
|
{
|
|
if (i == 0)
|
|
str += $"SerialNumbers like '%\"{serialNumbers[i]}\"%'";
|
|
//str += $"twoSerialNumber like '%\"{serialNumbers[i]}\"%'";
|
|
else
|
|
str += $" or SerialNumbers like '%\"{serialNumbers[i]}\"%'";
|
|
//str += $" or twoSerialNumber like '%\"{serialNumbers[i]}\"%'";
|
|
}
|
|
var fs = FormattableStringFactory.Create(str);
|
|
var list = _context.Set<BoxDetails>().FromSqlInterpolated(fs).ToList();
|
|
return Task.FromResult(list);
|
|
}
|
|
/// <summary>
|
|
/// 编辑
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<Box> Edit(Box entity, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
|
|
var res = await _context.Box
|
|
.Include(s => s.Details)
|
|
.FirstOrDefaultAsync(f => f.Id == entity.Id);
|
|
if (res == null) return null;
|
|
|
|
_mapper.Map(entity, res);
|
|
await _context.SaveChangesAsync();
|
|
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
|
|
return res;
|
|
|
|
}
|
|
/// <summary>
|
|
/// 获取装箱完成的箱信息
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<(List<Box>, int tota)> GetEntityByWmsList(WmsBoxRequest dto)
|
|
{
|
|
var query = _context.Box.Include(x => x.Details)
|
|
.Where(w => w.Status == BoxStatus.Complete);
|
|
|
|
if (!string.IsNullOrEmpty(dto.BoxBillNo))
|
|
query = query.Where(w => w.BoxBillNo.Equals(dto.BoxBillNo));
|
|
if (dto.StrartTime != null)
|
|
query = query.Where(w => w.CartonEndTime >= dto.StrartTime);
|
|
if (dto.EndTime != null)
|
|
query = query.Where(w => w.CartonEndTime <= dto.EndTime);
|
|
|
|
|
|
int total = await query.CountAsync();
|
|
var list = await query.Skip((dto.Page - 1) * dto.Limit).Take(dto.Limit).ToListAsync();
|
|
return (list, total);
|
|
}
|
|
}
|
|
}
|