AdoS0KittingExcludeRuleController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Text.Json;
  2. using Admin.NET.Plugin.AiDOP.Dto.S0.Sales;
  3. using Admin.NET.Plugin.AiDOP.Entity.S0.Sales;
  4. using Admin.NET.Plugin.AiDOP.Infrastructure;
  5. using Yitter.IdGenerator;
  6. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Sales;
  7. /// <summary>
  8. /// S0 工单齐套检查排除规则(一个租户一条配置)
  9. /// </summary>
  10. [ApiController]
  11. [Route("api/s0/sales/kitting-exclude-rule")]
  12. [AllowAnonymous]
  13. [NonUnify]
  14. public class AdoS0KittingExcludeRuleController : ControllerBase
  15. {
  16. private readonly SqlSugarRepository<AdoS0KittingExcludeRule> _rep;
  17. public AdoS0KittingExcludeRuleController(SqlSugarRepository<AdoS0KittingExcludeRule> rep)
  18. => _rep = rep;
  19. /// <summary>获取当前租户的齐套检查排除规则</summary>
  20. [HttpGet]
  21. public async Task<IActionResult> GetAsync()
  22. {
  23. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var error))
  24. return error!;
  25. var entity = await _rep.GetFirstAsync(x => x.TenantId == tenantId);
  26. if (entity == null)
  27. {
  28. return Ok(new AdoS0KittingExcludeRuleDto
  29. {
  30. Id = 0,
  31. IsActive = true,
  32. ExcludedItemTypes = Array.Empty<string>(),
  33. ExcludedOwnerApps = Array.Empty<string>(),
  34. });
  35. }
  36. return Ok(ToDto(entity));
  37. }
  38. /// <summary>获取当前物料类型和物料属性的可选项(从 ItemMaster 去重)</summary>
  39. [HttpGet("options")]
  40. public async Task<IActionResult> GetOptionsAsync()
  41. {
  42. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var error))
  43. return error!;
  44. var itemTypes = await _rep.AsQueryable()
  45. .InnerJoin<AdoS0ItemMaster>((r, im) => im.TenantId == tenantId)
  46. .Where((r, im) => im.ItemType != null && im.ItemType != "")
  47. .Select((r, im) => im.ItemType)
  48. .Distinct()
  49. .ToListAsync();
  50. var ownerApps = await _rep.AsQueryable()
  51. .InnerJoin<AdoS0ItemMaster>((r, im) => im.TenantId == tenantId)
  52. .Where((r, im) => im.OwnerApplication != null && im.OwnerApplication != "")
  53. .Select((r, im) => im.OwnerApplication)
  54. .Distinct()
  55. .ToListAsync();
  56. return Ok(new { itemTypes, ownerApps });
  57. }
  58. /// <summary>保存/更新当前租户的齐套检查排除规则</summary>
  59. [HttpPost]
  60. public async Task<IActionResult> UpsertAsync([FromBody] AdoS0KittingExcludeRuleUpsertDto dto)
  61. {
  62. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var error))
  63. return error!;
  64. var entity = await _rep.GetFirstAsync(x => x.TenantId == tenantId);
  65. var now = DateTime.Now;
  66. var jsonTypes = JsonSerializer.Serialize(dto.ExcludedItemTypes ?? Array.Empty<string>());
  67. var jsonApps = JsonSerializer.Serialize(dto.ExcludedOwnerApps ?? Array.Empty<string>());
  68. if (entity == null)
  69. {
  70. entity = new AdoS0KittingExcludeRule
  71. {
  72. Id = YitIdHelper.NextId(),
  73. TenantId = tenantId,
  74. ExcludedItemTypes = jsonTypes,
  75. ExcludedOwnerApps = jsonApps,
  76. IsActive = dto.IsActive,
  77. Remark = dto.Remark,
  78. CreateTime = now,
  79. UpdateTime = null,
  80. };
  81. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  82. }
  83. else
  84. {
  85. entity.ExcludedItemTypes = jsonTypes;
  86. entity.ExcludedOwnerApps = jsonApps;
  87. entity.IsActive = dto.IsActive;
  88. entity.Remark = dto.Remark;
  89. entity.UpdateTime = now;
  90. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  91. }
  92. return Ok(ToDto(entity));
  93. }
  94. private static AdoS0KittingExcludeRuleDto ToDto(AdoS0KittingExcludeRule entity)
  95. {
  96. return new AdoS0KittingExcludeRuleDto
  97. {
  98. Id = entity.Id,
  99. IsActive = entity.IsActive,
  100. ExcludedItemTypes = ParseJsonArray(entity.ExcludedItemTypes),
  101. ExcludedOwnerApps = ParseJsonArray(entity.ExcludedOwnerApps),
  102. Remark = entity.Remark,
  103. UpdateTime = entity.UpdateTime,
  104. };
  105. }
  106. private static string[] ParseJsonArray(string? json)
  107. {
  108. if (string.IsNullOrWhiteSpace(json)) return Array.Empty<string>();
  109. try { return JsonSerializer.Deserialize<string[]>(json) ?? Array.Empty<string>(); }
  110. catch { return Array.Empty<string>(); }
  111. }
  112. }