using Admin.NET.Plugin.AiDOP.Dto.S0.Manufacturing;
using Admin.NET.Plugin.AiDOP.Dto.S0.Sales;
using Admin.NET.Plugin.AiDOP.Entity.S0.Manufacturing;
using Admin.NET.Plugin.AiDOP.Infrastructure;
namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Manufacturing;
///
/// 生产线维护(LineMaster 语义,表 ado_s0_mfg_line_master)。
///
[ApiController]
[Route("api/s0/manufacturing/production-lines")]
[AllowAnonymous]
[NonUnify]
public class AdoS0MfgProductionLinesController : ControllerBase
{
private readonly SqlSugarRepository _rep;
public AdoS0MfgProductionLinesController(SqlSugarRepository rep)
{
_rep = rep;
}
[HttpGet]
public async Task GetPagedAsync([FromQuery] AdoS0LineMasterQueryDto 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.Line.Contains(q.Keyword!) || (x.Describe != null && x.Describe.Contains(q.Keyword!)))
.WhereIF(!string.IsNullOrWhiteSpace(q.Line), x => x.Line.Contains(q.Line!))
.WhereIF(!string.IsNullOrWhiteSpace(q.Workshop),
x => x.Workshop != null && x.Workshop.Contains(q.Workshop!))
.WhereIF(q.IsEnabled.HasValue, x => x.IsActive == q.IsEnabled!.Value);
var total = await query.CountAsync();
var entities = await query
.OrderByDescending(x => x.Id)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
// GeneralizedCodeMaster 中文说明:当前库无码表实体,占位供列表展示;后续可 JOIN 补全。
var list = entities.Select(x => new
{
x.Id,
x.CompanyRefId,
x.FactoryRefId,
x.Domain,
x.Line,
describe = x.Describe,
x.LineType,
x.LineCategory,
x.Location,
x.Workshop,
vLocation = x.VLocation,
x.Location2,
x.Location3,
pickingLocation = x.PickingLocation,
midLocation = x.MidLocation,
isActive = x.IsActive,
x.CreateUser,
x.CreateTime,
x.UpdateUser,
x.UpdateTime,
lineCategoryComments = (string?)null,
workshopComments = (string?)null
}).ToList();
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] AdoS0LineMasterUpsertDto dto)
{
var now = DateTime.Now;
var entity = new AdoS0LineMaster
{
CompanyRefId = dto.CompanyRefId,
FactoryRefId = dto.FactoryRefId,
Domain = dto.Domain.Trim(),
Line = dto.Line.Trim(),
Describe = string.IsNullOrWhiteSpace(dto.Describe) ? null : dto.Describe.Trim(),
LineType = string.IsNullOrWhiteSpace(dto.LineType) ? null : dto.LineType.Trim(),
LineCategory = string.IsNullOrWhiteSpace(dto.LineCategory) ? null : dto.LineCategory.Trim(),
Location = string.IsNullOrWhiteSpace(dto.Location) ? null : dto.Location.Trim(),
Workshop = string.IsNullOrWhiteSpace(dto.Workshop) ? null : dto.Workshop.Trim(),
VLocation = string.IsNullOrWhiteSpace(dto.VLocation) ? null : dto.VLocation.Trim(),
Location2 = string.IsNullOrWhiteSpace(dto.Location2) ? null : dto.Location2.Trim(),
Location3 = string.IsNullOrWhiteSpace(dto.Location3) ? null : dto.Location3.Trim(),
PickingLocation = string.IsNullOrWhiteSpace(dto.PickingLocation) ? null : dto.PickingLocation.Trim(),
MidLocation = string.IsNullOrWhiteSpace(dto.MidLocation) ? null : dto.MidLocation.Trim(),
IsActive = dto.IsActive,
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] AdoS0LineMasterUpsertDto 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.Line = dto.Line.Trim();
entity.Describe = string.IsNullOrWhiteSpace(dto.Describe) ? null : dto.Describe.Trim();
entity.LineType = string.IsNullOrWhiteSpace(dto.LineType) ? null : dto.LineType.Trim();
entity.LineCategory = string.IsNullOrWhiteSpace(dto.LineCategory) ? null : dto.LineCategory.Trim();
entity.Location = string.IsNullOrWhiteSpace(dto.Location) ? null : dto.Location.Trim();
entity.Workshop = string.IsNullOrWhiteSpace(dto.Workshop) ? null : dto.Workshop.Trim();
entity.VLocation = string.IsNullOrWhiteSpace(dto.VLocation) ? null : dto.VLocation.Trim();
entity.Location2 = string.IsNullOrWhiteSpace(dto.Location2) ? null : dto.Location2.Trim();
entity.Location3 = string.IsNullOrWhiteSpace(dto.Location3) ? null : dto.Location3.Trim();
entity.PickingLocation = string.IsNullOrWhiteSpace(dto.PickingLocation) ? null : dto.PickingLocation.Trim();
entity.MidLocation = string.IsNullOrWhiteSpace(dto.MidLocation) ? null : dto.MidLocation.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 ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
entity.IsActive = dto.IsEnabled;
entity.UpdateTime = DateTime.Now;
await _rep.AsUpdateable(entity).ExecuteCommandAsync();
return Ok(new { entity.Id, isActive = entity.IsActive, entity.UpdateTime });
}
[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 = "删除成功" });
}
}