AdoS0ProductDesignCyclesController.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Sales;
  2. using Admin.NET.Plugin.AiDOP.Entity.S0.Sales;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Sales;
  5. [ApiController]
  6. [Route("api/s0/sales/product-design-cycles")]
  7. [AllowAnonymous]
  8. [NonUnify]
  9. public class AdoS0ProductDesignCyclesController : ControllerBase
  10. {
  11. private readonly SqlSugarRepository<AdoS0ProductDesignCycle> _rep;
  12. public AdoS0ProductDesignCyclesController(SqlSugarRepository<AdoS0ProductDesignCycle> rep)
  13. => _rep = rep;
  14. [HttpGet]
  15. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0ProductDesignCycleQueryDto q)
  16. {
  17. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  18. var query = _rep.AsQueryable()
  19. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  20. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  21. .WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
  22. .WhereIF(!string.IsNullOrWhiteSpace(q.ItemType), x => x.ItemType == q.ItemType)
  23. .WhereIF(!string.IsNullOrWhiteSpace(q.OwnerApplication), x => x.OwnerApplication == q.OwnerApplication)
  24. .WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive!.Value);
  25. var total = await query.CountAsync();
  26. var list = await query.OrderByDescending(x => x.CreateTime).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  27. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  28. }
  29. [HttpGet("{id:long}")]
  30. public async Task<IActionResult> GetAsync(long id)
  31. {
  32. var item = await _rep.GetByIdAsync(id);
  33. return item == null ? NotFound() : Ok(item);
  34. }
  35. [HttpPost]
  36. public async Task<IActionResult> CreateAsync([FromBody] AdoS0ProductDesignCycleUpsertDto dto)
  37. {
  38. if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.ItemType == dto.ItemType && x.OwnerApplication == dto.OwnerApplication))
  39. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "该工厂下此物料类型+属性组合已存在");
  40. var entity = new AdoS0ProductDesignCycle
  41. {
  42. CompanyRefId = dto.CompanyRefId,
  43. FactoryRefId = dto.FactoryRefId,
  44. DomainCode = dto.DomainCode,
  45. ItemType = dto.ItemType,
  46. OwnerApplication = dto.OwnerApplication,
  47. StdHours = dto.StdHours,
  48. IsActive = dto.IsActive,
  49. CreateUser = dto.CreateUser,
  50. CreateTime = DateTime.Now,
  51. };
  52. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  53. return Ok(entity);
  54. }
  55. [HttpPut("{id:long}")]
  56. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0ProductDesignCycleUpsertDto dto)
  57. {
  58. var entity = await _rep.GetByIdAsync(id);
  59. if (entity == null) return NotFound();
  60. if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.ItemType == dto.ItemType && x.OwnerApplication == dto.OwnerApplication))
  61. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "该工厂下此物料类型+属性组合已存在");
  62. entity.CompanyRefId = dto.CompanyRefId;
  63. entity.FactoryRefId = dto.FactoryRefId;
  64. entity.DomainCode = dto.DomainCode;
  65. entity.ItemType = dto.ItemType;
  66. entity.OwnerApplication = dto.OwnerApplication;
  67. entity.StdHours = dto.StdHours;
  68. entity.IsActive = dto.IsActive;
  69. entity.UpdateUser = dto.UpdateUser;
  70. entity.UpdateTime = DateTime.Now;
  71. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  72. return Ok(entity);
  73. }
  74. [HttpPatch("{id:long}/toggle-enabled")]
  75. public async Task<IActionResult> ToggleActiveAsync(long id, [FromBody] AdoS0ProductDesignCycleToggleActiveDto dto)
  76. {
  77. var entity = await _rep.GetByIdAsync(id);
  78. if (entity == null) return NotFound();
  79. entity.IsActive = dto.IsActive;
  80. entity.UpdateTime = DateTime.Now;
  81. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  82. return Ok(entity);
  83. }
  84. [HttpDelete("{id:long}")]
  85. public async Task<IActionResult> DeleteAsync(long id)
  86. {
  87. var item = await _rep.GetByIdAsync(id);
  88. if (item == null) return NotFound();
  89. await _rep.DeleteAsync(item);
  90. return Ok(new { message = "删除成功" });
  91. }
  92. }