AdoS0SuppliersController.cs 10 KB

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