feat(um): 根据标的代码 基金管理人回显
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
using Dapper;
|
||||
using MySqlConnector;
|
||||
using YieldChain.Helpers;
|
||||
using YLErp.BLL;
|
||||
|
||||
namespace YLErp.Modules.UnderlyingModule
|
||||
{
|
||||
public enum FundManagerLookupStatus
|
||||
{
|
||||
NotFound,
|
||||
Unique,
|
||||
Multiple,
|
||||
Unavailable
|
||||
}
|
||||
|
||||
public sealed class FundManagerLookupResult
|
||||
{
|
||||
public FundManagerLookupStatus Status { get; init; }
|
||||
public string InvestAdvisorName { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询上游基金档案中的基金管理人。上游不可用时返回降级结果,不阻断页面编辑。
|
||||
/// </summary>
|
||||
public sealed class FundManagerLookupService
|
||||
{
|
||||
private sealed class FundManagerRow
|
||||
{
|
||||
public string InvestAdvisorCode { get; set; }
|
||||
public string InvestAdvisorName { get; set; }
|
||||
}
|
||||
|
||||
private const string LookupSql = @"
|
||||
SELECT
|
||||
ia.investadvisorcode AS InvestAdvisorCode,
|
||||
ia.investadvisorname AS InvestAdvisorName
|
||||
FROM glms_bigdata.mf_fundarchives AS fa
|
||||
INNER JOIN glms_bigdata.mf_investadvisoroutline AS ia
|
||||
ON CONVERT(fa.investadvisorcode USING utf8mb4) COLLATE utf8mb4_unicode_ci =
|
||||
CONVERT(ia.investadvisorcode USING utf8mb4) COLLATE utf8mb4_unicode_ci
|
||||
WHERE CONVERT(fa.secucode USING utf8mb4) COLLATE utf8mb4_unicode_ci =
|
||||
CONVERT(TRIM(SUBSTRING_INDEX(@UnderlyingCode, '.', 1)) USING utf8mb4) COLLATE utf8mb4_unicode_ci";
|
||||
|
||||
public FundManagerLookupResult Lookup(string underlyingCode)
|
||||
{
|
||||
var normalizedCode = NormalizeCode(underlyingCode);
|
||||
if (string.IsNullOrEmpty(normalizedCode))
|
||||
{
|
||||
return new FundManagerLookupResult { Status = FundManagerLookupStatus.NotFound };
|
||||
}
|
||||
|
||||
var connectionString = AppManager.GetConnectionString("glms_bigdata");
|
||||
if (string.IsNullOrWhiteSpace(connectionString))
|
||||
{
|
||||
return new FundManagerLookupResult { Status = FundManagerLookupStatus.Unavailable };
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new MySqlConnection(connectionString);
|
||||
var matches = connection.Query<FundManagerRow>(LookupSql, new { UnderlyingCode = normalizedCode }, commandTimeout: 10)
|
||||
.Where(row => !string.IsNullOrWhiteSpace(row.InvestAdvisorName))
|
||||
.GroupBy(row => (row.InvestAdvisorCode ?? string.Empty).Trim(), StringComparer.OrdinalIgnoreCase)
|
||||
.Select(group => group.Select(row => row.InvestAdvisorName.Trim()).Distinct(StringComparer.OrdinalIgnoreCase).ToArray())
|
||||
.Where(names => names.Length > 0)
|
||||
.ToArray();
|
||||
|
||||
return matches.Length switch
|
||||
{
|
||||
0 => new FundManagerLookupResult { Status = FundManagerLookupStatus.NotFound },
|
||||
1 when matches[0].Length == 1 => new FundManagerLookupResult { Status = FundManagerLookupStatus.Unique, InvestAdvisorName = matches[0][0] },
|
||||
_ => new FundManagerLookupResult { Status = FundManagerLookupStatus.Multiple }
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogFactory.GetLogger<FundManagerLookupService>().Error("查询基金管理人失败,代码:" + normalizedCode, ex);
|
||||
return new FundManagerLookupResult { Status = FundManagerLookupStatus.Unavailable };
|
||||
}
|
||||
}
|
||||
|
||||
public static string NormalizeCode(string underlyingCode)
|
||||
{
|
||||
var trimmed = underlyingCode?.Trim();
|
||||
if (string.IsNullOrEmpty(trimmed))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var dotIndex = trimmed.IndexOf('.');
|
||||
return (dotIndex < 0 ? trimmed : trimmed.Substring(0, dotIndex)).TrimToNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user