DigitalFactory/Admin.NET/Admin.NET.Application/Service/ProductWarehousing/ProductWarehousingService.cs

466 lines
22 KiB
C#
Raw Normal View History

2024-05-31 10:13:10 +00:00
using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
2024-06-06 10:22:07 +00:00
using static SKIT.FlurlHttpClient.Wechat.Api.Models.ComponentTCBBatchCreateContainerServiceVersionRequest.Types;
using RazorEngine.Compilation.ImpromptuInterface.InvokeExt;
using AngleSharp.Dom;
2024-05-31 10:13:10 +00:00
namespace Admin.NET.Application;
/// <summary>
/// 产品入库服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class ProductWarehousingService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<ProductWarehousing> _rep;
private readonly MaterialsService _materialsService;
private readonly SysUnitService _sysUnitService;
private readonly MaterialClassifyService _materialClassifyService;
2024-06-07 11:13:15 +00:00
//private readonly WarehouseDetailsService _warehouseDetails;
//private readonly WarehousingStatisticsService _warehousingStatisticsService;
//private readonly PrintCodeDetailService _codeDetailService;
//private readonly ProductRetrospectService _productRetrospect;
2024-06-06 10:22:07 +00:00
private readonly UserManager _userManager;
2024-05-31 10:13:10 +00:00
public ProductWarehousingService(SqlSugarRepository<ProductWarehousing> rep,
2024-06-06 10:22:07 +00:00
UserManager userManager,
2024-05-31 10:13:10 +00:00
MaterialsService materialsService,
SysUnitService sysUnitService,
2024-06-07 11:13:15 +00:00
MaterialClassifyService materialClassifyService)
2024-05-31 10:13:10 +00:00
{
_rep = rep;
2024-06-06 10:22:07 +00:00
_userManager = userManager;
2024-05-31 10:13:10 +00:00
_materialsService = materialsService;
_sysUnitService = sysUnitService;
_materialClassifyService = materialClassifyService;
2024-06-07 11:13:15 +00:00
//_codeDetailService = codeDetailService;
//_warehouseDetails = warehouseDetails;
//_productRetrospect = productRetrospect;
//_warehousingStatisticsService = warehousingStatisticsService;
2024-05-31 10:13:10 +00:00
}
/// <summary>
/// 分页查询产品入库
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<ProductWarehousingOutput>> Page(ProductWarehousingInput input)
{
2024-06-04 10:20:10 +00:00
var query = _rep.AsQueryable().Where(a => !a.IsDelete)
2024-05-31 10:13:10 +00:00
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.Name.Contains(input.SearchKey.Trim())
|| u.GoodCode.Contains(input.SearchKey.Trim())
|| u.MaterialsNum.Contains(input.SearchKey.Trim())
|| u.Specifications.Contains(input.SearchKey.Trim())
|| u.Classify.Contains(input.SearchKey.Trim())
|| u.Unit.Contains(input.SearchKey.Trim())
|| u.BaseUnit.Contains(input.SearchKey.Trim())
|| u.PackageScale.Contains(input.SearchKey.Trim())
|| u.Batch.Contains(input.SearchKey.Trim())
|| u.Custom.Contains(input.SearchKey.Trim())
|| u.Brand.Contains(input.SearchKey.Trim())
|| u.CodeType.Contains(input.SearchKey.Trim())
|| u.WarehouseLocation.Contains(input.SearchKey.Trim())
|| u.Remarks.Contains(input.SearchKey.Trim())
|| u.ExpandField.Contains(input.SearchKey.Trim())
|| u.WarehousingType.Contains(input.SearchKey.Trim())
|| u.CodeNum.Contains(input.SearchKey.Trim())
|| u.Supplier.Contains(input.SearchKey.Trim())
|| u.OddNumber.Contains(input.SearchKey.Trim())
|| u.ProductionLine.Contains(input.SearchKey.Trim())
)
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.GoodCode), u => u.GoodCode.Contains(input.GoodCode.Trim()))
2024-06-07 11:13:15 +00:00
.WhereIF(input.MaterialsId > 0, u => u.MaterialsId == input.MaterialsId)
2024-05-31 10:13:10 +00:00
.WhereIF(!string.IsNullOrWhiteSpace(input.MaterialsNum), u => u.MaterialsNum.Contains(input.MaterialsNum.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Specifications), u => u.Specifications.Contains(input.Specifications.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Classify), u => u.Classify.Contains(input.Classify.Trim()))
2024-06-07 11:13:15 +00:00
.WhereIF(input.Count > 0, u => u.Count == input.Count)
2024-05-31 10:13:10 +00:00
.WhereIF(!string.IsNullOrWhiteSpace(input.Unit), u => u.Unit.Contains(input.Unit.Trim()))
2024-06-07 11:13:15 +00:00
.WhereIF(input.BaseCount > 0, u => u.BaseCount == input.BaseCount)
2024-05-31 10:13:10 +00:00
.WhereIF(!string.IsNullOrWhiteSpace(input.BaseUnit), u => u.BaseUnit.Contains(input.BaseUnit.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.PackageScale), u => u.PackageScale.Contains(input.PackageScale.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Batch), u => u.Batch.Contains(input.Batch.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Custom), u => u.Custom.Contains(input.Custom.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Brand), u => u.Brand.Contains(input.Brand.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeType), u => u.CodeType.Contains(input.CodeType.Trim()))
2024-06-07 11:13:15 +00:00
.WhereIF(input.CodeCount > 0, u => u.CodeCount == input.CodeCount)
2024-05-31 10:13:10 +00:00
.WhereIF(!string.IsNullOrWhiteSpace(input.WarehouseLocation), u => u.WarehouseLocation.Contains(input.WarehouseLocation.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Remarks), u => u.Remarks.Contains(input.Remarks.Trim()))
2024-06-07 11:13:15 +00:00
.WhereIF(input.SourceId > 0, u => u.SourceId == input.SourceId)
.WhereIF(input.WarehouseId > 0, u => u.WarehouseId == input.WarehouseId)
2024-05-31 10:13:10 +00:00
.WhereIF(!string.IsNullOrWhiteSpace(input.ExpandField), u => u.ExpandField.Contains(input.ExpandField.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.WarehousingType), u => u.WarehousingType.Contains(input.WarehousingType.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeNum), u => u.CodeNum.Contains(input.CodeNum.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Supplier), u => u.Supplier.Contains(input.Supplier.Trim()))
2024-06-07 11:13:15 +00:00
.WhereIF(input.Status > 0, u => u.Status == input.Status)
2024-05-31 10:13:10 +00:00
.WhereIF(!string.IsNullOrWhiteSpace(input.OddNumber), u => u.OddNumber.Contains(input.OddNumber.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.ProductionLine), u => u.ProductionLine.Contains(input.ProductionLine.Trim()))
.Select<ProductWarehousingOutput>();
2024-06-07 11:13:15 +00:00
if (input.ProductDateRange != null && input.ProductDateRange.Count > 0)
2024-05-31 10:13:10 +00:00
{
2024-06-07 11:13:15 +00:00
DateTime? start = input.ProductDateRange[0];
2024-05-31 10:13:10 +00:00
query = query.WhereIF(start.HasValue, u => u.ProductDate > start);
2024-06-07 11:13:15 +00:00
if (input.ProductDateRange.Count > 1 && input.ProductDateRange[1].HasValue)
2024-05-31 10:13:10 +00:00
{
var end = input.ProductDateRange[1].Value.AddDays(1);
query = query.Where(u => u.ProductDate < end);
}
2024-06-07 11:13:15 +00:00
}
if (input.LoseDateRange != null && input.LoseDateRange.Count > 0)
2024-05-31 10:13:10 +00:00
{
2024-06-07 11:13:15 +00:00
DateTime? start = input.LoseDateRange[0];
2024-05-31 10:13:10 +00:00
query = query.WhereIF(start.HasValue, u => u.LoseDate > start);
2024-06-07 11:13:15 +00:00
if (input.LoseDateRange.Count > 1 && input.LoseDateRange[1].HasValue)
2024-05-31 10:13:10 +00:00
{
var end = input.LoseDateRange[1].Value.AddDays(1);
query = query.Where(u => u.LoseDate < end);
}
2024-06-07 11:13:15 +00:00
}
if (input.WarehousingDateRange != null && input.WarehousingDateRange.Count > 0)
2024-05-31 10:13:10 +00:00
{
2024-06-07 11:13:15 +00:00
DateTime? start = input.WarehousingDateRange[0];
2024-05-31 10:13:10 +00:00
query = query.WhereIF(start.HasValue, u => u.WarehousingDate > start);
2024-06-07 11:13:15 +00:00
if (input.WarehousingDateRange.Count > 1 && input.WarehousingDateRange[1].HasValue)
2024-05-31 10:13:10 +00:00
{
var end = input.WarehousingDateRange[1].Value.AddDays(1);
query = query.Where(u => u.WarehousingDate < end);
}
2024-06-07 11:13:15 +00:00
}
2024-05-31 10:13:10 +00:00
return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
}
2024-06-06 10:22:07 +00:00
/// <summary>
/// 产品入库
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
2024-06-07 11:13:15 +00:00
//[HttpPost]
//[ApiDescriptionSettings(Name = "ProductWarehousing")]
//public async Task ProductWarehousing(AddProductCodeInput input)
//{
// if (input == null || input.WarehousingTableId == null || input.CodeDatas == null || input.CodeDatas.Count == 0)
// {
// throw Oops.Oh(ErrorCodeEnum.xg1002);
// }
// var warehousing = await Detail(new QueryByIdProductWarehousingInput() { Id = input.WarehousingTableId.Value });
// if (warehousing == null)
// {
// throw Oops.Oh(ErrorCodeEnum.xg1002);
// }
// var materials = await _materialsService.GetById(warehousing.MaterialsId);
// if (materials == null)
// {
// throw Oops.Oh(ErrorCodeEnum.xg1002);
// }
// var units = await _sysUnitService.ListByGroupId(materials.UnitGroupId);
// var baseUnit = units.LastOrDefault().Name;
// var userId = _userManager.UserId;
// var userName = _userManager.RealName;
// var topDatas = input.CodeDatas.FindAll(a => string.IsNullOrEmpty(a.FatherCode));
// Dictionary<AddPrintCodeDetailInput,long> list = new Dictionary<AddPrintCodeDetailInput, long>();
// foreach (var item in topDatas)
// {
// var code = string.IsNullOrEmpty(item.BarCode) ? item.QrCode : item.BarCode;
// var codeType = string.IsNullOrEmpty(item.BarCode) ? "二维码" : "条码";
// var unit = units.Find(a => a.Name == item.PackageName);
2024-06-06 10:22:07 +00:00
2024-06-07 11:13:15 +00:00
// var detail = new AddPrintCodeDetailInput()
// {
// ReportTableId = input.WarehousingTableId,
// Code = code,
// CodeName = codeType,
// ChildCount = unit.ChildUnitCount,
// Count = 1,
// Unit = item.PackageName,
// BaseCount = unit.Rate,
// BaseUnit = baseUnit,
// PrintCodeTime = DateTime.Now,
// CreateUserId = userId,
// CreateUserName = userName
// };
// var detailId = await _codeDetailService.Add(detail);
// list.Add(detail, detailId);
2024-06-06 10:22:07 +00:00
2024-06-07 11:13:15 +00:00
// var childs = input.CodeDatas.FindAll(a => a.FatherCode == code);
// foreach (var child in childs)
// {
// var code2 = string.IsNullOrEmpty(child.BarCode) ? child.QrCode : child.BarCode;
// var unit2 = units.Find(a => a.Name == child.PackageName);
// var detail2 = new AddPrintCodeDetailInput()
// {
// ReportTableId = input.WarehousingTableId,
// Code = code2,
// CodeName = codeType,
// ChildCount = unit2.ChildUnitCount,
// Count = 1,
// Unit = child.PackageName,
// BaseCount = unit2.Rate,
// BaseUnit = baseUnit,
// PrintCodeTime = DateTime.Now,
// CreateUserId = userId,
// CreateUserName = userName,
// FatherCode = code,
// FatherId = detailId
// };
// var detailId2 = await _codeDetailService.Add(detail2);
// list.Add(detail2, detailId2);
2024-06-06 10:22:07 +00:00
2024-06-07 11:13:15 +00:00
// var childs3 = input.CodeDatas.FindAll(a => a.FatherCode == code2);
// foreach (var child3 in childs)
// {
// var code3 = string.IsNullOrEmpty(child3.BarCode) ? child3.QrCode : child3.BarCode;
// var unit3 = units.Find(a => a.Name == child3.PackageName);
// var detail3 = new AddPrintCodeDetailInput()
// {
// ReportTableId = input.WarehousingTableId,
// Code = code3,
// CodeName = codeType,
// ChildCount = unit3.ChildUnitCount,
// Count = 1,
// Unit = child3.PackageName,
// BaseCount = unit3.Rate,
// BaseUnit = baseUnit,
// PrintCodeTime = DateTime.Now,
// CreateUserId = userId,
// CreateUserName = userName,
// FatherCode = code2,
// FatherId = detailId2
// };
// var detailId3 = await _codeDetailService.Add(detail3);
// list.Add(detail3, detailId3);
// if (input.CodeDatas.Any(a => a.FatherCode == code3))
// {
// var childs4 = input.CodeDatas.FindAll(a => a.FatherCode == code3);
// foreach (var child4 in childs)
// {
// var code4 = string.IsNullOrEmpty(child4.BarCode) ? child4.QrCode : child4.BarCode;
// var unit4 = units.Find(a => a.Name == child4.PackageName);
// var detail4 = new AddPrintCodeDetailInput()
// {
// ReportTableId = input.WarehousingTableId,
// Code = code4,
// CodeName = codeType,
// ChildCount = unit4.ChildUnitCount,
// Count = 1,
// Unit = child4.PackageName,
// BaseCount = unit4.Rate,
// BaseUnit = baseUnit,
// PrintCodeTime = DateTime.Now,
// CreateUserId = userId,
// CreateUserName = userName,
// FatherCode = code3,
// FatherId = detailId3
// };
// var detailId4 = await _codeDetailService.Add(detail4);
// list.Add(detail4, detailId4);
// if (input.CodeDatas.Any(a => a.FatherCode == code4))
// {
// var childs5 = input.CodeDatas.FindAll(a => a.FatherCode == code4);
// foreach (var child5 in childs)
// {
// var code5 = string.IsNullOrEmpty(child5.BarCode) ? child5.QrCode : child5.BarCode;
// var unit5 = units.Find(a => a.Name == child5.PackageName);
// var detail5 = new AddPrintCodeDetailInput()
// {
// ReportTableId = input.WarehousingTableId,
// Code = code5,
// CodeName = codeType,
// ChildCount = unit5.ChildUnitCount,
// Count = 1,
// Unit = child5.PackageName,
// BaseCount = unit5.Rate,
// BaseUnit = baseUnit,
// PrintCodeTime = DateTime.Now,
// CreateUserId = userId,
// CreateUserName = userName,
// FatherCode = code4,
// FatherId = detailId4
// };
// var detailId5 = await _codeDetailService.Add(detail5);
// list.Add(detail5, detailId5);
// }
// }
// }
// }
2024-06-06 10:22:07 +00:00
2024-06-07 11:13:15 +00:00
// }
// }
2024-06-06 10:22:07 +00:00
2024-06-07 11:13:15 +00:00
// }
2024-06-06 10:22:07 +00:00
2024-06-07 11:13:15 +00:00
// foreach (var dic in list)
// {
// var item = dic.Key;
// var retrospect1 = new AddProductRetrospectInput()
// {
// BaseCount = item.BaseCount,
// BaseUnit = baseUnit,
// Batch = warehousing.Batch,
// BusinessType = "扫码入库",
// CodeType = item.CodeName,
// Department = warehousing.ProductionLine,
// Destination = warehousing.Supplier,
// WarehouseID = warehousing.WarehouseId,
// WarehousingDate = DateTime.Now,
// Count = 1,
// CreateTime = DateTime.Now,
// ScanCodeTime = DateTime.Now,
// Location = warehousing.WarehouseLocation,
// CreateUserId = userId,
// CreateUserName = userName,
// MaterialsId = dic.Value,
// OddNumber = warehousing.OddNumber,
// Receipt = "入库单",
// SourceId = warehousing.Id,
// Unit = item.Unit,
// Name = materials.Name,
// Code = item.Code
// };
// await _productRetrospect.Add(retrospect1);
// }
2024-06-06 10:22:07 +00:00
2024-06-07 11:13:15 +00:00
// var details = await _warehouseDetails.List();
// if (details != null && details.Any(a => a.MaterialsId == warehousing.MaterialsId && a.Unit == warehousing.Unit))
// {
// var unit = units.Find(a => a.Name == warehousing.Unit);
// var detail = details.Find(a => a.MaterialsId == warehousing.MaterialsId && a.Unit == warehousing.Unit);
// detail.Count += warehousing.Count;
// if (unit != null)
// {
// detail.BaseCount = detail.Count * unit.Rate;
// }
// await _warehouseDetails.UpdateByEntity(detail.Adapt<WarehouseDetails>());
// }
// else
// {
// var newEnt = warehousing.Adapt<AddWarehouseDetailsInput>();
// newEnt.SourceId = warehousing.Id;
// var addId = await _warehouseDetails.Add(newEnt);
// }
// var newDetail = warehousing.Adapt<AddWarehousingStatisticsInput>();
// newDetail.SourceId = warehousing.Id;
// await _warehousingStatisticsService.Add(newDetail);
//}
2024-06-06 10:22:07 +00:00
2024-05-31 10:13:10 +00:00
/// <summary>
/// 增加产品入库
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task<long> Add(AddProductWarehousingInput input)
{
SysUnitOutput unit = null;
2024-06-04 10:20:10 +00:00
try
2024-05-31 10:13:10 +00:00
{
2024-06-04 10:20:10 +00:00
var materials = await _materialsService.Detail(new QueryByIdMaterialsInput() { Id = input.MaterialsId.Value });
2024-06-07 11:13:15 +00:00
2024-06-04 10:20:10 +00:00
string unitScale = string.Empty;
if (materials != null)
2024-05-31 10:13:10 +00:00
{
2024-06-04 10:20:10 +00:00
input.Specifications = materials.Specifications;
input.CodeNum = materials.CodeNum;
input.Brand = materials.Brand;
var units = await _sysUnitService.ListByGroupId(materials.UnitGroupId.Value);
unit = units.Find(a => a.Name == input.Unit);
if (unit != null)
{
input.BaseCount = input.Count * unit.Rate;
input.BaseUnit = units.FirstOrDefault(a => a.IsBaseUnit == true).Name;
unitScale = SysUnitService.GetUnitScale(units);
}
input.GoodCode = materials.BarCode;
input.MaterialsNum = materials.CodeNum;
input.UnitPrice = materials.Price;
input.Custom = input.Supplier;
if (unit != null)
{
input.BaseCount = input.Count * unit.Rate;
}
input.TotalPrice = input.BaseCount * materials.Price;
input.PackageScale = unitScale;
input.Classify = (await _materialClassifyService.Detail(new QueryByIdMaterialClassifyInput() { Id = materials.Classify })).Name;
2024-05-31 10:13:10 +00:00
}
}
2024-06-04 10:20:10 +00:00
catch (Exception ex)
{
}
2024-05-31 10:13:10 +00:00
2024-06-06 10:22:07 +00:00
input.CreateUserId = _userManager.UserId;
input.CreateUserName = _userManager.RealName;
2024-05-31 10:13:10 +00:00
var entity = input.Adapt<ProductWarehousing>();
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 删除产品入库
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteProductWarehousingInput input)
{
var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
await _rep.FakeDeleteAsync(entity); //假删除
//await _rep.DeleteAsync(entity); //真删除
}
/// <summary>
/// 更新产品入库
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Update")]
public async Task Update(UpdateProductWarehousingInput input)
{
var entity = input.Adapt<ProductWarehousing>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 获取产品入库
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<ProductWarehousing> Detail([FromQuery] QueryByIdProductWarehousingInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
/// <summary>
/// 获取产品入库列表
/// </summary>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
2024-06-07 11:13:15 +00:00
public async Task<List<ProductWarehousingOutput>> List()
2024-05-31 10:13:10 +00:00
{
return await _rep.AsQueryable().Select<ProductWarehousingOutput>().ToListAsync();
}
}