AdoS0SuppliersController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. private readonly AdoS0ReferenceChecker _refChecker;
  18. public AdoS0SuppliersController(
  19. SqlSugarRepository<AdoS0SuppMaster> rep,
  20. SqlSugarRepository<AdoS0LocationMaster> locRep,
  21. AdoS0ReferenceChecker refChecker)
  22. {
  23. _rep = rep;
  24. _locRep = locRep;
  25. _refChecker = refChecker;
  26. }
  27. [HttpGet]
  28. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0SuppMasterQueryDto q)
  29. {
  30. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  31. var baseQuery = _rep.AsQueryable()
  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);
  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. var item = await _rep.GetByIdAsync(id);
  76. if (item == null) return NotFound();
  77. await ApplyLocationNamesAsync(new List<AdoS0SuppMaster> { item });
  78. item.APStartDay1Text = FormatApDay(item.APStartDay1);
  79. item.APEndDay1Text = FormatApDay(item.APEndDay1);
  80. item.APStartDay2Text = FormatApDay(item.APStartDay2);
  81. item.APEndDay2Text = FormatApDay(item.APEndDay2);
  82. return Ok(item);
  83. }
  84. [HttpPost]
  85. public async Task<IActionResult> CreateAsync([FromBody] AdoS0SuppMasterUpsertDto dto)
  86. {
  87. if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.Supp == dto.Supp))
  88. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.SupplierCodeExists, "供应商编码已存在");
  89. var now = DateTime.Now;
  90. var entity = new AdoS0SuppMaster
  91. {
  92. CompanyRefId = dto.CompanyRefId,
  93. FactoryRefId = dto.FactoryRefId,
  94. DomainCode = dto.DomainCode,
  95. Supp = dto.Supp,
  96. SortName = dto.SortName,
  97. Address = dto.Address,
  98. Contact = dto.Contact,
  99. Position = dto.Position,
  100. ContactInfo = dto.ContactInfo,
  101. CertificationLevel = dto.CertificationLevel,
  102. PurContact = dto.PurContact,
  103. CrTerms = dto.CrTerms,
  104. Curr = dto.Curr,
  105. Type = dto.Type,
  106. TaxIn = dto.TaxIn,
  107. TaxClass = dto.TaxClass,
  108. APStartDay1 = dto.APStartDay1,
  109. APEndDay1 = dto.APEndDay1,
  110. APStartDay2 = dto.APStartDay2,
  111. APEndDay2 = dto.APEndDay2,
  112. Remark = dto.Remark,
  113. IsActive = dto.IsActive,
  114. CreateUser = dto.CreateUser,
  115. CreateTime = now,
  116. UpdateUser = dto.UpdateUser,
  117. UpdateTime = null
  118. };
  119. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  120. return Ok(entity);
  121. }
  122. [HttpPut("{id:long}")]
  123. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0SuppMasterUpsertDto dto)
  124. {
  125. var entity = await _rep.GetByIdAsync(id);
  126. if (entity == null) return NotFound();
  127. if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.Supp == dto.Supp))
  128. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.SupplierCodeExists, "供应商编码已存在");
  129. entity.CompanyRefId = dto.CompanyRefId;
  130. entity.FactoryRefId = dto.FactoryRefId;
  131. entity.DomainCode = dto.DomainCode;
  132. entity.Supp = dto.Supp;
  133. entity.SortName = dto.SortName;
  134. entity.Address = dto.Address;
  135. entity.Contact = dto.Contact;
  136. entity.Position = dto.Position;
  137. entity.ContactInfo = dto.ContactInfo;
  138. entity.CertificationLevel = dto.CertificationLevel;
  139. entity.PurContact = dto.PurContact;
  140. entity.CrTerms = dto.CrTerms;
  141. entity.Curr = dto.Curr;
  142. entity.Type = dto.Type;
  143. entity.TaxIn = dto.TaxIn;
  144. entity.TaxClass = dto.TaxClass;
  145. entity.APStartDay1 = dto.APStartDay1;
  146. entity.APEndDay1 = dto.APEndDay1;
  147. entity.APStartDay2 = dto.APStartDay2;
  148. entity.APEndDay2 = dto.APEndDay2;
  149. entity.Remark = dto.Remark;
  150. entity.IsActive = dto.IsActive;
  151. entity.UpdateUser = dto.UpdateUser;
  152. entity.UpdateTime = DateTime.Now;
  153. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  154. return Ok(entity);
  155. }
  156. [HttpPatch("{id:long}/toggle-enabled")]
  157. public async Task<IActionResult> ToggleActiveAsync(long id, [FromBody] AdoS0SuppMasterToggleActiveDto dto)
  158. {
  159. var entity = await _rep.GetByIdAsync(id);
  160. if (entity == null) return NotFound();
  161. entity.IsActive = dto.IsActive;
  162. entity.UpdateTime = DateTime.Now;
  163. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  164. return Ok(entity);
  165. }
  166. [HttpDelete("{id:long}")]
  167. public async Task<IActionResult> DeleteAsync(long id)
  168. {
  169. var item = await _rep.GetByIdAsync(id);
  170. if (item == null) return NotFound();
  171. var refInfo = await _refChecker.SupplierReferencesAsync(item.Supp);
  172. if (refInfo is { } r)
  173. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DeleteBlocked,
  174. $"存在 {r.Count} 条 {r.Table} 引用该供应商,无法删除");
  175. await _rep.DeleteAsync(item);
  176. return Ok(new { message = "删除成功" });
  177. }
  178. /// <summary>
  179. /// 按 Domain + PurContact 批量匹配库位主数据,填充 LocationName(与源平台 left join LocationMaster 一致)。
  180. /// </summary>
  181. private async Task ApplyLocationNamesAsync(List<AdoS0SuppMaster> suppliers)
  182. {
  183. if (suppliers.Count == 0) return;
  184. var domainCodes = suppliers
  185. .Select(s => s.DomainCode)
  186. .Where(d => !string.IsNullOrWhiteSpace(d))
  187. .Distinct()
  188. .ToList();
  189. if (domainCodes.Count == 0)
  190. {
  191. foreach (var s in suppliers)
  192. s.LocationName = (s.PurContact ?? "").Trim();
  193. return;
  194. }
  195. var locRows = await _locRep.AsQueryable()
  196. .Where(l => domainCodes.Contains(l.DomainCode))
  197. .ToListAsync();
  198. foreach (var s in suppliers)
  199. {
  200. if (string.IsNullOrWhiteSpace(s.DomainCode) || string.IsNullOrWhiteSpace(s.PurContact))
  201. {
  202. s.LocationName = (s.PurContact ?? "").Trim();
  203. continue;
  204. }
  205. var loc = locRows.Find(l =>
  206. string.Equals(l.DomainCode, s.DomainCode, StringComparison.OrdinalIgnoreCase)
  207. && string.Equals(l.Location, s.PurContact, StringComparison.OrdinalIgnoreCase));
  208. s.LocationName = FormatLocationDisplay(s.PurContact, loc);
  209. }
  210. }
  211. /// <summary>与 MySQL:TRIM(CONCAT(l.Location,' ',IFNULL(l.Descr,''))) 一致;无匹配时退回库位编码。</summary>
  212. private static string FormatLocationDisplay(string? purContact, AdoS0LocationMaster? loc)
  213. {
  214. if (loc != null && !string.IsNullOrWhiteSpace(loc.Location))
  215. {
  216. var tail = loc.Descr ?? "";
  217. return $"{loc.Location.Trim()} {tail}".Trim();
  218. }
  219. return (purContact ?? "").Trim();
  220. }
  221. private static string FormatApDay(int? value)
  222. {
  223. if (!value.HasValue || value.Value == 0) return "";
  224. var v = value.Value;
  225. var prefix = v > 200 ? "下月" : "每月";
  226. var day = (v % 100).ToString("00");
  227. if (day == "31") day = "最后";
  228. return $"{prefix}{day}日";
  229. }
  230. }