AdoS0SuppliersController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. [NonUnify]
  12. public class AdoS0SuppliersController : ControllerBase
  13. {
  14. private readonly SqlSugarRepository<AdoS0SuppMaster> _rep;
  15. private readonly SqlSugarRepository<AdoS0LocationMaster> _locRep;
  16. private readonly AdoS0ReferenceChecker _refChecker;
  17. public AdoS0SuppliersController(
  18. SqlSugarRepository<AdoS0SuppMaster> rep,
  19. SqlSugarRepository<AdoS0LocationMaster> locRep,
  20. AdoS0ReferenceChecker refChecker)
  21. {
  22. _rep = rep;
  23. _locRep = locRep;
  24. _refChecker = refChecker;
  25. }
  26. [HttpGet]
  27. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0SuppMasterQueryDto q)
  28. {
  29. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  30. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  31. var baseQuery = _rep.ScopedTo(tenantId)
  32. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId.Value)
  33. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId.Value)
  34. .WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
  35. .WhereIF(!string.IsNullOrWhiteSpace(q.Supp), x => x.Supp.Contains(q.Supp!))
  36. .WhereIF(!string.IsNullOrWhiteSpace(q.SortName), x => x.SortName != null && x.SortName.Contains(q.SortName!))
  37. .WhereIF(!string.IsNullOrWhiteSpace(q.Address), x => x.Address != null && x.Address.Contains(q.Address!))
  38. .WhereIF(!string.IsNullOrWhiteSpace(q.Contact), x => x.Contact != null && x.Contact.Contains(q.Contact!))
  39. .WhereIF(!string.IsNullOrWhiteSpace(q.Position), x => x.Position != null && x.Position.Contains(q.Position!))
  40. .WhereIF(!string.IsNullOrWhiteSpace(q.ContactInfo), x => x.ContactInfo != null && x.ContactInfo.Contains(q.ContactInfo!))
  41. .WhereIF(!string.IsNullOrWhiteSpace(q.CertificationLevel), x => x.CertificationLevel != null && x.CertificationLevel.Contains(q.CertificationLevel!))
  42. .WhereIF(!string.IsNullOrWhiteSpace(q.CrTerms), x => x.CrTerms != null && x.CrTerms.Contains(q.CrTerms!))
  43. .WhereIF(!string.IsNullOrWhiteSpace(q.Curr), x => x.Curr != null && x.Curr.Contains(q.Curr!))
  44. .WhereIF(!string.IsNullOrWhiteSpace(q.Type), x => x.Type != null && x.Type.Contains(q.Type!))
  45. .WhereIF(q.TaxIn.HasValue, x => x.TaxIn == q.TaxIn.Value)
  46. .WhereIF(!string.IsNullOrWhiteSpace(q.TaxClass), x => x.TaxClass != null && x.TaxClass.Contains(q.TaxClass!))
  47. .WhereIF(!string.IsNullOrWhiteSpace(q.PurContact), x => x.PurContact != null && x.PurContact.Contains(q.PurContact!))
  48. .WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive.Value)
  49. .WhereIF(
  50. !string.IsNullOrWhiteSpace(q.Keyword),
  51. x => x.Supp.Contains(q.Keyword!)
  52. || (x.SortName != null && x.SortName.Contains(q.Keyword!))
  53. || (x.Address != null && x.Address.Contains(q.Keyword!))
  54. || (x.Contact != null && x.Contact.Contains(q.Keyword!))
  55. || (x.ContactInfo != null && x.ContactInfo.Contains(q.Keyword!)));
  56. var total = await baseQuery.CountAsync();
  57. var list = await baseQuery
  58. .OrderByDescending(x => x.CreateTime)
  59. .Skip((q.Page - 1) * q.PageSize)
  60. .Take(q.PageSize)
  61. .ToListAsync();
  62. await ApplyLocationNamesAsync(list, tenantId);
  63. foreach (var s in list)
  64. {
  65. s.APStartDay1Text = FormatApDay(s.APStartDay1);
  66. s.APEndDay1Text = FormatApDay(s.APEndDay1);
  67. s.APStartDay2Text = FormatApDay(s.APStartDay2);
  68. s.APEndDay2Text = FormatApDay(s.APEndDay2);
  69. }
  70. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  71. }
  72. [HttpGet("{id:long}")]
  73. public async Task<IActionResult> GetAsync(long id)
  74. {
  75. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  76. var item = await _rep.ByIdScopedAsync(id, tenantId);
  77. if (item == null) return NotFound();
  78. await ApplyLocationNamesAsync(new List<AdoS0SuppMaster> { item }, tenantId);
  79. item.APStartDay1Text = FormatApDay(item.APStartDay1);
  80. item.APEndDay1Text = FormatApDay(item.APEndDay1);
  81. item.APStartDay2Text = FormatApDay(item.APStartDay2);
  82. item.APEndDay2Text = FormatApDay(item.APEndDay2);
  83. return Ok(item);
  84. }
  85. [HttpPost]
  86. public async Task<IActionResult> CreateAsync([FromBody] AdoS0SuppMasterUpsertDto dto)
  87. {
  88. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  89. var (companyId, factoryId) = await AdoS0OrgScope.ResolveAsync(_rep.Context, tenantId);
  90. if (companyId <= 0 || factoryId <= 0) return AdoS0OrgScope.NotConfiguredError("供应商");
  91. if (await _rep.IsAnyAsync(x => x.TenantId == tenantId && x.FactoryRefId == factoryId && x.Supp == dto.Supp))
  92. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.SupplierCodeExists, "供应商编码已存在");
  93. var now = DateTime.Now;
  94. var entity = new AdoS0SuppMaster
  95. {
  96. TenantId = tenantId,
  97. // 组织字段后端强制 stamp 当前租户内解析出的组织,忽略 payload 组织值(杜绝跨租户组织引用)。
  98. CompanyRefId = companyId,
  99. FactoryRefId = factoryId,
  100. DomainCode = dto.DomainCode,
  101. Supp = dto.Supp,
  102. SortName = dto.SortName,
  103. Address = dto.Address,
  104. Contact = dto.Contact,
  105. Position = dto.Position,
  106. ContactInfo = dto.ContactInfo,
  107. CertificationLevel = dto.CertificationLevel,
  108. PurContact = dto.PurContact,
  109. CrTerms = dto.CrTerms,
  110. Curr = dto.Curr,
  111. Type = dto.Type,
  112. TaxIn = dto.TaxIn,
  113. TaxClass = dto.TaxClass,
  114. APStartDay1 = dto.APStartDay1,
  115. APEndDay1 = dto.APEndDay1,
  116. APStartDay2 = dto.APStartDay2,
  117. APEndDay2 = dto.APEndDay2,
  118. Remark = dto.Remark,
  119. IsActive = dto.IsActive,
  120. CreateUser = dto.CreateUser,
  121. CreateTime = now,
  122. UpdateUser = dto.UpdateUser,
  123. UpdateTime = null
  124. };
  125. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  126. return Ok(entity);
  127. }
  128. [HttpPut("{id:long}")]
  129. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0SuppMasterUpsertDto dto)
  130. {
  131. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  132. var entity = await _rep.ByIdScopedAsync(id, tenantId);
  133. if (entity == null) return NotFound();
  134. if (await _rep.IsAnyAsync(x => x.TenantId == tenantId && x.Id != id && x.FactoryRefId == entity.FactoryRefId && x.Supp == dto.Supp))
  135. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.SupplierCodeExists, "供应商编码已存在");
  136. // 组织字段 pin 到既有原值,忽略 payload 组织值:既杜绝篡改成他租户组织,
  137. // 也不因一次普通编辑就顺手迁移历史 CROSS-TENANT 存量。
  138. entity.DomainCode = dto.DomainCode;
  139. entity.Supp = dto.Supp;
  140. entity.SortName = dto.SortName;
  141. entity.Address = dto.Address;
  142. entity.Contact = dto.Contact;
  143. entity.Position = dto.Position;
  144. entity.ContactInfo = dto.ContactInfo;
  145. entity.CertificationLevel = dto.CertificationLevel;
  146. entity.PurContact = dto.PurContact;
  147. entity.CrTerms = dto.CrTerms;
  148. entity.Curr = dto.Curr;
  149. entity.Type = dto.Type;
  150. entity.TaxIn = dto.TaxIn;
  151. entity.TaxClass = dto.TaxClass;
  152. entity.APStartDay1 = dto.APStartDay1;
  153. entity.APEndDay1 = dto.APEndDay1;
  154. entity.APStartDay2 = dto.APStartDay2;
  155. entity.APEndDay2 = dto.APEndDay2;
  156. entity.Remark = dto.Remark;
  157. entity.IsActive = dto.IsActive;
  158. entity.UpdateUser = dto.UpdateUser;
  159. entity.UpdateTime = DateTime.Now;
  160. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  161. return Ok(entity);
  162. }
  163. [HttpPatch("{id:long}/toggle-enabled")]
  164. public async Task<IActionResult> ToggleActiveAsync(long id, [FromBody] AdoS0SuppMasterToggleActiveDto dto)
  165. {
  166. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  167. var entity = await _rep.ByIdScopedAsync(id, tenantId);
  168. if (entity == null) return NotFound();
  169. entity.IsActive = dto.IsActive;
  170. entity.UpdateTime = DateTime.Now;
  171. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  172. return Ok(entity);
  173. }
  174. [HttpDelete("{id:long}")]
  175. public async Task<IActionResult> DeleteAsync(long id)
  176. {
  177. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  178. var item = await _rep.ByIdScopedAsync(id, tenantId);
  179. if (item == null) return NotFound();
  180. var refInfo = await _refChecker.SupplierReferencesAsync(tenantId, item.Supp);
  181. if (refInfo is { } r)
  182. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DeleteBlocked,
  183. $"存在 {r.Count} 条 {r.Table} 引用该供应商,无法删除");
  184. await _rep.DeleteAsync(item);
  185. return Ok(new { message = "删除成功" });
  186. }
  187. /// <summary>
  188. /// 按 Domain + PurContact 批量匹配库位主数据,填充 LocationName(与源平台 left join LocationMaster 一致)。
  189. /// </summary>
  190. private async Task ApplyLocationNamesAsync(List<AdoS0SuppMaster> suppliers, long tenantId)
  191. {
  192. if (suppliers.Count == 0) return;
  193. var domainCodes = suppliers
  194. .Select(s => s.DomainCode)
  195. .Where(d => !string.IsNullOrWhiteSpace(d))
  196. .Distinct()
  197. .ToList();
  198. if (domainCodes.Count == 0)
  199. {
  200. foreach (var s in suppliers)
  201. s.LocationName = (s.PurContact ?? "").Trim();
  202. return;
  203. }
  204. var locRows = await _locRep.ScopedTo(tenantId)
  205. .Where(l => domainCodes.Contains(l.DomainCode))
  206. .ToListAsync();
  207. foreach (var s in suppliers)
  208. {
  209. if (string.IsNullOrWhiteSpace(s.DomainCode) || string.IsNullOrWhiteSpace(s.PurContact))
  210. {
  211. s.LocationName = (s.PurContact ?? "").Trim();
  212. continue;
  213. }
  214. var loc = locRows.Find(l =>
  215. string.Equals(l.DomainCode, s.DomainCode, StringComparison.OrdinalIgnoreCase)
  216. && string.Equals(l.Location, s.PurContact, StringComparison.OrdinalIgnoreCase));
  217. s.LocationName = FormatLocationDisplay(s.PurContact, loc);
  218. }
  219. }
  220. /// <summary>与 MySQL:TRIM(CONCAT(l.Location,' ',IFNULL(l.Descr,''))) 一致;无匹配时退回库位编码。</summary>
  221. private static string FormatLocationDisplay(string? purContact, AdoS0LocationMaster? loc)
  222. {
  223. if (loc != null && !string.IsNullOrWhiteSpace(loc.Location))
  224. {
  225. var tail = loc.Descr ?? "";
  226. return $"{loc.Location.Trim()} {tail}".Trim();
  227. }
  228. return (purContact ?? "").Trim();
  229. }
  230. private static string FormatApDay(int? value)
  231. {
  232. if (!value.HasValue || value.Value == 0) return "";
  233. var v = value.Value;
  234. var prefix = v > 200 ? "下月" : "每月";
  235. var day = (v % 100).ToString("00");
  236. if (day == "31") day = "最后";
  237. return $"{prefix}{day}日";
  238. }
  239. }