using Admin.NET.Plugin.AiDOP.Dto.S0.Supply;
using Admin.NET.Plugin.AiDOP.Entity.S0.Supply;
using Admin.NET.Plugin.AiDOP.Infrastructure;
namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Supply;
///
/// S0 品类采购前置期主数据(订单评审/MRP 取数优先配置)。
///
[ApiController]
[Route("api/s0/supply/category-lead-times")]
[AllowAnonymous]
[NonUnify]
public class AdoS0CategoryLeadTimesController : ControllerBase
{
private readonly SqlSugarRepository _rep;
public AdoS0CategoryLeadTimesController(SqlSugarRepository rep)
{
_rep = rep;
}
[HttpGet]
public async Task GetPagedAsync([FromQuery] AdoS0CategoryLeadTimeQueryDto q)
{
(q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
var baseQuery = _rep.AsQueryable()
.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.CategoryCode), x => x.CategoryCode.Contains(q.CategoryCode!))
.WhereIF(!string.IsNullOrWhiteSpace(q.OwnerApplication), x => x.OwnerApplication.Contains(q.OwnerApplication!))
.WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive.Value)
.WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
x => x.CategoryCode.Contains(q.Keyword!)
|| (x.CategoryName != null && x.CategoryName.Contains(q.Keyword!))
|| x.OwnerApplication.Contains(q.Keyword!));
var total = await baseQuery.CountAsync();
var list = await baseQuery
.OrderByDescending(x => x.CreateTime)
.Skip((q.Page - 1) * q.PageSize)
.Take(q.PageSize)
.ToListAsync();
return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
}
[HttpGet("{id:long}")]
public async Task GetAsync(long id)
{
var item = await _rep.GetByIdAsync(id);
if (item == null) return NotFound();
return Ok(item);
}
[HttpPost]
public async Task CreateAsync([FromBody] AdoS0CategoryLeadTimeUpsertDto dto)
{
var uniqueErr = await EnsureUniqueAsync(dto, null);
if (uniqueErr != null) return uniqueErr;
var now = DateTime.Now;
var entity = new AdoS0CategoryLeadTime
{
CompanyRefId = dto.CompanyRefId,
FactoryRefId = dto.FactoryRefId,
DomainCode = dto.DomainCode,
CategoryCode = dto.CategoryCode.Trim(),
CategoryName = dto.CategoryName,
OwnerApplication = dto.OwnerApplication.Trim(),
LeadTimeDays = dto.LeadTimeDays,
Remarks = dto.Remarks,
IsActive = dto.IsActive,
CreateUser = dto.CreateUser,
CreateTime = now,
UpdateUser = dto.UpdateUser,
UpdateTime = null
};
await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
return Ok(entity);
}
[HttpPut("{id:long}")]
public async Task UpdateAsync(long id, [FromBody] AdoS0CategoryLeadTimeUpsertDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
var uniqueErr = await EnsureUniqueAsync(dto, id);
if (uniqueErr != null) return uniqueErr;
entity.CompanyRefId = dto.CompanyRefId;
entity.FactoryRefId = dto.FactoryRefId;
entity.DomainCode = dto.DomainCode;
entity.CategoryCode = dto.CategoryCode.Trim();
entity.CategoryName = dto.CategoryName;
entity.OwnerApplication = dto.OwnerApplication.Trim();
entity.LeadTimeDays = dto.LeadTimeDays;
entity.Remarks = dto.Remarks;
entity.IsActive = dto.IsActive;
entity.UpdateUser = dto.UpdateUser;
entity.UpdateTime = DateTime.Now;
await _rep.AsUpdateable(entity).ExecuteCommandAsync();
return Ok(entity);
}
[HttpPatch("{id:long}/toggle-enabled")]
public async Task ToggleActiveAsync(long id, [FromBody] AdoS0CategoryLeadTimeToggleActiveDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
entity.IsActive = dto.IsActive;
entity.UpdateTime = DateTime.Now;
await _rep.AsUpdateable(entity).ExecuteCommandAsync();
return Ok(entity);
}
[HttpDelete("{id:long}")]
public async Task DeleteAsync(long id)
{
var item = await _rep.GetByIdAsync(id);
if (item == null) return NotFound();
await _rep.DeleteAsync(item);
return Ok(new { message = "删除成功" });
}
private async Task EnsureUniqueAsync(AdoS0CategoryLeadTimeUpsertDto dto, long? id)
{
var categoryCode = dto.CategoryCode.Trim();
var ownerApplication = dto.OwnerApplication.Trim();
if (await _rep.IsAnyAsync(x =>
x.Id != (id ?? 0)
&& x.FactoryRefId == dto.FactoryRefId
&& x.CategoryCode == categoryCode
&& x.OwnerApplication == ownerApplication))
{
return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.InvalidRequest,
"同工厂下“品类编码 + 物料属性”已存在前置期配置");
}
return null;
}
}