DigitalFactory/Admin.NET/Admin.NET.Application/Service/WarehousingStatistics/WarehousingStatisticsServic...

440 lines
20 KiB
C#

using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
/// <summary>
/// 入库统计服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class WarehousingStatisticsService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<WarehousingStatistics> _rep;
private readonly WarehouseDetailsService _warehouseDetails;
private readonly MaterialsService _materialsService;
private readonly SysUnitService _sysUnitService;
private readonly MaterialClassifyService _materialClassifyService;
private readonly PrintCodeDetailService _codeDetailService;
private readonly ProductRetrospectService _productRetrospect;
//private readonly ProductWarehousingService _productWarehousing;
private readonly ReportDetailTableService _reportDetailTable;
private readonly UserManager _userManager;
public WarehousingStatisticsService(SqlSugarRepository<WarehousingStatistics> rep,
UserManager userManager,
WarehouseDetailsService warehouseDetails,
MaterialsService materialsService,
SysUnitService sysUnitService,
PrintCodeDetailService codeDetailService,
ProductRetrospectService productRetrospect,
MaterialClassifyService materialClassifyService,
ReportDetailTableService reportDetailTable)
{
_rep = rep;
_userManager = userManager;
_warehouseDetails = warehouseDetails;
_materialsService = materialsService;
_sysUnitService = sysUnitService;
_codeDetailService = codeDetailService;
_productRetrospect = productRetrospect;
_materialClassifyService = materialClassifyService;
//_productWarehousing = productWarehousing;
_reportDetailTable = reportDetailTable;
}
/// <summary>
/// 分页查询入库统计
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<WarehousingStatisticsOutput>> Page(WarehousingStatisticsInput input)
{
var query = _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.Name.Contains(input.SearchKey.Trim())
|| u.CodeNum.Contains(input.SearchKey.Trim())
|| u.Unit.Contains(input.SearchKey.Trim())
|| u.BaseUnit.Contains(input.SearchKey.Trim())
|| u.Brand.Contains(input.SearchKey.Trim())
|| u.Specifications.Contains(input.SearchKey.Trim())
|| u.WarehousingType.Contains(input.SearchKey.Trim())
|| u.WarehouseLocation.Contains(input.SearchKey.Trim())
|| u.Supplier.Contains(input.SearchKey.Trim())
|| u.Remarks.Contains(input.SearchKey.Trim())
)
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeNum), u => u.CodeNum.Contains(input.CodeNum.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.Brand), u => u.Brand.Contains(input.Brand.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Specifications), u => u.Specifications.Contains(input.Specifications.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.WarehousingType), u => u.WarehousingType.Contains(input.WarehousingType.Trim()))
.WhereIF(input.MaterialsId > 0, u => u.MaterialsId == input.MaterialsId)
.WhereIF(input.SourceId > 0, u => u.SourceId == input.SourceId)
.WhereIF(input.WarehouseId > 0, u => u.WarehouseId == input.WarehouseId)
.WhereIF(!string.IsNullOrWhiteSpace(input.WarehouseLocation), u => u.WarehouseLocation.Contains(input.WarehouseLocation.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Supplier), u => u.Supplier.Contains(input.Supplier.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Remarks), u => u.Remarks.Contains(input.Remarks.Trim()))
.Select<WarehousingStatisticsOutput>();
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);
}
/// <summary>
/// 增加入库统计
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task<long> Add(AddWarehousingStatisticsInput input)
{
//input.WarehousingDate = DateTime.Now;
//var materials = await _materialsService.Detail(new QueryByIdMaterialsInput() { Id = input.MaterialsId.Value });
//SysUnitOutput unit = null;
//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);
// }
//}
var entity = input.Adapt<WarehousingStatistics>();
await _rep.InsertAsync(entity);
//var details = await _warehouseDetails.List();
//if (details != null && details.Any(a => a.MaterialsId == input.MaterialsId && a.Unit == input.Unit))
//{
// var detail = details.Find(a => a.MaterialsId == input.MaterialsId && a.Unit == input.Unit);
// detail.Count += input.Count;
// if (unit != null)
// {
// detail.BaseCount = detail.Count * unit.Rate;
// }
// await _warehouseDetails.UpdateByEntity(detail.Adapt<WarehouseDetails>());
//}
//else
//{
// var newEnt = input.Adapt<AddWarehouseDetailsInput>();
// if (materials!=null)
// {
// newEnt.GoodCode = materials.BarCode;
// newEnt.MaterialsNum = materials.CodeNum;
// newEnt.UnitPrice = materials.Price;
// newEnt.Custom = input.Supplier;
// if (unit != null)
// {
// newEnt.BaseCount = newEnt.Count * unit.Rate;
// }
// newEnt.TotalPrice = newEnt.BaseCount * materials.Price;
// newEnt.PackageScale = unitScale;
// newEnt.Classify = (await _materialClassifyService.Detail(new QueryByIdMaterialClassifyInput() { Id = materials.Classify })).Name;
// var addId = await _warehouseDetails.Add(newEnt);
// }
//}
return entity.Id;
}
/// <summary>
/// 更新入库统计
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Warehousing")]
public async Task Warehousing(WarehousingInputData input)
{
if (input == null || input.WarehouseId == null || input.CodeDatas == null || input.CodeDatas.Count == 0)
{
throw Oops.Oh(ErrorCodeEnum.xg1002);
}
var codeDetails = await _codeDetailService.List();
var details = await _warehouseDetails.List();
var userId = _userManager.UserId;
var userName = _userManager.RealName;
ReportDetailTable reportTable = null;
foreach (var item in input.CodeDatas)
{
var product = await _codeDetailService.GetByProductCode(item);
if (product==null)
{
continue;
}
//product.WarehouseID = input.WarehouseId;
reportTable = await _reportDetailTable.Detail(new QueryByIdReportDetailTableInput() { Id = product.ReportTableId.Value });
if (reportTable!=null)
{
var materials = await _materialsService.GetById(reportTable.MaterialsId);
if (materials == null)
continue;
var units = await _sysUnitService.ListByGroupId(materials.UnitGroupId);
var baseUnit = units.LastOrDefault().Name;
var unit = units.Find(a => a.Name == reportTable.Unit);
if (details != null && details.Any(a => a.MaterialsId == reportTable.MaterialsId && a.Unit == reportTable.Unit))
{
var detail = details.Find(a => a.MaterialsId == reportTable.MaterialsId && a.Unit == reportTable.Unit);
detail.Count += 1;
if (unit != null)
{
detail.BaseCount = detail.Count * unit.Rate;
}
await _warehouseDetails.UpdateByEntity(detail.Adapt<WarehouseDetails>());
}
else
{
var newEnt = reportTable.Adapt<AddWarehouseDetailsInput>();
newEnt.SourceId = reportTable.Id;
newEnt.Count = 1;
newEnt.BaseCount = unit.Rate;
newEnt.WarehouseId = input.WarehouseId;
newEnt.GoodCode = item;
newEnt.MaterialsNum = reportTable.ProductCodeNum;
var addId = await _warehouseDetails.Add(newEnt);
}
var retrospect1 = new AddProductRetrospectInput()
{
BaseCount = unit.Rate,
BaseUnit = baseUnit,
Batch = reportTable.Batch,
BusinessType = "入库单",
CodeType = input.WarehousingType,
Department = reportTable.ProductionLine,
//WarehouseID = input.WarehouseId,
WarehousingDate = DateTime.Now,
Count = 1,
CreateTime = DateTime.Now,
ScanCodeTime = DateTime.Now,
CreateUserId = userId,
CreateUserName = userName,
MaterialsId = reportTable.MaterialsId,
OddNumber = reportTable.OddNumber,
Receipt = "汇报单",
SourceId = reportTable.Id,
Unit = unit.Name,
Name = materials.Name,
Code = item
};//Destination = warehousing.Supplier,
await _productRetrospect.Add(retrospect1);
var childs = codeDetails.FindAll(a => a.FatherCode == item);
if (childs.Count>0)
{
var currentUnit = units.FirstOrDefault(a => a.Name == childs.FirstOrDefault().Unit);
foreach (var c1 in childs)
{
var retrospect2 = new AddProductRetrospectInput()
{
BaseCount = currentUnit.Rate,
BaseUnit = baseUnit,
Batch = reportTable.Batch,
BusinessType = "入库单",
CodeType = input.WarehousingType,
Department = reportTable.ProductionLine,
//WarehouseID = input.WarehouseId,
WarehousingDate = DateTime.Now,
Count = 1,
CreateTime = DateTime.Now,
ScanCodeTime = DateTime.Now,
CreateUserId = userId,
CreateUserName = userName,
MaterialsId = reportTable.MaterialsId,
OddNumber = reportTable.OddNumber,
Receipt = "汇报单",
SourceId = reportTable.Id,
Unit = currentUnit.Name,
Name = materials.Name,
Code = c1.Code
};//Destination = warehousing.Supplier,
await _productRetrospect.Add(retrospect2);
var childs3 = codeDetails.FindAll(a => a.FatherCode == c1.Code);
if (childs3.Count > 0)
{
var currentUnit3 = units.FirstOrDefault(a => a.Name == childs3.FirstOrDefault().Unit);
foreach (var c3 in childs3)
{
var retrospect3 = new AddProductRetrospectInput()
{
BaseCount = currentUnit3.Rate,
BaseUnit = baseUnit,
Batch = reportTable.Batch,
BusinessType = "入库单",
CodeType = input.WarehousingType,
Department = reportTable.ProductionLine,
//WarehouseID = input.WarehouseId,
WarehousingDate = DateTime.Now,
Count = 1,
CreateTime = DateTime.Now,
ScanCodeTime = DateTime.Now,
CreateUserId = userId,
CreateUserName = userName,
MaterialsId = reportTable.MaterialsId,
OddNumber = reportTable.OddNumber,
Receipt = "汇报单",
SourceId = reportTable.Id,
Unit = currentUnit3.Name,
Name = materials.Name,
Code = c3.Code
};//Destination = warehousing.Supplier,
await _productRetrospect.Add(retrospect3);
var childs4 = codeDetails.FindAll(a => a.FatherCode == c3.Code);
if (childs4.Count > 0)
{
var currentUnit4 = units.FirstOrDefault(a => a.Name == childs4.FirstOrDefault().Unit);
foreach (var c4 in childs4)
{
var retrospect4 = new AddProductRetrospectInput()
{
BaseCount = currentUnit4.Rate,
BaseUnit = baseUnit,
Batch = reportTable.Batch,
BusinessType = "入库单",
CodeType = input.WarehousingType,
Department = reportTable.ProductionLine,
//WarehouseID = input.WarehouseId,
WarehousingDate = DateTime.Now,
Count = 1,
CreateTime = DateTime.Now,
ScanCodeTime = DateTime.Now,
CreateUserId = userId,
CreateUserName = userName,
MaterialsId = reportTable.MaterialsId,
OddNumber = reportTable.OddNumber,
Receipt = "汇报单",
SourceId = reportTable.Id,
Unit = currentUnit4.Name,
Name = materials.Name,
Code = c4.Code
};//Destination = warehousing.Supplier,
await _productRetrospect.Add(retrospect4);
}
}
}
}
}
}
}
}
//var warehousing = await Detail(new QueryByIdReportDetailTableInput() { Id = input.WarehousingTableId.Value });
//if (warehousing == null)
//{
// throw Oops.Oh(ErrorCodeEnum.xg1002);
//}
//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 = reportTable.Adapt<AddWarehousingStatisticsInput>();
newDetail.SourceId = reportTable.Id;
await Add(newDetail);
}
/// <summary>
/// 删除入库统计
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteWarehousingStatisticsInput 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(UpdateWarehousingStatisticsInput input)
{
var entity = input.Adapt<WarehousingStatistics>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 获取入库统计
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<WarehousingStatistics> Detail([FromQuery] QueryByIdWarehousingStatisticsInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
/// <summary>
/// 获取入库统计列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task<List<WarehousingStatisticsOutput>> List()
{
return await _rep.AsQueryable().Select<WarehousingStatisticsOutput>().ToListAsync();
}
}