From a8f45fa02822529485ea0bf08ee1d5d5d1a35e15 Mon Sep 17 00:00:00 2001
From: 快乐的昕的电脑 <快乐的昕的电脑@DESKTOP-C2BQPQU>
Date: 星期五, 21 十一月 2025 15:34:20 +0800
Subject: [PATCH] 更换提示顺序

---
 Services/WomdaaManager.cs |  240 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 220 insertions(+), 20 deletions(-)

diff --git a/Services/WomdaaManager.cs b/Services/WomdaaManager.cs
index c3b7359..eb09f37 100644
--- a/Services/WomdaaManager.cs
+++ b/Services/WomdaaManager.cs
@@ -25,6 +25,13 @@
             list = list.Where(p => p.Daa018 != null && query.OrderStatus.Contains(p.Daa018)).ToList();
         }
 
+        // 鎸夊伐鍗旾D鍒嗙粍锛屽彧淇濈暀姣忕粍涓渶鏃╂姤宸ョ殑涓�鏉�
+        list = list
+            .GroupBy(p => p.ID)
+            .Select(g => g.OrderBy(x => x.BgDate).First())
+            .OrderBy(x => x.BgDate)
+            .ToList();
+
         return list;
     }
 
@@ -91,8 +98,7 @@
     {
         // 鏌ヨ闇�瑕佹樉绀虹殑宸ュ崟ID
         var ids = Db.Queryable<MesOrderSelect>()
-            .Where(b => b.MachineNo == query.machineNo
-                        && b.IsShow == 0)
+            .Where(b => b.MachineNo == query.machineNo && b.IsShow == 0)
             .Select(s => s.OrderId)
             .ToList();
 
@@ -104,12 +110,88 @@
         if (decimals.Count == 0)
             return new List<VOrderBycl>();
 
-        // 鏌ヨ宸ュ崟瑙嗗浘锛岀瓫閫夊嚭闇�瑕佹樉绀虹殑宸ュ崟
-        return Db.Queryable<VOrderBycl>()
+        // 鏌ヨ榻胯疆鏁伴噰琛�
+        var numericalList = Db.Queryable<MesNumericalBycl>()
+            .Where(x => ids.Contains(x.OrderId ?? 0))
+            .OrderBy(x => x.CjTiem)
+            .ToList();
+
+        var vOrderBycl = new VOrderBycl();
+        if (numericalList.Count > 0)
+        {
+            // 鎸夐噰闆嗘椂闂村崌搴忔帓搴�
+            var sortedList = numericalList
+                .OrderBy(x => DateTime.TryParse(x.CjTiem, out var dt) ? dt : DateTime.MinValue)
+                .ToList();
+
+            // 寮�宸ユ暟閲囷細鏈�鏃╀竴鏉�
+            vOrderBycl.StartCjNum = vOrderBycl.preStartCjNum ?? (sortedList.First().CjNum ?? 0);
+            // 褰撳墠鏁伴噰锛氭渶鏂颁竴鏉�
+            vOrderBycl.CurrentCjNum = sortedList.Last().CjNum ?? 0;
+        }
+        else
+        {
+            vOrderBycl.StartCjNum = 0;
+            vOrderBycl.CurrentCjNum = 0;
+        }
+
+        // 鏌ヨ鎶ュ伐璁板綍锛岃幏鍙栨渶鍚庝竴鏉℃姤宸ヨ褰�
+        var lastReport = Db.Queryable<MesReporting>()
+            .AS("MES_REPORTING") // 寮哄埗琛ㄥ悕
+            .Where(r => r.BillNo == query.orderNo)
+            .OrderBy(r => r.BgDate, OrderByType.Desc)
+            .First();
+
+        if (lastReport == null || !lastReport.BgDate.HasValue)
+        {
+            if (string.IsNullOrEmpty(vOrderBycl.Daa016)) // 娌℃湁寮�宸ユ椂闂�
+            {
+                // 娌℃湁鎶ュ伐璁板綍涓旀病鏈夊紑宸ユ椂闂达紝鍒濆閲囬泦鏁颁负璋冩満鏃堕棿鍓嶄竴鏉$殑閲囬泦鏁伴噺
+                vOrderBycl.InitCjNum = vOrderBycl.preStartTjNum;
+            }
+            else
+            {
+                // 娌℃湁鎶ュ伐璁板綍锛屽垵濮嬮噰闆嗘暟涓哄紑宸ユ椂闂村墠涓�鏉$殑閲囬泦鏁伴噺
+                vOrderBycl.InitCjNum = vOrderBycl.preStartCjNum;
+            }
+
+            //// 娌℃湁鎶ュ伐璁板綍锛屽垵濮嬮噰闆嗘暟涓哄紑宸ラ噰鏁�
+            //VOrdrerBycl.InitCjNum = VOrdrerBycl.StartCjNum;
+        }
+        else
+        {
+            // 鏈夋姤宸ヨ褰曪紝鍒濆閲囬泦鏁颁负鎶ュ伐鏃堕棿鐐瑰墠锛堝惈锛夋渶杩戜竴鏉¢娇杞暟閲囪〃鐨勯噰闆嗘暟閲�
+            var beforeOrEqualReportNum = numericalList
+                .Select(x => new { Item = x, ParsedTime = DateTime.TryParse(x.CjTiem, out var dt) ? dt : (DateTime?)null })
+                .Where(x => x.ParsedTime.HasValue && x.ParsedTime.Value <= lastReport.BgDate.Value)
+                .OrderByDescending(x => x.ParsedTime) // 闄嶅簭锛屽彇绗竴鏉�
+                .Select(x => x.Item)
+                .FirstOrDefault();
+
+            vOrderBycl.InitCjNum = beforeOrEqualReportNum?.CjNum ?? vOrderBycl.StartCjNum;
+        }
+
+        var startCjNum = vOrderBycl.StartCjNum;
+        var currentCjNum = vOrderBycl.CurrentCjNum;
+        var initCjNum = vOrderBycl.InitCjNum;
+
+        // 鏌ヨ宸ュ崟瑙嗗浘锛岀瓫閫夊嚭闇�瑕佹樉绀虹殑宸ュ崟锛屽彧鍙栨渶鏂颁竴鏉�
+        var orders = Db.Queryable<VOrderBycl>()
             .Where(c =>
                 decimals.Contains(c.ID) &&
                 (c.EditDate == date || c.EditDate == null))
+            .OrderBy(c => c.BgDate, OrderByType.Desc)
+            .Take(1)
             .ToList();
+
+        foreach (var order in orders)
+        {
+            order.StartCjNum = startCjNum;
+            order.CurrentCjNum = currentCjNum;
+            order.InitCjNum = initCjNum;
+        }
+
+        return orders;
     }
 
     /// <summary>
