Explorar el Código

工单生产进程管理优化

qiuenguang hace 1 hora
padre
commit
26b3ceb1af
Se han modificado 1 ficheros con 79 adiciones y 29 borrados
  1. 79 29
      application/api/controller/Course.php

+ 79 - 29
application/api/controller/Course.php

@@ -29,56 +29,106 @@ class Course extends Api
         if ($this->request->isGet() === false) {
             $this->error('请求错误');
         }
-        $customers = \db('产品_基本资料')
+        $customerRows = \db('产品_基本资料')
             ->where('客户编号', '<>', '')
             ->field('rtrim(客户编号) as 客户编号, MAX(rtrim(客户名称)) as 客户名称')
             ->group('客户编号')
             ->order('客户编号 asc')
             ->select();
-        if (empty($customers)) {
-            $this->success('', []);
+        $productCustomers = [];
+        foreach ($customerRows as $row) {
+            $customerCode = rtrim((string)$row['客户编号']);
+            if ($customerCode === '') {
+                continue;
+            }
+            $productCustomers[] = [
+                'code' => $customerCode,
+                'name' => rtrim((string)$row['客户名称']),
+            ];
         }
 
-        $productCodes = \db('工单_基本资料')
+        $orders = \db('工单_基本资料')
             ->where('行号', '1')
-            ->where('成品代号', '<>', '')
             ->where('gd_statu', 'in', ['2-生产中', '3-计划中'])
-            ->distinct(true)
-            ->column('成品代号');
-        $codesByInitial = [];
-        foreach ($productCodes as $productCode) {
-            $productCode = rtrim((string)$productCode);
-            if ($productCode === '') {
-                continue;
+            ->field('rtrim(成品代号) as 成品代号, rtrim(Gd_客户代号) as Gd_客户代号, MAX(rtrim(Gd_客户名称)) as Gd_客户名称')
+            ->group('成品代号, Gd_客户代号')
+            ->order('Gd_客户代号 asc')
+            ->select();
+        if (empty($productCustomers) && empty($orders)) {
+            $this->success('', []);
+        }
+        $ordersByInitial = [];
+        foreach ($orders as $index => $order) {
+            $orders[$index]['成品代号'] = rtrim((string)$order['成品代号']);
+            $orders[$index]['Gd_客户代号'] = rtrim((string)$order['Gd_客户代号']);
+            $orders[$index]['Gd_客户名称'] = rtrim((string)$order['Gd_客户名称']);
+            $productCode = $orders[$index]['成品代号'];
+            if ($productCode !== '') {
+                $ordersByInitial[strtoupper($productCode[0])][] = $orders[$index];
             }
-            $codesByInitial[$productCode[0]][] = $productCode;
         }
 
         $data = [
             '印刷工单' => [],
             '糊盒工单' => [],
         ];
-        foreach ($customers as $value) {
-            $customerCode = rtrim((string)$value['客户编号']);
-            if ($customerCode === '') {
-                continue;
+        $usedCodes = [];
+        $appendCustomer = function ($code, $name) use (&$data, &$usedCodes) {
+            $code = rtrim((string)$code);
+            $codeKey = strtoupper($code);
+            if ($code === '' || isset($usedCodes[$codeKey])) {
+                return;
             }
-            $matched = false;
-            foreach ($codesByInitial[$customerCode[0]] ?? [] as $productCode) {
-                if (stripos($productCode, $customerCode) === 0) {
-                    $matched = true;
-                    break;
+            $usedCodes[$codeKey] = true;
+            $label = $code . rtrim((string)$name);
+            if (strpos($code, 'J') !== false) {
+                $data['糊盒工单'][] = $label;
+            } elseif (strpos($code, 'Y') !== false) {
+                $data['印刷工单'][] = $label;
+            }
+        };
+        foreach ($productCustomers as $customer) {
+            $customerCode = $customer['code'];
+            $customerName = $customer['name'];
+            $matchedOrders = [];
+            foreach ($ordersByInitial[strtoupper($customerCode[0])] ?? [] as $order) {
+                if (stripos($order['成品代号'], $customerCode) === 0) {
+                    $matchedOrders[] = $order;
                 }
             }
-            if (!$matched) {
+            if (empty($matchedOrders)) {
+                continue;
+            }
+            if ($customerName !== '') {
+                $appendCustomer($customerCode, $customerName);
                 continue;
             }
-            $name = $customerCode . $value['客户名称'];
-            if (strpos($customerCode, 'J') !== false) {
-                $data['糊盒工单'][] = $name;
-            } elseif (strpos($customerCode, 'Y') !== false) {
-                $data['印刷工单'][] = $name;
+            foreach ($matchedOrders as $order) {
+                if ($order['Gd_客户代号'] === '' && $order['Gd_客户名称'] === '') {
+                    continue;
+                }
+                $appendCustomer(
+                    $order['Gd_客户代号'] !== '' ? $order['Gd_客户代号'] : $customerCode,
+                    $order['Gd_客户名称']
+                );
+            }
+        }
+        foreach ($orders as $order) {
+            $productCode = $order['成品代号'];
+            $matchedProductCustomer = false;
+            if ($productCode !== '') {
+                foreach ($productCustomers as $customer) {
+                    $customerCode = $customer['code'];
+                    if ($customerCode !== '' && strcasecmp($productCode[0], $customerCode[0]) === 0 && stripos($productCode, $customerCode) === 0) {
+                        $matchedProductCustomer = true;
+                        break;
+                    }
+                }
+            }
+            if ($matchedProductCustomer || ($order['Gd_客户代号'] === '' && $order['Gd_客户名称'] === '')) {
+                continue;
             }
+            $appendCustomer($order['Gd_客户代号'], $order['Gd_客户名称']);
         }
         $this->success('成功', $data);
     }
@@ -102,7 +152,7 @@ class Course extends Api
             $where['a.Gd_cpdh|a.成品代号'] = ['like', $param['order'] . '%'];
         }
         if (!empty($param['search'])) {
-            $where['a.Gd_gdbh'] = ['like', '%' . $param['search'] . '%'];
+            $where['a.Gd_gdbh|a.Gd_cpmc|a.成品名称'] = ['like', '%' . $param['search'] . '%'];
         }
         $list = \db('工单_基本资料')
             ->alias('a')