AdoS0CustomersController.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. if (await _rep.IsAnyAsync(x => x.Code == dto.Code))
  46. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.CustomerCodeExists, "客户编码已存在");
  47. var entity = new AdoS0Customer
  48. {
  49. CompanyRefId = dto.CompanyRefId,
  50. FactoryRefId = dto.FactoryRefId,
  51. Code = dto.Code,
  52. Name = dto.Name,
  53. NameEn = dto.NameEn,
  54. CustomerType = dto.CustomerType,
  55. ContactPerson = dto.ContactPerson,
  56. ContactPhone = dto.ContactPhone,
  57. Address = dto.Address,
  58. Currency = dto.Currency,
  59. IsTaxIncluded = dto.IsTaxIncluded,
  60. PrimarySales = dto.PrimarySales,
  61. BackupSales = dto.BackupSales,
  62. CustomerLevel = dto.CustomerLevel,
  63. Remark = dto.Remark,
  64. IsEnabled = dto.IsEnabled,
  65. CreatedAt = DateTime.Now
  66. };
  67. entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
  68. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  69. return Ok(entity);
  70. }
  71. [HttpPut("{id:long}")]
  72. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0CustomerUpsertDto dto)
  73. {
  74. var entity = await _rep.GetByIdAsync(id);
  75. if (entity == null) return NotFound();
  76. if (await _rep.IsAnyAsync(x => x.Id != id && x.Code == dto.Code))
  77. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.CustomerCodeExists, "客户编码已存在");
  78. entity.CompanyRefId = dto.CompanyRefId;
  79. entity.FactoryRefId = dto.FactoryRefId;
  80. entity.Code = dto.Code;
  81. entity.Name = dto.Name;
  82. entity.NameEn = dto.NameEn;
  83. entity.CustomerType = dto.CustomerType;
  84. entity.ContactPerson = dto.ContactPerson;
  85. entity.ContactPhone = dto.ContactPhone;
  86. entity.Address = dto.Address;
  87. entity.Currency = dto.Currency;
  88. entity.IsTaxIncluded = dto.IsTaxIncluded;
  89. entity.PrimarySales = dto.PrimarySales;
  90. entity.BackupSales = dto.BackupSales;
  91. entity.CustomerLevel = dto.CustomerLevel;
  92. entity.Remark = dto.Remark;
  93. entity.IsEnabled = dto.IsEnabled;
  94. entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
  95. entity.UpdatedAt = DateTime.Now;
  96. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  97. return Ok(entity);
  98. }
  99. [HttpPatch("{id:long}/toggle-enabled")]
  100. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
  101. {
  102. var entity = await _rep.GetByIdAsync(id);
  103. if (entity == null) return NotFound();
  104. entity.IsEnabled = dto.IsEnabled;
  105. entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
  106. entity.UpdatedAt = DateTime.Now;
  107. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  108. return Ok(entity);
  109. }
  110. [HttpDelete("{id:long}")]
  111. public async Task<IActionResult> DeleteAsync(long id)
  112. {
  113. var item = await _rep.GetByIdAsync(id);
  114. if (item == null) return NotFound();
  115. await _rep.DeleteAsync(item);
  116. return Ok(new { message = "删除成功" });
  117. }
  118. }