AdoS0SrmPurchasesController.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Supply;
  2. using Admin.NET.Plugin.AiDOP.Entity.S0.Sales;
  3. using Admin.NET.Plugin.AiDOP.Entity.S0.Supply;
  4. using Admin.NET.Plugin.AiDOP.Infrastructure;
  5. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Supply;
  6. /// <summary>
  7. /// S0 货源清单(srm_purchase 语义,左联物料主数据展示)
  8. /// </summary>
  9. [ApiController]
  10. [Route("api/s0/supply/srm-purchases")]
  11. [AllowAnonymous]
  12. [NonUnify]
  13. public class AdoS0SrmPurchasesController : ControllerBase
  14. {
  15. private readonly SqlSugarRepository<AdoS0SrmPurchase> _rep;
  16. private readonly SqlSugarRepository<AdoS0ItemMaster> _itemRep;
  17. private readonly SqlSugarRepository<AdoS0SuppMaster> _suppRep;
  18. public AdoS0SrmPurchasesController(
  19. SqlSugarRepository<AdoS0SrmPurchase> rep,
  20. SqlSugarRepository<AdoS0ItemMaster> itemRep,
  21. SqlSugarRepository<AdoS0SuppMaster> suppRep)
  22. {
  23. _rep = rep;
  24. _itemRep = itemRep;
  25. _suppRep = suppRep;
  26. }
  27. [HttpGet]
  28. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0SrmPurchaseQueryDto 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.SupplierType), x => x.SupplierType != null && x.SupplierType.Contains(q.SupplierType!))
  36. .WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive.Value)
  37. .WhereIF(!string.IsNullOrWhiteSpace(q.SupplierName), x => x.SupplierName != null && x.SupplierName.Contains(q.SupplierName!))
  38. .WhereIF(!string.IsNullOrWhiteSpace(q.SupplierNumber), x => x.SupplierNumber != null && x.SupplierNumber.Contains(q.SupplierNumber!))
  39. .WhereIF(!string.IsNullOrWhiteSpace(q.CurrencyType), x => x.CurrencyType != null && x.CurrencyType.Contains(q.CurrencyType!));
  40. if (!string.IsNullOrWhiteSpace(q.Keyword))
  41. {
  42. var kw = q.Keyword!;
  43. var itemIds = await _itemRep.AsQueryable()
  44. .Where(it =>
  45. it.ItemNum.Contains(kw) ||
  46. it.Descr.Contains(kw) ||
  47. (it.Descr1 != null && it.Descr1.Contains(kw)))
  48. .Select(it => it.Id)
  49. .ToListAsync();
  50. baseQuery = baseQuery.Where(x =>
  51. (x.IcitemName != null && x.IcitemName.Contains(kw)) ||
  52. (x.SupplierName != null && x.SupplierName.Contains(kw)) ||
  53. (x.SupplierNumber != null && x.SupplierNumber.Contains(kw)) ||
  54. (itemIds.Count > 0 && itemIds.Contains(x.IcitemId)));
  55. }
  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 ApplyItemAndSupplierDisplayAsync(list);
  63. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  64. }
  65. [HttpGet("{id:long}")]
  66. public async Task<IActionResult> GetAsync(long id)
  67. {
  68. var item = await _rep.GetByIdAsync(id);
  69. if (item == null) return NotFound();
  70. await ApplyItemAndSupplierDisplayAsync(new List<AdoS0SrmPurchase> { item });
  71. return Ok(item);
  72. }
  73. [HttpPost]
  74. public async Task<IActionResult> CreateAsync([FromBody] AdoS0SrmPurchaseUpsertDto dto)
  75. {
  76. var dateErr = ValidateDateRange(dto.EffectiveDate, dto.ExpiringDate);
  77. if (dateErr != null) return AdoS0ApiErrors.InvalidRequest(dateErr);
  78. var refErr = await ValidateReferencesAsync(dto.IcitemId, dto.SupplierId);
  79. if (refErr != null) return refErr;
  80. var now = DateTime.Now;
  81. var entity = MapDtoToEntity(dto, new AdoS0SrmPurchase(), now, isNew: true);
  82. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  83. await ApplyItemAndSupplierDisplayAsync(new List<AdoS0SrmPurchase> { entity });
  84. return Ok(entity);
  85. }
  86. [HttpPut("{id:long}")]
  87. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0SrmPurchaseUpsertDto dto)
  88. {
  89. var entity = await _rep.GetByIdAsync(id);
  90. if (entity == null) return NotFound();
  91. var dateErr = ValidateDateRange(dto.EffectiveDate, dto.ExpiringDate);
  92. if (dateErr != null) return AdoS0ApiErrors.InvalidRequest(dateErr);
  93. var refErr = await ValidateReferencesAsync(dto.IcitemId, dto.SupplierId);
  94. if (refErr != null) return refErr;
  95. MapDtoToEntity(dto, entity, DateTime.Now, isNew: false);
  96. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  97. await ApplyItemAndSupplierDisplayAsync(new List<AdoS0SrmPurchase> { entity });
  98. return Ok(entity);
  99. }
  100. [HttpDelete("{id:long}")]
  101. public async Task<IActionResult> DeleteAsync(long id)
  102. {
  103. var item = await _rep.GetByIdAsync(id);
  104. if (item == null) return NotFound();
  105. await _rep.DeleteAsync(item);
  106. return Ok(new { message = "删除成功" });
  107. }
  108. private static string? ValidateDateRange(DateTime? effective, DateTime? expiring)
  109. {
  110. if (effective.HasValue && expiring.HasValue && effective.Value.Date > expiring.Value.Date)
  111. return "生效日期不能晚于失效日期";
  112. return null;
  113. }
  114. private async Task<IActionResult?> ValidateReferencesAsync(long icitemId, long supplierId)
  115. {
  116. if (!await _itemRep.IsAnyAsync(x => x.Id == icitemId))
  117. return AdoS0ApiErrors.InvalidReference(AdoS0ErrorCodes.MaterialReferenceInvalid, "物料主数据引用无效");
  118. if (!await _suppRep.IsAnyAsync(x => x.Id == supplierId))
  119. return AdoS0ApiErrors.InvalidReference(AdoS0ErrorCodes.InvalidReference, "供应商主数据引用无效");
  120. return null;
  121. }
  122. private static AdoS0SrmPurchase MapDtoToEntity(AdoS0SrmPurchaseUpsertDto dto, AdoS0SrmPurchase entity, DateTime now, bool isNew)
  123. {
  124. entity.CompanyRefId = dto.CompanyRefId;
  125. entity.FactoryRefId = dto.FactoryRefId;
  126. entity.DomainCode = dto.DomainCode;
  127. entity.IcitemId = dto.IcitemId;
  128. entity.IcitemName = dto.IcitemName;
  129. entity.SupplierType = dto.SupplierType;
  130. entity.IsActive = dto.IsActive;
  131. entity.SupplierId = dto.SupplierId;
  132. entity.SupplierName = dto.SupplierName;
  133. entity.SupplierNumber = dto.SupplierNumber;
  134. entity.OrderPrice = dto.OrderPrice;
  135. entity.CurrencyType = dto.CurrencyType;
  136. entity.Taxrate = dto.Taxrate;
  137. entity.Tariff = dto.Tariff;
  138. entity.Freight = dto.Freight;
  139. entity.PriceTerms = dto.PriceTerms;
  140. entity.EffectiveDate = dto.EffectiveDate;
  141. entity.ExpiringDate = dto.ExpiringDate;
  142. entity.QuotaRate = dto.QuotaRate;
  143. entity.LeadTime = dto.LeadTime;
  144. entity.QtyMin = dto.QtyMin;
  145. entity.PackagingQty = dto.PackagingQty;
  146. entity.OrderRectorName = dto.OrderRectorName;
  147. entity.OrderRectorNum = dto.OrderRectorNum;
  148. entity.IsRequireGoods = dto.IsRequireGoods;
  149. if (isNew)
  150. {
  151. entity.CreateUser = dto.CreateUser;
  152. entity.CreateTime = now;
  153. entity.UpdateUser = dto.UpdateUser;
  154. entity.UpdateTime = null;
  155. }
  156. else
  157. {
  158. entity.UpdateUser = dto.UpdateUser;
  159. entity.UpdateTime = now;
  160. }
  161. return entity;
  162. }
  163. private async Task ApplyItemAndSupplierDisplayAsync(List<AdoS0SrmPurchase> rows)
  164. {
  165. if (rows.Count == 0) return;
  166. var itemIds = rows.Select(x => x.IcitemId).Where(id => id > 0).Distinct().ToList();
  167. var items = itemIds.Count == 0
  168. ? new List<AdoS0ItemMaster>()
  169. : await _itemRep.AsQueryable().Where(it => itemIds.Contains(it.Id)).ToListAsync();
  170. foreach (var sp in rows)
  171. {
  172. var it = items.Find(i => i.Id == sp.IcitemId);
  173. sp.MaterialCode = it?.ItemNum;
  174. sp.Model = it?.Descr1;
  175. sp.Unit = it?.UM;
  176. sp.ItemTypeLabel = string.IsNullOrWhiteSpace(it?.ItemType) ? "原材料" : it!.ItemType;
  177. var num = sp.MaterialCode ?? "";
  178. var name = sp.IcitemName ?? "";
  179. sp.Icitem = $"{num}{name}";
  180. var sname = sp.SupplierName ?? "";
  181. var snum = sp.SupplierNumber ?? "";
  182. sp.Supplier = $"{sname}{snum}";
  183. }
  184. }
  185. }