liuhairui 10 luni în urmă
părinte
comite
76bbbf95a0
1 a modificat fișierele cu 292 adăugiri și 68 ștergeri
  1. 292 68
      application/api/controller/Achievementatestatistics.php

+ 292 - 68
application/api/controller/Achievementatestatistics.php

@@ -53,9 +53,6 @@ class Achievementatestatistics extends Api
     }
 
 
-    /**
-     * 1.2达成率统计->机台列表
-     */
     public function Machine_List()
     {
         if (!$this->request->isGet()) {
@@ -67,17 +64,16 @@ class Achievementatestatistics extends Api
             $this->error('缺少参数 Machine');
         }
 
-        // 最近 7 天日期(含今天)
+        // 日期映射:近 7 天
         $dateMap = [];
-        $recentDates = []; // 用来存放最近七天的日期
+        $recentDates = [];
         for ($i = 0; $i < 7; $i++) {
             $date = date('Y-m-d', strtotime("-{$i} days"));
             $label = date('m月d日', strtotime($date));
             $dateMap[$date] = $label;
-            $recentDates[] = $date; // 记录日期
+            $recentDates[] = $date;
         }
 
-        // 查设备
         $devices = Db::name('设备_基本资料')
             ->field('设备编号, 设备名称')
             ->whereLike('使用部门', '%' . $param['Machine'] . '%')
@@ -89,82 +85,125 @@ class Achievementatestatistics extends Api
             $jtbh = $device['设备编号'];
             $jtname = trim($device['设备名称']);
 
-            // 查询上报数据(产量+工单+印件号+工序号)
+            // 获取原始产量数据
             $records = Db::name('设备_产量计酬')
-                ->field('sczl_bzdh as 班组, sczl_cl as 实际产量, sczl_rq as 日期, sczl_gdbh, sczl_yjno, sczl_gxh')
+                ->field([
+                    'sczl_bzdh as 班组',
+                    'sczl_cl',
+                    'sczl_Pgcl',
+                    'sczl_rq as 日期',
+                    'sczl_gdbh',
+                    'sczl_yjno',
+                    'sczl_gxh'
+                ])
                 ->where('sczl_jtbh', $jtbh)
                 ->where('sczl_rq', '>=', date('Y-m-d 00:00:00', strtotime('-6 days')))
                 ->where('sczl_rq', '<=', date('Y-m-d 23:59:59'))
                 ->select();
 
-            // 数据结构:班组 → 日期 → 实际产量 + 工单明细
-            $bzData = [];
+            $stat = [];
+            $summary = [];
 
             foreach ($records as $row) {
                 $bz = $row['班组'] ?: '未分组';
                 $rq = date('Y-m-d', strtotime($row['日期']));
-                if (!isset($bzData[$bz][$rq])) {
-                    $bzData[$bz][$rq] = [
-                        '实际产量' => 0,
-                        '排产产量' => 0,
-                        '工单明细' => []
-                    ];
-                }
-
-                $bzData[$bz][$rq]['实际产量'] += floatval($row['实际产量']);
-
-                // 收集唯一组合键用于查询计划数
-                $key = $row['sczl_gdbh'] . '_' . $row['sczl_yjno'] . '_' . $row['sczl_gxh'];
-                if (!in_array($key, $bzData[$bz][$rq]['工单明细'])) {
-                    $bzData[$bz][$rq]['工单明细'][] = $key;
+                $cl = floatval($row['sczl_cl']);
+                $pgcl = floatval($row['sczl_Pgcl']);
+                $actual = ($cl == 0) ? 0 : (($pgcl > 0) ? $cl * $pgcl : $cl);
+
+                // 查询排产产量
+                $plan = Db::name('工单_工艺资料')
+                    ->where('Gy0_gdbh', $row['sczl_gdbh'])
+                    ->where('Gy0_yjno', $row['sczl_yjno'])
+                    ->where('Gy0_gxh', $row['sczl_gxh'])
+                    ->value('Gy0_计划接货数');
+                $plan = floatval($plan);
+
+                // 班组维度统计
+                if (!isset($stat[$bz][$rq])) {
+                    $stat[$bz][$rq] = ['实际产量' => 0, '排产产量' => 0];
                 }
-            }
+                $stat[$bz][$rq]['实际产量'] += $actual;
+                $stat[$bz][$rq]['排产产量'] += $plan;
 
-            // 查询排产产量(计划接货数)
-            foreach ($bzData as $bz => &$dateList) {
-                foreach ($dateList as $rq => &$info) {
-                    $totalPlan = 0;
-                    foreach ($info['工单明细'] as $key) {
-                        list($gdbh, $yjno, $gxh) = explode('_', $key);
-                        $plan = Db::name('工单_工艺资料')
-                            ->where('Gy0_gdbh', $gdbh)
-                            ->where('Gy0_yjno', $yjno)
-                            ->where('Gy0_gxh', $gxh)
-                            ->value('Gy0_计划接货数');
-
-                        $totalPlan += floatval($plan);
-                    }
-                    $info['排产产量'] = $totalPlan;
+                // 合计维度统计
+                if (!isset($summary[$rq])) {
+                    $summary[$rq] = ['实际产量' => 0, '排产产量' => 0];
                 }
+                $summary[$rq]['实际产量'] += $actual;
+                $summary[$rq]['排产产量'] += $plan;
             }
 
-            // 输出结构:每个机台 + 班组一条
-            foreach ($bzData as $bz => $dateList) {
+            // 输出每个班组
+            foreach ($stat as $bz => $dateList) {
                 $item = [
                     '机台编号' => $jtbh,
                     '机台名称' => $jtname,
                     '班组' => $bz,
                     '实际总产量' => 0,
-                    '排产总产量' => 0
+                    '排产总产量' => 0,
+                    '近7天综合达成率' => '0%'
                 ];
 
                 $i = 1;
                 foreach ($dateMap as $date => $label) {
-                    $sl = isset($dateList[$date]) ? $dateList[$date]['实际产量'] : 0;
-                    $jh = isset($dateList[$date]) ? $dateList[$date]['排产产量'] : 0;
-                    $rate = $jh > 0 ? round($sl / $jh * 100, 2) . '%' : '0%';
+                    $actual = isset($dateList[$date]) ? round($dateList[$date]['实际产量'], 2) : 0;
+                    $plan = isset($dateList[$date]) ? round($dateList[$date]['排产产量'], 2) : 0;
+
+                    $rate = ($plan > 0) ? round($actual / $plan * 100, 2) . '%' : '0%';
 
                     $item[$label] = $rate;
-                    $item["实际产量{$i}"] = $sl;
-                    $item["排产产量{$i}"] = $jh;
-                    $item['实际总产量'] += $sl;
-                    $item['排产总产量'] += $jh;
+                    $item["实际产量{$i}"] = $actual;
+                    $item["排产产量{$i}"] = $plan;
+
+                    $item['实际总产量'] += $actual;
+                    $item['排产总产量'] += $plan;
                     $i++;
                 }
 
+                $item['近7天综合达成率'] = ($item['排产总产量'] > 0)
+                    ? round($item['实际总产量'] / $item['排产总产量'] * 100, 2) . '%'
+                    : '0%';
+
                 $results[] = $item;
             }
+
+            // 合计行
+            $item = [
+                '机台编号' => $jtbh,
+                '机台名称' => '合计',
+                '班组' => '',
+                '实际总产量' => 0,
+                '排产总产量' => 0,
+                '近7天综合达成率' => '0%'
+            ];
+
+            $i = 1;
+            foreach ($dateMap as $date => $label) {
+                $actual = isset($summary[$date]) ? round($summary[$date]['实际产量'], 2) : 0;
+                $plan = isset($summary[$date]) ? round($summary[$date]['排产产量'], 2) : 0;
+
+                // 重新计算达成率,不使用之前累加的百分比
+                $rate = ($plan > 0) ? round($actual / $plan * 100, 2) . '%' : '0%';
+
+                $item[$label] = $rate;
+                $item["实际产量{$i}"] = $actual;
+                $item["排产产量{$i}"] = $plan;
+
+                $item['实际总产量'] += $actual;
+                $item['排产总产量'] += $plan;
+                $i++;
+            }
+
+// 修正“近7天综合达成率”的计算
+            $item['近7天综合达成率'] = ($item['排产总产量'] > 0)
+                ? round($item['实际总产量'] / $item['排产总产量'] * 100, 2) . '%'
+                : '0%';
+
+            $results[] = $item;
+
         }
+
         $this->success('成功', [
             'data' => $results,
             'list' => $recentDates
@@ -181,7 +220,7 @@ class Achievementatestatistics extends Api
         }
         $param = $this->request->param();
         if (empty($param['jtbh']) || empty($param['bz'])) {
-            $this->error('缺少参数:jtbh(机台编号)或 bz(班组)');
+//            $this->error('缺少参数:jtbh(机台编号)或 bz(班组)');
         }
         $jtbh = $param['jtbh'];
         $bz = $param['bz'];
@@ -274,24 +313,29 @@ class Achievementatestatistics extends Api
                 'sczl_rq',
                 'sczl_cl',
                 'sczl_ls',
+                'sczl_rq',
                 'sczl_装版总工时 as 装板实际工时',
                 'sczl_装版工时 as 装板补产工时',
                 'sczl_保养工时 as 保养工时',
                 'sczl_打样总工时 as 打样总工时',
                 'sczl_打样工时 as 打样补产工时',
                 'sczl_异常停机工时 as 异常总工时',
-                'sczl_异常工时1 as 异常补时'
+                'sczl_异常工时1 as 异常补时',
+                'sczl_设备运行工时 as 运行工时'
             ])
             ->whereIn('sczl_jtbh', $devices)
             ->where('sczl_rq', 'like', $ym . '%')
             ->select();
 
-        // 分组汇总:按“机台编号 + 班组”
+        $小时产能 = 50000;
+
+        // 分组汇总:按 机台编号 + 班组
         $resultList = [];
         foreach ($records as $row) {
             $jtbh = $row['机台编号'];
             $bz = $row['班组'] ?: '未分组';
             $key = $jtbh . '|' . $bz;
+
             if (!isset($resultList[$key])) {
                 $resultList[$key] = [
                     '机台编号' => $jtbh,
@@ -303,10 +347,16 @@ class Achievementatestatistics extends Api
                     '打样总工时' => 0,
                     '打样补产工时' => 0,
                     '异常总工时' => 0,
-                    '异常补时' => 0
+                    '异常补时' => 0,
+                    '运行工时' => 0,
+                    'sczl_rq' => date('Ym', strtotime($row['sczl_rq'])),
+                    // 新增字段
+                    '目标产量' => 0,
+                    '负荷产量' => 0,
                 ];
             }
-            //汇总逻辑
+
+            // 累加字段
             $resultList[$key]['实际产量'] += floatval($row['sczl_cl']);
             $resultList[$key]['装板实际工时'] += floatval($row['装板实际工时']);
             $resultList[$key]['装板补产工时'] += floatval($row['装板补产工时']);
@@ -315,26 +365,198 @@ class Achievementatestatistics extends Api
             $resultList[$key]['打样补产工时'] += floatval($row['打样补产工时']);
             $resultList[$key]['异常总工时'] += floatval($row['异常总工时']);
             $resultList[$key]['异常补时'] += floatval($row['异常补时']);
+            $resultList[$key]['运行工时'] += floatval($row['运行工时']);
+        }
+
+        // 汇总并计算目标/负荷产量及达成率
+        $grouped = []; // [机台编号 => [班组数据...]]
+        foreach ($resultList as $item) {
+            $jtbh = $item['机台编号'];
+            $grouped[$jtbh][] = $item;
+        }
+
+        $finalList = [];
+        foreach ($grouped as $jtbh => $rows) {
+            $sum = [
+                '机台编号' => $jtbh,
+                '班组' => '合计',
+                '实际产量' => 0,
+                '装板实际工时' => 0,
+                '装板补产工时' => 0,
+                '保养工时' => 0,
+                '打样总工时' => 0,
+                '打样补产工时' => 0,
+                '异常总工时' => 0,
+                '异常补时' => 0,
+                '运行工时' => 0,
+                '目标产量' => 0,
+                '负荷产量' => 0,
+                'sczl_rq' => $rows[0]['sczl_rq'] ?? '',
+            ];
+
+            foreach ($rows as &$row) {
+                // 计算目标 & 负荷产量
+                $有效工时 = max(0, $row['运行工时'] - $row['保养工时'] - $row['装板补产工时'] - $row['异常总工时']);
+                $row['目标产量'] = round($有效工时 * $小时产能, 2);
+                $row['负荷产量'] = $row['目标产量'];
+
+                // 计算达成率 & 利用率
+                $row['目标达成'] = ($row['目标产量'] > 0)
+                    ? round($row['实际产量'] / $row['目标产量'] * 100, 2) . '%'
+                    : '0%';
+
+                $row['综合利用率'] = ($row['负荷产量'] > 0)
+                    ? round($row['实际产量'] / $row['负荷产量'] * 100, 2) . '%'
+                    : '0%';
+
+                $finalList[] = $row;
+
+                // 合计累加
+                $sum['实际产量'] += $row['实际产量'];
+                $sum['装板实际工时'] += $row['装板实际工时'];
+                $sum['装板补产工时'] += $row['装板补产工时'];
+                $sum['保养工时'] += $row['保养工时'];
+                $sum['打样总工时'] += $row['打样总工时'];
+                $sum['打样补产工时'] += $row['打样补产工时'];
+                $sum['异常总工时'] += $row['异常总工时'];
+                $sum['异常补时'] += $row['异常补时'];
+                $sum['运行工时'] += $row['运行工时'];
+                $sum['目标产量'] += $row['目标产量'];
+                $sum['负荷产量'] += $row['负荷产量'];
+            }
+
+            // 合计达成率
+            $sum['目标达成'] = ($sum['目标产量'] > 0)
+                ? round($sum['实际产量'] / $sum['目标产量'] * 100, 2) . '%'
+                : '0%';
+
+            $sum['综合利用率'] = ($sum['负荷产量'] > 0)
+                ? round($sum['实际产量'] / $sum['负荷产量'] * 100, 2) . '%'
+                : '0%';
+
+            $finalList[] = $sum;
         }
-        $this->success('汇总成功', array_values($resultList));
+
+        $this->success('汇总成功', $finalList);
     }
 
     /**
-     * 1.5达成率统计->按日期机台生产详情
+     * 1.5达成率统计 -> 按日期+工单统计生产明细
      */
     public function Machine_production_details()
     {
         if (!$this->request->isGet()) {
             $this->error('请求方式错误');
         }
+
         $param = $this->request->param();
-        if (empty($param['jtbh']) || empty($param['bz'])) {
-            $this->error('缺少参数:jtbh(机台编号)或 bz(班组)');
+
+        if (empty($param['jtbh']) || empty($param['rq'])) {
+            $this->error('缺少必要参数:jtbh(机台编号)、rq(年月)');
         }
+
         $jtbh = $param['jtbh'];
-        $bz = $param['bz'];
+        $rq = $param['rq'];
+        $ym = substr($rq, 0, 4) . '-' . substr($rq, 4, 2);
 
-        // 查询近7天该机台该班组的产量明细
+        // 查询该月份内该机台的每日工单产量明细
+        $records = Db::name('设备_产量计酬')->alias('a')
+            ->field([
+                'a.sczl_jtbh as 机台编号',
+                'a.sczl_rq as 日期',
+                'a.sczl_bzdh as 班组',
+                'a.sczl_gdbh as 工单编号',
+                'b.yj_yjmc as 印件名称',
+                'a.sczl_yjno as 印件号',
+                'a.sczl_gxh as 工序号',
+                'a.sczl_gxmc as 工序名称',
+                'a.sczl_ls',
+                'SUM(CASE 
+                     WHEN a.sczl_Pgcl IS NOT NULL AND a.sczl_Pgcl != 0 
+                     THEN a.sczl_cl * a.sczl_Pgcl 
+                     ELSE a.sczl_cl 
+                 END) as 实际产量',
+                'SUM(sczl_装版总工时) as 装版实际工时',
+                'SUM(sczl_装版工时) as 装版补产工时',
+                'SUM(sczl_保养工时) as 保养工时',
+                'SUM(sczl_打样总工时) as 打样总工时',
+                'SUM(sczl_打样工时) as 打样补产工时',
+                'SUM(sczl_异常停机工时) as 异常总工时',
+                'SUM(sczl_异常工时1) as 异常补时',
+                'SUM(sczl_设备运行工时) as 运行工时'
+            ])
+            ->join('工单_印件资料 b', 'a.sczl_gdbh = b.Yj_Gdbh', 'LEFT')
+            ->where('a.sczl_jtbh', $jtbh)
+            ->whereLike('a.sczl_rq', $ym . '%')
+            ->group('a.sczl_rq, a.sczl_gdbh')
+            ->order('a.sczl_rq desc')
+            ->select();
+
+        // 数据格式化与目标产量计算
+        foreach ($records as &$row) {
+            $row['日期'] = date('Y-m-d', strtotime($row['日期']));
+            $row['印件工序'] = $row['印件号'] . '-' . $row['工序名称'];
+            $row['小时产能'] = 50000;
+
+            // 计算目标产量
+            $row['目标产量'] = round(
+                max(0, $row['运行工时'] - $row['保养工时'] - $row['装版补产工时'] - $row['异常总工时']) * 50000,
+                2
+            );
+
+            // 计算负荷产量
+            $row['负荷产量'] = round(
+                max(0, $row['运行工时'] - $row['保养工时'] - $row['装版补产工时'] - $row['异常总工时']) * 50000,
+                2
+            );
+
+            // 计算目标达成率 (避免除以0)
+            if ($row['目标产量'] > 0) {
+                $row['目标达成'] = round($row['实际产量'] / $row['目标产量'] * 100, 2) . '%';
+            } else {
+                $row['目标达成'] = '0%';
+            }
+
+            // 计算总和利用率 (避免除以0)
+            if ($row['负荷产量'] > 0) {
+                $row['综合利用率'] = round($row['实际产量'] / $row['负荷产量'] * 100, 2) . '%';
+            } else {
+                $row['综合利用率'] = '0%';
+            }
+        }
+
+        $this->success('明细获取成功', $records);
+    }
+
+    /**
+     * 1.6达成率统计->按时段导出Excel
+     */
+    public function Machine_date_excel()
+    {
+        if (!$this->request->isGet()) {
+            $this->error('请求方式错误');
+        }
+
+        $param = $this->request->param();
+        if (empty($param['start_rq']) || empty($param['end_rq']) || empty($param['bm'])) {
+            $this->error('缺少参数:start_rq(开始日期)或 end_rq(结束日期)或 bm(部门)');
+        }
+
+        $start_rq = $param['start_rq'];
+        $end_rq = $param['end_rq'];
+        $bm = $param['bm'];
+
+        // 1. 先查询设备_基本资料表,获取该部门下的所有设备编号(去重)
+        $deviceIds = Db::name('设备_基本资料')
+            ->where('使用部门', $bm)
+            ->group('设备编号') // 去重
+            ->column('设备编号');
+
+        if (empty($deviceIds)) {
+            $this->error('该部门下没有设备数据');
+        }
+
+        // 2. 查询设备_产量计酬表,筛选符合条件的记录
         $records = Db::name('设备_产量计酬')->alias('a')
             ->field([
                 'a.sczl_jtbh as 机台编号',
@@ -357,20 +579,22 @@ class Achievementatestatistics extends Api
                 'sczl_设备运行工时 as 运行工时'
             ])
             ->join('工单_印件资料 b', 'a.sczl_gdbh = b.Yj_Gdbh')
-            ->where('a.sczl_jtbh', $jtbh)
-            ->where('a.sczl_bzdh', $bz)
+            ->whereIn('a.sczl_jtbh', $deviceIds) // 使用设备编号列表筛选
             ->whereBetween('a.sczl_rq', [
-                date('Y-m-d 00:00:00', strtotime('-6 days')),
-                date('Y-m-d 23:59:59')
+                $start_rq . ' 00:00:00',
+                $end_rq . ' 23:59:59'
             ])
+            ->group('a.sczl_jtbh, a.sczl_rq, a.sczl_bzdh, a.sczl_gdbh') // 按设备、日期、班组、工单分组
             ->order('a.sczl_rq desc')
             ->select();
 
+        // 格式化数据
         foreach ($records as &$row) {
             $row['日期'] = date('Y-m-d', strtotime($row['日期']));
-            $row['印件工序'] = $row['印件号'].'-'.$row['工序名称'];
-//            $row['目标产量'] =  ($row['运行工时'] - $row['保养工时'] - $row['装板补产工时'] - $row['异常总工时']) * $row['小时产能'];
+            $row['印件工序'] = $row['印件号'] . '-' . $row['工序名称'];
         }
+
         $this->success('明细获取成功', $records);
     }
+
 }