Implement API security baseline (sys_user, JWT, audit fields, transactions, unique constraints) and Web real login with 401 handling. Document setup and development workflows in root, WmsLite.Api, and WmsLite.Web README files. Co-authored-by: Cursor <cursoragent@cursor.com>
111 lines
4.4 KiB
C#
111 lines
4.4 KiB
C#
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using SqlSugar;
|
|
using WmsLite.Api.Common;
|
|
using WmsLite.Api.Dtos.Inventory;
|
|
using WmsLite.Api.Enums;
|
|
using WmsLite.Api.Infrastructure.Auth;
|
|
using WmsLite.Api.Models;
|
|
using WmsLite.Api.Repositories.Interfaces;
|
|
|
|
namespace WmsLite.Api.Repositories;
|
|
|
|
public class InventoryRepository : BaseRepository<Inventory>, IInventoryRepository
|
|
{
|
|
private static readonly Regex KeyRegex = new("^[a-z0-9_]+$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
public InventoryRepository(ISqlSugarClient db, ICurrentUserContext userContext) : base(db, userContext)
|
|
{
|
|
}
|
|
|
|
public async Task<PagedResult<Inventory>> GetPagedListAsync(InventoryPagedQuery query)
|
|
{
|
|
var pageIndex = query.PageIndex <= 0 ? 1 : query.PageIndex;
|
|
var pageSize = query.PageSize <= 0 ? 20 : query.PageSize;
|
|
var tenantId = query.TenantId.Trim();
|
|
var ownerCode = query.OwnerCode.Trim();
|
|
|
|
var dbQuery = Db.Queryable<Inventory>()
|
|
.Where(x => !x.IsDeleted && x.TenantId == tenantId && x.OwnerCode == ownerCode)
|
|
.WhereIF(query.InventoryKind.HasValue, x => x.InventoryKind == query.InventoryKind!.Value)
|
|
.WhereIF(query.InventoryStatus.HasValue, x => x.InventoryStatus == query.InventoryStatus!.Value)
|
|
.WhereIF(!string.IsNullOrWhiteSpace(query.WarehouseCode), x => x.WarehouseCode == query.WarehouseCode!.Trim())
|
|
.WhereIF(!string.IsNullOrWhiteSpace(query.AreaCode), x => x.AreaCode == query.AreaCode!.Trim())
|
|
.WhereIF(!string.IsNullOrWhiteSpace(query.ZoneCode), x => x.ZoneCode == query.ZoneCode!.Trim())
|
|
.WhereIF(!string.IsNullOrWhiteSpace(query.MaterialCode),
|
|
x => x.MaterialCode != null && x.MaterialCode.Contains(query.MaterialCode!.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(query.ContainerCode),
|
|
x => x.ContainerCode != null && x.ContainerCode.Contains(query.ContainerCode!.Trim()));
|
|
|
|
if (query.SearchParam is { Count: > 0 })
|
|
{
|
|
foreach (var entry in query.SearchParam)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(entry.Key) || entry.Value is null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var key = entry.Key.Trim();
|
|
if (!KeyRegex.IsMatch(key))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var paramName = $"p_{key}";
|
|
dbQuery = dbQuery.Where(
|
|
$"attrs->>'{key}' = @{paramName}",
|
|
new[] { new SugarParameter(paramName, entry.Value?.ToString()) });
|
|
}
|
|
}
|
|
|
|
RefAsync<int> total = 0;
|
|
var items = await dbQuery
|
|
.OrderByDescending(x => x.UpdateTime)
|
|
.OrderByDescending(x => x.CreateTime)
|
|
.ToPageListAsync(pageIndex, pageSize, total);
|
|
|
|
return new PagedResult<Inventory>
|
|
{
|
|
PageIndex = pageIndex,
|
|
PageSize = pageSize,
|
|
TotalCount = total,
|
|
Items = items
|
|
};
|
|
}
|
|
|
|
public async Task<Inventory?> GetByStockKeyAsync(
|
|
string tenantId,
|
|
string ownerCode,
|
|
string warehouseCode,
|
|
string areaCode,
|
|
string zoneCode,
|
|
string locationCode,
|
|
InventoryKind inventoryKind,
|
|
string? materialCode,
|
|
string? containerCode)
|
|
{
|
|
var tenant = tenantId.Trim();
|
|
var owner = ownerCode.Trim();
|
|
var warehouse = warehouseCode.Trim();
|
|
var area = areaCode.Trim();
|
|
var zone = zoneCode.Trim();
|
|
var location = locationCode.Trim();
|
|
var material = string.IsNullOrWhiteSpace(materialCode) ? null : materialCode.Trim();
|
|
var container = string.IsNullOrWhiteSpace(containerCode) ? null : containerCode.Trim();
|
|
|
|
return await Db.Queryable<Inventory>()
|
|
.Where(x => !x.IsDeleted
|
|
&& x.TenantId == tenant
|
|
&& x.OwnerCode == owner
|
|
&& x.WarehouseCode == warehouse
|
|
&& x.AreaCode == area
|
|
&& x.ZoneCode == zone
|
|
&& x.LocationCode == location
|
|
&& x.InventoryKind == inventoryKind)
|
|
.WhereIF(inventoryKind == InventoryKind.Material, x => x.MaterialCode == material)
|
|
.WhereIF(inventoryKind == InventoryKind.EmptyContainer, x => x.ContainerCode == container)
|
|
.FirstAsync();
|
|
}
|
|
}
|