Przeglądaj źródła

工序合格率优化

qiuenguang 10 miesięcy temu
rodzic
commit
79073ae6c0
1 zmienionych plików z 54 dodań i 195 usunięć
  1. 54 195
      application/api/controller/Decision.php

+ 54 - 195
application/api/controller/Decision.php

@@ -427,124 +427,6 @@ class Decision extends Api
      * @throws \think\db\exception\ModelNotFoundException
      * @throws \think\exception\DbException
      */
-//    public function ProcessOutputRate()
-//    {
-//        if ($this->request->isGet() === false){
-//            $this->error('请求错误');
-//        }
-//        $param = $this->request->param();
-//        if (empty($param)){
-//            $this->error('参数错误');
-//        }
-//        //查询已经进入超节损的月份
-//        $mouth = \db('工单_质量考核汇总')
-//            ->where('sys_ny','like',$param['year'].'%')
-//            ->column('distinct(sys_ny) as mouth');
-//        //创建工序数组
-//        $processType = ['胶印','卷凹','圆烫','圆切','烫金','模切','丝印','喷码','单凹'];
-//        $result = $data = [];
-//        foreach ($mouth as $key=>$value){
-//            foreach ($processType as $item){
-//                //查询进入超节损一年内所有数据
-//                $list = \db('工单_质量考核汇总')
-//                    ->alias('a')
-//                    ->join('工单_工艺资料 b','a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
-//                    ->join('工单_印件资料 c','a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
-//                    ->field('a.sys_ny,((sum(a.班组制程废品)/a.联数)+(sum(a.班组质检废品)/a.联数))/a.印件工序产量 as 废品率')
-//                    ->where('b.Gy0_gxmc','like',$item.'%')
-//                    ->where('a.sys_ny',$value)
-//                    ->group('a.Gy0_gdbh,b.Gy0_yjno,a.工序')
-//                    ->select();
-//                $data[$value][$item] = $list;
-//            }
-//        }
-//        $months = [
-//            $param['year'].'01' => '01月',
-//            $param['year'].'02' => '02月',
-//            $param['year'].'03' => '03月',
-//            $param['year'].'04' => '04月',
-//            $param['year'].'05' => '05月',
-//            $param['year'].'06' => '06月',
-//            $param['year'].'07' => '07月',
-//            $param['year'].'08' => '08月',
-//            $param['year'].'09' => '09月',
-//            $param['year'].'10' => '10月',
-//            $param['year'].'11' => '11月',
-//            $param['year'].'12' => '12月'
-//        ];
-//        //汇总数据
-//        foreach ($processType as $index => $process) {
-//            $result[$index] = array(
-//                "工序类型" => $process,
-//                "01月" => "",
-//                "02月" => "",
-//                "03月" => "",
-//                "04月" => "",
-//                "05月" => "",
-//                "06月" => "",
-//                "07月" => "",
-//                "08月" => "",
-//                "09月" => "",
-//                "10月" => "",
-//                "11月" => "",
-//                "12月" => ""
-//            );
-//
-//            // 遍历月份
-//            foreach ($months as $month => $monthName) {
-//                if (isset($data[$month])){
-//                    $count = count($data[$month][$process]);
-//                }
-//
-//                $totalRate = 0;
-//
-//                // 检查月份是否存在于数据中
-//                if (isset($data[$month])) {
-//                    // 检查工序是否存在于月份数据中
-//                    if (isset($data[$month][$process])) {
-//                        // 累加废品率
-//                        foreach ($data[$month][$process] as $rate) {
-//                            $totalRate += floatval(1-$rate["废品率"]);
-//                        }
-//                    }
-//                }
-//                if ($count > 0){
-//                    $rateNumber = $totalRate/$count;
-//                }
-//                // 格式化为百分比字符串,保留两位小数
-//                if ($totalRate > 0) {
-//                    $result[$index][$monthName] = sprintf("%.2f%%", $rateNumber * 100);
-//                } else {
-//                    $result[$index][$monthName] = ""; // 如果没有数据,则为空字符串
-//                }
-//            }
-//        }
-//        foreach ($result as &$item) {
-//            $total = 0;
-//            $count = 0;
-//
-//            // 遍历月份,计算总和和数量
-//            for ($i = 1; $i <= 12; $i++) {
-//                $month = sprintf("%02d月", $i); // 格式化月份,例如 "01月"
-//                if (isset($item[$month]) && $item[$month] !== "") {
-//                    // 移除百分号,并将字符串转换为浮点数
-//                    $value = floatval(str_replace("%", "", $item[$month]));
-//                    $total += $value;
-//                    $count++;
-//                }
-//            }
-//
-//            // 计算平均值
-//            if ($count > 0) {
-//                $average = $total / $count;
-//                // 格式化为百分比字符串,保留两位小数
-//                $item["平均值"] = sprintf("%.2f%%", $average);
-//            } else {
-//                $item["平均值"] = "0.00%"; // 如果没有数据,则为 0.00%
-//            }
-//        }
-//        $this->success('成功',$result);
-//    }
     public function ProcessOutputRate()
     {
         if (!$this->request->isGet()) {
@@ -556,7 +438,7 @@ class Decision extends Api
             $this->error('参数错误');
         }
 
-        // 新版工序结构
+        // 工序分类
         $processOrder = [
             1 => ['name' => '胶印工序', 'keys' => ['胶印', '上光']],
             2 => ['name' => '凹印工序', 'keys' => ['卷凹']],
@@ -569,102 +451,78 @@ class Decision extends Api
         ];
 
         // 获取存在数据的月份
-        $mouth = \db('工单_质量考核汇总')
-            ->where('sys_ny', 'like', $param['year'].'%')
-            ->column('distinct(sys_ny) as mouth');
+        $monthsWithData = \db('工单_质量考核汇总')
+            ->where('sys_ny', 'like', $param['year'] . '%')
+            ->column('distinct(sys_ny) as month');
 
-        $data = [];
-        foreach ($mouth as $month) {
+        // 初始化结果数组,按工序名称归类
+        $result = [];
+        foreach ($processOrder as $processId => $process) {
+            $result[$process['name']] = [
+                '01月' => '',
+                '02月' => '',
+                '03月' => '',
+                '04月' => '',
+                '05月' => '',
+                '06月' => '',
+                '07月' => '',
+                '08月' => '',
+                '09月' => '',
+                '10月' => '',
+                '11月' => '',
+                '12月' => '',
+                '平均值' => '0.00%'
+            ];
+        }
+
+        // 遍历每个月份
+        foreach ($monthsWithData as $month) {
             foreach ($processOrder as $processId => $process) {
+                // 查询该月份该工序的合格率数据
                 $query = \db('工单_质量考核汇总')
                     ->alias('a')
                     ->join('工单_工艺资料 b', 'a.Gy0_gdbh = b.Gy0_gdbh AND a.印件及工序 = b.Gy0_yjno AND a.工序 = b.Gy0_gxh')
                     ->join('工单_印件资料 c', 'a.Gy0_gdbh = c.Yj_Gdbh AND a.印件及工序 = c.yj_Yjno')
-                    ->join('成品入仓 d','a.Gy0_gdbh = d.jjcp_gdbh AND a.印件及工序 = d.jjcp_yjno')
-                    ->field('a.sys_ny, ((SUM(a.班组制程废品)) + (SUM(a.班组质检废品))) / a.印件工序产量 as 废品率')
-                    ->where(function($query) use ($process) {
+                    ->join('成品入仓 d', 'a.Gy0_gdbh = d.jjcp_gdbh AND a.印件及工序 = d.jjcp_yjno')
+                    ->field('a.sys_ny,   
+                        (1 - ((SUM(a.班组制程废品) + SUM(a.班组分摊质检废品)) / SUM(a.班组产量))) * 100 as 合格率')
+                    ->where(function ($query) use ($process) {
                         foreach ($process['keys'] as $key) {
                             $query->whereOr('a.工序名称', 'like', "%{$key}%");
                         }
                     })
-                    ->where('d.jjcp_smb','like','末%')
+                    ->where('d.jjcp_smb', 'like', '末%')
                     ->where('a.sys_ny', $month)
-                    ->group('a.Gy0_gdbh, b.Gy0_yjno, a.工序');
+                    ->group('a.sys_ny');
 
-                $data[$month][$processId] = $query->select();
+                $records = $query->select();
+
+                if (!empty($records)) {
+                    $monthName = substr($month, 4, 2) . '月';
+                    $result[$process['name']][$monthName] = sprintf("%.2f%%", $records[0]['合格率']);
+                }
             }
         }
-        // 构建月份模板
-        $months = [
-            $param['year'].'01' => '01月',
-            $param['year'].'02' => '02月',
-            $param['year'].'03' => '03月',
-            $param['year'].'04' => '04月',
-            $param['year'].'05' => '05月',
-            $param['year'].'06' => '06月',
-            $param['year'].'07' => '07月',
-            $param['year'].'08' => '08月',
-            $param['year'].'09' => '09月',
-            $param['year'].'10' => '10月',
-            $param['year'].'11' => '11月',
-            $param['year'].'12' => '12月'
-        ];
 
-        $result = [];
-        foreach ($processOrder as $processId => $process) {
-            $processData = [
-                "工序类型" => $process['name'],
-                "01月" => "",
-                "02月" => "",
-                "03月" =>"",
-                "04月" => "",
-                "05月" => "",
-                "06月" => "",
-                "07月" => "",
-                "08月" => "",
-                "09月" => "",
-                "10月" => "",
-                "11月" => "",
-                "12月" => "",
-                "平均值" => "0.00%"
-        ];
-
-        $totalAnnual = 0;
-        $monthCount = 0;
-
-        foreach ($months as $sysMonth => $monthName) {
-            $count = 0;
-            $totalRate = 0;
-
-            if (isset($data[$sysMonth][$processId])) {
-                $records = $data[$sysMonth][$processId];
-                $count = count($records);
-
-                foreach ($records as $record) {
-                    $totalRate += floatval(1 - $record['废品率']);
+        // 计算年度平均值
+        foreach ($result as $processName => &$processData) {
+            $totalAnnual = 0;
+            $monthCount = 0;
+
+            foreach ($processData as $month => $rate) {
+                if ($month !== '平均值' && $rate !== '') {
+                    $totalAnnual += floatval(str_replace('%', '', $rate));
+                    $monthCount++;
                 }
             }
 
-            if ($count > 0) {
-                $rateValue = ($totalRate / $count) * 100;
-                $processData[$monthName] = sprintf("%.2f%%", $rateValue);
-                $totalAnnual += $rateValue;
-                $monthCount++;
-            } else {
-                $processData[$monthName] = "";
+            if ($monthCount > 0) {
+                $processData['平均值'] = sprintf("%.2f%%", $totalAnnual / $monthCount);
             }
         }
 
-        // 计算年度平均值
-        if ($monthCount > 0) {
-            $processData['平均值'] = sprintf("%.2f%%", $totalAnnual / $monthCount);
-        }
-
-        $result[] = $processData;
+        $this->success('成功', array_values($result));
     }
-
-    $this->success('成功', $result);
-}
     /**
      * 获取年分数据
      * @return void
@@ -724,7 +582,7 @@ class Decision extends Api
                 a.联数,
                 a.班组产量,
                 a.班组制程废品,
-                a.班组质检废品,
+                a.班组分摊质检废品,
                 a.sczl_bzdh AS 班组编号,
                 b.印刷方式,
                 b.版距,
@@ -743,7 +601,8 @@ class Decision extends Api
         // 执行查询
         $rawList = isset($param['page'], $param['limit'])
             ? $query->page($param['page'])->limit($param['limit'])->select()
-            : $query->select();
+            : $query->fetchSql(true)->select();
+        halt($rawList);
         // 处理分类标记
         $processedList = [];
         foreach ($rawList as $item) {