AdoS0CustomersController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. /// <summary>
  6. /// S0 客户主数据
  7. /// </summary>
  8. [ApiController]
  9. [Route("api/s0/sales/customers")]
  10. [AllowAnonymous]
  11. [NonUnify]
  12. public class AdoS0CustomersController : ControllerBase
  13. {
  14. private readonly SqlSugarRepository<AdoS0Customer> _rep;
  15. public AdoS0CustomersController(SqlSugarRepository<AdoS0Customer> rep)
  16. {
  17. _rep = rep;
  18. }
  19. [HttpGet]
  20. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0CustomerQueryDto 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.Keyword), x => x.Code.Contains(q.Keyword!) || x.Name.Contains(q.Keyword!))
  27. .WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled.Value);
  28. var total = await query.CountAsync();
  29. var list = await query
  30. .OrderByDescending(x => x.CreatedAt)
  31. .Skip((q.Page - 1) * q.PageSize)
  32. .Take(q.PageSize)
  33. .ToListAsync();
  34. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  35. }
  36. [HttpGet("{id:long}")]
  37. public async Task<IActionResult> GetAsync(long id)
  38. {
  39. var item = await _rep.GetByIdAsync(id);
  40. return item == null ? NotFound() : Ok(item);
  41. }
  42. [HttpPost]
  43. public async Task<IActionResult> CreateAsync([FromBody] AdoS0CustomerUpsertDto dto)
  44. {
  45. var entity = new AdoS0Customer
  46. {
  47. CompanyRefId = dto.CompanyRefId,
  48. FactoryRefId = dto.FactoryRefId,
  49. Code = dto.Code,
  50. Name = dto.Name,
  51. NameEn = dto.NameEn,
  52. CustomerType = dto.CustomerType,
  53. ContactPerson = dto.ContactPerson,
  54. ContactPhone = dto.ContactPhone,
  55. Address = dto.Address,
  56. Currency = dto.Currency,
  57. IsTaxIncluded = dto.IsTaxIncluded,
  58. PrimarySales = dto.PrimarySales,
  59. BackupSales = dto.BackupSales,
  60. CustomerLevel = dto.CustomerLevel,
  61. Remark = dto.Remark,
  62. IsEnabled = dto.IsEnabled,
  63. CreatedAt = DateTime.Now
  64. };
  65. entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
  66. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  67. return Ok(entity);
  68. }
  69. [HttpPut("{id:long}")]
  70. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0CustomerUpsertDto dto)
  71. {
  72. var entity = await _rep.GetByIdAsync(id);
  73. if (entity == null) return NotFound();
  74. entity.CompanyRefId = dto.CompanyRefId;
  75. entity.FactoryRefId = dto.FactoryRefId;
  76. entity.Code = dto.Code;
  77. entity.Name = dto.Name;
  78. entity.NameEn = dto.NameEn;
  79. entity.CustomerType = dto.CustomerType;
  80. entity.ContactPerson = dto.ContactPerson;
  81. entity.ContactPhone = dto.ContactPhone;
  82. entity.Address = dto.Address;
  83. entity.Currency = dto.Currency;
  84. entity.IsTaxIncluded = dto.IsTaxIncluded;
  85. entity.PrimarySales = dto.PrimarySales;
  86. entity.BackupSales = dto.BackupSales;
  87. entity.CustomerLevel = dto.CustomerLevel;
  88. entity.Remark = dto.Remark;
  89. entity.IsEnabled = dto.IsEnabled;
  90. entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
  91. entity.UpdatedAt = DateTime.Now;
  92. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  93. return Ok(entity);
  94. }
  95. [HttpPatch("{id:long}/toggle-enabled")]
  96. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
  97. {
  98. var entity = await _rep.GetByIdAsync(id);
  99. if (entity == null) return NotFound();
  100. entity.IsEnabled = dto.IsEnabled;
  101. entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
  102. entity.UpdatedAt = DateTime.Now;
  103. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  104. return Ok(entity);
  105. }
  106. [HttpDelete("{id:long}")]
  107. public async Task<IActionResult> DeleteAsync(long id)
  108. {
  109. var item = await _rep.GetByIdAsync(id);
  110. if (item == null) return NotFound();
  111. await _rep.DeleteAsync(item);
  112. return Ok(new { message = "删除成功" });
  113. }
  114. }