Jelajahi Sumber

年度废品率统计

unknown 6 bulan lalu
induk
melakukan
1f9aa78a63
1 mengubah file dengan 216 tambahan dan 77 penghapusan
  1. 216 77
      application/api/controller/Decision.php

+ 216 - 77
application/api/controller/Decision.php

@@ -895,10 +895,12 @@ class Decision extends Api
     }
     }
 
 
 
 
-    //年度质检废品统计右侧上方列表
-
     /**
     /**
-     * @return array
+     * 年度质检废品统计右侧上方列表
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
      */
      */
     public function QualityInspectionList()
     public function QualityInspectionList()
     {
     {
@@ -906,91 +908,228 @@ class Decision extends Api
             $this->error('请求错误');
             $this->error('请求错误');
         }
         }
         $param = $this->request->param();
         $param = $this->request->param();
-        $mouth = date_create_from_format('Ym', $param['month'])->format('Y-m');
-        $where = ['qczl_rq' => ['like', $mouth . '%']];
-        $list = \db('db_qczl')
-            ->alias('a')
-            ->join('工单_基本资料 b','a.qczl_gdbh = b.Gd_gdbh and a.qczl_yjno = b.行号','LEFT')
-            ->field([
-                'a.qczl_gdbh',
-                'a.qczl_yjno',
-                'a.qczl_fp',
-                'a.fp_lb1',
-                'a.fp_lb2',
-                'a.fp_lb3',
-                'a.fp_lb4',
-                'a.fp_lb5',
-                'a.fp_lb6',
-                'a.fp_lb7',
-                'a.fp_lb8',
-                'a.fp_lb9',
-                'a.fp_lb10',
-                'a.fp_lb11',
-                'a.fp_lb12',
-                'a.fp_lb13',
-                'a.fp_sl1',
-                'a.fp_sl2',
-                'a.fp_sl3',
-                'a.fp_sl4',
-                'a.fp_sl5',
-                'a.fp_sl6',
-                'a.fp_sl7',
-                'a.fp_sl8',
-                'a.fp_sl9',
-                'a.fp_sl10',
-                'a.fp_sl11',
-                'a.fp_sl12',
-                'a.fp_sl13',
-            ])
+        $where = ['年月' => ['like', $param['month'] . '%']];
+
+        // 查询工单列表
+        $workList = \db('rec_月度废品汇总')
+            ->where($where)
+            ->field("CONCAT(Gd_gdbh,'-',印件号) as 工单,实际投料")
+            ->group('工单')
+            ->select();
+        $work = [];
+        foreach ($workList as $item) {
+            $work[$item['工单']] = $item['实际投料'];
+        }
+
+        // 查询工单对应的废品类别首字母(按工单和首字母分组)
+        $WasteWorkList = \db('rec_月度废品汇总')
             ->where($where)
             ->where($where)
+            ->field("CONCAT(Gd_gdbh,'-',印件号) as 工单, left(废品类别,1) as 废品类别首字母")
+            ->group('工单, 废品类别首字母')
+            ->order('废品类别首字母 asc')
             ->select();
             ->select();
