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 单号规则主数据(NbrControl 语义)
///
[ApiController]
[Route("api/s0/warehouse/nbr-controls")]
[AllowAnonymous]
[NonUnify]
public class AdoS0NbrControlsController : ControllerBase
{
private readonly SqlSugarRepository _rep;
public AdoS0NbrControlsController(SqlSugarRepository rep)
{
_rep = rep;
}
[HttpGet]
public async Task GetPagedAsync([FromQuery] AdoS0NbrControlQueryDto 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.Keyword),
x => x.NbrType.Contains(q.Keyword!) || (x.Description != null && x.Description.Contains(q.Keyword!)));
var total = await query.CountAsync();
var list = await query
.OrderBy(x => x.NbrType)
.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] AdoS0NbrControlUpsertDto dto)
{
if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.NbrType == dto.NbrType))
return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "单号类型编码已存在");
var entity = new AdoS0NbrControl
{
CompanyRefId = dto.CompanyRefId,
FactoryRefId = dto.FactoryRefId,
DomainCode = dto.DomainCode ?? string.Empty,
NbrType = dto.NbrType,
Description = dto.Description,
NbrPre1 = dto.NbrPre1,
NbrPre2 = dto.NbrPre2,
NbrPre3 = dto.NbrPre3,
IniValue = dto.IniValue,
MinValue = dto.MinValue,
MaxValue = dto.MaxValue,
AllowReset = dto.AllowReset,
AllowSkip = dto.AllowSkip,
AllowManual = dto.AllowManual,
DateType = dto.DateType,
IsDateType = dto.IsDateType,
CreateUser = dto.CreateUser,
CreateTime = DateTime.Now
};
await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
return Ok(entity);
}
[HttpPut("{id:long}")]
public async Task UpdateAsync(long id, [FromBody] AdoS0NbrControlUpsertDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.NbrType == dto.NbrType))
return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "单号类型编码已存在");
entity.CompanyRefId = dto.CompanyRefId;
entity.FactoryRefId = dto.FactoryRefId;
entity.DomainCode = dto.DomainCode ?? string.Empty;
entity.NbrType = dto.NbrType;
entity.Description = dto.Description;
entity.NbrPre1 = dto.NbrPre1;
entity.NbrPre2 = dto.NbrPre2;
entity.NbrPre3 = dto.NbrPre3;
entity.IniValue = dto.IniValue;
entity.MinValue = dto.MinValue;
entity.MaxValue = dto.MaxValue;
entity.AllowReset = dto.AllowReset;
entity.AllowSkip = dto.AllowSkip;
entity.AllowManual = dto.AllowManual;
entity.DateType = dto.DateType;
entity.IsDateType = dto.IsDateType;
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 = "删除成功" });
}
}