liuhairui 1 日 前
コミット
7b4577119f
1 ファイル変更82 行追加17 行削除
  1. 82 17
      application/api/controller/Staff.php

+ 82 - 17
application/api/controller/Staff.php

@@ -22,10 +22,16 @@ class Staff extends Api
 
     /**
      * 获取部门列表
+     * 一级:部门 dept_name(固定排序)
+     * 二级:工序 big_process
+     * 排序:裁剪、车缝、手工、大烫、总检、包装、其他
+     * 空工序 = 其他
      */
     /**
      * 获取部门列表
-     * 固定排序:裁剪、车缝、手工、大烫、总检、包装、其他
+     * 一级:部门 dept_name
+     * 二级:工序 big_process(按固定顺序:裁剪、车缝、手工、大烫、总检、包装、其他)
+     * 空工序 = 其他
      */
     public function getDepartment()
     {
@@ -33,46 +39,105 @@ class Staff extends Api
             $this->error('非法请求');
         }
 
-        // 1. 查询数据
+        // 1. 查询数据,空工序转为"其他"
         $list = db('人员_基本资料')
-            ->field('big_process, dept_name, COUNT(*) AS count')
+            ->field('dept_name, IF(big_process IS NULL OR big_process = "", "其他", big_process) as big_process, COUNT(*) AS count')
             ->whereNull('mod_rq')
-            ->group('big_process, dept_name')
+            ->group('dept_name, big_process')
             ->select();
 
-        // 2. 构建树形结构
+        // 2. 构建树形结构(一级:部门,二级:工序)
         $tree = [];
         foreach ($list as $item) {
-            $process = $item['big_process'] ?: '其他'; // 空工序 = 其他
             $dept = $item['dept_name'];
+            $process = $item['big_process'];
             $count = $item['count'];
 
-            if (!isset($tree[$process])) {
-                $tree[$process] = [
-                    'label' => $process,
+            // 初始化部门节点
+            if (!isset($tree[$dept])) {
+                $tree[$dept] = [
+                    'label' => $dept,
                     'count' => 0,
                     'children' => []
                 ];
             }
 
-            $tree[$process]['count'] += $count;
-            $tree[$process]['children'][] = [
-                'label' => $dept,
+            // 部门总人数累加
+            $tree[$dept]['count'] += $count;
+
+            // 工序加入children
+            $tree[$dept]['children'][] = [
+                'label' => $process,
                 'count' => $count
             ];
         }
 
-        // 3. 固定排序:裁剪、车缝、手工、大烫、总检、包装、其他
+        // 3. 对每个部门下的工序,按固定顺序排序
         $sortList = ['裁剪', '车缝', '手工', '大烫', '总检', '包装', '其他'];
-        $result = [];
-        foreach ($sortList as $item) {
-            if (isset($tree[$item])) {
-                $result[] = $tree[$item];
+        foreach ($tree as &$deptNode) {
+            $sortedChildren = [];
+            foreach ($sortList as $process) {
+                foreach ($deptNode['children'] as $child) {
+                    if ($child['label'] === $process) {
+                        $sortedChildren[] = $child;
+                        break;
+                    }
+                }
             }
+            $deptNode['children'] = $sortedChildren;
         }
 
+        // 4. 转成索引数组返回
+        $result = array_values($tree);
+
         $this->success('获取部门数据', $result);
     }
+//    public function getDepartment()
+//    {
+//        if (Request::instance()->isGet() == false) {
+//            $this->error('非法请求');
+//        }
+//
+//        // 1. 查询数据
+//        $list = db('人员_基本资料')
+//            ->field('big_process, dept_name, COUNT(*) AS count')
+//            ->whereNull('mod_rq')
+//            ->group('big_process, dept_name')
+//            ->select();
+//
+//        // 2. 构建树形结构
+//        $tree = [];
+//        foreach ($list as $item) {
+//            $process = $item['big_process'] ?: '其他'; // 空工序 = 其他
+//            $dept = $item['dept_name'];
+//            $count = $item['count'];
+//
+//            if (!isset($tree[$process])) {
+//                $tree[$process] = [
+//                    'label' => $process,
+//                    'count' => 0,
+//                    'children' => []
+//                ];
+//            }
+//
+//            $tree[$process]['count'] += $count;
+//            $tree[$process]['children'][] = [
+//                'label' => $dept,
+//                'count' => $count
+//            ];
+//        }
+//
+//        // 3. 固定排序:裁剪、车缝、手工、大烫、总检、包装、其他
+//        $sortList = ['裁剪', '车缝', '手工', '大烫', '总检', '包装', '其他'];
+//        $result = [];
+//        foreach ($sortList as $item) {
+//            if (isset($tree[$item])) {
+//                $result[] = $tree[$item];
+//            }
+//        }
+//
+//        $this->success('获取部门数据', $result);
+//    }
 
     /**
      * 获取员工列表信息