Files
tnb.server/WarehouseMgr/Tnb.WarehouseMgr/WmsStockReportService.cs
alex 778a7c7819 1
2023-08-25 17:35:22 +08:00

108 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JNPF.Common.Core.Manager;
using JNPF.Common.Filter;
using JNPF.Common.Security;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys.Dto.VisualDevModelData;
using Mapster;
using MimeKit.Cryptography;
using SqlSugar;
using Tnb.BasicData.Entities;
using Tnb.WarehouseMgr.Entities;
using Tnb.WarehouseMgr.Entities.Enums;
namespace Tnb.WarehouseMgr
{
/// <summary>
/// 库存报表服务类
/// </summary>
[OverideVisualDev(ModuleConsts.MODULE_WMSSTOCKREPORT_ID)]
public class WmsStockReportService : BaseWareHouseService
{
private readonly ISqlSugarClient _db;
private readonly IUserManager _userManager;
public WmsStockReportService(ISqlSugarRepository<WmsCarryCode> repository, IUserManager userManager)
{
_db = repository.AsSugarClient();
_userManager = userManager;
OverideFuncs.GetListAsync = GetListAsync;
}
private async Task<dynamic> GetListAsync(VisualDevModelListQueryInput input)
{
var items = await _db.Queryable<WmsCarryCode>().InnerJoin<BasMaterial>((a, b) => a.material_id == b.id)
.InnerJoin<BasMaterialSendWarehouse>((a, b, c) => b.id == c.material_id)
.InnerJoin<WmsCarryH>((a, b, c, d) => a.carry_id == d.id)
.InnerJoin<BasLocation>((a, b, c, d, e) => d.location_id == e.id)
.Where((a, b, c, d, e) => e.is_type == ((int)EnumLocationType.).ToString())
.Select((a, b, c, d, e) => new WmsStockReportH
{
warehouse_id = a.warehouse_id,
mater_name = b.name,
create_time = a.create_time,
create_id = a.create_id,
}, true)
.ToListAsync();
var carryCodes = await _db.Queryable<WmsCarryCode>().ToListAsync();
var storeMap = items.DistinctBy(x=> new {x.warehouse_id,x.material_id }).ToDictionary(x => new {x.warehouse_id,x.material_id }, x => x);
var result = items.GroupBy(g => new { g.warehouse_id, g.material_id }).Select(itGroup =>
{
storeMap.TryGetValue(itGroup.Key, out var report);
WmsStockReportH stockReport = new();
stockReport.material_code = report?.material_code ?? "";
stockReport.mater_name = report?.mater_name ?? "";
stockReport.existing_stock_qty = itGroup.Sum(d => d.codeqty);
stockReport.max_stock = report?.max_stock ?? 0;
stockReport.safe_stock = report?.safe_stock ?? 0;
stockReport.min_stock = report?.min_stock ?? 0;
stockReport.storage_valid_day = items.Find(t => t.material_id == itGroup.Key.material_id)?.storage_valid_day;
stockReport.early_warn_day = items.Find(t => t.material_id == itGroup.Key.material_id)?.early_warn_day;
stockReport.create_id = _userManager.UserId;
stockReport.create_time = DateTime.Now;
if (stockReport.storage_valid_day.HasValue)
{
decimal expired_warning_qty = 0;
foreach (var item in itGroup)
{
if (DateTime.Now.Subtract(item.create_time.Value).TotalDays >= (item.storage_valid_day - item.early_warn_day))
{
expired_warning_qty += item.codeqty;
}
}
stockReport.expired_warning_qty = expired_warning_qty;
}
var curCarryCodes = carryCodes.FindAll(x => x.warehouse_id == itGroup.Key.warehouse_id && x.material_id == itGroup.Key.material_id);
stockReport.Details = curCarryCodes.Adapt<List<WmsStockReportCode>>();
stockReport.Details.ForEach(x =>
{
x.id = SnowflakeIdHelper.NextId();
x.report_id = stockReport.id;
});
return stockReport;
});
var pages = result.Skip((input.currentPage - 1) * input.pageSize).Take(input.pageSize).ToList();
SqlSugarPagedList<WmsStockReportH> pagedList = new()
{
list = pages,
pagination = new()
{
CurrentPage = input.currentPage,
PageSize = input.pageSize,
Total = result.Count()
}
};
return PageResult<WmsStockReportH>.SqlSugarPageResult(pagedList);
}
}
}