using GB5Shared.DTO.Framework.Login; using MMBLL.Inspection; using MMDAL.CustomCode.Inspection; using MMDAL.DTO.Inspection; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace MMBLL.InspectionAnalytics { public class InspectionAnalyticsBLL : IInspectionAnalyticsBLL { private readonly IInspectionAnalyticsDAL _dal; private readonly IInspectionParameterComputationBLL _paramComp; public InspectionAnalyticsBLL(IInspectionAnalyticsDAL dal, IInspectionParameterComputationBLL paramComp) { _dal = dal; _paramComp = paramComp; } public async Task GetKpi(int ouId, int periodId, LoginDTO loginDTO, CancellationToken ct) { try { return await _dal.GetKpi(ouId, periodId, loginDTO, ct).ConfigureAwait(false); } catch (Exception) { throw; } } public async Task GetTrend(int ouId, int months, LoginDTO loginDTO, CancellationToken ct) { try { return await _dal.GetTrend(ouId, months, loginDTO, ct).ConfigureAwait(false); } catch (Exception) { throw; } } public async Task GetDefectPareto(int ouId, int periodId, LoginDTO loginDTO, CancellationToken ct) { try { var json = await _dal.GetDefectPareto(ouId, periodId, loginDTO, ct).ConfigureAwait(false); var rows = JsonConvert.DeserializeObject>(json) ?? new List(); if (rows.Count == 0) return json; double total = rows.Sum(r => r.TotalCount); double running = 0; foreach (var row in rows) { running += row.TotalCount; row.CumulativePct = total > 0 ? Math.Round(running * 100.0 / total, 2) : 0; } return JsonConvert.SerializeObject(rows); } catch (Exception) { throw; } } public async Task GetProcessYield(int ouId, int periodId, LoginDTO loginDTO, CancellationToken ct) { try { var json = await _dal.GetProcessYield(ouId, periodId, loginDTO, ct).ConfigureAwait(false); var rows = JsonConvert.DeserializeObject>(json) ?? new List(); foreach (var row in rows) row.YieldPercent = row.PiecesQuantity > 0 ? Math.Round(row.GoodQuantity * 100.0 / row.PiecesQuantity, 2) : 0; return JsonConvert.SerializeObject(rows); } catch (Exception) { throw; } } public async Task GetParamSummary(int inspectionId, LoginDTO loginDTO, CancellationToken ct) { try { var json = await _dal.GetParamSummary(inspectionId, loginDTO, ct).ConfigureAwait(false); var parameters = JsonConvert.DeserializeObject>(json) ?? new List(); var summary = _paramComp.BuildSummary(parameters); return JsonConvert.SerializeObject(summary); } catch (Exception) { throw; } } } }