AdoS0SuppliersController.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Supply;
  2. using Admin.NET.Plugin.AiDOP.Entity.S0.Supply;
  3. using Admin.NET.Plugin.AiDOP.Entity.S0.Warehouse;
  4. using Admin.NET.Plugin.AiDOP.Infrastructure;
  5. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Supply;
  6. /// <summary>
  7. /// S0 供应商主数据(SuppMaster 语义)
  8. /// </summary>
  9. [ApiController]
  10. [Route("api/s0/supply/suppliers")]
  11. [AllowAnonymous]
  12. [NonUnify]
  13. public class AdoS0SuppliersController : ControllerBase
  14. {
  15. private readonly SqlSugarRepository<AdoS0SuppMaster> _rep;
  16. private readonly SqlSugarRepository<AdoS0LocationMaster> _locRep;
  17. public AdoS0SuppliersController(
  18. SqlSugarRepository<AdoS0SuppMaster> rep,
  19. SqlSugarRepository<AdoS0LocationMaster> locRep)
  20. {
  21. _rep = rep;
  22. _locRep = locRep;
  23. }
  24. [HttpGet]
  25. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0SuppMasterQueryDto q)
  26. {
  27. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  28. var baseQuery = _rep.AsQueryable()
  29. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId.Value)
  30. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId.Value)
  31. .WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
  32. .WhereIF(!string.IsNullOrWhiteSpace(q.Supp), x => x.Supp.Contains(q.Supp!))
  33. .WhereIF(!string.IsNullOrWhiteSpace(q.SortName), x => x.SortName != null && x.SortName.Contains(q.SortName!))
  34. .WhereIF(!string.IsNullOrWhiteSpace(q.CrTerms), x => x.CrTerms != null && x.CrTerms.Contains(q.CrTerms!))
  35. .WhereIF(!string.IsNullOrWhiteSpace(q.Curr), x => x.Curr != null && x.Curr.Contains(q.Curr!))
  36. .WhereIF(!string.IsNullOrWhiteSpace(q.Type), x => x.Type != null && x.Type.Contains(q.Type!))
  37. .WhereIF(q.TaxIn.HasValue, x => x.TaxIn == q.TaxIn.Value)
  38. .WhereIF(!string.IsNullOrWhiteSpace(q.TaxClass), x => x.TaxClass != null && x.TaxClass.Contains(q.TaxClass!))
  39. .WhereIF(!string.IsNullOrWhiteSpace(q.PurContact), x => x.PurContact != null && x.PurContact.Contains(q.PurContact!))
  40. .WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive.Value)
  41. .WhereIF(
  42. !string.IsNullOrWhiteSpace(q.Keyword),
  43. x => x.Supp.Contains(q.Keyword!) || (x.SortName != null && x.SortName.Contains(q.Keyword!)));
  44. var total = await baseQuery.CountAsync();
  45. var list = await baseQuery
  46. .OrderByDescending(x => x.CreateTime)
  47. .Skip((q.Page - 1) * q.PageSize)
  48. .Take(q.PageSize)
  49. .ToListAsync();
  50. await ApplyLocationNamesAsync(list);
  51. foreach (var s in list)
  52. {
  53. s.APStartDay1Text = FormatApDay(s.APStartDay1);
  54. s.APEndDay1Text = FormatApDay(s.APEndDay1);
  55. s.APStartDay2Text = FormatApDay(s.APStartDay2);
  56. s.APEndDay2Text = FormatApDay(s.APEndDay2);
  57. }
  58. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  59. }
  60. [HttpGet("{id:long}")]
  61. public async Task<IActionResult> GetAsync(long id)
  62. {
  63. var item = await _rep.GetByIdAsync(id);
  64. if (item == null) return NotFound();
  65. await ApplyLocationNamesAsync(new List<AdoS0SuppMaster> { item });
  66. item.APStartDay1Text = FormatApDay(item.APStartDay1);
  67. item.APEndDay1Text = FormatApDay(item.APEndDay1);
  68. item.APStartDay2Text = FormatApDay(item.APStartDay2);
  69. item.APEndDay2Text = FormatApDay(item.APEndDay2);
  70. return Ok(item);
  71. }
  72. [HttpPost]
  73. public async Task<IActionResult> CreateAsync([FromBody] AdoS0SuppMasterUpsertDto dto)
  74. {
  75. if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.Supp == dto.Supp))
  76. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.SupplierCodeExists, "供应商编码已存在");
  77. var now = DateTime.Now;
  78. var entity = new AdoS0SuppMaster
  79. {
  80. CompanyRefId = dto.CompanyRefId,
  81. FactoryRefId = dto.FactoryRefId,
  82. DomainCode = dto.DomainCode,
  83. Supp = dto.Supp,
  84. SortName = dto.SortName,
  85. PurContact = dto.PurContact,
  86. CrTerms = dto.CrTerms,
  87. Curr = dto.Curr,
  88. Type = dto.Type,
  89. TaxIn = dto.TaxIn,
  90. TaxClass = dto.TaxClass,
  91. APStartDay1 = dto.APStartDay1,
  92. APEndDay1 = dto.APEndDay1,
  93. APStartDay2 = dto.APStartDay2,
  94. APEndDay2 = dto.APEndDay2,
  95. Remark = dto.Remark,
  96. IsActive = dto.IsActive,
  97. CreateUser = dto.CreateUser,
  98. CreateTime = now,
  99. UpdateUser = dto.UpdateUser,
  100. UpdateTime = null
  101. };
  102. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  103. return Ok(entity);
  104. }
  105. [HttpPut("{id:long}")]
  106. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0SuppMasterUpsertDto dto)
  107. {
  108. var entity = await _rep.GetByIdAsync(id);
  109. if (entity == null) return NotFound();
  110. if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.Supp == dto.Supp))
  111. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.SupplierCodeExists, "供应商编码已存在");
  112. entity.CompanyRefId = dto.CompanyRefId;
  113. entity.FactoryRefId = dto.FactoryRefId;
  114. entity.DomainCode = dto.DomainCode;
  115. entity.Supp = dto.Supp;
  116. entity.SortName = dto.SortName;
  117. entity.PurContact = dto.PurContact;
  118. entity.CrTerms = dto.CrTerms;
  119. entity.Curr = dto.Curr;
  120. entity.Type = dto.Type;
  121. entity.TaxIn = dto.TaxIn;
  122. entity.TaxClass = dto.TaxClass;
  123. entity.APStartDay1 = dto.APStartDay1;
  124. entity.APEndDay1 = dto.APEndDay1;
  125. entity.APStartDay2 = dto.APStartDay2;
  126. entity.APEndDay2 = dto.APEndDay2;
  127. entity.Remark = dto.Remark;
  128. entity.IsActive = dto.IsActive;
  129. entity.UpdateUser = dto.UpdateUser;
  130. entity.UpdateTime = DateTime.Now;
  131. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  132. return Ok(entity);
  133. }
  134. [HttpPatch("{id:long}/toggle-enabled")]
  135. public async Task<IActionResult> ToggleActiveAsync(long id, [FromBody] AdoS0SuppMasterToggleActiveDto dto)
  136. {
  137. var entity = await _rep.GetByIdAsync(id);
  138. if (entity == null) return NotFound();
  139. entity.IsActive = dto.IsActive;
  140. entity.UpdateTime = DateTime.Now;
  141. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  142. return Ok(entity);
  143. }
  144. [HttpDelete("{id:long}")]
  145. public async Task<IActionResult> DeleteAsync(long id)
  146. {
  147. var item = await _rep.GetByIdAsync(id);
  148. if (item == null) return NotFound();
  149. await _rep.DeleteAsync(item);
  150. return Ok(new { message = "删除成功" });
  151. }
  152. /// <summary>
  153. /// 按 Domain + PurContact 批量匹配库位主数据,填充 LocationName(与源平台 left join LocationMaster 一致)。
  154. /// </summary>
  155. private async Task ApplyLocationNamesAsync(List<AdoS0SuppMaster> suppliers)
  156. {
  157. if (suppliers.Count == 0) return;
  158. var domainCodes = suppliers
  159. .Select(s => s.DomainCode)
  160. .Where(d => !string.IsNullOrWhiteSpace(d))
  161. .Distinct()
  162. .ToList();
  163. if (domainCodes.Count == 0)
  164. {
  165. foreach (var s in suppliers)
  166. s.LocationName = (s.PurContact ?? "").Trim();
  167. return;
  168. }
  169. var locRows = await _locRep.AsQueryable()
  170. .Where(l => domainCodes.Contains(l.DomainCode))
  171. .ToListAsync();
  172. foreach (var s in suppliers)
  173. {
  174. if (string.IsNullOrWhiteSpace(s.DomainCode) || string.IsNullOrWhiteSpace(s.PurContact))
  175. {
  176. s.LocationName = (s.PurContact ?? "").Trim();
  177. continue;
  178. }
  179. var loc = locRows.Find(l =>
  180. string.Equals(l.DomainCode, s.DomainCode, StringComparison.OrdinalIgnoreCase)
  181. && string.Equals(l.Location, s.PurContact, StringComparison.OrdinalIgnoreCase));
  182. s.LocationName = FormatLocationDisplay(s.PurContact, loc);
  183. }
  184. }
  185. /// <summary>与 MySQL:TRIM(CONCAT(l.Location,' ',IFNULL(l.Descr,''))) 一致;无匹配时退回库位编码。</summary>
  186. private static string FormatLocationDisplay(string? purContact, AdoS0LocationMaster? loc)
  187. {
  188. if (loc != null && !string.IsNullOrWhiteSpace(loc.Location))
  189. {
  190. var tail = loc.Descr ?? "";
  191. return $"{loc.Location.Trim()} {tail}".Trim();
  192. }
  193. return (purContact ?? "").Trim();
  194. }
  195. private static string FormatApDay(int? value)
  196. {
  197. if (!value.HasValue || value.Value == 0) return "";
  198. var v = value.Value;
  199. var prefix = v > 200 ? "下月" : "每月";
  200. var day = (v % 100).ToString("00");
  201. if (day == "31") day = "最后";
  202. return $"{prefix}{day}日";
  203. }
  204. }