| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664 |
- using System.Text.Json;
- namespace Admin.NET.Plugin.AiDOP.MaterialWarehouse;
- /// <summary>
- /// S5 物料仓储 — T8 KPI 数据底座与刷新转换服务。
- /// 路径 A:方老师 v5.4 KPI 字段对照表 J 列 SQL 原逻辑直发 T8 SQL Server(ConfigId=t8_v5);
- /// 一期不做 mdp_stg_t8_* 贴源层;结果直接落 dwd_t8_* 与 ado_s9_kpi_value_l1_day。
- /// 包含 KPI:S5_L1_001 物料上线周期 / S5_L1_002 物料上线满足率 /
- /// S5_L1_003 物料仓储人效 / S5_L1_004 品类物料库存周转。
- /// </summary>
- public class S5MdpSyncTransformService : ITransient
- {
- private readonly ISqlSugarClient _db;
- private const string JobCode = "S5_MDP_SYNC_TRANSFORM";
- private const string JobName = "S5 物料仓储 MDP 同步与转换";
- private const string T8ConfigId = "t8_v5";
- private const string ModuleCode = "S5";
- public S5MdpSyncTransformService(ISqlSugarClient db)
- {
- _db = db;
- }
- public async Task<S5MdpSyncTransformResult> RunFullAsync(
- CancellationToken cancellationToken = default,
- string triggerType = "AUTO",
- S5MdpRefreshOption? option = null)
- {
- cancellationToken.ThrowIfCancellationRequested();
- option ??= S5MdpRefreshOption.Default();
- var now = DateTime.Now;
- var batchId = $"S5_MDP_FULL_{now:yyyyMMddHHmmss}";
- var normalizedTrigger = NormalizeTriggerType(triggerType);
- var runLogId = await InsertTransformRunLogAsync(batchId, now, normalizedTrigger, option);
- var result = new S5MdpSyncTransformResult
- {
- BatchId = batchId,
- RunLogId = runLogId,
- TriggerType = normalizedTrigger,
- SourceZtid = option.SourceZtid,
- BizDate = option.BizDate,
- BizMonth = option.BizMonth,
- DailyPeriodStart = option.DailyPeriodStart,
- DailyPeriodEnd = option.DailyPeriodEnd,
- MonthlyPeriodStart = option.MonthlyPeriodStart,
- MonthlyPeriodEnd = option.MonthlyPeriodEnd
- };
- try
- {
- // 路径 A:直发 T8 SQL,不做 stg / std 中间层
- result.StageRows = 0;
- result.StandardRows = 0;
- var sub16 = await BuildS5L1001MaterialOnlineCycleAsync(batchId, now, option, cancellationToken);
- result.MergeSub("S5_L1_001", sub16);
- var sub17 = await BuildS5L1002MaterialOnlineFulfillmentAsync(batchId, now, option, cancellationToken);
- result.MergeSub("S5_L1_002", sub17);
- var sub18 = await BuildS5L1003MaterialWarehouseEfficiencyAsync(batchId, now, option, cancellationToken);
- result.MergeSub("S5_L1_003", sub18);
- var sub19 = await BuildS5L1004MaterialInventoryTurnoverAsync(batchId, now, option, cancellationToken);
- result.MergeSub("S5_L1_004", sub19);
- await MarkTransformRunSuccessAsync(runLogId, now, result);
- return result;
- }
- catch (Exception ex)
- {
- await MarkTransformRunFailedAsync(runLogId, now, ex.Message);
- throw;
- }
- }
- // ─────────────────────────────────────────────────────────────────────────
- // KPI 实现(方老师 v5.4 KPI J 列 SQL 原逻辑直发 T8)
- // ─────────────────────────────────────────────────────────────────────────
- /// <summary>S5_L1_001 物料上线周期 = 配送到产线日期(lbs=生产领料) - 收货日期(lbs=采购入库)。</summary>
- private async Task<KpiBuildSubResult> BuildS5L1001MaterialOnlineCycleAsync(
- string batchId, DateTime now, S5MdpRefreshOption option, CancellationToken ct)
- {
- var sub = new KpiBuildSubResult();
- const string sqlOnline = @"
- select b.code as code, min(a.shtime) as shtime
- from kc_tz_head a with(nolock)
- inner join kc_tz_list b with(nolock) on a.Id=b.idid
- where a.ztid=@ztid and a.lbs='生产领料' and a.hzyn=0 and a.zfyn=0 and a.shyn=1
- group by b.code";
- const string sqlReceipt = @"
- select b.code as code, min(a.shtime) as shtime
- from kc_tz_head a with(nolock)
- inner join kc_tz_list b with(nolock) on a.Id=b.idid
- where a.ztid=@ztid and a.lbs='采购入库' and a.hzyn=0 and a.zfyn=0 and a.shyn=1
- group by b.code";
- var p = new[] { new SugarParameter("@ztid", option.SourceZtid) };
- var onlineRows = await QueryT8Async<S5OnlineCycleRow>(sqlOnline, p);
- var receiptRows = await QueryT8Async<S5OnlineCycleRow>(sqlReceipt, p);
- sub.T8Rows = onlineRows.Count + receiptRows.Count;
- var onlineByCode = onlineRows.Where(r => !string.IsNullOrEmpty(r.code))
- .ToDictionary(r => r.code!, r => r.shtime, StringComparer.OrdinalIgnoreCase);
- var receiptByCode = receiptRows.Where(r => !string.IsNullOrEmpty(r.code))
- .ToDictionary(r => r.code!, r => r.shtime, StringComparer.OrdinalIgnoreCase);
- var allCodes = new HashSet<string>(onlineByCode.Keys, StringComparer.OrdinalIgnoreCase);
- allCodes.UnionWith(receiptByCode.Keys);
- var dwdAffected = 0;
- var cycleDaysList = new List<int>();
- foreach (var code in allCodes)
- {
- ct.ThrowIfCancellationRequested();
- var online = onlineByCode.GetValueOrDefault(code);
- var receipt = receiptByCode.GetValueOrDefault(code);
- int? cycleDays = null;
- if (online.HasValue && receipt.HasValue)
- {
- cycleDays = (int)(online.Value.Date - receipt.Value.Date).TotalDays;
- cycleDaysList.Add(cycleDays.Value);
- }
- dwdAffected += await _db.Ado.ExecuteCommandAsync(@"
- INSERT INTO dwd_t8_material_online_cycle
- (tenant_id, factory_id, biz_date, source_ztid, item_code, online_date, receipt_date, cycle_days, batch_id, create_time)
- VALUES
- (0, 1, @bizDate, @ztid, @itemCode, @online, @receipt, @cycleDays, @batchId, @now)
- ON DUPLICATE KEY UPDATE
- online_date=VALUES(online_date), receipt_date=VALUES(receipt_date),
- cycle_days=VALUES(cycle_days), batch_id=VALUES(batch_id), update_time=@now",
- new SugarParameter("@bizDate", option.BizDate),
- new SugarParameter("@ztid", option.SourceZtid),
- new SugarParameter("@itemCode", code),
- new SugarParameter("@online", online),
- new SugarParameter("@receipt", receipt),
- new SugarParameter("@cycleDays", cycleDays),
- new SugarParameter("@batchId", batchId),
- new SugarParameter("@now", now));
- }
- sub.DwdRows = dwdAffected;
- // KPI 值:所有物料 cycle_days 的算术平均;没有任一可计算物料时写 NULL,不伪装 0
- decimal? metricValue = cycleDaysList.Count > 0
- ? (decimal)cycleDaysList.Average()
- : null;
- sub.KpiRows = await UpsertKpiValueAsync("S5_L1_001", option.BizDate, metricValue, now);
- sub.DenominatorStatus = cycleDaysList.Count > 0 ? "OK" : "NO_NUMERATOR";
- return sub;
- }
- /// <summary>S5_L1_002 物料上线满足率 = 开工日期前完成上线行数 / 工单物料总行数。</summary>
- private async Task<KpiBuildSubResult> BuildS5L1002MaterialOnlineFulfillmentAsync(
- string batchId, DateTime now, S5MdpRefreshOption option, CancellationToken ct)
- {
- var sub = new KpiBuildSubResult();
- const string sqlNumer = @"
- select lynoid as lynoid, count(*) as codenum
- from (
- select a.lynoid as lynoid, b.code as code
- from kc_tz_head a with(nolock)
- inner join kc_tz_list b with(nolock) on a.Id=b.idid
- left join (
- select noid as noid, min(kgdate) as kgdate
- from Cj_Bg_Head_Rep with(nolock)
- where ztid=@ztid group by noid
- ) c on a.lynoid=c.noid
- where a.ztid=@ztid and a.lbs='生产领料' and a.hzyn=0 and a.zfyn=0 and a.shyn=1 and a.shtime<=c.kgdate
- group by a.lynoid, b.code
- ) n
- group by lynoid";
- const string sqlDenom = @"
- select a.noid as noid, count(b.id) as listnum
- from kc_dd_head a with(nolock)
- left join kc_dd_list_cllist b with(nolock) on a.Id=b.idid
- where a.ztid=@ztid and a.lbs='生产任务' and a.zf=0 and a.shyn=1
- group by a.noid";
- var p = new[] { new SugarParameter("@ztid", option.SourceZtid) };
- var numerRows = await QueryT8Async<S5FulfillmentNumerRow>(sqlNumer, p);
- var denomRows = await QueryT8Async<S5FulfillmentDenomRow>(sqlDenom, p);
- sub.T8Rows = numerRows.Count + denomRows.Count;
- var numerByOrder = numerRows.Where(r => !string.IsNullOrEmpty(r.lynoid))
- .ToDictionary(r => r.lynoid!, r => r.codenum, StringComparer.OrdinalIgnoreCase);
- var dwdAffected = 0;
- var rateList = new List<decimal>();
- foreach (var d in denomRows)
- {
- ct.ThrowIfCancellationRequested();
- if (string.IsNullOrEmpty(d.noid)) continue;
- var beforeKg = numerByOrder.GetValueOrDefault(d.noid, 0);
- decimal? rate = d.listnum > 0
- ? Math.Round((decimal)beforeKg / d.listnum, 4)
- : null; // 分母为 0 时不伪装真实 0
- if (rate.HasValue) rateList.Add(rate.Value);
- dwdAffected += await _db.Ado.ExecuteCommandAsync(@"
- INSERT INTO dwd_t8_material_online_fulfillment
- (tenant_id, factory_id, biz_date, source_ztid, work_order_no,
- before_kgdate_rows, total_rows, fulfillment_rate, batch_id, create_time)
- VALUES
- (0, 1, @bizDate, @ztid, @workOrderNo, @beforeKg, @total, @rate, @batchId, @now)
- ON DUPLICATE KEY UPDATE
- before_kgdate_rows=VALUES(before_kgdate_rows),
- total_rows=VALUES(total_rows),
- fulfillment_rate=VALUES(fulfillment_rate),
- batch_id=VALUES(batch_id), update_time=@now",
- new SugarParameter("@bizDate", option.BizDate),
- new SugarParameter("@ztid", option.SourceZtid),
- new SugarParameter("@workOrderNo", d.noid),
- new SugarParameter("@beforeKg", beforeKg),
- new SugarParameter("@total", d.listnum),
- new SugarParameter("@rate", rate),
- new SugarParameter("@batchId", batchId),
- new SugarParameter("@now", now));
- }
- sub.DwdRows = dwdAffected;
- decimal? metricValue = rateList.Count > 0
- ? Math.Round(rateList.Average() * 100m, 4) // 百分号
- : null;
- sub.KpiRows = await UpsertKpiValueAsync("S5_L1_002", option.BizDate, metricValue, now);
- sub.DenominatorStatus = rateList.Count > 0 ? "OK" : "NO_VALID_ORDER";
- return sub;
- }
- /// <summary>S5_L1_003 物料仓储人效 = SUM(slzx where lbs=生产领料) / count(gw=仓管)。</summary>
- private async Task<KpiBuildSubResult> BuildS5L1003MaterialWarehouseEfficiencyAsync(
- string batchId, DateTime now, S5MdpRefreshOption option, CancellationToken ct)
- {
- var sub = new KpiBuildSubResult();
- const string sqlNumer = @"
- select sum(b.slzx) as slzx
- from kc_tz_head a with(nolock)
- inner join kc_tz_list b with(nolock) on a.Id=b.idid
- where a.ztid=@ztid and a.lbs='生产领料' and a.hzyn=0 and a.zfyn=0 and a.shyn=1
- and a.shtime between @startDate and @endDate";
- const string sqlDenom = @"
- select count(*) as penum
- from sys_pelist with(nolock)
- where ztid=@ztid and zzzt='在职' and gw='仓管'";
- var pNumer = new[]
- {
- new SugarParameter("@ztid", option.SourceZtid),
- new SugarParameter("@startDate", option.MonthlyPeriodStart),
- new SugarParameter("@endDate", option.MonthlyPeriodEnd)
- };
- var pDenom = new[] { new SugarParameter("@ztid", option.SourceZtid) };
- var numerRows = await QueryT8Async<S5SumQtyRow>(sqlNumer, pNumer);
- var denomRows = await QueryT8Async<S5CountRow>(sqlDenom, pDenom);
- sub.T8Rows = numerRows.Count + denomRows.Count;
- decimal? onlineQty = numerRows.FirstOrDefault()?.slzx;
- int? headcount = denomRows.FirstOrDefault()?.penum;
- // 分母 = 0 或 NULL:efficiency 写 NULL,并标记 denominator_status;不伪装真实 0
- decimal? efficiency = null;
- string denomStatus;
- if (!headcount.HasValue || headcount.Value <= 0)
- {
- denomStatus = "NO_HEADCOUNT";
- }
- else if (!onlineQty.HasValue)
- {
- denomStatus = "NO_NUMERATOR";
- }
- else
- {
- efficiency = Math.Round(onlineQty.Value / headcount.Value, 4);
- denomStatus = "OK";
- }
- sub.DenominatorStatus = denomStatus;
- // 月度 KPI 用 biz_month 唯一键,整月 1 行
- var dwdAffected = await _db.Ado.ExecuteCommandAsync(@"
- INSERT INTO dwd_t8_material_warehouse_efficiency
- (tenant_id, factory_id, biz_month, source_ztid, period_start, period_end,
- online_qty, warehouse_headcount, efficiency, denominator_status, batch_id, create_time)
- VALUES
- (0, 1, @bizMonth, @ztid, @periodStart, @periodEnd,
- @onlineQty, @headcount, @efficiency, @denomStatus, @batchId, @now)
- ON DUPLICATE KEY UPDATE
- period_start=VALUES(period_start), period_end=VALUES(period_end),
- online_qty=VALUES(online_qty), warehouse_headcount=VALUES(warehouse_headcount),
- efficiency=VALUES(efficiency), denominator_status=VALUES(denominator_status),
- batch_id=VALUES(batch_id), update_time=@now",
- new SugarParameter("@bizMonth", option.BizMonth),
- new SugarParameter("@ztid", option.SourceZtid),
- new SugarParameter("@periodStart", option.MonthlyPeriodStart),
- new SugarParameter("@periodEnd", option.MonthlyPeriodEnd),
- new SugarParameter("@onlineQty", onlineQty),
- new SugarParameter("@headcount", headcount),
- new SugarParameter("@efficiency", efficiency),
- new SugarParameter("@denomStatus", denomStatus),
- new SugarParameter("@batchId", batchId),
- new SugarParameter("@now", now));
- sub.DwdRows = dwdAffected;
- // 月度 KPI 入日表:用月末日作 biz_date,月内每天可由聚合 API 再分发;分母缺失时 metric_value=NULL
- sub.KpiRows = await UpsertKpiValueAsync("S5_L1_003", option.MonthlyPeriodEnd, efficiency, now);
- return sub;
- }
- /// <summary>S5_L1_004 品类物料库存周转 = D1/D2 × 30;D1=je3 月均库存金额,D2=je2 出库成本。</summary>
- private async Task<KpiBuildSubResult> BuildS5L1004MaterialInventoryTurnoverAsync(
- string batchId, DateTime now, S5MdpRefreshOption option, CancellationToken ct)
- {
- var sub = new KpiBuildSubResult();
- // TVF:Rep_总账_存货_V3(账套, '普通', '正常', 起期 YYYYMM, 止期 YYYYMM)
- const string sqlTvf = @"
- select ckcode as ckcode, ckname as ckname,
- code as code, cname as cname,
- pcode as pcode, pname as pname,
- je3 as je3, je2 as je2
- from dbo.Rep_总账_存货_V3(@ztid, N'普通', N'正常', @startYm, @endYm)";
- var p = new[]
- {
- new SugarParameter("@ztid", option.SourceZtid),
- new SugarParameter("@startYm", option.TvfPeriodStartYyyymm),
- new SugarParameter("@endYm", option.TvfPeriodEndYyyymm)
- };
- var tvfRows = await QueryT8Async<S5InventoryTurnoverRow>(sqlTvf, p);
- sub.T8Rows = tvfRows.Count;
- var dwdAffected = 0;
- var turnoverDaysList = new List<decimal>();
- foreach (var r in tvfRows)
- {
- ct.ThrowIfCancellationRequested();
- // 周转天数:D2=0 或 NULL 时 NULL,不伪装 0
- decimal? turnoverDays = (r.je2.HasValue && r.je2.Value > 0m && r.je3.HasValue)
- ? Math.Round(r.je3.Value / r.je2.Value * 30m, 4)
- : null;
- if (turnoverDays.HasValue) turnoverDaysList.Add(turnoverDays.Value);
- dwdAffected += await _db.Ado.ExecuteCommandAsync(@"
- INSERT INTO dwd_t8_material_inventory_turnover
- (tenant_id, factory_id, biz_month, source_ztid, period_start_yyyymm, period_end_yyyymm,
- warehouse_code, warehouse_name, item_code, item_name, category_code, category_name,
- avg_inventory_value, monthly_outbound_cost, turnover_days, batch_id, create_time)
- VALUES
- (0, 1, @bizMonth, @ztid, @startYm, @endYm,
- @ckcode, @ckname, @itemCode, @itemName, @pcode, @pname,
- @je3, @je2, @turnoverDays, @batchId, @now)
- ON DUPLICATE KEY UPDATE
- warehouse_name=VALUES(warehouse_name), item_name=VALUES(item_name),
- category_code=VALUES(category_code), category_name=VALUES(category_name),
- avg_inventory_value=VALUES(avg_inventory_value),
- monthly_outbound_cost=VALUES(monthly_outbound_cost),
- turnover_days=VALUES(turnover_days),
- period_start_yyyymm=VALUES(period_start_yyyymm),
- period_end_yyyymm=VALUES(period_end_yyyymm),
- batch_id=VALUES(batch_id), update_time=@now",
- new SugarParameter("@bizMonth", option.BizMonth),
- new SugarParameter("@ztid", option.SourceZtid),
- new SugarParameter("@startYm", option.TvfPeriodStartYyyymm),
- new SugarParameter("@endYm", option.TvfPeriodEndYyyymm),
- new SugarParameter("@ckcode", r.ckcode ?? ""),
- new SugarParameter("@ckname", r.ckname),
- new SugarParameter("@itemCode", r.code ?? ""),
- new SugarParameter("@itemName", r.cname),
- new SugarParameter("@pcode", r.pcode),
- new SugarParameter("@pname", r.pname),
- new SugarParameter("@je3", r.je3),
- new SugarParameter("@je2", r.je2),
- new SugarParameter("@turnoverDays", turnoverDays),
- new SugarParameter("@batchId", batchId),
- new SugarParameter("@now", now));
- }
- sub.DwdRows = dwdAffected;
- // KPI 值:所有品类周转天数算术平均;无任一可计算品类时 NULL
- decimal? metricValue = turnoverDaysList.Count > 0
- ? Math.Round(turnoverDaysList.Average(), 4)
- : null;
- sub.KpiRows = await UpsertKpiValueAsync("S5_L1_004", option.MonthlyPeriodEnd, metricValue, now);
- sub.DenominatorStatus = turnoverDaysList.Count > 0 ? "OK" : "NO_VALID_OUTBOUND_COST";
- return sub;
- }
- // ─────────────────────────────────────────────────────────────────────────
- // 跨库 / 写入 / 日志 封装
- // ─────────────────────────────────────────────────────────────────────────
- private async Task<List<T>> QueryT8Async<T>(string sql, SugarParameter[] parameters)
- {
- var t8 = _db.AsTenant().GetConnectionScope(T8ConfigId);
- return await t8.Ado.SqlQueryAsync<T>(sql, parameters);
- }
- private async Task<int> UpsertKpiValueAsync(string metricCode, DateTime bizDate, decimal? metricValue, DateTime now)
- {
- // 沿用 S3 UpsertS3KpiValueAsync 范式:先查现存行 → UPDATE;不存在 → SELECT MAX(id)+1 显式生成 id 后 INSERT。
- // ado_s9_kpi_value_l1_day.id 为手工分配主键(无 AUTO_INCREMENT),必须显式 set;
- // metric_value 允许 NULL(分母缺失不得伪装真实 0)。
- // FIX-2:截断时分秒(月度 KPI 入参可能为 YYYY-MM-DD 23:59:59),保证 SELECT WHERE biz_date=@BizDate 与 DB date 列匹配,避免重复 INSERT。
- bizDate = bizDate.Date;
- var existingId = await _db.Ado.GetLongAsync(
- "SELECT IFNULL((SELECT id FROM ado_s9_kpi_value_l1_day WHERE tenant_id=0 AND factory_id=1 " +
- "AND module_code=@ModuleCode AND metric_code=@MetricCode AND biz_date=@BizDate AND is_deleted=0 " +
- "ORDER BY id LIMIT 1), 0)",
- new List<SugarParameter>
- {
- new("@ModuleCode", ModuleCode),
- new("@MetricCode", metricCode),
- new("@BizDate", bizDate)
- });
- if (existingId > 0)
- {
- return await _db.Ado.ExecuteCommandAsync(
- "UPDATE ado_s9_kpi_value_l1_day SET metric_value=@MetricValue, calc_time=@Now, " +
- "update_time=@Now, is_deleted=0, is_active=1 WHERE id=@Id",
- new SugarParameter("@MetricValue", metricValue),
- new SugarParameter("@Now", now),
- new SugarParameter("@Id", existingId));
- }
- var nextId = await _db.Ado.GetLongAsync(
- "SELECT COALESCE(MAX(id), 0) + 1 FROM ado_s9_kpi_value_l1_day");
- return await _db.Ado.ExecuteCommandAsync(@"
- INSERT INTO ado_s9_kpi_value_l1_day
- (id, tenant_id, org_id, company_id, factory_id, status, biz_date,
- create_time, update_time, is_deleted, is_active,
- module_code, metric_code, metric_value, calc_time)
- VALUES
- (@Id, 0, NULL, NULL, 1, NULL, @BizDate,
- @Now, @Now, 0, 1,
- @ModuleCode, @MetricCode, @MetricValue, @Now)",
- new SugarParameter("@Id", nextId),
- new SugarParameter("@BizDate", bizDate),
- new SugarParameter("@Now", now),
- new SugarParameter("@ModuleCode", ModuleCode),
- new SugarParameter("@MetricCode", metricCode),
- new SugarParameter("@MetricValue", metricValue));
- }
- private async Task<long> InsertTransformRunLogAsync(string batchId, DateTime startedAt, string triggerType, S5MdpRefreshOption option)
- {
- await _db.Ado.ExecuteCommandAsync(@"
- INSERT INTO mdp_transform_run_log
- (tenant_id, job_code, job_name, trigger_type, batch_id, status, start_time, stage_rows, standard_rows, dwd_rows, create_time, update_time)
- VALUES
- (0, @JobCode, @JobName, @TriggerType, @BatchId, 'RUNNING', @StartTime, 0, 0, 0, @StartTime, @StartTime)",
- new SugarParameter("@JobCode", JobCode),
- new SugarParameter("@JobName", JobName),
- new SugarParameter("@TriggerType", triggerType),
- new SugarParameter("@BatchId", batchId),
- new SugarParameter("@StartTime", startedAt));
- return await _db.Ado.GetLongAsync(
- "SELECT id FROM mdp_transform_run_log WHERE batch_id=@BatchId ORDER BY id DESC LIMIT 1",
- new List<SugarParameter> { new("@BatchId", batchId) });
- }
- private async Task MarkTransformRunSuccessAsync(long runLogId, DateTime startedAt, S5MdpSyncTransformResult result)
- {
- var finishedAt = DateTime.Now;
- await _db.Ado.ExecuteCommandAsync(@"
- UPDATE mdp_transform_run_log
- SET status='SUCCESS', end_time=@EndTime, duration_ms=@DurationMs,
- stage_rows=@StageRows, standard_rows=@StandardRows, dwd_rows=@DwdRows,
- summary_json=@SummaryJson, update_time=CURRENT_TIMESTAMP
- WHERE id=@Id",
- new SugarParameter("@EndTime", finishedAt),
- new SugarParameter("@DurationMs", (int)(finishedAt - startedAt).TotalMilliseconds),
- new SugarParameter("@StageRows", result.StageRows),
- new SugarParameter("@StandardRows", result.StandardRows),
- new SugarParameter("@DwdRows", result.DwdRows),
- new SugarParameter("@SummaryJson", BuildRunSummaryJson(result)),
- new SugarParameter("@Id", runLogId));
- }
- private async Task MarkTransformRunFailedAsync(long runLogId, DateTime startedAt, string message)
- {
- try
- {
- var finishedAt = DateTime.Now;
- await _db.Ado.ExecuteCommandAsync(@"
- UPDATE mdp_transform_run_log
- SET status='FAILED', end_time=@EndTime, duration_ms=@DurationMs,
- error_message=@ErrorMessage, update_time=CURRENT_TIMESTAMP
- WHERE id=@Id",
- new SugarParameter("@EndTime", finishedAt),
- new SugarParameter("@DurationMs", (int)(finishedAt - startedAt).TotalMilliseconds),
- new SugarParameter("@ErrorMessage", Truncate(message, 2000)),
- new SugarParameter("@Id", runLogId));
- }
- catch (Exception ex)
- {
- // 写库本身失败兜底:远端 MySQL 瞬断导致 MarkFailed 自身也连不上
- Console.Error.WriteLine($"[S5MdpSyncTransform] MarkTransformRunFailed write failed (runLogId={runLogId}): {ex.Message}");
- }
- }
- private static string BuildRunSummaryJson(S5MdpSyncTransformResult r)
- {
- var summary = new
- {
- batchId = r.BatchId,
- sourceZtid = r.SourceZtid,
- bizDate = r.BizDate.ToString("yyyy-MM-dd"),
- bizMonth = r.BizMonth,
- triggerType = r.TriggerType,
- dwdRows = r.DwdRows,
- kpiRows = r.KpiRows,
- perKpiDwdRows = r.PerKpiDwdRows,
- perKpiKpiRows = r.PerKpiKpiRows,
- denominatorStatus = r.KpiDenominatorStatus,
- tvfPeriod = $"{r.MonthlyPeriodStart:yyyy-MM-dd}~{r.MonthlyPeriodEnd:yyyy-MM-dd}"
- };
- return JsonSerializer.Serialize(summary);
- }
- private static string NormalizeTriggerType(string s) =>
- string.IsNullOrWhiteSpace(s) ? "AUTO" : s.Trim().ToUpperInvariant();
- private static string Truncate(string s, int max) =>
- string.IsNullOrEmpty(s) ? "" : (s.Length <= max ? s : s.Substring(0, max));
- }
- // ─────────────────────────────────────────────────────────────────────────────
- // Refresh 入参与结果 DTO
- // ─────────────────────────────────────────────────────────────────────────────
- public sealed class S5MdpRefreshOption
- {
- /// <summary>T8 账套(kc_tz_head.ztid);实测当前唯一账套为 pbxfxp。</summary>
- public string SourceZtid { get; set; } = "pbxfxp";
- /// <summary>日 T+1 KPI 的业务日期(默认昨天)。</summary>
- public DateTime BizDate { get; set; }
- /// <summary>月 M+1 KPI 的业务月 YYYY-MM(默认上月)。</summary>
- public string BizMonth { get; set; } = "";
- /// <summary>日 T+1 KPI 区间起(含),默认昨天 00:00。</summary>
- public DateTime DailyPeriodStart { get; set; }
- /// <summary>日 T+1 KPI 区间止(含),默认昨天 23:59:59。</summary>
- public DateTime DailyPeriodEnd { get; set; }
- /// <summary>月 M+1 KPI 区间起(含),默认上月 1 日。</summary>
- public DateTime MonthlyPeriodStart { get; set; }
- /// <summary>月 M+1 KPI 区间止(含),默认上月末日。</summary>
- public DateTime MonthlyPeriodEnd { get; set; }
- /// <summary>TVF Rep_总账_存货_V3 入参起期 YYYYMM。</summary>
- public string TvfPeriodStartYyyymm { get; set; } = "";
- /// <summary>TVF Rep_总账_存货_V3 入参止期 YYYYMM。</summary>
- public string TvfPeriodEndYyyymm { get; set; } = "";
- public static S5MdpRefreshOption Default()
- {
- var today = DateTime.Today;
- var yesterday = today.AddDays(-1);
- var lastMonth = today.AddMonths(-1);
- var monthStart = new DateTime(lastMonth.Year, lastMonth.Month, 1);
- var monthEnd = monthStart.AddMonths(1).AddDays(-1);
- return new S5MdpRefreshOption
- {
- SourceZtid = "pbxfxp",
- BizDate = yesterday,
- BizMonth = lastMonth.ToString("yyyy-MM"),
- DailyPeriodStart = yesterday,
- DailyPeriodEnd = yesterday.AddDays(1).AddSeconds(-1),
- MonthlyPeriodStart = monthStart,
- MonthlyPeriodEnd = monthEnd.AddDays(1).AddSeconds(-1),
- TvfPeriodStartYyyymm = monthStart.ToString("yyyyMM"),
- TvfPeriodEndYyyymm = monthEnd.ToString("yyyyMM")
- };
- }
- }
- public sealed class S5MdpSyncTransformResult
- {
- public string BatchId { get; set; } = "";
- public long RunLogId { get; set; }
- public string TriggerType { get; set; } = "AUTO";
- public string SourceZtid { get; set; } = "";
- public DateTime BizDate { get; set; }
- public string BizMonth { get; set; } = "";
- public DateTime DailyPeriodStart { get; set; }
- public DateTime DailyPeriodEnd { get; set; }
- public DateTime MonthlyPeriodStart { get; set; }
- public DateTime MonthlyPeriodEnd { get; set; }
- public int StageRows { get; set; }
- public int StandardRows { get; set; }
- public int DwdRows { get; set; }
- public int KpiRows { get; set; }
- public Dictionary<string, int> PerKpiDwdRows { get; } = new();
- public Dictionary<string, int> PerKpiKpiRows { get; } = new();
- public List<string> KpiDenominatorStatus { get; } = new();
- public void MergeSub(string kpiCode, KpiBuildSubResult sub)
- {
- PerKpiDwdRows[kpiCode] = sub.DwdRows;
- PerKpiKpiRows[kpiCode] = sub.KpiRows;
- DwdRows += sub.DwdRows;
- KpiRows += sub.KpiRows;
- KpiDenominatorStatus.Add($"{kpiCode}:{sub.DenominatorStatus}");
- }
- }
- public sealed class KpiBuildSubResult
- {
- public int T8Rows { get; set; }
- public int DwdRows { get; set; }
- public int KpiRows { get; set; }
- public string DenominatorStatus { get; set; } = "OK";
- }
- // ─────────────────────────────────────────────────────────────────────────────
- // T8 result set 投影类型(与方老师 SQL SELECT 列名严格一致;SqlSugar 映射)
- // ─────────────────────────────────────────────────────────────────────────────
- internal sealed class S5OnlineCycleRow
- {
- public string? code { get; set; }
- public DateTime? shtime { get; set; }
- }
- internal sealed class S5FulfillmentNumerRow
- {
- public string? lynoid { get; set; }
- public int codenum { get; set; }
- }
- internal sealed class S5FulfillmentDenomRow
- {
- public string? noid { get; set; }
- public int listnum { get; set; }
- }
- internal sealed class S5SumQtyRow
- {
- public decimal? slzx { get; set; }
- }
- internal sealed class S5CountRow
- {
- public int penum { get; set; }
- }
- internal sealed class S5InventoryTurnoverRow
- {
- public string? ckcode { get; set; }
- public string? ckname { get; set; }
- public string? code { get; set; }
- public string? cname { get; set; }
- public string? pcode { get; set; }
- public string? pname { get; set; }
- public decimal? je3 { get; set; }
- public decimal? je2 { get; set; }
- }
|