352 lines
15 KiB
C#
352 lines
15 KiB
C#
using AutoMapper;
|
|
using BarCode.Web.Core.Dto.Erp.Org;
|
|
using BarCode.Web.Core.Dto.Erp.Supplier;
|
|
using BarCode.Web.Core.Dto.Erp;
|
|
using BarCode.Web.Core.Dto.SerialNumbers;
|
|
using BarCode.Web.Domain.Entitys;
|
|
using BarCode.Web.Domain.Infrastructure;
|
|
using BarCode.Web.Domain.Mappers;
|
|
using BarCode.Web.Repositories.Configuration;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
|
using Newtonsoft.Json;
|
|
using Org.BouncyCastle.Crypto;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
|
|
using BarCode.Web.Domain.IService.Public;
|
|
using BarCode.Web.Core.Dto.Box;
|
|
using BarCode.Web.Domain.Services.Public;
|
|
using BarCode.Web.Domain.Values.Single;
|
|
using System.ComponentModel.Design;
|
|
using BarCode.Web.Core.Help;
|
|
using NPOI.SS.Formula.Functions;
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
|
using BarCode.Web.Core.Dto.Login;
|
|
using BarCode.Web.Core.Dto.SingleData;
|
|
using BarCode.Web.Core.Internal.Results;
|
|
|
|
namespace BarCode.Web.Repositories
|
|
{
|
|
/// <summary>
|
|
/// 序列号生成记录
|
|
/// </summary>
|
|
public class SGenerateRecordRepositories : ISGenerateRecordRepositories
|
|
{
|
|
private readonly ILoginRepositories _loginRepositories;
|
|
private readonly RepositoryDbContext _context;
|
|
private readonly IBasicsRepositories _basicsRepositories;
|
|
private readonly IMapper _mapper;
|
|
private readonly IErpService _erpService;
|
|
private readonly IErpBasicDataExtendService _erpBasicDataExtendService;
|
|
private readonly ISingleDataService _singleDataService;
|
|
|
|
public SGenerateRecordRepositories(RepositoryDbContext context,
|
|
ILoginRepositories loginRepositories,
|
|
IBasicsRepositories basicsRepositories, IMapper mapper, IErpService erpService,
|
|
IErpBasicDataExtendService erpBasicDataExtendService, ISingleDataService singleDataService)
|
|
{
|
|
_context = context;
|
|
_basicsRepositories = basicsRepositories;
|
|
_loginRepositories = loginRepositories;
|
|
_mapper = mapper;
|
|
_erpService = erpService;
|
|
_erpBasicDataExtendService = erpBasicDataExtendService;
|
|
_singleDataService = singleDataService;
|
|
}
|
|
/// <summary>
|
|
/// 新增
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<SerialNumberGenerateRecord> Add(SerialNumberGenerateRecord entity, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
|
|
await _context.SerialNumberGenerateRecord.AddAsync(entity);
|
|
await _context.SaveChangesAsync();
|
|
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
return entity;
|
|
}
|
|
/// <summary>
|
|
/// 批量添加
|
|
/// </summary>
|
|
/// <param name="entitys"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AddRange(List<SerialNumberGenerateRecord> entitys, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
|
|
if (entitys != null && entitys.Count != 0)
|
|
{
|
|
await _context.SerialNumberGenerateRecord.AddRangeAsync(entitys);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
return true;
|
|
}
|
|
/// <summary>
|
|
/// 修改
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<SerialNumberGenerateRecord> Edit(SerialNumberGenerateRecord entity, bool isTransaction = true)
|
|
{
|
|
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
|
|
var res = await _context.SerialNumberGenerateRecord
|
|
.FirstOrDefaultAsync(f => f.Id == entity.Id);
|
|
if (res == null) return null;
|
|
|
|
_mapper.Map(entity, res);
|
|
var result = await _context.SaveChangesAsync();
|
|
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
|
|
return res;
|
|
}
|
|
/// <summary>
|
|
/// 批量修改
|
|
/// </summary>
|
|
/// <param name="entitys"></param>
|
|
/// <param name="isTransaction"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> EditEntityList(List<SerialNumberGenerateRecord> entitys, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
|
|
List<int> list = entitys.Select(s => s.Id).ToList();
|
|
var res = await _context.SerialNumberGenerateRecord
|
|
.Where(f => list.Contains(f.Id)).ToListAsync();
|
|
|
|
_mapper.ToMapList(entitys, res);
|
|
await _context.SaveChangesAsync();
|
|
|
|
if (_transaction != null)
|
|
_transaction.Commit();
|
|
|
|
return true;
|
|
}
|
|
/// <summary>
|
|
/// 查询实体
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<SerialNumberGenerateRecord?> GetEntity(int id)
|
|
{
|
|
return await _context.SerialNumberGenerateRecord.AsNoTracking()
|
|
.FirstOrDefaultAsync(f => f.Id == id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取集合
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <param name="strUpdate">更新而使用的</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<List<SerialNumberGenerateRecord>> GetEntityList(List<int> ids,string strUpdate)
|
|
{
|
|
if(strUpdate=="1")
|
|
{
|
|
return await _context.SerialNumberGenerateRecord.AsNoTracking()
|
|
.Where(f => f.IsTwo==3)
|
|
.ToListAsync();
|
|
}
|
|
else
|
|
{
|
|
return await _context.SerialNumberGenerateRecord.AsNoTracking()
|
|
.Where(f => ids.Contains(f.Id))
|
|
.ToListAsync();
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 列表
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<(List<SGenerateRecordInfoResponse> list, int total)> GetListAsync(SGenerateRecordQueryRequest 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();
|
|
}
|
|
List<int> cr_ids = new List<int>();
|
|
if (!string.IsNullOrEmpty(dto.CreateUser))
|
|
{
|
|
cr_ids = await _basicsRepositories.GetUserIdsAsync(dto.CreateUser, loginInfo.UserInfo.CompanyId);
|
|
}
|
|
List<int> sIds = new List<int>();
|
|
if (dto.SerialNumbers != null && dto.SerialNumbers.Count() > 0)
|
|
{
|
|
sIds = await _context.SerialNumbers.Where(w => dto.SerialNumbers.Contains(w.SerialNumber))
|
|
.GroupBy(g => g.GenerateRecordId).Select(s => s.Key).ToListAsync();
|
|
}
|
|
|
|
List<int> nIds = new List<int>();
|
|
if (dto.NumberCodes != null && dto.NumberCodes.Count() > 0)
|
|
{
|
|
nIds = await _context.SerialNumbers.Where(w => dto.NumberCodes.Contains(w.NumberCode))
|
|
.GroupBy(g => g.GenerateRecordId).Select(s => s.Key).ToListAsync();
|
|
}
|
|
|
|
List<int> bIds = new List<int>();
|
|
if (dto.BoxBillNos != null && dto.BoxBillNos.Count() > 0)
|
|
{
|
|
bIds = await _context.SerialNumbers
|
|
.GroupJoin(_context.Box, serial => serial.BoxId, box => box.Id, (serial, box) => new { serial, box })
|
|
.SelectMany(x => x.box.DefaultIfEmpty(), (p, box) => new { p.serial, box })
|
|
.Where(w => dto.BoxBillNos.Contains(w.box.BoxBillNo))
|
|
.Select(s => s.serial.GenerateRecordId)
|
|
.ToListAsync();
|
|
}
|
|
|
|
|
|
//供应商
|
|
//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();
|
|
|
|
var query = _context.SerialNumberGenerateRecord
|
|
.OrderByDescending(o => o.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.SupplierCode);
|
|
else//查其他单据(组织)
|
|
query = query.Where(w => rec_code == w.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.SupplierCode);
|
|
else//查其他单据(组织)
|
|
query = query.Where(w => rec_code == w.OrgCode);
|
|
}
|
|
if (!string.IsNullOrEmpty(dto.MaterialNumber))
|
|
query = query.Where(w => mNumber.Contains(w.MaterialNumber));
|
|
if (dto.CreateBeginDate != null)
|
|
query = query.Where(w => w.GenerateCompleteTime >= dto.CreateBeginDate);
|
|
if (dto.CreateEndDate != null)
|
|
{
|
|
DateTime dt_end = ((DateTime)dto.CreateEndDate).AddDays(1);
|
|
query = query.Where(w => w.GenerateCompleteTime <= dt_end);
|
|
}
|
|
if (dto.SerialNumbers != null && dto.SerialNumbers.Count() > 0)
|
|
query = query.Where(w => sIds.Contains(w.Id));
|
|
if (dto.NumberCodes != null && dto.NumberCodes.Count() > 0)
|
|
query = query.Where(w => nIds.Contains(w.Id));
|
|
if (dto.BoxBillNos != null && dto.BoxBillNos.Count() > 0)
|
|
query = query.Where(w => bIds.Contains(w.Id));
|
|
if (dto.PurchaseBillNos != null && dto.PurchaseBillNos.Count() > 0)
|
|
query = query.Where(w => dto.PurchaseBillNos.Contains(w.PurchaseBillNo));
|
|
if (dto.GenerateComplete != null)
|
|
query = query.Where(w => dto.GenerateComplete == w.IsGenerateComplete);
|
|
if (!string.IsNullOrEmpty(dto.CreateUser))
|
|
query = query.Where(w => cr_ids.Contains(w.CreatorId));
|
|
if (dto.IsUpdateMaterial != null)
|
|
{
|
|
if (dto.IsUpdateMaterial == true)
|
|
query = query.Where(w => w.IsUpdateMaterial == true);
|
|
else
|
|
query = query.Where(w => w.IsUpdateMaterial == false || w.IsUpdateMaterial == null);
|
|
}
|
|
|
|
if (dto.IsTwo > 1)
|
|
{
|
|
query = query.Where(w => w.IsTwo > 1);
|
|
}
|
|
else if (dto.IsTwo == 1)
|
|
{
|
|
query = query.Where(w => w.IsTwo == 1);
|
|
}
|
|
else if (dto.IsTwo == 0)
|
|
{
|
|
}
|
|
else
|
|
{
|
|
//query = query.Where(w => w.IsTwo == 1);
|
|
}
|
|
|
|
|
|
|
|
int total = await query.CountAsync();
|
|
var list = await query.Select(s => new SGenerateRecordInfoResponse()
|
|
{
|
|
Id = s.Id,
|
|
//Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.MaterialNumber),
|
|
Specifications =s.Specifications,
|
|
MaterialNumber = s.MaterialNumber,
|
|
MaterialName =s.MaterialName,// _erpBasicDataExtendService.GetMaterialName(materials, s.MaterialNumber),
|
|
BarCode =s.BarCode,// _erpBasicDataExtendService.GetMaterialBarCode(materials, s.MaterialNumber),
|
|
PurchaseBillNo = s.PurchaseBillNo,
|
|
GenerateComplete = s.IsGenerateComplete == true ? "已完成" : "生成中",
|
|
Number = s.Number,
|
|
DownLoadNumber = s.DownLoadNumber,
|
|
UseNumber = s.UseNumber,
|
|
IsUpdateMaterial = s.IsUpdateMaterial,
|
|
Creator = _singleDataService.GetSingleData(SingleAction.Users, companyId, s.CreatorId),
|
|
CreateTime = s.CreateTime.DateToStringSeconds(),
|
|
GenerateCompleteTime = s.GenerateCompleteTime.DateToStringSeconds(),
|
|
PrintNumber = s.PrintNumber,
|
|
IsTwo= s.IsTwo,//add by yzh
|
|
SupplierOrOrg = string.IsNullOrEmpty(s.SupplierCode) ? _erpBasicDataExtendService.GetSingleOrgName(orgs, s.OrgCode)
|
|
: _singleDataService.GetSingleData(SingleAction.Suppliers, companyId, s.SupplierCode)//供应商取单点的
|
|
}).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
|
|
return (list, total);
|
|
}
|
|
}
|
|
}
|