284 lines
12 KiB
C#
284 lines
12 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.SecurityNumbers;
|
|
using NPOI.OpenXmlFormats.Wordprocessing;
|
|
using BarCode.Web.Core.Dto.Login;
|
|
|
|
namespace BarCode.Web.Repositories
|
|
{
|
|
/// <summary>
|
|
/// 防伪码生成记录
|
|
/// </summary>
|
|
public class SecurityGenerateRecordRepositories : ISecurityGenerateRecordRepositories
|
|
{
|
|
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 SecurityGenerateRecordRepositories(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<SecurityNumberGenerateRecord> Add(SecurityNumberGenerateRecord entity, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
|
|
await _context.SecurityNumberGenerateRecord.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<SecurityNumberGenerateRecord> entitys, bool isTransaction = true)
|
|
{
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
|
|
if (entitys != null && entitys.Count != 0)
|
|
{
|
|
await _context.SecurityNumberGenerateRecord.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<SecurityNumberGenerateRecord> Edit(SecurityNumberGenerateRecord entity, bool isTransaction = true)
|
|
{
|
|
|
|
IDbContextTransaction _transaction = null;
|
|
if (isTransaction)
|
|
_transaction = _context.Database.BeginTransaction();
|
|
|
|
var res = await _context.SecurityNumberGenerateRecord
|
|
.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<SecurityNumberGenerateRecord> 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.SecurityNumberGenerateRecord
|
|
.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<SecurityNumberGenerateRecord?> GetEntity(int id)
|
|
{
|
|
return await _context.SecurityNumberGenerateRecord.AsNoTracking()
|
|
.FirstOrDefaultAsync(f => f.Id == id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取集合
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<List<SecurityNumberGenerateRecord>> GetEntityList(List<int> ids)
|
|
{
|
|
return await _context.SecurityNumberGenerateRecord.AsNoTracking()
|
|
.Where(f => ids.Contains(f.Id))
|
|
.ToListAsync();
|
|
}
|
|
/// <summary>
|
|
/// 获取当天生成记录条数
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<int> GetGenerateRecordDayCount()
|
|
{
|
|
return await _context.SecurityNumberGenerateRecord.AsNoTracking()
|
|
.Where(f => f.CreateTime >= DateTime.Now.Date).CountAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 列表
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<(List<SecurityGenerateRecordInfoResponse> list, int total)> GetListAsync(SecurityGenerateRecordQueryRequest 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.SecurityNumbers != null && dto.SecurityNumbers.Count() > 0)
|
|
{
|
|
sIds = await _context.SecurityNumbers.Where(w => dto.SecurityNumbers.Contains(w.SecurityNumber))
|
|
.GroupBy(g => g.GenerateRecordId).Select(s => s.Key).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 query = _context.SecurityNumberGenerateRecord
|
|
.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.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 (mNumber.Count() > 0)
|
|
query = query.Where(w => mNumber.Contains(w.MaterialNumber));
|
|
if (dto.GenerateComplete != null)
|
|
query = query.Where(w => dto.GenerateComplete == w.IsGenerateComplete);
|
|
if (dto.LotNumbers != null && dto.LotNumbers.Count() > 0)
|
|
query = query.Where(w => dto.LotNumbers.Contains(w.LotNumber));
|
|
if (dto.SecurityNumbers != null && dto.SecurityNumbers.Count() > 0)
|
|
query = query.Where(w => sIds.Contains(w.Id));
|
|
|
|
if (!string.IsNullOrEmpty(dto.CreateUser))
|
|
query = query.Where(w => cr_ids.Contains(w.CreatorId));
|
|
|
|
int total = await query.CountAsync();
|
|
var list = await query.Select(s => new SecurityGenerateRecordInfoResponse()
|
|
{
|
|
Id = s.Id,
|
|
Specifications = _erpBasicDataExtendService.GetMaterialSpecifications(materials, s.MaterialNumber),
|
|
MaterialNumber = s.MaterialNumber,
|
|
MaterialName = _erpBasicDataExtendService.GetMaterialName(materials, s.MaterialNumber),
|
|
BarCode = _erpBasicDataExtendService.GetMaterialBarCode(materials, s.MaterialNumber),
|
|
LotNumber = s.LotNumber,
|
|
GenerateComplete = s.IsGenerateComplete == true ? "已完成" : "生成中",
|
|
Number = s.Number,
|
|
DownLoadNumber = s.DownLoadNumber,
|
|
Creator = _singleDataService.GetSingleData(SingleAction.Users, companyId, s.CreatorId),
|
|
CreateTime = s.CreateTime.DateToStringSeconds(),
|
|
GenerateCompleteTime = s.GenerateCompleteTime.DateToStringSeconds()
|
|
}).Skip((dto.PageNo - 1) * dto.PageSize).Take(dto.PageSize).ToListAsync();
|
|
return (list, total);
|
|
}
|
|
}
|
|
}
|