快乐的昕的电脑
2025-11-19 f3d97fe8a7072071ddb1b19e015f8a8f823a5957
Services/WomdaaManager.cs
@@ -25,12 +25,13 @@
            list = list.Where(p => p.Daa018 != null && query.OrderStatus.Contains(p.Daa018)).ToList();
        }
        //// 分组逻辑(如需分组,按刀具ID分组取第一条)
        //var vOrders = list.GroupBy(p => p.cutterId)
        //    .Select(g => g.First()).ToList();
        // 按工单ID分组,只保留每组中最早报工的一条
        list = list
            .GroupBy(p => p.ID)
            .Select(g => g.OrderBy(x => x.BgDate).First())
            .OrderBy(x => x.BgDate)
            .ToList();
        //return vOrders;
        // 如需分组可加分组逻辑,否则直接返回
        return list;
    }
@@ -97,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();
@@ -110,12 +110,77 @@
        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 = 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)
        {
            // 没有报工记录或报工时间无效,初始采集数为开工采数
            vOrderBycl.InitCjNum = vOrderBycl.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>
@@ -144,43 +209,159 @@
            .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;
        //DevMachine
        var devMachine = Db.Queryable<DevMacBycl>()
            .Where(s => s.MachineNo == VOrdrerBycl.MachineNo)
            .First();
        var sql = string.Format(
            "select FINAL_RESULT from MES_EQ_SPOT_CHECKS where department_no = 'XS0101' and EQ_NO = '{0}' and substr(SUBMIT_DATE, 1, 10) = to_char(sysdate, 'yyyy-mm-dd')",
            devMachine.UniqueBarcode);
        var sqlQuerySingle = Db.Ado.SqlQuerySingle<string>(sql);
        VOrdrerBycl.finalResult = sqlQuerySingle;
        // 查询MES_NUMERICAL_BYCL表,获取开工数采和当前数采
        var numericalList = Db.Queryable<MesNumericalBycl>()
            .Where(x => x.OrderId == VOrdrerBycl.ID)
            .OrderBy(x => x.CjTiem)
            .ToList();
        if (numericalList.Count > 0)
        {
            // 先按采集时间升序排序
            var sortedList = numericalList
                .OrderBy(x => DateTime.TryParse(x.CjTiem, out var dt) ? dt : DateTime.MinValue)
                .ToList();
            // 开工数采:最早一条
            VOrdrerBycl.StartCjNum = sortedList.First().CjNum ?? 0;
            // 当前数采:最新一条
            VOrdrerBycl.CurrentCjNum = sortedList.Last().CjNum ?? 0;
        }
        else
        {
            VOrdrerBycl.StartCjNum = 0;
            VOrdrerBycl.CurrentCjNum = 0;
        }
        // 计算当天停机次数
        //VOrdrerBycl.TodayDowntimeCount = GetTodayDowntimeCount(numericalList);
        //计算日停机次数、todayOnlineTime(总在线时长)、todayRunTime(实际运行时长)、稼动率(百分比,保留两位小数)
        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)
        {
            // 没有报工记录,初始采集数为开工时间前一条的采集数量
            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>
    /// 计算日停机次数、todayOnlineTime(总在线时长)、todayRunTime(实际运行时长)、稼动率(百分比,保留两位小数)
    /// </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>
@@ -216,39 +397,52 @@
        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)
        string billNo, string machineNo, DateTime? from, DateTime? to, int pageIndex, int pageSize)
    {
        var query = Db.Queryable<MesReporting>()
            .Where(x => x.BillNo == billNo);
            // 关联员工信息表,使用报工人编码匹配员工编码
            .LeftJoin<MesStaff>((r, s) => r.BgPerson == s.StaffNo)
            .Where((r, s) => r.BillNo == billNo);
        if (!string.IsNullOrWhiteSpace(machineNo))
            query = query.Where(x => x.MachineNo == machineNo);
            query = query.Where((r, s) => r.MachineNo == machineNo);
        if (from.HasValue)
            query = query.Where(x => x.BgDate >= from.Value);
            query = query.Where((r, s) => r.BgDate >= from.Value);
        if (to.HasValue)
            query = query.Where(x => x.BgDate < to.Value.AddDays(1));
            query = query.Where((r, s) => r.BgDate < to.Value.AddDays(1));
        var totalCount = query.Count();
        var tbBillList = query.OrderBy(x => x.BgDate, OrderByType.Desc)
            .Select(x => new MesReportingBgDto
        var tbBillList = query.OrderBy((r, s) => r.BgDate, OrderByType.Desc)
            .Select((r, s) => new MesReportingBgDto
            {
                Id = (long)x.Id,
                BillNo = x.BillNo,
                ItemNo = x.ItemNo,
                MachineNo = x.MachineNo,
                StaffNo = x.BgPerson,//报工人编号
                StaffName = x.BgPerson, // 如有独立姓名字段可替换
                BgDate = x.BgDate,//报工时间
                CsQty = SqlFunc.ToInt32(x.CsQty),//初始采集数
                CjQty = SqlFunc.ToInt32(x.CjQty),//报工时采集数
                BfQty = SqlFunc.ToInt32(x.BfQty),//不良数量
                OkQty = SqlFunc.ToInt32(x.OkQty)//良品数量
                Id = (long)r.Id,
                BillNo = r.BillNo,
                ItemNo = r.ItemNo,
                MachineNo = r.MachineNo,
                StaffNo = r.BgPerson,//报工人编号
                StaffName = SqlFunc.IsNullOrEmpty(s.StaffName) ? r.BgPerson : s.StaffName, // 如有独立姓名字段可替换
                BgDate = r.BgDate,//报工时间
                CsQty = SqlFunc.ToInt32(r.CsQty),//初始采集数
                CjQty = SqlFunc.ToInt32(r.CjQty),//报工时采集数
                BfQty = SqlFunc.ToInt32(r.BfQty),//不良数量
                OkQty = SqlFunc.ToInt32(r.OkQty)//良品数量
            })
            .ToPageList((pageIndex < 1 ? 1 : pageIndex), (pageSize < 1 ? 200 : (pageSize > 1000 ? 1000 : pageSize)), ref totalCount);
            .ToPageList((pageIndex < 1 ? 1 : pageIndex),
                (pageSize < 1 ? 200 : (pageSize > 1000 ? 1000 : pageSize)), ref totalCount);
        return (tbBillList, totalCount);
    }