111 lines
5.3 KiB
C#
111 lines
5.3 KiB
C#
using JNPF.Common.Core.Manager;
|
|
using JNPF.Common.Extension;
|
|
using JNPF.Common.Filter;
|
|
using JNPF.Common.Security;
|
|
using JNPF.VisualDev;
|
|
using JNPF.VisualDev.Entitys.Dto.VisualDevModelData;
|
|
using Mapster;
|
|
using Newtonsoft.Json.Linq;
|
|
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 materialCode = "";
|
|
if (!input.queryJson.IsNullOrWhiteSpace())
|
|
{
|
|
materialCode = JObject.Parse(input.queryJson).Value<string>(nameof(WmsCarryCode.material_code));
|
|
}
|
|
|
|
List<WmsStockReportH> 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())
|
|
.WhereIF(!string.IsNullOrEmpty(materialCode), (a, b, c, d, e) => a.material_code.Contains(materialCode))
|
|
.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();
|
|
List<WmsCarryCode> 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);
|
|
|
|
IEnumerable<WmsStockReportH> result = items.GroupBy(g => new { g.warehouse_id, g.material_id }).Select(itGroup =>
|
|
{
|
|
_ = storeMap.TryGetValue(itGroup.Key, out WmsStockReportH? report);
|
|
WmsStockReportH stockReport = new()
|
|
{
|
|
material_code = report?.material_code ?? "",
|
|
mater_name = report?.mater_name ?? "",
|
|
existing_stock_qty = itGroup.Sum(d => d.codeqty),
|
|
max_stock = report?.max_stock ?? 0,
|
|
safe_stock = report?.safe_stock ?? 0,
|
|
min_stock = report?.min_stock ?? 0,
|
|
storage_valid_day = items.Find(t => t.material_id == itGroup.Key.material_id)?.storage_valid_day,
|
|
early_warn_day = items.Find(t => t.material_id == itGroup.Key.material_id)?.early_warn_day,
|
|
create_id = _userManager.UserId,
|
|
create_time = DateTime.Now
|
|
};
|
|
if (stockReport.storage_valid_day.HasValue)
|
|
{
|
|
decimal expired_warning_qty = 0;
|
|
foreach (WmsStockReportH? 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;
|
|
}
|
|
List<WmsCarryCode> 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;
|
|
});
|
|
|
|
|
|
List<WmsStockReportH> 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);
|
|
}
|
|
}
|
|
}
|