unknown 1 месяц назад
Родитель
Сommit
ae80f01536
1 измененных файлов с 219 добавлено и 14 удалено
  1. 219 14
      application/api/controller/ProductionInformationStatistics.php

+ 219 - 14
application/api/controller/ProductionInformationStatistics.php

@@ -157,6 +157,59 @@ class ProductionInformationStatistics extends Api
         $this->success('成功', $list);
         $this->success('成功', $list);
     }
     }
 
 
+    /**
+     * 2.1 任意两个日期之间的汇总数据(含填写字段与计算字段)
+     * @ApiMethod (GET)
+     * @param string start_date|start 开始日期 Y-m-d
+     * @param string end_date|end     结束日期 Y-m-d
+     */
+    public function getRangeSummary()
+    {
+        if (!$this->request->isGet()) {
+            $this->error('请求错误');
+        }
+
+        $params = $this->request->param();
+        $startDate = isset($params['start_date']) ? trim($params['start_date']) : (isset($params['start']) ? trim($params['start']) : '');
+        $endDate = isset($params['end_date']) ? trim($params['end_date']) : (isset($params['end']) ? trim($params['end']) : '');
+
+        if ($startDate === '' || $endDate === '') {
+            $this->error('请传入 start_date、end_date,格式 Y-m-d');
+        }
+
+        $startTs = strtotime($startDate);
+        $endTs = strtotime($endDate);
+        if ($startTs === false || date('Y-m-d', $startTs) !== $startDate) {
+            $this->error('开始日期格式错误,请传 Y-m-d');
+        }
+        if ($endTs === false || date('Y-m-d', $endTs) !== $endDate) {
+            $this->error('结束日期格式错误,请传 Y-m-d');
+        }
+        if ($startTs > $endTs) {
+            $this->error('开始日期不能大于结束日期');
+        }
+
+        $query = [
+            'type'       => 'range',
+            'start_date' => $startDate,
+            'end_date'   => $endDate,
+            'start'      => date('Y-m-d 00:00:00', $startTs),
+            'end'        => date('Y-m-d 23:59:59', $endTs),
+        ];
+
+        if (!$this->hasStatsInPeriod($query)) {
+            $this->error($startDate . ' 至 ' . $endDate . ' 没有数据');
+        }
+
+        $list = $this->buildDisplayList($query);
+
+        $this->success('成功', [
+            'start_date' => $startDate,
+            'end_date'   => $endDate,
+            'list'       => $list,
+        ]);
+    }
+
     /**
     /**
      * 判断指定时间段内是否存在统计数据
      * 判断指定时间段内是否存在统计数据
      */
      */
@@ -240,8 +293,10 @@ class ProductionInformationStatistics extends Api
     }
     }
 
 
     /**
     /**
-     * 3. 获取车间、机组、机台编号、机台名称、班组基础数据
+     * 3. 获取车间、机组、机台编号、机台名称、班组基础数据(新增弹窗预填)
+     * 除理论速度外,其余业务字段取自 设备_产量计酬:当前机台+当前日期+当前班次之和
      * @ApiMethod (GET)
      * @ApiMethod (GET)
+     * @param string date|sczl_rq 日期(必填,用于汇总产量计酬)
      */
      */
     public function getBaseData()
     public function getBaseData()
     {
     {
@@ -249,28 +304,169 @@ class ProductionInformationStatistics extends Api
             $this->error('请求错误');
             $this->error('请求错误');
         }
         }
 
 
+        $params = $this->request->param();
+        $date = isset($params['date']) ? trim($params['date']) : (isset($params['sczl_rq']) ? trim($params['sczl_rq']) : '');
+        if ($date === '') {
+            $this->error('参数 date 不能为空');
+        }
+        $date = date('Y-m-d', strtotime($date));
+        if ($date === '1970-01-01' || $date === false) {
+            $this->error('参数 date 格式错误');
+        }
+
         $equipmentMap = $this->fetchEquipmentMap(true);
         $equipmentMap = $this->fetchEquipmentMap(true);
         if (empty($equipmentMap)) {
         if (empty($equipmentMap)) {
             $this->success('未获取到机台数据', []);
             $this->success('未获取到机台数据', []);
         }
         }
 
 
