From 4f33ac39577a3a20860e3895f748d6f10bd39b23 Mon Sep 17 00:00:00 2001
From: 快乐的昕的电脑 <快乐的昕的电脑@DESKTOP-C2BQPQU>
Date: 星期四, 04 十二月 2025 16:25:09 +0800
Subject: [PATCH] 修改注释

---
 Services/WomdaaManager.cs |  223 +++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 175 insertions(+), 48 deletions(-)

diff --git a/Services/WomdaaManager.cs b/Services/WomdaaManager.cs
index 60ed15c..f51401a 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;
     }
 
@@ -112,10 +119,15 @@
         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 = numericalList.First().CjNum ?? 0;
+            vOrderBycl.StartCjNum = vOrderBycl.preStartCjNum ?? (sortedList.First().CjNum ?? 0);
             // 褰撳墠鏁伴噰锛氭渶鏂颁竴鏉�
-            vOrderBycl.CurrentCjNum = numericalList.Last().CjNum ?? 0;
+            vOrderBycl.CurrentCjNum = sortedList.Last().CjNum ?? 0;
         }
         else
         {
@@ -125,42 +137,38 @@
 
         // 鏌ヨ鎶ュ伐璁板綍锛岃幏鍙栨渶鍚庝竴鏉℃姤宸ヨ褰�
         var lastReport = Db.Queryable<MesReporting>()
-            .Where(r => r.BillNo != null && r.BillNo.Contains(query.orderNo))
-            .OrderBy(r => r.Id, OrderByType.Desc)
+            .AS("MES_REPORTING") // 寮哄埗琛ㄥ悕
+            .Where(r => r.BillNo == query.orderNo)
+            .OrderBy(r => r.BgDate, OrderByType.Desc)
             .First();
 
         if (lastReport == null || !lastReport.BgDate.HasValue)
         {
-            // 娌℃湁鎶ュ伐璁板綍鎴栨姤宸ユ椂闂存棤鏁堬紝鍒濆閲囬泦鏁颁负寮�宸ラ噰鏁�
-            vOrderBycl.InitCjNum = vOrderBycl.StartCjNum;
-        }
-        else
-        {
-            // 鏈夋姤宸ヨ褰曪紝鍒濆閲囬泦鏁颁负涓婁竴娆℃姤宸ユ椂闂村悗鏈�杩戜竴鏉¢娇杞暟閲囪〃鐨勯噰闆嗘暟閲�
-            // 1. 绮剧‘鍖归厤鎶ュ伐鏃堕棿
-            var exactMatch = 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)
-                .OrderBy(x => x.ParsedTime)
-                .Select(x => x.Item)
-                .FirstOrDefault();
-
-            if (exactMatch != null)
+            if (string.IsNullOrEmpty(vOrderBycl.Daa016)) // 娌℃湁寮�宸ユ椂闂�
             {
-                vOrderBycl.InitCjNum = exactMatch.CjNum ?? vOrderBycl.StartCjNum;
+                // 娌℃湁鎶ュ伐璁板綍涓旀病鏈夊紑宸ユ椂闂达紝鍒濆閲囬泦鏁颁负璋冩満鏃堕棿鍓嶄竴鏉$殑閲囬泦鏁伴噺
+                vOrderBycl.InitCjNum = vOrderBycl.preStartTjNum;
             }
             else
             {
-                // 2. 鎵炬姤宸ユ椂闂翠箣鍚庣殑绗竴鏉�
-                var afterReportNum = 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)
-                    .OrderBy(x => x.ParsedTime)
-                    .Select(x => x.Item)
-                    .FirstOrDefault();
-
-                vOrderBycl.InitCjNum = afterReportNum?.CjNum ?? vOrderBycl.StartCjNum;
+                // 娌℃湁鎶ュ伐璁板綍锛屽垵濮嬮噰闆嗘暟涓哄紑宸ユ椂闂村墠涓�鏉$殑閲囬泦鏁伴噺
+                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;
@@ -172,8 +180,8 @@
             .Where(c =>
                 decimals.Contains(c.ID) &&
                 (c.EditDate == date || c.EditDate == null))
-            .OrderBy(c => c.EditDate, OrderByType.Desc)
-            //.Take(1)
+            .OrderBy(c => c.BgDate, OrderByType.Desc)
+            .Take(1)
             .ToList();
 
         foreach (var order in orders)
@@ -212,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;
@@ -253,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
         {
@@ -264,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>
@@ -300,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)
     {
@@ -332,7 +458,8 @@
                 CsQty = SqlFunc.ToInt32(r.CsQty),//鍒濆閲囬泦鏁�
                 CjQty = SqlFunc.ToInt32(r.CjQty),//鎶ュ伐鏃堕噰闆嗘暟
                 BfQty = SqlFunc.ToInt32(r.BfQty),//涓嶈壇鏁伴噺
-                OkQty = SqlFunc.ToInt32(r.OkQty)//鑹搧鏁伴噺
+                OkQty = SqlFunc.ToInt32(r.OkQty),//鑹搧鏁伴噺
+                REMARK = r.Remark //澶囨敞
             })
             .ToPageList((pageIndex < 1 ? 1 : pageIndex),
                 (pageSize < 1 ? 200 : (pageSize > 1000 ? 1000 : pageSize)), ref totalCount);

--
Gitblit v1.9.3