AdoS0NbrControlsController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Warehouse;
  2. using Admin.NET.Plugin.AiDOP.Entity.S0.Warehouse;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Warehouse;
  5. /// <summary>
  6. /// S0 单号规则主数据(NbrControl 语义)
  7. /// </summary>
  8. [ApiController]
  9. [Route("api/s0/warehouse/nbr-controls")]
  10. [AllowAnonymous]
  11. [NonUnify]
  12. public class AdoS0NbrControlsController : ControllerBase
  13. {
  14. private readonly SqlSugarRepository<AdoS0NbrControl> _rep;
  15. public AdoS0NbrControlsController(SqlSugarRepository<AdoS0NbrControl> rep)
  16. {
  17. _rep = rep;
  18. }
  19. [HttpGet]
  20. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0NbrControlQueryDto q)
  21. {
  22. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  23. var query = _rep.AsQueryable()
  24. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  25. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  26. .WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
  27. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  28. x => x.NbrType.Contains(q.Keyword!) || (x.Description != null && x.Description.Contains(q.Keyword!)));
  29. var total = await query.CountAsync();
  30. var list = await query
  31. .OrderBy(x => x.NbrType)
  32. .Skip((q.Page - 1) * q.PageSize)
  33. .Take(q.PageSize)
  34. .ToListAsync();
  35. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  36. }
  37. [HttpGet("{id:long}")]
  38. public async Task<IActionResult> GetAsync(long id)
  39. {
  40. var item = await _rep.GetByIdAsync(id);
  41. return item == null ? NotFound() : Ok(item);
  42. }
  43. [HttpPost]
  44. public async Task<IActionResult> CreateAsync([FromBody] AdoS0NbrControlUpsertDto dto)
  45. {
  46. if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.NbrType == dto.NbrType))
  47. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "单号类型编码已存在");
  48. var entity = new AdoS0NbrControl
  49. {
  50. CompanyRefId = dto.CompanyRefId,
  51. FactoryRefId = dto.FactoryRefId,
  52. DomainCode = dto.DomainCode ?? string.Empty,
  53. NbrType = dto.NbrType,
  54. Description = dto.Description,
  55. NbrPre1 = dto.NbrPre1,
  56. NbrPre2 = dto.NbrPre2,
  57. NbrPre3 = dto.NbrPre3,
  58. IniValue = dto.IniValue,
  59. MinValue = dto.MinValue,
  60. MaxValue = dto.MaxValue,
  61. AllowReset = dto.AllowReset,
  62. AllowSkip = dto.AllowSkip,
  63. AllowManual = dto.AllowManual,
  64. DateType = dto.DateType,
  65. IsDateType = dto.IsDateType,
  66. CreateUser = dto.CreateUser,
  67. CreateTime = DateTime.Now
  68. };
  69. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  70. return Ok(entity);
  71. }
  72. [HttpPut("{id:long}")]
  73. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0NbrControlUpsertDto dto)
  74. {
  75. var entity = await _rep.GetByIdAsync(id);
  76. if (entity == null) return NotFound();
  77. if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.NbrType == dto.NbrType))
  78. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "单号类型编码已存在");
  79. entity.CompanyRefId = dto.CompanyRefId;
  80. entity.FactoryRefId = dto.FactoryRefId;
  81. entity.DomainCode = dto.DomainCode ?? string.Empty;
  82. entity.NbrType = dto.NbrType;
  83. entity.Description = dto.Description;
  84. entity.NbrPre1 = dto.NbrPre1;
  85. entity.NbrPre2 = dto.NbrPre2;
  86. entity.NbrPre3 = dto.NbrPre3;
  87. entity.IniValue = dto.IniValue;
  88. entity.MinValue = dto.MinValue;
  89. entity.MaxValue = dto.MaxValue;
  90. entity.AllowReset = dto.AllowReset;
  91. entity.AllowSkip = dto.AllowSkip;
  92. entity.AllowManual = dto.AllowManual;
  93. entity.DateType = dto.DateType;
  94. entity.IsDateType = dto.IsDateType;
  95. entity.UpdateUser = dto.UpdateUser;
  96. entity.UpdateTime = DateTime.Now;
  97. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  98. return Ok(entity);
  99. }
  100. [HttpDelete("{id:long}")]
  101. public async Task<IActionResult> DeleteAsync(long id)
  102. {
  103. var item = await _rep.GetByIdAsync(id);
  104. if (item == null) return NotFound();
  105. await _rep.DeleteAsync(item);
  106. return Ok(new { message = "删除成功" });
  107. }
  108. }