@@ -138,24 +220,14 @@
             .Where(a => a.OrderId == VOrdrerBycl.ID
                         && a.IsShow == 0).First();
 
-        VOrdrerBycl.todayRunTime = 0;
-        VOrdrerBycl.TodayFaultNum = 0;
+        // 淇濈暀瑙嗗浘閲岀殑 todayRunTime 鍜� JDL锛屼笉鍋氫换浣曡祴鍊煎拰杞崲
+        // VOrdrerBycl.todayRunTime 淇濇寔鍘熷��
+        // VOrdrerBycl.JDL 淇濇寔鍘熷��
+        //VOrdrerBycl.TodayFaultNum = 0;
 
         if (mesOrderSelect != null)
         {
-            VOrdrerBycl.todayRunTime = mesOrderSelect.TjTime;
             VOrdrerBycl.TodayFaultNum = mesOrderSelect.TjCount;
-
-            if (mesOrderSelect.TjTime is null or 0)
-            {
-                VOrdrerBycl.JDL = 100;
-            }
-            else
-            {
-                var round = Math.Round(mesOrderSelect.TjTime.Value / 1440, 2);
-                var round1 = (1 - round) * 100;
-                VOrdrerBycl.JDL = round1;
-            }
         }
 
         VOrdrerBycl.wjQty = VOrdrerBycl.Daa008 - VOrdrerBycl.Daa011;
