using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.ComponentTCBBatchCreateContainerServiceVersionRequest.Types;
using RazorEngine.Compilation.ImpromptuInterface.InvokeExt;
using AngleSharp.Dom;
namespace Admin.NET.Application;
///
/// 产品入库服务
///
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class ProductWarehousingService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly MaterialsService _materialsService;
private readonly SysUnitService _sysUnitService;
private readonly MaterialClassifyService _materialClassifyService;
//private readonly WarehouseDetailsService _warehouseDetails;
//private readonly WarehousingStatisticsService _warehousingStatisticsService;
//private readonly PrintCodeDetailService _codeDetailService;
//private readonly ProductRetrospectService _productRetrospect;
private readonly UserManager _userManager;
public ProductWarehousingService(SqlSugarRepository rep,
UserManager userManager,
MaterialsService materialsService,
SysUnitService sysUnitService,
MaterialClassifyService materialClassifyService)
{
_rep = rep;
_userManager = userManager;
_materialsService = materialsService;
_sysUnitService = sysUnitService;
_materialClassifyService = materialClassifyService;
//_codeDetailService = codeDetailService;
//_warehouseDetails = warehouseDetails;
//_productRetrospect = productRetrospect;
//_warehousingStatisticsService = warehousingStatisticsService;
}
///
/// 分页查询产品入库
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task> Page(ProductWarehousingInput input)
{
var query = _rep.AsQueryable().Where(a => !a.IsDelete)
.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()))
.WhereIF(input.MaterialsId > 0, u => u.MaterialsId == input.MaterialsId)
.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()))
.WhereIF(input.Count > 0, u => u.Count == input.Count)
.WhereIF(!string.IsNullOrWhiteSpace(input.Unit), u => u.Unit.Contains(input.Unit.Trim()))
.WhereIF(input.BaseCount > 0, u => u.BaseCount == input.BaseCount)
.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()))
.WhereIF(input.CodeCount > 0, u => u.CodeCount == input.CodeCount)
.WhereIF(!string.IsNullOrWhiteSpace(input.WarehouseLocation), u => u.WarehouseLocation.Contains(input.WarehouseLocation.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Remarks), u => u.Remarks.Contains(input.Remarks.Trim()))
.WhereIF(input.SourceId > 0, u => u.SourceId == input.SourceId)
.WhereIF(input.WarehouseId > 0, u => u.WarehouseId == input.WarehouseId)
.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()))
.WhereIF(input.Status > 0, u => u.Status == input.Status)
.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();
if (input.ProductDateRange != null && input.ProductDateRange.Count > 0)
{
DateTime? start = input.ProductDateRange[0];
query = query.WhereIF(start.HasValue, u => u.ProductDate > start);
if (input.ProductDateRange.Count > 1 && input.ProductDateRange[1].HasValue)
{
var end = input.ProductDateRange[1].Value.AddDays(1);
query = query.Where(u => u.ProductDate < end);
}
}
if (input.LoseDateRange != null && input.LoseDateRange.Count > 0)
{
DateTime? start = input.LoseDateRange[0];
query = query.WhereIF(start.HasValue, u => u.LoseDate > start);
if (input.LoseDateRange.Count > 1 && input.LoseDateRange[1].HasValue)
{
var end = input.LoseDateRange[1].Value.AddDays(1);
query = query.Where(u => u.LoseDate < end);
}
}
if (input.WarehousingDateRange != null && input.WarehousingDateRange.Count > 0)
{
DateTime? start = input.WarehousingDateRange[0];
query = query.WhereIF(start.HasValue, u => u.WarehousingDate > start);
if (input.WarehousingDateRange.Count > 1 && input.WarehousingDateRange[1].HasValue)
{
var end = input.WarehousingDateRange[1].Value.AddDays(1);
query = query.Where(u => u.WarehousingDate < end);
}
}
return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
}
///
/// 产品入库
///
///
///
//[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 list = new Dictionary();
// 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);
// 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);
// 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);
// 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);
// }
// }
// }
// }
// }
// }
// }
// 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);
// }
// 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());
// }
// else
// {
// var newEnt = warehousing.Adapt();
// newEnt.SourceId = warehousing.Id;
// var addId = await _warehouseDetails.Add(newEnt);
// }
// var newDetail = warehousing.Adapt();
// newDetail.SourceId = warehousing.Id;
// await _warehousingStatisticsService.Add(newDetail);
//}
///
/// 增加产品入库
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task Add(AddProductWarehousingInput input)
{
SysUnitOutput unit = null;
try
{
var materials = await _materialsService.Detail(new QueryByIdMaterialsInput() { Id = input.MaterialsId.Value });
string unitScale = string.Empty;
if (materials != null)
{
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;
}
}
catch (Exception ex)
{
}
input.CreateUserId = _userManager.UserId;
input.CreateUserName = _userManager.RealName;
var entity = input.Adapt();
await _rep.InsertAsync(entity);
return entity.Id;
}
///
/// 删除产品入库
///
///
///
[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); //真删除
}
///
/// 更新产品入库
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Update")]
public async Task Update(UpdateProductWarehousingInput input)
{
var entity = input.Adapt();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 获取产品入库
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task Detail([FromQuery] QueryByIdProductWarehousingInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
///
/// 获取产品入库列表
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task> List()
{
return await _rep.AsQueryable().Select().ToListAsync();
}
}