AdoS0CustomersController.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 客户主数据(CustMaster 语义)
  7. /// </summary>
  8. [ApiController]
  9. [Route("api/s0/sales/customers")]
  10. [AllowAnonymous]
  11. [NonUnify]
  12. public class AdoS0CustomersController : ControllerBase
  13. {
  14. private readonly SqlSugarRepository<AdoS0CustMaster> _rep;
  15. public AdoS0CustomersController(SqlSugarRepository<AdoS0CustMaster> rep)
  16. {
  17. _rep = rep;
  18. }
  19. [HttpGet]
  20. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0CustMasterQueryDto 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.Cust), x => x.Cust.Contains(q.Cust!))
  28. .WhereIF(!string.IsNullOrWhiteSpace(q.SortName), x => x.SortName != null && x.SortName.Contains(q.SortName!))
  29. .WhereIF(!string.IsNullOrWhiteSpace(q.Curr), x => x.Curr != null && x.Curr.Contains(q.Curr!))
  30. .WhereIF(!string.IsNullOrWhiteSpace(q.Slspsn1), x => x.Slspsn1 != null && x.Slspsn1.Contains(q.Slspsn1!))
  31. .WhereIF(!string.IsNullOrWhiteSpace(q.ShipTo), x => x.ShipTo != null && x.ShipTo.Contains(q.ShipTo!))
  32. .WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive.Value)
  33. .WhereIF(q.IsConfirm.HasValue, x => x.IsConfirm == q.IsConfirm.Value)
  34. .WhereIF(
  35. !string.IsNullOrWhiteSpace(q.Keyword),
  36. x => x.Cust.Contains(q.Keyword!) || (x.SortName != null && x.SortName.Contains(q.Keyword!)));
  37. var total = await query.CountAsync();
  38. var list = await query
  39. .OrderByDescending(x => x.CreateTime)
  40. .Skip((q.Page - 1) * q.PageSize)
  41. .Take(q.PageSize)
  42. .ToListAsync();
  43. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  44. }
  45. [HttpGet("{id:long}")]
  46. public async Task<IActionResult> GetAsync(long id)
  47. {
  48. var item = await _rep.GetByIdAsync(id);
  49. return item == null ? NotFound() : Ok(item);
  50. }
  51. [HttpPost]
  52. public async Task<IActionResult> CreateAsync([FromBody] AdoS0CustMasterUpsertDto dto)
  53. {
  54. if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.Cust == dto.Cust))
  55. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.CustomerCodeExists, "客户编码已存在");
  56. var now = DateTime.Now;
  57. var entity = new AdoS0CustMaster
  58. {
  59. CompanyRefId = dto.CompanyRefId,
  60. FactoryRefId = dto.FactoryRefId,
  61. DomainCode = dto.DomainCode,
  62. Cust = dto.Cust,
  63. SortName = dto.SortName,
  64. Curr = dto.Curr,
  65. Slspsn1 = dto.Slspsn1,
  66. Slspsn2 = dto.Slspsn2,
  67. ShipTo = dto.ShipTo,
  68. TaxClass = dto.TaxClass,
  69. TaxIn = dto.TaxIn,
  70. CustClass = dto.CustClass,
  71. Address = dto.Address,
  72. Contact = dto.Contact,
  73. Position = dto.Position,
  74. ContactInfo = dto.ContactInfo,
  75. CreditRating = dto.CreditRating,
  76. Remarks = dto.Remarks,
  77. IsActive = dto.IsActive,
  78. IsConfirm = dto.IsConfirm,
  79. CreateUser = dto.CreateUser,
  80. CreateTime = now,
  81. UpdateUser = dto.UpdateUser,
  82. UpdateTime = null
  83. };
  84. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  85. return Ok(entity);
  86. }
  87. [HttpPut("{id:long}")]
  88. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0CustMasterUpsertDto dto)
  89. {
  90. var entity = await _rep.GetByIdAsync(id);
  91. if (entity == null) return NotFound();
  92. if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.Cust == dto.Cust))
  93. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.CustomerCodeExists, "客户编码已存在");
  94. entity.CompanyRefId = dto.CompanyRefId;
  95. entity.FactoryRefId = dto.FactoryRefId;
  96. entity.DomainCode = dto.DomainCode;
  97. entity.Cust = dto.Cust;
  98. entity.SortName = dto.SortName;
  99. entity.Curr = dto.Curr;
  100. entity.Slspsn1 = dto.Slspsn1;
  101. entity.Slspsn2 = dto.Slspsn2;
  102. entity.ShipTo = dto.ShipTo;
  103. entity.TaxClass = dto.TaxClass;
  104. entity.TaxIn = dto.TaxIn;
  105. entity.CustClass = dto.CustClass;
  106. entity.Address = dto.Address;
  107. entity.Contact = dto.Contact;
  108. entity.Position = dto.Position;
  109. entity.ContactInfo = dto.ContactInfo;
  110. entity.CreditRating = dto.CreditRating;
  111. entity.Remarks = dto.Remarks;
  112. entity.IsActive = dto.IsActive;
  113. entity.IsConfirm = dto.IsConfirm;
  114. entity.UpdateUser = dto.UpdateUser;
  115. entity.UpdateTime = DateTime.Now;
  116. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  117. return Ok(entity);
  118. }
  119. [HttpPatch("{id:long}/toggle-enabled")]
  120. public async Task<IActionResult> ToggleActiveAsync(long id, [FromBody] AdoS0CustMasterToggleActiveDto dto)
  121. {
  122. var entity = await _rep.GetByIdAsync(id);
  123. if (entity == null) return NotFound();
  124. entity.IsActive = dto.IsActive;
  125. entity.UpdateTime = DateTime.Now;
  126. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  127. return Ok(entity);
  128. }
  129. [HttpDelete("{id:long}")]
  130. public async Task<IActionResult> DeleteAsync(long id)
  131. {
  132. var item = await _rep.GetByIdAsync(id);
  133. if (item == null) return NotFound();
  134. await _rep.DeleteAsync(item);
  135. return Ok(new { message = "删除成功" });
  136. }
  137. }