@@ -179,10 +251,15 @@
 
         if (numericalList.Count > 0)
         {
+            // 鍏堟寜閲囬泦鏃堕棿鍗囧簭鎺掑簭
+            var sortedList = numericalList
+                .OrderBy(x => DateTime.TryParse(x.CjTiem, out var dt) ? dt : DateTime.MinValue)
+                .ToList();
+
             // 寮�宸ユ暟閲囷細鏈�鏃╀竴鏉�
-            VOrdrerBycl.StartCjNum = numericalList.First().CjNum ?? 0;
+            VOrdrerBycl.StartCjNum = VOrdrerBycl.preStartCjNum ?? (sortedList.First().CjNum ?? 0);
             // 褰撳墠鏁伴噰锛氭渶鏂颁竴鏉�
-            VOrdrerBycl.CurrentCjNum = numericalList.Last().CjNum ?? 0;
+            VOrdrerBycl.CurrentCjNum = sortedList.Last().CjNum ?? 0;
         }
         else
         {
@@ -190,7 +267,120 @@
             VOrdrerBycl.CurrentCjNum = 0;
         }
 
+        // 璁$畻褰撳ぉ鍋滄満娆℃暟
+        //VOrdrerBycl.TodayDowntimeCount = GetTodayDowntimeCount(numericalList);
+
+        //璁$畻鏃ュ仠鏈烘鏁般�乼odayOnlineTime锛堟�诲湪绾挎椂闀匡級銆乼odayRunTime锛堝疄闄呰繍琛屾椂闀匡級銆佺鍔ㄧ巼锛堢櫨鍒嗘瘮锛屼繚鐣欎袱浣嶅皬鏁帮級
+        VOrdrerBycl.TodayDowntimeCount = GetTodayDowntimeStats(numericalList).downtimeCount;
+        VOrdrerBycl.TodayOnlineTime = GetTodayDowntimeStats(numericalList).todayOnlineTime;
+        VOrdrerBycl.todayRunTime = GetTodayDowntimeStats(numericalList).todayRunTime;
+        VOrdrerBycl.JDL = GetTodayDowntimeStats(numericalList).utilizationRate;
+
+        Console.WriteLine($"orderNo: [{query.orderNo}]");
+
+        // 鏌ヨ鎶ュ伐璁板綍锛岃幏鍙栨渶鍚庝竴鏉℃姤宸ヨ褰�
+        var lastReport = Db.Queryable<MesReporting>()
+            .Where(r => r.BillNo.Trim().ToUpper() == query.orderNo.Trim().ToUpper())
+            .OrderBy(r => r.BgDate, OrderByType.Desc)
+            .First();
+
+        if (lastReport == null || !lastReport.BgDate.HasValue)
+        {
+            if (string.IsNullOrEmpty(VOrdrerBycl.Daa016)) // 娌℃湁寮�宸ユ椂闂�
+            {
+                // 娌℃湁鎶ュ伐璁板綍涓旀病鏈夊紑宸ユ椂闂达紝鍒濆閲囬泦鏁颁负璋冩満鏃堕棿鍓嶄竴鏉$殑閲囬泦鏁伴噺
+                VOrdrerBycl.InitCjNum = VOrdrerBycl.preStartTjNum;
+            }
+            else
+            {
+                // 娌℃湁鎶ュ伐璁板綍锛屽垵濮嬮噰闆嗘暟涓哄紑宸ユ椂闂村墠涓�鏉$殑閲囬泦鏁伴噺
+                VOrdrerBycl.InitCjNum = VOrdrerBycl.preStartCjNum;
+            }
+
+            //// 娌℃湁鎶ュ伐璁板綍锛屽垵濮嬮噰闆嗘暟涓哄紑宸ラ噰鏁�
+            //VOrdrerBycl.InitCjNum = VOrdrerBycl.StartCjNum;
+        }
+        else
+        {
+            // 鏈夋姤宸ヨ褰曪紝鍒濆閲囬泦鏁颁负鎶ュ伐鏃堕棿鐐瑰墠锛堝惈锛夋渶杩戜竴鏉¢娇杞暟閲囪〃鐨勯噰闆嗘暟閲�
+            var beforeOrEqualReportNum = numericalList
+                .Select(x => new { Item = x, ParsedTime = DateTime.TryParse(x.CjTiem, out var dt) ? dt : (DateTime?)null })
+                .Where(x => x.ParsedTime.HasValue && x.ParsedTime.Value <= lastReport.BgDate.Value)
+                .OrderByDescending(x => x.ParsedTime) // 闄嶅簭锛屽彇绗竴鏉�
+                .Select(x => x.Item)
+                .FirstOrDefault();
+
+            VOrdrerBycl.InitCjNum = beforeOrEqualReportNum?.CjNum ?? VOrdrerBycl.StartCjNum;
+        }
+
         return VOrdrerBycl;
