using System.Text.Json;
using Fake.Application;
using Fake.DependencyInjection;
using Oms.Application.Services.Approval.Contracts;
using Oms.Domain.ApprovalAggregate.Consts;
using Oms.Domain.ChannelsAggregate.Entities;
using Oms.Domain.ChannelsAggregate.Repositories;
using Oms.Domain.Shared.Enums;
namespace Oms.Application.Services.Channels;
///
/// 销售产品价格发布审批终审执行器:按冻结快照写入发布记录。
///
[ExposeServices(typeof(IApprovalOperationHandler))]
public class SalesProductPricePublishApprovalHandler(
ISalesProductRepository salesProductRepository) : IApprovalOperationHandler, ITransientDependency
{
///
public string OperationCode => ApprovalCodes.SalesProductPublishPrice;
///
public async Task ExecuteAsync(ApprovalExecutionContext context, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(context.PayloadJson))
{
throw new BusinessException("价格发布审批载荷为空");
}
using var document = JsonDocument.Parse(context.PayloadJson);
var root = document.RootElement;
var salesProductId = ReadInt32(root, "salesProductId", "SalesProductId");
var content = ReadJsonPayload(root, "content", "Content");
if (salesProductId <= 0 || string.IsNullOrWhiteSpace(content))
{
throw new BusinessException("价格发布审批载荷缺少产品或快照");
}
var product = await salesProductRepository.FirstOrDefaultAsync(
x => x.Id == salesProductId,
cancellationToken);
if (product == null)
{
throw new BusinessException("销售产品不存在");
}
var applicantUserName = ReadString(root, "applicantUserName", "ApplicantUserName");
var publishUser = string.IsNullOrWhiteSpace(applicantUserName)
? (string.IsNullOrWhiteSpace(context.ApplicantUserGuid)
? context.ApplicantUserId.ToString()
: context.ApplicantUserGuid)
: applicantUserName.Trim();
var descriptionRaw = ReadString(root, "description", "Description");
var description = string.IsNullOrWhiteSpace(descriptionRaw)
? $"审批通过发布 {context.InstanceNo}"
: $"{descriptionRaw.Trim()}(审批单 {context.InstanceNo})";
var aiReport = ReadString(root, "aiAnalysisReport", "AiAnalysisReport");
await salesProductRepository.InsertPublishRecordAsync(new SalesProductPublishRecord
{
SalesProductId = salesProductId,
Version = DateTime.Now.ToString("yyyyMMddHHmmss"),
OperatorType = "price",
Status = EsSnapshotStatus.Active,
PublishUser = publishUser,
PublishTime = DateTime.Now,
Description = description,
AiAnalysisReport = string.IsNullOrWhiteSpace(aiReport) ? null : aiReport.Trim(),
Content = content
}, cancellationToken);
}
private static int ReadInt32(JsonElement root, params string[] names)
{
foreach (var name in names)
{
if (!root.TryGetProperty(name, out var el))
{
continue;
}
if (el.ValueKind == JsonValueKind.Number && el.TryGetInt32(out var n))
{
return n;
}
if (el.ValueKind == JsonValueKind.String && int.TryParse(el.GetString(), out n))
{
return n;
}
}
return 0;
}
private static string? ReadString(JsonElement root, params string[] names)
{
foreach (var name in names)
{
if (!root.TryGetProperty(name, out var el))
{
continue;
}
if (el.ValueKind == JsonValueKind.String)
{
return el.GetString();
}
if (el.ValueKind is not (JsonValueKind.Null or JsonValueKind.Undefined))
{
return el.GetRawText();
}
}
return null;
}
///
/// content 可能是 JSON 字符串,也可能是对象(历史/异常序列化);统一成快照正文。
///
private static string? ReadJsonPayload(JsonElement root, params string[] names)
{
foreach (var name in names)
{
if (!root.TryGetProperty(name, out var el))
{
continue;
}
if (el.ValueKind == JsonValueKind.String)
{
return el.GetString();
}
if (el.ValueKind is JsonValueKind.Object or JsonValueKind.Array)
{
return el.GetRawText();
}
}
return null;
}
}