using Admin.NET.Plugin.AiDOP.Dto.S0.Manufacturing;
using Admin.NET.Plugin.AiDOP.Entity.S0.Manufacturing;
using Admin.NET.Plugin.AiDOP.Infrastructure;
namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Manufacturing;
///
/// 标准工序维护(StdOpMaster 语义,表 ado_s0_mfg_std_op_master)。
///
[ApiController]
[Route("api/s0/manufacturing/standard-operations")]
[AllowAnonymous]
[NonUnify]
public class AdoS0MfgStandardOperationsController : ControllerBase
{
private readonly SqlSugarRepository _rep;
public AdoS0MfgStandardOperationsController(SqlSugarRepository rep)
{
_rep = rep;
}
[HttpGet]
public async Task GetPagedAsync([FromQuery] AdoS0StdOpMasterQueryDto q)
{
var page = q.EffectivePage;
var pageSize = q.PageSize;
(page, pageSize) = PagingGuard.Normalize(page, 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.Keyword), x => x.StdOp.Contains(q.Keyword!))
.WhereIF(!string.IsNullOrWhiteSpace(q.StdOp), x => x.StdOp.Contains(q.StdOp!))
.WhereIF(!string.IsNullOrWhiteSpace(q.MilestoneOp), x => x.MilestoneOp != null && x.MilestoneOp.Contains(q.MilestoneOp!));
var total = await query.CountAsync();
var list = await query
.OrderByDescending(x => x.Id)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
return Ok(new { total, page, 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] AdoS0StdOpMasterUpsertDto dto)
{
var now = DateTime.Now;
var entity = new AdoS0StdOpMaster
{
CompanyRefId = dto.CompanyRefId,
FactoryRefId = dto.FactoryRefId,
Domain = dto.Domain.Trim(),
StdOp = dto.StdOp.Trim(),
MilestoneOp = string.IsNullOrWhiteSpace(dto.MilestoneOp) ? null : dto.MilestoneOp.Trim(),
CreateUser = dto.CreateUser,
CreateTime = now,
UpdateUser = null,
UpdateTime = null
};
await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
return Ok(entity);
}
[HttpPut("{id:long}")]
public async Task UpdateAsync(long id, [FromBody] AdoS0StdOpMasterUpsertDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
entity.CompanyRefId = dto.CompanyRefId;
entity.FactoryRefId = dto.FactoryRefId;
entity.Domain = dto.Domain.Trim();
entity.StdOp = dto.StdOp.Trim();
entity.MilestoneOp = string.IsNullOrWhiteSpace(dto.MilestoneOp) ? null : dto.MilestoneOp.Trim();
entity.UpdateUser = dto.UpdateUser;
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 = "删除成功" });
}
}