AdoS0DepartmentsController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 部门主数据(DepartmentMaster 语义)
  7. /// </summary>
  8. [ApiController]
  9. [Route("api/s0/warehouse/departments")]
  10. [AllowAnonymous]
  11. [NonUnify]
  12. public class AdoS0DepartmentsController : ControllerBase
  13. {
  14. private readonly SqlSugarRepository<AdoS0DepartmentMaster> _rep;
  15. public AdoS0DepartmentsController(SqlSugarRepository<AdoS0DepartmentMaster> rep)
  16. {
  17. _rep = rep;
  18. }
  19. [HttpGet]
  20. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0DepartmentQueryDto 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(q.IsActive.HasValue, x => x.IsActive == q.IsActive!.Value)
  28. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  29. x => x.Department.Contains(q.Keyword!) || (x.Descr != null && x.Descr.Contains(q.Keyword!)));
  30. var total = await query.CountAsync();
  31. var list = await query
  32. .OrderBy(x => x.Department)
  33. .Skip((q.Page - 1) * q.PageSize)
  34. .Take(q.PageSize)
  35. .ToListAsync();
  36. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  37. }
  38. [HttpGet("{id:long}")]
  39. public async Task<IActionResult> GetAsync(long id)
  40. {
  41. var item = await _rep.GetByIdAsync(id);
  42. return item == null ? NotFound() : Ok(item);
  43. }
  44. [HttpPost]
  45. public async Task<IActionResult> CreateAsync([FromBody] AdoS0DepartmentUpsertDto dto)
  46. {
  47. if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.Department == dto.Department))
  48. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "部门编码已存在");
  49. var entity = new AdoS0DepartmentMaster
  50. {
  51. CompanyRefId = dto.CompanyRefId,
  52. FactoryRefId = dto.FactoryRefId,
  53. DomainCode = dto.DomainCode ?? string.Empty,
  54. Department = dto.Department,
  55. Descr = dto.Descr,
  56. IsActive = dto.IsActive,
  57. CreateUser = dto.CreateUser,
  58. CreateTime = DateTime.Now
  59. };
  60. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  61. return Ok(entity);
  62. }
  63. [HttpPut("{id:long}")]
  64. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0DepartmentUpsertDto dto)
  65. {
  66. var entity = await _rep.GetByIdAsync(id);
  67. if (entity == null) return NotFound();
  68. if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.Department == dto.Department))
  69. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "部门编码已存在");
  70. entity.CompanyRefId = dto.CompanyRefId;
  71. entity.FactoryRefId = dto.FactoryRefId;
  72. entity.DomainCode = dto.DomainCode ?? string.Empty;
  73. entity.Department = dto.Department;
  74. entity.Descr = dto.Descr;
  75. entity.IsActive = dto.IsActive;
  76. entity.UpdateUser = dto.UpdateUser;
  77. entity.UpdateTime = DateTime.Now;
  78. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  79. return Ok(entity);
  80. }
  81. [HttpDelete("{id:long}")]
  82. public async Task<IActionResult> DeleteAsync(long id)
  83. {
  84. var item = await _rep.GetByIdAsync(id);
  85. if (item == null) return NotFound();
  86. await _rep.DeleteAsync(item);
  87. return Ok(new { message = "删除成功" });
  88. }
  89. }