using Admin.NET.Plugin.AiDOP.Dto.S0.Warehouse;
using Admin.NET.Plugin.AiDOP.Entity.S0.Warehouse;
using Admin.NET.Plugin.AiDOP.Infrastructure;
namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Warehouse;
///
/// S0 货架主数据(LocationShelfMaster 语义)。独立货架页只读查询。
/// 货架的新增/修改/删除统一走「库位维护」主从保存(AdoS0LocationsController),本控制器不再暴露写入口。
/// 多租户隔离:所有读取显式限定当前请求租户;不依赖全局 AOP。
///
[ApiController]
[Route("api/s0/warehouse/location-shelves")]
[NonUnify]
public class AdoS0LocationShelvesController : ControllerBase
{
private readonly SqlSugarRepository _rep;
private readonly SqlSugarRepository _locRep;
public AdoS0LocationShelvesController(
SqlSugarRepository rep,
SqlSugarRepository locRep)
{
_rep = rep;
_locRep = locRep;
}
[HttpGet]
public async Task GetPagedAsync([FromQuery] AdoS0LocationShelfQueryDto q)
{
if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
(q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
var query = _rep.AsQueryable()
.Where(x => x.TenantId == tenantId)
.WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
.WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
.WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
.WhereIF(!string.IsNullOrWhiteSpace(q.Location), x => x.Location == q.Location)
.WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
x => x.InvShelf.Contains(q.Keyword!) || x.Location.Contains(q.Keyword!));
var total = await query.CountAsync();
var list = await query
.OrderBy(x => x.Location)
.OrderBy(x => x.InvShelf)
.Skip((q.Page - 1) * q.PageSize)
.Take(q.PageSize)
.ToListAsync();
await ApplyLocationDescrAsync(list, tenantId);
return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
}
[HttpGet("{id:long}")]
public async Task GetAsync(long id)
{
if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
var item = await _rep.AsQueryable().Where(x => x.Id == id && x.TenantId == tenantId).FirstAsync();
if (item == null) return NotFound();
await ApplyLocationDescrAsync(new List { item }, tenantId);
return Ok(item);
}
// 写入口(POST/PUT/DELETE)已下线:S0 货架的新增/修改/删除统一走「库位维护」主从保存
// (AdoS0LocationsController 的 locations 端点,随库位主表 FULL Replace)。本控制器仅保留 GET 查询。
///
/// 补显库位说明 + 「库位:货架」拼接。关联 LocationMaster 时限定当前租户,避免同编码库位跨租户补错描述。
///
private async Task ApplyLocationDescrAsync(List shelves, long tenantId)
{
if (shelves.Count == 0) return;
var domainCodes = shelves.Select(s => s.DomainCode).Distinct().ToList();
var locations = await _locRep.AsQueryable()
.Where(l => l.TenantId == tenantId && domainCodes.Contains(l.DomainCode))
.ToListAsync();
foreach (var shelf in shelves)
{
var loc = locations.Find(l =>
string.Equals(l.DomainCode, shelf.DomainCode, StringComparison.OrdinalIgnoreCase)
&& string.Equals(l.Location, shelf.Location, StringComparison.OrdinalIgnoreCase));
shelf.LocationDescr = loc?.Descr;
shelf.KwhjName = $"{shelf.Location}:{shelf.InvShelf}";
}
}
}