+        $yieldMap = $this->fetchYieldSumMapByDate($date);
+
         $list = [];
         $list = [];
         foreach ($equipmentMap as $code => $equipment) {
         foreach ($equipmentMap as $code => $equipment) {
-            $list[] = [
-                '车间'       => $equipment['车间'],
-                '机组'       => $equipment['机组'],
-                '编组'       => $equipment['编组'],
-                '机台编号'   => $code,
-                '机台名称'   => $equipment['机台名称'],
-                '设备编号'   => $code,
-                '理论速度'   => $equipment['理论速度'],
-                '班组列表'   => $this->defaultShifts,
-            ];
+            foreach ($this->defaultShifts as $shift) {
+                $yield = isset($yieldMap[$code][$shift]) ? $yieldMap[$code][$shift] : $this->emptyYieldSum();
+                $list[] = $this->buildAddPrefillRow($equipment, $code, $shift, $date, $yield);
+            }
         }
         }
 
 
         $this->success('成功', $this->sortEquipmentList($list));
         $this->success('成功', $this->sortEquipmentList($list));
     }
     }
 
 
+    /**
+     * 按日期汇总设备_产量计酬(机台 + 班次)
+     * @param string $date Y-m-d
+     * @return array [机台编号][A班|B班] => sums
+     */
+    protected function fetchYieldSumMapByDate($date)
+    {
+        $start = $date . ' 00:00:00';
+        $end = $date . ' 23:59:59';
+
+        $rows = Db::name('设备_产量计酬')
+            ->where('sczl_rq', '>=', $start)
+            ->where('sczl_rq', '<=', $end)
+            ->field([
+                'rtrim(sczl_jtbh) as sczl_jtbh',
+                'rtrim(sczl_bzdh) as sczl_bzdh',
+                'SUM(sczl_保养工时) as sczl_保养工时',
+                'SUM(sczl_异常停机工时) as sczl_异常停机工时',
+                'SUM(sczl_装版总工时) as sczl_装版总工时',
+                'SUM(sczl_打样总工时) as sczl_打样总工时',
+                'SUM(sczl_cl) as sczl_cl',
+                'SUM(sczl_zcfp) as sczl_zcfp',
+                'SUM(sczl_设备运行工时) as sczl_设备运行工时',
+            ])
+            ->group('sczl_jtbh, sczl_bzdh')
+            ->select();
+
+        $map = [];
+        foreach ($rows as $row) {
+            $code = $this->trimText($row['sczl_jtbh']);
+            $shift = $this->normalizeShift($row['sczl_bzdh']);
+            if ($code === '' || $shift === '') {
+                continue;
+            }
+
+            if (!isset($map[$code])) {
+                $map[$code] = [];
+            }
+            // 同一机台同一班次可能有 A / A班 等写法,normalize 后累加
+            if (!isset($map[$code][$shift])) {
+                $map[$code][$shift] = $this->emptyYieldSum();
+            }
+            $map[$code][$shift]['scheduled_maintenance'] += $this->formatNumber($row['sczl_保养工时']);
+            $map[$code][$shift]['fault'] += $this->formatNumber($row['sczl_异常停机工时']);
+            $map[$code][$shift]['changeover'] += $this->formatNumber($row['sczl_装版总工时']);
+            $map[$code][$shift]['proofing'] += $this->formatNumber($row['sczl_打样总工时']);
+            $map[$code][$shift]['actual_output'] += $this->formatNumber($row['sczl_cl']);
+            $map[$code][$shift]['defective_product'] += $this->formatNumber($row['sczl_zcfp']);
+            $map[$code][$shift]['scheduled_production'] += $this->formatNumber($row['sczl_设备运行工时']);
+        }
+
+        // 当班设备运行工时合计为 12 时,计划生产时间记为 11
+        foreach ($map as $code => $shifts) {
+            foreach ($shifts as $shift => $sums) {
+                if (abs($sums['scheduled_production'] - 12) < 0.00001) {
+                    $map[$code][$shift]['scheduled_production'] = 11;
+                }
+            }
+        }
+
+        return $map;
+    }
+
+    /**
+     * 产量计酬汇总空值
+     * @return array
+     */
+    protected function emptyYieldSum()
+    {
+        return [
+            'scheduled_maintenance' => 0,
+            'fault'                 => 0,
+            'changeover'            => 0,
+            'proofing'              => 0,
+            'actual_output'         => 0,
+            'defective_product'     => 0,
+            'scheduled_production'  => 0,
+        ];
+    }
+
+    /**
+     * 构建新增预填行
+     */
+    protected function buildAddPrefillRow(array $equipment, $code, $shift, $date, array $yield)
+    {
+        $scheduledMaintenance = round($this->formatNumber($yield['scheduled_maintenance']), 2);
+        $fault = round($this->formatNumber($yield['fault']), 2);
+        $changeover = round($this->formatNumber($yield['changeover']), 2);
+        $proofing = round($this->formatNumber($yield['proofing']), 2);
+        $actualOutput = round($this->formatNumber($yield['actual_output']), 2);
+        $defectiveProduct = round($this->formatNumber($yield['defective_product']), 2);
+        $scheduledProduction = round($this->formatNumber($yield['scheduled_production']), 2);
+        $theoreticalSpeed = (int)$equipment['理论速度'];
+
+        return [
+            '车间'                   => $equipment['车间'],
+            '机组'                   => $equipment['机组'],
+            '编组'                   => $equipment['编组'],
+            '机台编号'               => $code,
+            '机台名称'               => $equipment['机台名称'],
+            '设备编号'               => $code,
+            '班组'                   => $shift,
+            '班组列表'               => $this->defaultShifts,
+            'sczl_jtbh'              => $code,
+            'sczl_bzdh'              => $shift,
+            'sczl_rq'                => $date . ' 00:00:00',
+            'theoretical_speed'      => $theoreticalSpeed,
+            '理论速度'               => $theoreticalSpeed,
+            'scheduled_maintenance'  => $scheduledMaintenance,
+            '标准计划保养时间'       => $scheduledMaintenance,
+            '计划保养时间'           => $scheduledMaintenance,
+            'fault'                  => $fault,
+            '设备故障时间'           => $fault,
+            'changeover'             => $changeover,
+            '实际换型时间'           => $changeover,
+            '换型时间'               => $changeover,
+            'proofing'               => $proofing,
+            '实际打样时间'           => $proofing,
+            '打样时间'               => $proofing,
+            'actual_output'          => $actualOutput,
+            '实际产量'               => $actualOutput,
+            'defective_product'      => $defectiveProduct,
+            '不良品数量'             => $defectiveProduct,
+            'scheduled_production'   => $scheduledProduction,
+            '计划生产时间'           => $scheduledProduction,
+            // 未从产量计酬预填的字段默认 0,便于直接提交
+            'scheduled_handover'     => 0,
+            '计划早会交接班时间'     => 0,
+            'abnormal'               => 0,
+            '异常时间'               => 0,
+            'planned_output'         => 0,
+            '计划完成产量'           => 0,
+        ];
+    }
+
     /**
     /**
      * 4. 批量新增(每个机台每个班组一条)
      * 4. 批量新增(每个机台每个班组一条)
      * @ApiMethod (POST)
      * @ApiMethod (POST)
@@ -529,13 +725,20 @@ class ProductionInformationStatistics extends Api
                 'query_type'  => 'day',
                 'query_type'  => 'day',
                 'sczl_rq'     => date('Y-m-d 00:00:00', strtotime($query['start'])),
                 'sczl_rq'     => date('Y-m-d 00:00:00', strtotime($query['start'])),
                 'query_month' => '',
                 'query_month' => '',
+                'query_start' => date('Y-m-d', strtotime($query['start'])),
+                'query_end'   => date('Y-m-d', strtotime($query['end'])),
             ];
             ];
         } else {
         } else {
+            // month / range:按机台+班组汇总填写字段,再计算衍生指标
             $statsRows = $this->fetchMonthlyStatsRows($query['start'], $query['end']);
             $statsRows = $this->fetchMonthlyStatsRows($query['start'], $query['end']);
             $dateMeta = [
             $dateMeta = [
-                'query_type'  => 'month',
-                'sczl_rq'     => $query['month'] . '-01 00:00:00',
-                'query_month' => $query['month'],
+                'query_type'  => $query['type'],
+                'sczl_rq'     => isset($query['month'])
+                    ? ($query['month'] . '-01 00:00:00')
+                    : date('Y-m-d 00:00:00', strtotime($query['start'])),
+                'query_month' => isset($query['month']) ? $query['month'] : '',
+                'query_start' => date('Y-m-d', strtotime($query['start'])),
+                'query_end'   => date('Y-m-d', strtotime($query['end'])),
             ];
             ];
         }
         }
 
 
@@ -726,6 +929,8 @@ class ProductionInformationStatistics extends Api
             'sczl_rq'              => $dateMeta['sczl_rq'],
             'sczl_rq'              => $dateMeta['sczl_rq'],
             'query_type'           => $dateMeta['query_type'],
             'query_type'           => $dateMeta['query_type'],
             'query_month'          => $dateMeta['query_month'],
             'query_month'          => $dateMeta['query_month'],
+            'query_start'          => isset($dateMeta['query_start']) ? $dateMeta['query_start'] : '',
+            'query_end'            => isset($dateMeta['query_end']) ? $dateMeta['query_end'] : '',
             'theoretical_speed'    => !empty($stat['id'])
             'theoretical_speed'    => !empty($stat['id'])
                 ? (int)$this->formatNumber($stat['theoretical_speed'])
                 ? (int)$this->formatNumber($stat['theoretical_speed'])
                 : $equipment['理论速度'],
                 : $equipment['理论速度'],