| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using System.Text.Json;
- using Admin.NET.Plugin.AiDOP.Dto.S0.Sales;
- using Admin.NET.Plugin.AiDOP.Entity.S0.Sales;
- using Admin.NET.Plugin.AiDOP.Infrastructure;
- using Yitter.IdGenerator;
- namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Sales;
- /// <summary>
- /// S0 工单齐套检查排除规则(一个租户一条配置)
- /// </summary>
- [ApiController]
- [Route("api/s0/sales/kitting-exclude-rule")]
- [AllowAnonymous]
- [NonUnify]
- public class AdoS0KittingExcludeRuleController : ControllerBase
- {
- private readonly SqlSugarRepository<AdoS0KittingExcludeRule> _rep;
- public AdoS0KittingExcludeRuleController(SqlSugarRepository<AdoS0KittingExcludeRule> rep)
- => _rep = rep;
- /// <summary>获取当前租户的齐套检查排除规则</summary>
- [HttpGet]
- public async Task<IActionResult> GetAsync()
- {
- if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var error))
- return error!;
- var entity = await _rep.GetFirstAsync(x => x.TenantId == tenantId);
- if (entity == null)
- {
- return Ok(new AdoS0KittingExcludeRuleDto
- {
- Id = 0,
- IsActive = true,
- ExcludedItemTypes = Array.Empty<string>(),
- ExcludedOwnerApps = Array.Empty<string>(),
- });
- }
- return Ok(ToDto(entity));
- }
- /// <summary>获取当前物料类型和物料属性的可选项(从 ItemMaster 去重)</summary>
- [HttpGet("options")]
- public async Task<IActionResult> GetOptionsAsync()
- {
- if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var error))
- return error!;
- var itemTypes = await _rep.AsQueryable()
- .InnerJoin<AdoS0ItemMaster>((r, im) => im.TenantId == tenantId)
- .Where((r, im) => im.ItemType != null && im.ItemType != "")
- .Select((r, im) => im.ItemType)
- .Distinct()
- .ToListAsync();
- var ownerApps = await _rep.AsQueryable()
- .InnerJoin<AdoS0ItemMaster>((r, im) => im.TenantId == tenantId)
- .Where((r, im) => im.OwnerApplication != null && im.OwnerApplication != "")
- .Select((r, im) => im.OwnerApplication)
- .Distinct()
- .ToListAsync();
- return Ok(new { itemTypes, ownerApps });
- }
- /// <summary>保存/更新当前租户的齐套检查排除规则</summary>
- [HttpPost]
- public async Task<IActionResult> UpsertAsync([FromBody] AdoS0KittingExcludeRuleUpsertDto dto)
- {
- if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var error))
- return error!;
- var entity = await _rep.GetFirstAsync(x => x.TenantId == tenantId);
- var now = DateTime.Now;
- var jsonTypes = JsonSerializer.Serialize(dto.ExcludedItemTypes ?? Array.Empty<string>());
- var jsonApps = JsonSerializer.Serialize(dto.ExcludedOwnerApps ?? Array.Empty<string>());
- if (entity == null)
- {
- entity = new AdoS0KittingExcludeRule
- {
- Id = YitIdHelper.NextId(),
- TenantId = tenantId,
- ExcludedItemTypes = jsonTypes,
- ExcludedOwnerApps = jsonApps,
- IsActive = dto.IsActive,
- Remark = dto.Remark,
- CreateTime = now,
- UpdateTime = null,
- };
- await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
- }
- else
- {
- entity.ExcludedItemTypes = jsonTypes;
- entity.ExcludedOwnerApps = jsonApps;
- entity.IsActive = dto.IsActive;
- entity.Remark = dto.Remark;
- entity.UpdateTime = now;
- await _rep.AsUpdateable(entity).ExecuteCommandAsync();
- }
- return Ok(ToDto(entity));
- }
- private static AdoS0KittingExcludeRuleDto ToDto(AdoS0KittingExcludeRule entity)
- {
- return new AdoS0KittingExcludeRuleDto
- {
- Id = entity.Id,
- IsActive = entity.IsActive,
- ExcludedItemTypes = ParseJsonArray(entity.ExcludedItemTypes),
- ExcludedOwnerApps = ParseJsonArray(entity.ExcludedOwnerApps),
- Remark = entity.Remark,
- UpdateTime = entity.UpdateTime,
- };
- }
- private static string[] ParseJsonArray(string? json)
- {
- if (string.IsNullOrWhiteSpace(json)) return Array.Empty<string>();
- try { return JsonSerializer.Deserialize<string[]>(json) ?? Array.Empty<string>(); }
- catch { return Array.Empty<string>(); }
- }
- }
|