using SqlSugar; using WmsLite.Api.Common; using WmsLite.Api.Dtos.Location; using WmsLite.Api.Infrastructure.Auth; using WmsLite.Api.Models; using WmsLite.Api.Repositories.Interfaces; namespace WmsLite.Api.Repositories; public class LocationRepository : BaseRepository, ILocationRepository { public LocationRepository(ISqlSugarClient db, ICurrentUserContext userContext) : base(db, userContext) { } public async Task GetByWarehouseAreaZoneAndLocationCodeAsync( string tenantId, string ownerCode, string warehouseCode, string areaCode, string zoneCode, string locationCode) { if (string.IsNullOrWhiteSpace(warehouseCode) || string.IsNullOrWhiteSpace(areaCode) || string.IsNullOrWhiteSpace(zoneCode) || string.IsNullOrWhiteSpace(locationCode)) { return null; } var w = warehouseCode.Trim(); var a = areaCode.Trim(); var z = zoneCode.Trim(); var l = locationCode.Trim(); var tenant = tenantId.Trim(); var owner = ownerCode.Trim(); return await Db.Queryable() .Where(x => !x.IsDeleted && x.TenantId == tenant && x.OwnerCode == owner && x.WarehouseCode == w && x.AreaCode == a && x.ZoneCode == z && x.Code == l) .FirstAsync(); } public async Task GetByWarehouseAreaAndLocationCodeAsync( string tenantId, string ownerCode, string warehouseCode, string areaCode, string locationCode) { if (string.IsNullOrWhiteSpace(warehouseCode) || string.IsNullOrWhiteSpace(areaCode) || string.IsNullOrWhiteSpace(locationCode)) { return null; } var w = warehouseCode.Trim(); var a = areaCode.Trim(); var l = locationCode.Trim(); var tenant = tenantId.Trim(); var owner = ownerCode.Trim(); return await Db.Queryable() .Where(x => !x.IsDeleted && x.TenantId == tenant && x.OwnerCode == owner && x.WarehouseCode == w && x.AreaCode == a && x.Code == l) .OrderBy(x => x.ZoneCode) .FirstAsync(); } public async Task> GetPagedListAsync(LocationPagedQuery query) { var pageIndex = query.PageIndex <= 0 ? 1 : query.PageIndex; var pageSize = query.PageSize <= 0 ? 20 : query.PageSize; var tenantId = ScopeTenantId; var ownerCode = ScopeOwnerCode; var dbQuery = Db.Queryable() .Where(x => !x.IsDeleted && x.TenantId == tenantId && x.OwnerCode == ownerCode) .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.AisleCode), x => x.AisleCode == query.AisleCode!.Trim()) .WhereIF(!string.IsNullOrWhiteSpace(query.Code), x => x.Code.Contains(query.Code!.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(query.Name), x => x.Name != null && x.Name.Contains(query.Name!.Trim())) .WhereIF(query.LocationStatus.HasValue, x => x.LocationStatus == query.LocationStatus!.Value) .WhereIF(query.IsOuter.HasValue, x => x.IsOuter == query.IsOuter!.Value) .WhereIF(!string.IsNullOrWhiteSpace(query.ContainerCode), x => x.ContainerCode != null && x.ContainerCode.Contains(query.ContainerCode!.Trim())); RefAsync total = 0; var items = await dbQuery .OrderBy(x => x.Id) .ToPageListAsync(pageIndex, pageSize, total); return new PagedResult { PageIndex = pageIndex, PageSize = pageSize, TotalCount = total, Items = items }; } /// 已存在于库内的编码。 public async Task> GetExistingCodesAsync( string tenantId, string ownerCode, string warehouseCode, string areaCode, string zoneCode, IReadOnlyCollection codes) { if (codes.Count == 0) { return new HashSet(StringComparer.Ordinal); } var tenant = tenantId.Trim(); var owner = ownerCode.Trim(); var w = warehouseCode.Trim(); var a = areaCode.Trim(); var z = zoneCode.Trim(); var list = codes .Where(static c => !string.IsNullOrWhiteSpace(c)) .Select(static c => c.Trim()) .Distinct() .ToList(); if (list.Count == 0) { return new HashSet(StringComparer.Ordinal); } var existing = await Db.Queryable() .Where(x => !x.IsDeleted && x.TenantId == tenant && x.OwnerCode == owner && x.WarehouseCode == w && x.AreaCode == a && x.ZoneCode == z) .Where(x => list.Contains(x.Code)) .Select(x => x.Code) .ToListAsync(); return existing.ToHashSet(StringComparer.Ordinal); } /// 批量插入。 public async Task InsertManyAsync(IReadOnlyList entities) { if (entities.Count == 0) { return; } var now = DateTime.Now; var list = new List(entities.Count); foreach (var entity in entities) { if (entity.Id == 0) { entity.Id = SnowFlakeSingle.Instance.NextId(); } entity.CreateTime ??= now; entity.IsDeleted = false; list.Add(entity); } await Db.Insertable(list).ExecuteCommandAsync(); } }