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 物料计划周期标准(与订单评审/排程周期维度一致:工厂 + 订单类型 + std_hours)。
[ApiController]
[Route("api/s0/supply/material-plan-cycles")]
[AllowAnonymous]
[NonUnify]
public class AdoS0MaterialPlanCyclesController : ControllerBase
{
private readonly SqlSugarRepository _rep;
public AdoS0MaterialPlanCyclesController(SqlSugarRepository rep) => _rep = rep;
private static string NormalizeOrderType(string? orderType) => orderType?.Trim() ?? string.Empty;
[HttpGet]
public async Task GetPagedAsync([FromQuery] AdoS0MaterialPlanCycleQueryDto q)
{
(q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
var query = _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.OrderType), x => x.OrderType == q.OrderType)
.WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive!.Value)
.WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
x => x.OrderType.Contains(q.Keyword!)
|| (x.DomainCode != null && x.DomainCode.Contains(q.Keyword!))
|| (x.Remarks != null && x.Remarks.Contains(q.Keyword!)));
var total = await query.CountAsync();
var list = await query.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);
return item == null ? NotFound() : Ok(item);
}
[HttpPost]
public async Task CreateAsync([FromBody] AdoS0MaterialPlanCycleUpsertDto dto)
{
var orderType = NormalizeOrderType(dto.OrderType);
if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.OrderType == orderType))
return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "该工厂与订单类型组合已存在物料计划周期标准");
var now = DateTime.Now;
var entity = new AdoS0MaterialPlanCycle
{
CompanyRefId = dto.CompanyRefId,
FactoryRefId = dto.FactoryRefId,
DomainCode = dto.DomainCode,
OrderType = orderType,
StdHours = dto.StdHours,
Remarks = dto.Remarks?.Trim(),
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] AdoS0MaterialPlanCycleUpsertDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
var orderType = NormalizeOrderType(dto.OrderType);
if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.OrderType == orderType))
return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "该工厂与订单类型组合已存在其他记录");
entity.CompanyRefId = dto.CompanyRefId;
entity.FactoryRefId = dto.FactoryRefId;
entity.DomainCode = dto.DomainCode;
entity.OrderType = orderType;
entity.StdHours = dto.StdHours;
entity.Remarks = dto.Remarks?.Trim();
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] AdoS0MaterialPlanCycleToggleActiveDto 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 = "删除成功" });
}
}