AdoS0KittingExcludeRuleController.cs 4.4 KB

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