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

382 lines
17 KiB
C#

using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
using Mapster;
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 PrintCodeDetailService _codeDetailService;
private readonly ProductRetrospectService _productRetrospect;
private readonly ReportDetailTableService _reportDetailTable;
private readonly UserManager _userManager;
private readonly ReportTableService _reportTableService;
private readonly WarehouseService _warehouseService;
public WarehousingStatisticsService(SqlSugarRepository<WarehousingStatistics> rep,
UserManager userManager,
WarehouseDetailsService warehouseDetails,
MaterialsService materialsService,
SysUnitService sysUnitService,
PrintCodeDetailService codeDetailService,
ProductRetrospectService productRetrospect,
ReportDetailTableService reportDetailTable,
ReportTableService reportTableService,
WarehouseService warehouseService)
{
_rep = rep;
_userManager = userManager;
_warehouseDetails = warehouseDetails;
_materialsService = materialsService;
_sysUnitService = sysUnitService;
_codeDetailService = codeDetailService;
_productRetrospect = productRetrospect;
_reportDetailTable = reportDetailTable;
_reportTableService = reportTableService;
_warehouseService = warehouseService;
}
/// <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;
ReportTable reportTable = null;
var warehouse = await _warehouseService.Detail(new QueryByIdWarehouseInput() { Id = input.WarehouseId.Value });
var warehouseName = warehouse == null ? "" : warehouse.Name;
var baseCount = input.CodeDatas.Count;
var tempAddList = new List<PrintCodeDetail>();
string tempBaseUnit = null;
string tempBrand = null;
foreach (var item in input.CodeDatas)
{
var product = await _codeDetailService.GetByProductCode(item);
if (product==null)
{
continue;
}
tempAddList.Add(product);
//product.WarehouseID = input.WarehouseId;
reportTable = await _reportTableService.Detail(new QueryByIdReportTableInput() { Id = product.ReportTableId.Value,SourceId=product.ReportTableId });
if (reportTable==null)
{
var table = await _reportDetailTable.Detail(new QueryByIdReportDetailTableInput() { Id = product.ReportTableId.Value });
if (table!=null)
{
reportTable = new ReportTable()
{
BaseProductCount = table.BaseProductCount,
Batch = table.Batch,
CodeNum = table.CodeNum,
CreateTime = table.CreateTime,
CreateUser = table.CreateUser,
CreateUserId = table.CreateUserId,
CreateUserName = table.CreateUserName,
MaterialsId = table.MaterialsId,
OddNumber = table.OddNumber,
ProductCount = table.ProductCount,
ProductionLine = table.ProductionLine,
ProductType = table.ProductType,
SourceNumber = table.SourceNumber,
StartDate = table.ProductDate,
Unit = table.Unit,
Remarks = table.Remarks
};
}
}
if (reportTable!=null)
{
var materials = await _materialsService.GetById(reportTable.MaterialsId);
if (materials == null)
continue;
var units = await _sysUnitService.ListByGroupId(materials.UnitGroupId);
var baseUnit = units.FirstOrDefault().Name;
var unit = units.Find(a => a.Name == product.Unit);
if (unit == null)
unit = units.FirstOrDefault();
baseCount = baseCount * unit.Rate.Value;
if (string.IsNullOrEmpty(reportTable.Unit))
{
reportTable.Unit = product.Unit;
}
tempBaseUnit = baseUnit;
tempBrand = materials.Brand;
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 = materials.CodeNum;
var addId = await _warehouseDetails.Add(newEnt);
}
await _productRetrospect.AddRetrospect(product, reportTable, "入库单", warehouseName, reportTable.Id, input.WarehouseId);
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 chEnt1 = c1.Adapt<PrintCodeDetail>();
await _productRetrospect.AddRetrospect(chEnt1, reportTable, "入库单", warehouseName, reportTable.Id, input.WarehouseId);
tempAddList.Add(chEnt1);
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 chEnt3 = c3.Adapt<PrintCodeDetail>();
await _productRetrospect.AddRetrospect(chEnt3, reportTable, "入库单", warehouseName, reportTable.Id, input.WarehouseId);
tempAddList.Add(chEnt3);
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 chEnt4 = c4.Adapt<PrintCodeDetail>();
await _productRetrospect.AddRetrospect(chEnt4, reportTable, "入库单", warehouseName, reportTable.Id, input.WarehouseId);
tempAddList.Add(chEnt4);
}
}
}
}
}
}
}
}
var newDetail = reportTable.Adapt<AddWarehousingStatisticsInput>();
newDetail.Count = input.CodeDatas.Count;
newDetail.BaseCount = baseCount;
newDetail.WarehousingDate = DateTime.Now;
newDetail.BaseUnit = tempBaseUnit;
newDetail.Brand = tempBrand;
if (reportTable != null)
{
newDetail.SourceId = reportTable?.Id;
}
var warehousingStaId = await Add(newDetail);
foreach (var item in tempAddList)
{
item.ReportTableId = warehousingStaId;
item.Id = 0;
await _codeDetailService.Add(item.Adapt<AddPrintCodeDetailInput>());
}
}
/// <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();
}
}