-        $data = Db::name('db_qczl')
+        $waste = [];
+        foreach ($WasteWorkList as $item) {
+            if (!isset($waste[$item['废品类别首字母']])) {
+                $waste[$item['废品类别首字母']] = [];
+            }
+            $waste[$item['废品类别首字母']][] = $item['工单'];
+        }
+
+        // 计算每个首字母的实际投料总和
+        $res = [];
+        foreach ($waste as $key => $item) {
+            $res[$key] = array_sum(array_intersect_key($work, array_flip($item)));
+        }
+
+        // 查询废品类别对应的废品数量
+        $wasteList = \db('rec_月度废品汇总')
+            ->where($where)
+            ->group('废品类别')
+            ->order('废品类别 asc')
+            ->column("sum(废品数量) as 废品数量", "废品类别");
+
+        // 从废品类别数据计算每个首字母的废品数量总和
+        $classification = [];
+        foreach ($wasteList as $category => $amount) {
+            $firstChar = substr($category, 0, 1);
+            if (!isset($classification[$firstChar])) {
+                $classification[$firstChar] = 0;
+            }
+            $classification[$firstChar] += $amount;
+        }
+
+        // 构建数据数组
+        $data = [];
+        foreach ($wasteList as $category => $amount) {
+            $firstChar = substr($category, 0, 1);
+            $number = $res[$firstChar] ?? 0;
+            $data[] = [
+                '废品类别' => $category,
+                '实际投料' => $number,
+                '废品数量' => $amount,
+                '质检废品率' => (round($amount/$number, 7) * 100) . '%',
+            ];
+        }
+
+        // 添加首字母合计行
+        foreach ($classification as $firstChar => $amount) {
+            $number = $res[$firstChar] ?? 0;
+            $data[] = [
+                '废品类别' => $firstChar . '-合计',
+                '实际投料' => $number,
+                '废品数量' => $amount,
+                '质检废品率' => (round($amount/$number, 7) * 100) . '%',
+            ];
+        }
+
+        $this->success('成功', $data);
+    }
+
+    //判断字符串首位是否为英文字母
+    function isFirstCharEnglish($str) {
+        if (mb_strlen($str) == 0) return false;
+
+        $firstChar = mb_substr($str, 0, 1);
+        return preg_match('/^[A-Za-z]$/u', $firstChar);
+    }
+
+
+    /**
+     * 年度质检废品率统计下方列表
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function QualityInspectionDetailList()
+    {
+        if ($this->request->isGet() === false) {
+            $this->error('请求错误');
+        }
+        $param = $this->request->param();
+        $where = [
+            '年月' => ['like',$param['month'].'%'],
+            '废品类别' => $param['class']
+        ];
+        $list = \db('rec_月度废品汇总')
+            ->where($where)
+            ->field('年月,客户编号,客户名称,产品名称,Gd_gdbh as 工单编号,印件号,实际投料,废品类别,sum(废品数量) as 废品数量,质检完工时间')
+            ->group('工单编号,印件号,废品类别')
+            ->select();
+        foreach ($list as $key => $item) {
+            $list[$key]['质检废品率'] = (round($item['废品数量']/$item['实际投料'], 7)*100).'%';
+            $list[$key]['年周数'] = substr($list[$key]['质检完工时间'],0,4).'年第'.date('W',strtotime($item['质检完工时间'])).'周';
+        }
+        $this->success('成功', $list);
+
+    }
+
+
+    /**
+     * 创建月度废品率系数
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function QualityInspectionAdd()
+    {
+        if (!$this->request->isGet()) {
+            $this->error('请求错误');
+        }
+
+        $param = $this->request->param();
+
+        // 1. 修复WHERE条件(原来的第二个WHERE会覆盖第一个)
+        $where = ['jjcp_sj' => ['between', [$param['startMonth'], $param['endMonth']]]];
+        $where = ['jjcp_smb' => ['like', '末%']];
+
+        // 2. 优化字段选择,只选择必要的字段
+        $field = [
+            'a.qczl_gdbh', 'a.qczl_yjno', 'a.qczl_rq',
+            'c.成品编码', 'c.成品名称', 'd.规格',
+            'b.实际投料', 'e.客户编号', 'e.客户名称',
+            "DATE_FORMAT(c.jjcp_sj, '%Y%m') AS ym",
+            'c.jjcp_cpdh', 'c.jjcp_cpmc'
+        ];
+
+        // 3. 添加fp_lb和fp_sl字段
+        for ($i = 1; $i <= 13; $i++) {
+            $field[] = "a.fp_lb{$i}";
+            $field[] = "a.fp_sl{$i}";
+        }
+
+        // 4. 执行查询
+        $list = \db('db_qczl')
             ->alias('a')
             ->alias('a')
-            ->join('工单_基本资料 b', 'a.qczl_gdbh = b.Gd_gdbh AND a.qczl_yjno = b.行号', 'LEFT')
-            ->field([
-                'b.实际投料*10000 as 实际投料', 'CONCAT(a.qczl_gdbh,"-",a.qczl_yjno) as 工单'
-            ])
+            ->join('工单_基本资料 b', 'a.qczl_gdbh = b.Gd_gdbh and a.qczl_yjno = b.行号', 'LEFT')
+            ->join('成品入仓 c', 'a.qczl_gdbh = c.jjcp_gdbh and a.qczl_yjno = c.jjcp_yjno', 'LEFT')
+            ->join('物料_存货编码 d', 'c.jjcp_cpdh = d.物料代码', 'LEFT')
+            ->join('产品_基本资料 e', 'c.成品编码 = e.产品编号', 'LEFT')
+            ->field($field)
             ->where($where)
             ->where($where)
-            ->group('a.qczl_gdbh, a.qczl_yjno')
             ->select();
             ->select();
-        $workOrder = [];
-        foreach ($data as $item) {
-            $workOrder[$item['工单']] = $item['实际投料'];
+
+        if (empty($list)) {
+            $this->error('没有找到符合条件的数据');
         }
         }
-        // 用于存储废品类别和对应废品数量的数组
-        $categories = array();
-        // 循环遍历原始数组
+
+        $data = [];
+        $currentTime = date('Y-m-d H:i:s');
+        // 5. 优化循环处理
         foreach ($list as $item) {
         foreach ($list as $item) {
-            $str = $item['qczl_gdbh'].'-'.$item['qczl_yjno'];
-            // 提取废品类别和对应废品数量
-            foreach ($item as $key => $value) {
-                if (strpos($key, "fp_lb") === 0) {
-                    $categoryKey = $key;
-                    $quantityKey = str_replace("fp_lb", "fp_sl", $key);
-                    $quantityValue = $item[$quantityKey];
-
-                    if (!isset($categories[$value]['number1'])) {
-                        $categories[$value]['number1'] = 0;
-                        $categories[$value]['number2'] = 0;
-                    }
-
-                    $categories[$value]['number1'] += (int)$quantityValue;
-                    $categories[$value]['number2'] += (int)$workOrder[$str];
+            // 预先处理公共数据
+            $commonData = [
+                '年月' => $item['ym'],
+                '客户编号' => $item['客户编号'],
+                '客户名称' => $item['客户名称'],
+                '产品类别' => $item['规格'],
+                '产品编号' => $item['成品编码'],
+                '产品名称' => $item['成品名称'],
+                'Gd_gdbh' => $item['qczl_gdbh'],
+                '印件号' => $item['qczl_yjno'],
+                'Gd_cpdh' => $item['jjcp_cpdh'] ?? '',
+                'Gd_cpmc' => $item['jjcp_cpmc'] ?? '',
+                '实际投料' => ($item['实际投料'] ?? 0) * 10000,
+                '质检完工时间' => $item['qczl_rq'],
+                'sys_id' => $param['sys_id'],
+                'Sys_rq' => $currentTime
+            ];
+
+            // 处理13个废品类别
+            for ($i = 1; $i <= 13; $i++) {
+                $lbField = "fp_lb{$i}";
+                $slField = "fp_sl{$i}";
+
+                if (!empty($item[$lbField]) && $this->isFirstCharEnglish($item[$lbField]) && $item[$slField] != 0) {
+                    $data[] = array_merge($commonData, [
+                        '废品类别' => $item[$lbField],
+                        '废品数量' => $item[$slField] ?? 0
+                    ]);
                 }
                 }
             }
             }
         }
         }
-        ksort($categories);
-        $result = [];
-        foreach ($categories as $category => $value) {
-            if ($category !== '') {
-                $result[] = [
-                    '类别' => $category,
-                    '实际投料' => $value['number2'],
-                    '废品数量' => $value['number1'],
-                    '质检废品率' => (round($value['number1']/$value['number2'], 7)*100).'%'
-                ];
+
+        if (empty($data)) {
+            $this->error('没有符合条件的数据需要插入');
+        }
+
+        // 6. 分批插入避免单次数据量过大
+        $chunks = array_chunk($data, 100); // 每批100条
+        $successCount = 0;
+
+        foreach ($chunks as $chunk) {
+            $result = \db('rec_月度废品汇总')->insertAll($chunk);
+            if ($result !== false) {
+                $successCount += $result;
             }
             }
         }
         }
-        $this->success('成功', $result);
+
+        if ($successCount > 0) {
+            $this->success("成功插入 {$successCount} 条数据");
+        } else {
+            $this->error('插入数据失败');
+        }
     }
     }
 }
 }