+    }
+
+    /// <summary>
+    /// 璁$畻鏃ュ仠鏈烘鏁�
+    /// </summary>
+    /// <param name="numericalList"></param>
+    /// <returns></returns>
+    public int GetTodayDowntimeCount(List<MesNumericalBycl> numericalList)
+    {
+        var today = DateTime.Now.Date;
+        // 1. 鍙繚鐣欏綋澶╂暟鎹紝骞惰浆鎹负 DateTime
+        var todayTimes = numericalList
+            .Where(x => DateTime.TryParse(x.CjTiem, out var dt) && dt.Date == today)
+            .Select(x => DateTime.Parse(x.CjTiem))
+            .OrderBy(x => x)
+            .ToList();
+
+        if (todayTimes.Count < 2)
+            return 0;
+
+        int downtimeCount = 0;
+        for (int i = 1; i < todayTimes.Count; i++)
+        {
+            var diff = (todayTimes[i] - todayTimes[i - 1]).TotalMinutes;
+            if (diff > 3)
+                downtimeCount++;
+        }
+        return downtimeCount;
+    }
+
+    /// <summary>
+    /// 璁$畻鏃ュ仠鏈烘鏁般�乼odayOnlineTime锛堟�诲湪绾挎椂闀匡級銆乼odayRunTime锛堝疄闄呰繍琛屾椂闀匡級銆佺鍔ㄧ巼锛堢櫨鍒嗘瘮锛屼繚鐣欎袱浣嶅皬鏁帮級
+    /// </summary>
+    public (int downtimeCount, double todayOnlineTime, double todayRunTime, double utilizationRate) GetTodayDowntimeStats(List<MesNumericalBycl> numericalList)
+    {
+        var today = DateTime.Now.Date;
+        var todayTimes = numericalList
+            .Where(x => DateTime.TryParse(x.CjTiem, out var dt) && dt.Date == today)
+            .Select(x => DateTime.Parse(x.CjTiem))
+            .OrderBy(x => x)
+            .ToList();
+
+        int downtimeCount = 0;
+        double downtimeMinutes = 0;
+        double todayOnlineTime = 0;
+        double todayRunTime = 0;
+        double utilizationRate = 0;
+
+        if (todayTimes.Count < 2)
+            return (0, 0, 0, 0);
+
+        for (int i = 1; i < todayTimes.Count; i++)
+        {
+            var diff = (todayTimes[i] - todayTimes[i - 1]).TotalMinutes;
+            if (diff > 3)
+            {
+                downtimeCount++;
+                downtimeMinutes += diff;
+            }
+        }
+
+        todayOnlineTime = (todayTimes.Last() - todayTimes.First()).TotalMinutes;
+        todayRunTime = todayOnlineTime - downtimeMinutes;
+        if (todayOnlineTime > 0)
+            utilizationRate = Math.Round(todayRunTime / todayOnlineTime * 100, 2);
+
+        return (downtimeCount, todayOnlineTime, todayRunTime, utilizationRate);
     }
 
     /// <summary>
@@ -226,6 +416,16 @@
         return vPrint;
     }
 
+    /// <summary>
+    /// 鎶ュ伐璁板綍琛�
+    /// </summary>
+    /// <param name="billNo"></param>
+    /// <param name="machineNo"></param>
+    /// <param name="from"></param>
+    /// <param name="to"></param>
+    /// <param name="pageIndex"></param>
+    /// <param name="pageSize"></param>
+    /// <returns></returns>
     public (List<MesReportingBgDto> tbBillList, int totalCount) GetByBillNoBG(
         string billNo, string machineNo, DateTime? from, DateTime? to, int pageIndex, int pageSize)
     {

--
Gitblit v1.9.3