m0_70156489 20 hours ago
parent
commit
fa5efa852a

+ 311 - 303
application/api/controller/OrderSuperLoss.php

@@ -1,303 +1,311 @@
-<?php
-
-namespace app\api\controller;
-
-use app\common\controller\Api;
-use think\Cache;
-use think\Exception;
-use think\Log;
-use \think\Request;
-use \think\Db;
-/**
- * 报表接口
- */
-class OrderSuperLoss extends Api
-{
-    protected $noNeedLogin = ['*'];
-    protected $noNeedRight = ['*'];
-
-    /**
-     * 首页-已核查成品编号
-     *
-     */
-    public function index()
-    {
-        $this->success('请求成功');
-    }
-
-
-    /**
-     * 获取工单接单日期的年月列表(左侧菜单栏)
-     * @return void
-     * @throws \think\db\exception\DataNotFoundException
-     * @throws \think\db\exception\ModelNotFoundException
-     * @throws \think\exception\DbException
-     */
-    public function getWorkOrderDates()
-    {
-        if ($this->request->isGet() === false) {
-            $this->error('请求错误');
-        }
-
-        $list = \think\Db::table('工单_基本资料')
-            ->whereNull('Mod_rq')  // 排除 Mod_rq 不为 null 的数据
-            ->where('落货日期', '<>', '')  // 排除空值
-            ->field(\think\Db::raw('DATE_FORMAT(落货日期, "%Y-%m") as 年月'))
-            ->group('年月')
-            ->order('年月', 'desc')  // 按年月倒序排列,最新的在前面
-            ->select();
-
-        if (empty($list)) {
-            $this->success('', []);
-        }
-
-        // 提取年月值组成数组
-        $data = [];
-        foreach ($list as $item) {
-            $data[] = $item['年月'];
-        }
-
-        $this->success('成功', $data);
-    }
-
-    // /**
-    //  * 根据月份获取工单数据(排除已修改记录)
-    //  * @return void
-    //  * @throws \think\db\exception\DataNotFoundException
-    //  * @throws \think\db\exception\ModelNotFoundException
-    //  * @throws \think\exception\DbException
-    //  */
-    // public function getWorkOrdersByMonth()
-    // {
-    //     if ($this->request->isGet() === false) {
-    //         $this->error('请求错误');
-    //     }
-
-    //     $rq = $this->request->get('rq');
-    //     if (empty($rq) || !preg_match('/^\d{4}-\d{2}$/', $rq)) {
-    //         $this->error('参数格式错误,请使用 YYYY-MM 格式');
-    //     }
-
-    //     // 计算月份起止日期
-    //     list($year, $month) = explode('-', $rq);
-    //     $start = "{$year}-{$month}-01";
-    //     $end = date('Y-m-d', strtotime("{$year}-{$month}-01 +1 month"));
-
-    //     $list = \think\Db::table('工单_基本资料')
-    //         ->alias('g')
-    //         ->join('库存_出入库明细 d', 'g.订单编号 = d.order_id', 'LEFT')
-    //         ->where('g.落货日期', '>=', $start)
-    //         ->where('g.落货日期', '<', $end)
-    //         ->whereNull('g.Mod_rq')
-    //         ->field([
-    //             'g.Uniqid',
-    //             'g.订单编号',
-    //             'g.审核 as 核批',
-    //             'g.审核日期 as 核批日期',
-    //             'g.接单日期 as 下单日期',
-    //             'g.落货日期 as 货期',
-    //             'g.生产款号 as 款号',
-    //             'g.订单数量',
-    //             'g.工单入仓数量 as 入库数量',
-    //             'g.款式',
-    //             'g.客户编号 as 客人编号',
-    //             'g.辅料计划入库时间 as 辅料计划入库时间',
-
-    //             // 面料入库时间 - 使用子查询避免重复
-    //             \think\Db::raw('(SELECT MIN(rq) FROM 库存_出入库明细 WHERE order_id = g.订单编号 AND name = "入库") as 面料入库时间'),
-
-    //             // 裁剪相关 - 使用子查询
-    //             \think\Db::raw('(SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "裁剪" AND Mod_rq IS NULL) as 开裁日期'),
-    //             \think\Db::raw('(SELECT SUM(数量) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "裁剪" AND Mod_rq IS NULL) as 实裁数量'),
-
-    //             // 车缝相关 - 使用子查询
-    //             \think\Db::raw('(SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "车缝" AND Mod_rq IS NULL) as 上车位日期'),
-    //             \think\Db::raw('(SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "车缝" AND 尾包 = 1 AND Mod_rq IS NULL) as 车位完成日期'),
-
-    //             // 后道相关 - 使用子查询
-    //             \think\Db::raw('(SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "手工" AND 尾包 = 1 AND Mod_rq IS NULL) as 后道完成日期'),
-
-    //             // 包装完成数量 - 使用子查询
-    //             \think\Db::raw('(SELECT SUM(数量) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "包装" AND Mod_rq IS NULL) as 已完成数量'),
-
-    //             // 生产组别 - 使用子查询
-    //             \think\Db::raw('(SELECT GROUP_CONCAT(DISTINCT SUBSTRING(sczl_jtbh, 3, 2) ORDER BY SUBSTRING(sczl_jtbh, 3, 2) SEPARATOR "、")
-    //                         FROM 设备_产量计酬
-    //                         WHERE 订单编号 = g.订单编号
-    //                         AND 工序名称 = "车缝"
-    //                         AND Mod_rq IS NULL) as 生产组别'),
-
-    //             // 占位字段
-    //             \think\Db::raw('NULL as 辅料入库时间'),
-    //             \think\Db::raw('NULL as 台产'),
-    //             \think\Db::raw('NULL as 产前样批核'),
-    //             \think\Db::raw('NULL as 备注'),
-    //         ])
-    //         ->group('g.订单编号')
-    //         ->select();
-
-    //     if (empty($list)) {
-    //         $this->success('', []);
-    //     }
-
-    //     // 处理日期字段,去掉时分秒
-    //     $dateFields = ['下单日期', '货期', '面料入库时间', '辅料入库时间', '开裁日期', '上车位日期', '车位完成日期', '后道完成日期'];
-
-    //     foreach ($list as &$item) {
-    //         foreach ($dateFields as $field) {
-    //             if (isset($item[$field]) && !empty($item[$field])) {
-    //                 $item[$field] = date('Y-m-d', strtotime($item[$field]));
-    //             }
-    //         }
-    //         if (empty($item['生产组别'])) {
-    //             $item['生产组别'] = '';
-    //         }
-    //         // 确保数值字段为整数
-    //         $item['实裁数量'] = intval($item['实裁数量'] ?? 0);
-    //         $item['已完成数量'] = intval($item['已完成数量'] ?? 0);
-    //         $item['订单数量'] = intval($item['订单数量'] ?? 0);
-    //         $item['入库数量'] = intval($item['入库数量'] ?? 0);
-    //     }
-
-    //     $this->success('成功', $list);
-    // }
-
-    /**
-     * 根据月份获取工单数据
-     */
-    public function getWorkOrdersByMonth()
-    {
-        if (!$this->request->isGet()) {
-            return $this->error('请求错误');
-        }
-
-        $params = Request::instance()->param();
-        $rq = $this->request->get('rq', '', 'trim');
-
-        // 分页参数
-        $page = isset($params['page']) ? (int)$params['page'] : 1;
-        $limit = isset($params['limit']) ? (int)$params['limit'] : 30;
-
-        // 构建基础查询条件
-        $where = [];
-        // 搜索条件
-        if (!empty($params['search'])) {
-            $where['g.订单编号|g.生产款号'] = array('like', '%' . $params['search'] . '%');
-        }
-        // 通用删除标记条件
-        $where['g.Mod_rq'] = null;
-
-        //月份条件非必传
-        if (!empty($rq)) {
-            // 只有传了 rq 月份,才拼接日期范围
-            [$year, $month] = explode('-', $rq);
-            $start = "{$year}-{$month}-01 00:00:00";
-            $end = date('Y-m-01 00:00:00', strtotime("{$year}-{$month}-01 +1 month"));
-
-            $where['g.落货日期'] = ['between', [$start, $end]];
-        }
-
-        // 生产计酬子查询
-        $productSub = \think\Db::table('设备_产量计酬')
-            ->whereNull('Mod_rq')
-            ->group('订单编号')
-            ->field([
-                '订单编号',
-                "MIN(CASE WHEN 工序名称 = '裁剪' THEN sczl_rq END) AS 开裁日期",
-                "SUM(CASE WHEN 工序名称 = '裁剪' THEN 数量 END) AS 实裁数量",
-                "MIN(CASE WHEN 工序名称 = '车缝' THEN sczl_rq END) AS 上车位日期",
-                "MIN(CASE WHEN 工序名称 = '车缝' AND 尾包 = 1 THEN sczl_rq END) AS 车位完成日期",
-                "MIN(CASE WHEN 工序名称 = '手工' AND 尾包 = 1 THEN sczl_rq END) AS 后道完成日期",
-                "SUM(CASE WHEN 工序名称 = '包装' THEN 数量 END) AS 已完成数量",
-                // 1. sczl_bh 作为 生产组别
-                \think\Db::raw('GROUP_CONCAT(DISTINCT CASE WHEN 工序名称 = "车缝" THEN sczl_bh END ORDER BY sczl_bh SEPARATOR "、") AS 生产组别'),
-                // 2. 原来的 sczl_jtbh 截取结果 作为 机台号
-                \think\Db::raw('GROUP_CONCAT(DISTINCT CASE WHEN 工序名称 = "车缝" THEN SUBSTRING(sczl_jtbh, 3, 2) END ORDER BY SUBSTRING(sczl_jtbh, 3, 2) SEPARATOR "、") AS 机台号'),
-            ])
-            ->buildSql();
-
-        // 库存出入库子查询
-        $stockSub = \think\Db::table('库存_出入库明细')
-            ->whereNull('Mod_rq')
-            ->group('order_id')
-            ->field([
-                'order_id',
-                'MIN(CASE WHEN name = "入库" THEN rq END) AS 面料入库时间',
-            ])
-            ->buildSql();
-
-        // ===================== 先查询 总条数 =====================
-        $total = \think\Db::table('工单_基本资料')
-            ->alias('g')
-            ->join([$productSub => 'p'], 'g.订单编号 = p.订单编号', 'LEFT')
-            ->join([$stockSub => 's'], 'g.订单编号 = s.order_id', 'LEFT')
-            ->where($where)
-            ->count();
-
-        // ===================== 再查询 分页列表 =====================
-        $list = \think\Db::table('工单_基本资料')
-            ->alias('g')
-            ->join([$productSub => 'p'], 'g.订单编号 = p.订单编号', 'LEFT')
-            ->join([$stockSub => 's'], 'g.订单编号 = s.order_id', 'LEFT')
-            ->where($where)
-            ->limit(($page - 1) * $limit, $limit)
-            ->field([
-                'g.Uniqid',
-                'g.订单编号',
-                'g.审核 AS 核批',
-                'g.审核日期 AS 核批日期',
-                'g.接单日期 AS 下单日期',
-                'g.落货日期 AS 货期',
-                'g.生产款号 AS 款号',
-                'g.订单数量',
-                'g.工单入仓数量 AS 入库数量',
-                'g.款式',
-                'g.客户编号 AS 客人编号',
-                'g.辅料计划入库时间',
-                'g.计划生产小组',
-                's.面料入库时间',
-                'p.开裁日期',
-                'p.实裁数量',
-                'p.上车位日期',
-                'p.车位完成日期',
-                'p.后道完成日期',
-                'p.已完成数量',
-                'p.机台号',
-                'p.生产组别',
-                \think\Db::raw('NULL AS 辅料入库时间'),
-                \think\Db::raw('NULL AS 台产'),
-                \think\Db::raw('NULL AS 产前样批核'),
-                \think\Db::raw('NULL AS 备注'),
-            ])
-            ->select();
-
-        // 日期格式化 + 数据类型处理
-        $dateFields = [
-            '下单日期', '货期', '面料入库时间', '开裁日期',
-            '上车位日期', '车位完成日期', '后道完成日期'
-        ];
-
-        foreach ($list as &$item) {
-            foreach ($dateFields as $field) {
-                $item[$field] = !empty($item[$field]) ? date('Y-m-d', strtotime($item[$field])) : null;
-            }
-            $item['实裁数量'] = (int)($item['实裁数量'] ?? 0);
-            $item['已完成数量'] = (int)($item['已完成数量'] ?? 0);
-            $item['订单数量'] = (int)($item['订单数量'] ?? 0);
-            $item['入库数量'] = (int)($item['入库数量'] ?? 0);
-            $item['机台号'] = $item['机台号'] ?? '';
-            $item['实际生产小组'] = $item['生产组别'] ?? '';
-        }
-
-        // 返回格式:data + total
-        return $this->success('成功', [
-            'list' => $list,
-            'total' => $total,
-            'page' => $page,
-            'limit' => $limit
-        ]);
-    }
-
-
-}
-
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Cache;
+use think\Exception;
+use think\Log;
+use \think\Request;
+use \think\Db;
+/**
+ * 报表接口
+ */
+class OrderSuperLoss extends Api
+{
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+
+    /**
+     * 首页-已核查成品编号
+     *
+     */
+    public function index()
+    {
+        $this->success('请求成功');
+    }
+
+
+    /**
+     * 获取工单接单日期的年月列表(左侧菜单栏)
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function getWorkOrderDates()
+    {
+        if ($this->request->isGet() === false) {
+            $this->error('请求错误');
+        }
+
+        $list = \think\Db::table('工单_基本资料')
+            ->whereNull('Mod_rq')  // 排除 Mod_rq 不为 null 的数据
+            ->where('落货日期', '<>', '')  // 排除空值
+            ->field(\think\Db::raw('DATE_FORMAT(落货日期, "%Y-%m") as 年月'))
+            ->group('年月')
+            ->order('年月', 'desc')  // 按年月倒序排列,最新的在前面
+            ->select();
+
+        if (empty($list)) {
+            $this->success('', []);
+        }
+
+        // 提取年月值组成数组
+        $data = [];
+        foreach ($list as $item) {
+            $data[] = $item['年月'];
+        }
+
+        $this->success('成功', $data);
+    }
+
+    // /**
+    //  * 根据月份获取工单数据(排除已修改记录)
+    //  * @return void
+    //  * @throws \think\db\exception\DataNotFoundException
+    //  * @throws \think\db\exception\ModelNotFoundException
+    //  * @throws \think\exception\DbException
+    //  */
+    // public function getWorkOrdersByMonth()
+    // {
+    //     if ($this->request->isGet() === false) {
+    //         $this->error('请求错误');
+    //     }
+
+    //     $rq = $this->request->get('rq');
+    //     if (empty($rq) || !preg_match('/^\d{4}-\d{2}$/', $rq)) {
+    //         $this->error('参数格式错误,请使用 YYYY-MM 格式');
+    //     }
+
+    //     // 计算月份起止日期
+    //     list($year, $month) = explode('-', $rq);
+    //     $start = "{$year}-{$month}-01";
+    //     $end = date('Y-m-d', strtotime("{$year}-{$month}-01 +1 month"));
+
+    //     $list = \think\Db::table('工单_基本资料')
+    //         ->alias('g')
+    //         ->join('库存_出入库明细 d', 'g.订单编号 = d.order_id', 'LEFT')
+    //         ->where('g.落货日期', '>=', $start)
+    //         ->where('g.落货日期', '<', $end)
+    //         ->whereNull('g.Mod_rq')
+    //         ->field([
+    //             'g.Uniqid',
+    //             'g.订单编号',
+    //             'g.审核 as 核批',
+    //             'g.审核日期 as 核批日期',
+    //             'g.接单日期 as 下单日期',
+    //             'g.落货日期 as 货期',
+    //             'g.生产款号 as 款号',
+    //             'g.订单数量',
+    //             'g.工单入仓数量 as 入库数量',
+    //             'g.款式',
+    //             'g.客户编号 as 客人编号',
+    //             'g.辅料计划入库时间 as 辅料计划入库时间',
+
+    //             // 面料入库时间 - 使用子查询避免重复
+    //             \think\Db::raw('(SELECT MIN(rq) FROM 库存_出入库明细 WHERE order_id = g.订单编号 AND name = "入库") as 面料入库时间'),
+
+    //             // 裁剪相关 - 使用子查询
+    //             \think\Db::raw('(SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "裁剪" AND Mod_rq IS NULL) as 开裁日期'),
+    //             \think\Db::raw('(SELECT SUM(数量) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "裁剪" AND Mod_rq IS NULL) as 实裁数量'),
+
+    //             // 车缝相关 - 使用子查询
+    //             \think\Db::raw('(SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "车缝" AND Mod_rq IS NULL) as 上车位日期'),
+    //             \think\Db::raw('(SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "车缝" AND 尾包 = 1 AND Mod_rq IS NULL) as 车位完成日期'),
+
+    //             // 后道相关 - 使用子查询
+    //             \think\Db::raw('(SELECT MIN(sczl_rq) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "手工" AND 尾包 = 1 AND Mod_rq IS NULL) as 后道完成日期'),
+
+    //             // 包装完成数量 - 使用子查询
+    //             \think\Db::raw('(SELECT SUM(数量) FROM 设备_产量计酬 WHERE 订单编号 = g.订单编号 AND 工序名称 = "包装" AND Mod_rq IS NULL) as 已完成数量'),
+
+    //             // 生产组别 - 使用子查询
+    //             \think\Db::raw('(SELECT GROUP_CONCAT(DISTINCT SUBSTRING(sczl_jtbh, 3, 2) ORDER BY SUBSTRING(sczl_jtbh, 3, 2) SEPARATOR "、")
+    //                         FROM 设备_产量计酬
+    //                         WHERE 订单编号 = g.订单编号
+    //                         AND 工序名称 = "车缝"
+    //                         AND Mod_rq IS NULL) as 生产组别'),
+
+    //             // 占位字段
+    //             \think\Db::raw('NULL as 辅料入库时间'),
+    //             \think\Db::raw('NULL as 台产'),
+    //             \think\Db::raw('NULL as 产前样批核'),
+    //             \think\Db::raw('NULL as 备注'),
+    //         ])
+    //         ->group('g.订单编号')
+    //         ->select();
+
+    //     if (empty($list)) {
+    //         $this->success('', []);
+    //     }
+
+    //     // 处理日期字段,去掉时分秒
+    //     $dateFields = ['下单日期', '货期', '面料入库时间', '辅料入库时间', '开裁日期', '上车位日期', '车位完成日期', '后道完成日期'];
+
+    //     foreach ($list as &$item) {
+    //         foreach ($dateFields as $field) {
+    //             if (isset($item[$field]) && !empty($item[$field])) {
+    //                 $item[$field] = date('Y-m-d', strtotime($item[$field]));
+    //             }
+    //         }
+    //         if (empty($item['生产组别'])) {
+    //             $item['生产组别'] = '';
+    //         }
+    //         // 确保数值字段为整数
+    //         $item['实裁数量'] = intval($item['实裁数量'] ?? 0);
+    //         $item['已完成数量'] = intval($item['已完成数量'] ?? 0);
+    //         $item['订单数量'] = intval($item['订单数量'] ?? 0);
+    //         $item['入库数量'] = intval($item['入库数量'] ?? 0);
+    //     }
+
+    //     $this->success('成功', $list);
+    // }
+
+    /**
+     * 根据月份获取工单数据
+     */
+    public function getWorkOrdersByMonth()
+    {
+        if (!$this->request->isGet()) {
+            return $this->error('请求错误');
+        }
+
+        $params = Request::instance()->param();
+        $rq = $this->request->get('rq', '', 'trim');
+
+        // 分页参数
+        $page = isset($params['page']) ? (int)$params['page'] : 1;
+        $limit = isset($params['limit']) ? (int)$params['limit'] : 30;
+
+        // 构建基础查询条件
+        $where = [];
+        // 搜索条件
+        if (!empty($params['search'])) {
+            $where['g.订单编号|g.生产款号'] = array('like', '%' . $params['search'] . '%');
+        }
+        // 通用删除标记条件
+        $where['g.Mod_rq'] = null;
+
+        //月份条件非必传
+        if (!empty($rq)) {
+            // 校验月份格式(YYYY-MM),避免 explode 后取不到月份导致查询错乱
+            if (!preg_match('/^(\d{4})-(\d{2})$/', $rq, $matches)) {
+                $this->error('参数格式错误,请使用 YYYY-MM 格式');
+            }
+            $year = $matches[1];
+            $month = $matches[2];
+
+            // 月份区间用左闭右开:[当月1号00:00:00, 下月1号00:00:00)
+            // 注意不能用 between,因为 between 是闭区间,
+            // 会把「下月1号 00:00:00」的订单也算进本月
+            $start = "{$year}-{$month}-01 00:00:00";
+            $end = date('Y-m-d 00:00:00', strtotime("{$year}-{$month}-01 +1 month"));
+
+            $where['g.落货日期'] = [['>=', $start], ['<', $end]];
+        }
+
+        // 生产计酬子查询
+        $productSub = \think\Db::table('设备_产量计酬')
+            ->whereNull('Mod_rq')
+            ->group('订单编号')
+            ->field([
+                '订单编号',
+                "MIN(CASE WHEN 工序名称 = '裁剪' THEN sczl_rq END) AS 开裁日期",
+                "SUM(CASE WHEN 工序名称 = '裁剪' THEN 数量 END) AS 实裁数量",
+                "MIN(CASE WHEN 工序名称 = '车缝' THEN sczl_rq END) AS 上车位日期",
+                "MIN(CASE WHEN 工序名称 = '车缝' AND 尾包 = 1 THEN sczl_rq END) AS 车位完成日期",
+                "MIN(CASE WHEN 工序名称 = '手工' AND 尾包 = 1 THEN sczl_rq END) AS 后道完成日期",
+                "SUM(CASE WHEN 工序名称 = '包装' THEN 数量 END) AS 已完成数量",
+                // 1. sczl_bh 作为 生产组别
+                \think\Db::raw('GROUP_CONCAT(DISTINCT CASE WHEN 工序名称 = "车缝" THEN sczl_bh END ORDER BY sczl_bh SEPARATOR "、") AS 生产组别'),
+                // 2. 原来的 sczl_jtbh 截取结果 作为 机台号
+                \think\Db::raw('GROUP_CONCAT(DISTINCT CASE WHEN 工序名称 = "车缝" THEN SUBSTRING(sczl_jtbh, 3, 2) END ORDER BY SUBSTRING(sczl_jtbh, 3, 2) SEPARATOR "、") AS 机台号'),
+            ])
+            ->buildSql();
+
+        // 库存出入库子查询
+        $stockSub = \think\Db::table('库存_出入库明细')
+            ->whereNull('Mod_rq')
+            ->group('order_id')
+            ->field([
+                'order_id',
+                'MIN(CASE WHEN name = "入库" THEN rq END) AS 面料入库时间',
+            ])
+            ->buildSql();
+
+        // ===================== 先查询 总条数 =====================
+        $total = \think\Db::table('工单_基本资料')
+            ->alias('g')
+            ->join([$productSub => 'p'], 'g.订单编号 = p.订单编号', 'LEFT')
+            ->join([$stockSub => 's'], 'g.订单编号 = s.order_id', 'LEFT')
+            ->where($where)
+            ->count();
+
+        // ===================== 再查询 分页列表 =====================
+        $list = \think\Db::table('工单_基本资料')
+            ->alias('g')
+            ->join([$productSub => 'p'], 'g.订单编号 = p.订单编号', 'LEFT')
+            ->join([$stockSub => 's'], 'g.订单编号 = s.order_id', 'LEFT')
+            ->where($where)
+            ->limit(($page - 1) * $limit, $limit)
+            ->field([
+                'g.Uniqid',
+                'g.订单编号',
+                'g.审核 AS 核批',
+                'g.审核日期 AS 核批日期',
+                'g.接单日期 AS 下单日期',
+                'g.落货日期 AS 货期',
+                'g.生产款号 AS 款号',
+                'g.订单数量',
+                'g.工单入仓数量 AS 入库数量',
+                'g.款式',
+                'g.客户编号 AS 客人编号',
+                'g.辅料计划入库时间',
+                'g.计划生产小组',
+                's.面料入库时间',
+                'p.开裁日期',
+                'p.实裁数量',
+                'p.上车位日期',
+                'p.车位完成日期',
+                'p.后道完成日期',
+                'p.已完成数量',
+                'p.机台号',
+                'p.生产组别',
+                \think\Db::raw('NULL AS 辅料入库时间'),
+                \think\Db::raw('NULL AS 台产'),
+                \think\Db::raw('NULL AS 产前样批核'),
+                \think\Db::raw('NULL AS 备注'),
+            ])
+            ->select();
+
+        // 日期格式化 + 数据类型处理
+        $dateFields = [
+            '下单日期', '货期', '面料入库时间', '开裁日期',
+            '上车位日期', '车位完成日期', '后道完成日期'
+        ];
+
+        foreach ($list as &$item) {
+            foreach ($dateFields as $field) {
+                $item[$field] = !empty($item[$field]) ? date('Y-m-d', strtotime($item[$field])) : null;
+            }
+            $item['实裁数量'] = (int)($item['实裁数量'] ?? 0);
+            $item['已完成数量'] = (int)($item['已完成数量'] ?? 0);
+            $item['订单数量'] = (int)($item['订单数量'] ?? 0);
+            $item['入库数量'] = (int)($item['入库数量'] ?? 0);
+            $item['机台号'] = $item['机台号'] ?? '';
+            $item['实际生产小组'] = $item['生产组别'] ?? '';
+        }
+
+        // 返回格式:data + total
+        return $this->success('成功', [
+            'list' => $list,
+            'total' => $total,
+            'page' => $page,
+            'limit' => $limit
+        ]);
+    }
+
+
+}
+

+ 206 - 125
application/api/controller/ReportingWork.php

@@ -1018,7 +1018,8 @@ SQL;
 
     /**
      * 左侧菜单 - 工分产量报工
-     * 三级结构:大工序 → 机台编号 → 上报日期
+     * 树结构:大工序 → 小组+机台 → 上报日期
+     * 字段对应:majorprocess → sys_id(小组) + machine(机台) → sys_rq
      * @ApiMethod (GET)
      */
     public function WorkpointsOutput()
@@ -1027,124 +1028,139 @@ SQL;
             $this->error('请求方法错误');
         }
 
-        $list = db('设备_工分计酬')
+        // 1. 查询去重后的基础数据,日期只取前10位(YYYY-MM-DD)
+        $rows = db('设备_工分计酬')
             ->where('del_rq', null)
+            ->where('majorprocess', '<>', '')
+            ->where('sys_id', '<>', '')
             ->where('machine', '<>', '')
-            ->field('majorprocess, machine, sys_rq')
+            ->where('sys_rq', '<>', '')
+            ->field("DISTINCT majorprocess, sys_id, machine, LEFT(sys_rq, 10) as sys_rq")
             ->select();
 
-        $map = [];
-        foreach ($list as $row) {
-            $proc = trim((string)($row['majorprocess'] ?? ''));
-            $machine = trim((string)($row['machine'] ?? ''));
-            $date = trim((string)($row['sys_rq'] ?? ''));
-            if ($proc === '' || $machine === '' || $date === '') continue;
-
-            $dateOnly = substr($date, 0, 10);
-
-            if (!isset($map[$proc])) $map[$proc] = [];
-            if (!isset($map[$proc][$machine])) $map[$proc][$machine] = [];
+        if (empty($rows)) {
+            $this->success('成功', []);
+        }
 
-            if (!in_array($dateOnly, $map[$proc][$machine])) {
-                $map[$proc][$machine][] = $dateOnly;
+        // 2. 分组:大工序 → 小组+机台 → 日期列表
+        $grouped = [];
+        foreach ($rows as $row) {
+            $proc = trim($row['majorprocess']);    // 大工序
+            $group = trim($row['sys_id']);        // 小组名称
+            $machine = trim($row['machine']);     // 机台编号
+            $date = trim($row['sys_rq']);         // 上报日期
+
+            // 初始化大工序分组
+            if (!isset($grouped[$proc])) {
+                $grouped[$proc] = [];
+            }
+            // 初始化小组+机台节点(一次性把dates数组初始化好,避免未定义索引报错)
+            $machineKey = $group . '|' . $machine;
+            if (!isset($grouped[$proc][$machineKey])) {
+                $grouped[$proc][$machineKey] = [
+                    'group_name' => $group,
+                    'machine' => $machine,
+                    'dates' => [],
+                ];
+            }
+            // 日期去重后加入列表
+            if (!in_array($date, $grouped[$proc][$machineKey]['dates'])) {
+                $grouped[$proc][$machineKey]['dates'][] = $date;
             }
         }
 
-        $data = [];
+        // 3. 按业务顺序排序
+        // 3.1 大工序固定顺序
         $procOrder = ['裁剪', '车缝', '手工', '大烫', '总检', '包装'];
-        $procSorted = array_keys($map);
-        usort($procSorted, function ($a, $b) use ($procOrder) {
-            $ai = array_search($a, $procOrder);
-            $bi = array_search($b, $procOrder);
-            $ai = $ai === false ? 999 : $ai;
-            $bi = $bi === false ? 999 : $bi;
-            return $ai - $bi;
+        $procNames = array_keys($grouped);
+        usort($procNames, function ($a, $b) use ($procOrder) {
+            $posA = array_search($a, $procOrder);
+            $posB = array_search($b, $procOrder);
+            $posA = $posA === false ? 999 : $posA;
+            $posB = $posB === false ? 999 : $posB;
+            return $posA - $posB;
         });
 
-        foreach ($procSorted as $proc) {
-            $machines = $map[$proc];
-            $children1 = [];
-            $machineKeys = array_keys($machines);
-            sort($machineKeys, SORT_NATURAL); // 机台编号自然排序 1,2,3...
-            foreach ($machineKeys as $machine) {
-                $dates = $machines[$machine];
-                rsort($dates);
-                $children2 = [];
-                foreach ($dates as $i => $d) {
-                    $children2[] = [
-                        'id' => "date-{$proc}-{$machine}-{$i}",
+        // 4. 构建前端树需要的最终结构
+        $data = [];
+        foreach ($procNames as $proc) {
+            $machineList = $grouped[$proc];
+            // 机台节点自然排序
+            ksort($machineList, SORT_NATURAL);
+
+            $children = [];
+            foreach ($machineList as $item) {
+                // 日期倒序,最新日期在最前面
+                rsort($item['dates']);
+
+                $dateNodes = [];
+                foreach ($item['dates'] as $d) {
+                    $dateNodes[] = [
                         'label' => $d,
                         'sys_rq' => $d,
                     ];
                 }
-                $children1[] = [
-                    'id' => "machine-{$proc}-{$machine}",
-                    'label' => $machine,
-                    'machine' => $machine,
-                    'children' => $children2,
+
+                // 拼接显示文本:小组名(机台号)
+                $children[] = [
+                    'label' => $item['group_name'] . '(' . $item['machine'] . ')',
+                    'children' => $dateNodes,
                 ];
             }
+
             $data[] = [
-                'id' => "proc-{$proc}",
                 'label' => $proc,
-                'majorprocess' => $proc,
-                'children' => $children1,
+                'children' => $children,
             ];
         }
 
         $this->success('成功', $data);
     }
 
-    /**
-     * 工分产量报工列表
-     * @ApiMethod (GET)
-     */
     public function WorkpointsOutputList()
     {
         if ($this->request->isGet() === false) {
             $this->error('请求错误');
         }
 
-        $search = input('search');
-        $page = input('page');
-        $limit = input('limit');
-        $majorprocess = input('majorprocess');
-        $machine = input('machine');
-        $date = input('date');
-
-        $where = [];
-        if (!empty($search)) {
-            // 关键词搜索模式
-            $searchLike = "%{$search}%";
-            $where[] = "(del_rq IS NULL AND (work_order LIKE '{$searchLike}' OR staff_no LIKE '{$searchLike}' OR staff_name LIKE '{$searchLike}'))";
-        } else {
-            // 菜单模式
-            if (!empty($majorprocess)) {
-                $where['majorprocess'] = $majorprocess;
-            }
-            if (!empty($machine)) {
-                $where['machine'] = $machine;
+        $search = trim(input('search'));
+        $page = input('page', 1);
+        $limit = input('limit', 50);
+        $majorprocess = trim(input('majorprocess'));
+        $sysId = trim(input('sys_id'));
+        $date = trim(input('date', input('sys_rq', '')));
+
+        // ========== 固定走明细分页模式,去掉透视聚合判断 ==========
+        // 注意:where 数组不能混用关联键和数字索引,统一用闭包写法
+        $where = function ($query) use ($search, $majorprocess, $sysId, $date) {
+            $query->whereNull('del_rq');
+
+            if (!empty($search)) {
+                $searchLike = "%{$search}%";
+                $query->where(function ($q) use ($searchLike) {
+                    $q->where('work_order', 'like', $searchLike)
+                      ->whereOr('staff_no', 'like', $searchLike)
+                      ->whereOr('staff_name', 'like', $searchLike);
+                });
+            } else {
+                if (!empty($majorprocess)) $query->where('majorprocess', $majorprocess);
+                if (!empty($sysId)) $query->where('sys_id', $sysId);
+                if (!empty($date)) $query->where('sys_rq', 'like', $date . '%');
             }
-            if (!empty($date)) {
-                $where['sys_rq'] = ['like', $date . '%'];
-            }
-            $where['del_rq'] = null;
-        }
+        };
 
         $list = db('设备_工分计酬')
             ->where($where)
             ->field('id,work_order,majorprocess,process_code,process_name,part_code,
-                     staff_no,staff_name,standard_hour,standard_score,money,coefficient,
-                     number,production_hour,production_score,salary,machine,sys_id,sys_rq')
-            ->order('sys_rq desc,process_code asc,staff_no asc')
+         staff_no,staff_name,standard_hour,standard_score,money,coefficient,
+         number,production_hour,production_score,salary,machine,sys_id,sys_rq')
+            ->order('work_order asc, process_code asc, staff_no asc')
             ->limit(($page - 1) * $limit, $limit)
             ->select();
 
-        $count = db('设备_工分计酬')
-            ->where($where)
-            ->count();
+        $count = db('设备_工分计酬')->where($where)->count();
 
-        // 批量查询生产款号、款式
+        // 批量查询款号、款式
         $workOrders = array_unique(array_filter(array_column($list, 'work_order')));
         $orderMap = [];
         if (!empty($workOrders)) {
@@ -1167,9 +1183,7 @@ SQL;
         foreach ($list as $row) {
             if (!empty($row['part_code']) && !empty($row['work_order'])) {
                 $key = $row['work_order'] . '|' . $row['part_code'];
-                if (!in_array($key, $partKeys)) {
-                    $partKeys[] = $key;
-                }
+                if (!in_array($key, $partKeys)) $partKeys[] = $key;
             }
         }
         if (!empty($partKeys)) {
@@ -1189,12 +1203,10 @@ SQL;
             }
         }
 
-        // 组装返回数据
         $data = [];
         foreach ($list as $value) {
             $orderInfo = $orderMap[$value['work_order']] ?? [];
             $partKey = $value['work_order'] . '|' . $value['part_code'];
-            // 工分和工资直接从表字段取值,不做计算
             $data[] = [
                 'id' => $value['id'],
                 'work_order' => $value['work_order'],
@@ -1217,16 +1229,15 @@ SQL;
             ];
         }
 
-        $this->success('成功', ['list' => $data, 'total' => $count]);
+        $this->success('成功', ['list' => $data, 'total' => $count, 'is_pivot' => false]);
     }
 
-
     // ==================== 手工新增工分报工 ====================
 
     /**
      * 手工新增工分报工 - 左侧菜单
-     * 一级:报工日期(年月日)
-     * 二级:机台编号 + 手工新增人员
+     * 树结构:年 → 月日 → 添加人
+     * 数据来源:设备_工分计酬 中 machine 为空的手工报工记录
      * @ApiMethod (GET)
      */
     public function GetManualReportingLeft()
@@ -1234,56 +1245,94 @@ SQL;
         if (!$this->request->isGet()) {
             $this->error('请求方法错误');
         }
-        $startDate = date('Y-m-d', strtotime('-90 day'));
-        $endDate = date('Y-m-d');
 
-        // 查询手工报工数据(machine为空或手工标记)
+        // 手工报工量很小(全表仅少量记录),直接全量取出手工报工记录
         $list = db('设备_工分计酬')
-            ->where('del_rq', null)
-            ->where('date', 'between time', [$startDate, $endDate])
-            ->where(function($query) {
+            ->whereNull('del_rq')
+            ->where(function ($query) {
                 $query->where('machine', '')->whereOr('machine', null);
             })
-            ->field('machine, date, staff_no, staff_name')
-            ->order('date desc, machine asc')
+            ->field('date, sys_id')
             ->select();
 
-        // 按日期分组
-        $dateMap = [];
+        // 按 年 → 月日 → 添加人 分组去重
+        $tree = [];
         foreach ($list as $row) {
-            $date = $row['date'];
-            if (!isset($dateMap[$date])) {
-                $dateMap[$date] = [];
+            $rawDate = trim((string)($row['date'] ?? ''));
+            if ($rawDate === '') {
+                continue;
+            }
+            $ts = strtotime($rawDate);
+            if ($ts === false) {
+                continue;
+            }
+            $year = date('Y', $ts);        // 一级:年
+            $monthDay = date('m-d', $ts);   // 二级:月日
+            $dateFull = date('Y-m-d', $ts);
+            $operator = trim((string)($row['sys_id'] ?? ''));  // 三级:添加人
+            if ($operator === '') {
+                $operator = '未知';
             }
-            // 同一日期下按机台+人员去重
-            $key = ($row['machine'] ?: '手工') . '|' . $row['staff_no'];
-            if (!isset($dateMap[$date][$key])) {
-                $dateMap[$date][$key] = [
-                    'machine' => $row['machine'] ?: '',
-                    'staff_no' => $row['staff_no'],
-                    'staff_name' => $row['staff_name'],
+
+            if (!isset($tree[$year])) {
+                $tree[$year] = [];
+            }
+            if (!isset($tree[$year][$monthDay])) {
+                $tree[$year][$monthDay] = [
+                    'date' => $dateFull,
+                    'operators' => [],
                 ];
             }
+            // 同一年同一月日下添加人去重
+            if (!isset($tree[$year][$monthDay]['operators'][$operator])) {
+                $tree[$year][$monthDay]['operators'][$operator] = true;
+            }
+        }
+
+        // 年份倒序(同一年内月日倒序)
+        krsort($tree);
+        foreach ($tree as $year => &$monthMap) {
+            krsort($monthMap);
+            foreach ($monthMap as &$monthItem) {
+                $monthItem['operators'] = array_keys($monthItem['operators']);
+            }
+            unset($monthItem);
         }
+        unset($monthMap);
 
+        // 组装前端树结构
         $data = [];
-        foreach ($dateMap as $date => $items) {
-            $children = [];
-            foreach ($items as $item) {
-                $machine = $item['machine'] ?: '手工-' . $item['staff_no'];
-                $children[] = [
-                    'label' => $machine . ' - ' . $item['staff_name'],
-                    'id' => 'machine-' . $item['staff_no'] . '-' . $date,
-                    'staff_no' => $item['staff_no'],
-                    'staff_name' => $item['staff_name'],
-                    'machine' => $item['machine'],
+        foreach ($tree as $year => $monthMap) {
+            $monthChildren = [];
+            foreach ($monthMap as $monthDay => $monthItem) {
+                $operatorChildren = [];
+                foreach ($monthItem['operators'] as $operator) {
+                    $operatorChildren[] = [
+                        'label' => $operator,
+                        'id' => "operator-{$monthItem['date']}-{$operator}",
+                        'sys_id' => $operator,
+                        'date' => $monthItem['date'],
+                        'year' => $year,
+                        'month_day' => $monthDay,
+                        'level' => 3,
+                    ];
+                }
+                $monthChildren[] = [
+                    'label' => $monthDay,
+                    'id' => "monthday-{$monthItem['date']}",
+                    'date' => $monthItem['date'],
+                    'year' => $year,
+                    'month_day' => $monthDay,
+                    'level' => 2,
+                    'children' => $operatorChildren,
                 ];
             }
             $data[] = [
-                'label' => $date,
-                'id' => 'date-' . $date,
-                'sys_rq' => $date,
-                'children' => $children,
+                'label' => $year,
+                'id' => "year-{$year}",
+                'year' => $year,
+                'level' => 1,
+                'children' => $monthChildren,
             ];
         }
 
@@ -1293,7 +1342,10 @@ SQL;
     /**
      * 手工新增工分报工 - 列表
      * @ApiMethod (GET)
-     * @param string $date 报工日期
+     * @param string $date 报工日期(YYYY-MM-DD,可选)
+     * @param string $year 年份(YYYY,可选,配合 $month_day 使用)
+     * @param string $month_day 月日(MM-DD,可选,配合 $year 使用)
+     * @param string $sys_id 添加人(可选)
      * @param string $staff_no 员工编号(可选)
      */
     public function GetManualReportingList()
@@ -1303,20 +1355,49 @@ SQL;
         }
         $params = $this->request->param();
         $date = isset($params['date']) ? trim($params['date']) : '';
+        $year = isset($params['year']) ? trim($params['year']) : '';
+        $monthDay = isset($params['month_day']) ? trim($params['month_day']) : '';
+        $sysId = isset($params['sys_id']) ? trim($params['sys_id']) : '';
         $staffNo = isset($params['staff_no']) ? trim($params['staff_no']) : '';
+        $search = isset($params['search']) ? trim($params['search']) : '';
+        $workOrder = isset($params['work_order']) ? trim($params['work_order']) : '';
 
         $query = db('设备_工分计酬')
-            ->where('del_rq', null)
-            ->where(function($q) {
+            ->whereNull('del_rq')
+            ->where(function ($q) {
                 $q->where('machine', '')->whereOr('machine', null);
             });
 
         if (!empty($date)) {
             $query->where('date', $date);
         }
+        // 年份筛选(一级节点)
+        if (!empty($year) && preg_match('/^\d{4}$/', $year)) {
+            $query->where('date', 'like', "{$year}-%");
+        }
+        // 年 + 月日 组合筛选(二级节点)
+        if (!empty($year) && !empty($monthDay) && preg_match('/^\d{2}-\d{2}$/', $monthDay)) {
+            $query->where('date', 'like', "{$year}-{$monthDay}%");
+        }
+        // 添加人(三级节点)
+        if (!empty($sysId)) {
+            $query->where('sys_id', $sysId);
+        }
         if (!empty($staffNo)) {
             $query->where('staff_no', $staffNo);
         }
+        if (!empty($workOrder)) {
+            $query->where('work_order', $workOrder);
+        }
+        // 关键词搜索(订单编号 / 员工编号 / 员工姓名)
+        if (!empty($search)) {
+            $query->where(function ($q) use ($search) {
+                $like = "%{$search}%";
+                $q->where('work_order', 'like', $like)
+                  ->whereOr('staff_no', 'like', $like)
+                  ->whereOr('staff_name', 'like', $like);
+            });
+        }
 
         $list = $query->order('sys_rq desc')->select();
         $count = count($list);

+ 576 - 574
application/api/controller/Staff.php

@@ -1,574 +1,576 @@
-<?php
-
-namespace app\api\controller;
-
-use app\common\controller\Api;
-use \think\Request;
-use \think\Db;
-/**
- * 员工资料接口
- */
-class Staff extends Api
-{
-    protected $noNeedLogin = ['*'];
-    protected $noNeedRight = ['*'];
-    /**
-     * 首页
-     */
-    public function index()
-    {
-        $this->success('员工资料接口');
-    }
-
-    /**
-     * 获取部门列表
-     * 一级:部门 dept_name
-     * 二级:工序 big_process(按固定顺序:裁剪、车缝、手工、大烫、总检、包装、其他)
-     * 空工序 = 其他
-     */
-    public function getDepartment()
-    {
-        if (Request::instance()->isGet() == false) {
-            $this->error('非法请求');
-        }
-
-        // 1. 查询数据,空工序转为"其他"
-        $list = db('人员_基本资料')
-            ->field('dept_name, IF(big_process IS NULL OR big_process = "", "其他", big_process) as big_process, COUNT(*) AS count')
-            ->whereNull('mod_rq')
-            ->group('dept_name, big_process')
-            ->select();
-
-        // 2. 构建树形结构(一级:部门,二级:工序)
-        $tree = [];
-        foreach ($list as $item) {
-            $dept = $item['dept_name'];
-            $process = $item['big_process'];
-            $count = $item['count'];
-
-            // 初始化部门节点
-            if (!isset($tree[$dept])) {
-                $tree[$dept] = [
-                    'label' => $dept,
-                    'count' => 0,
-                    'children' => []
-                ];
-            }
-
-            // 部门总人数累加
-            $tree[$dept]['count'] += $count;
-
-            // 工序加入children
-            $tree[$dept]['children'][] = [
-                'label' => $process,
-                'count' => $count
-            ];
-        }
-
-        // 3. 对每个部门下的工序,按固定顺序排序
-        $sortList = ['裁剪', '车缝', '手工', '大烫', '总检', '包装', '其他'];
-        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);
-//    }
-
-    /**
-     * 获取员工列表信息
-    */
-    public function getStaffList(){
-        if (Request::instance()->isGet() == false){
-            $this->error('非法请求');
-        }
-        $params = Request::instance()->param();
-        $where = [];
-
-        if (!empty($params['search'])){
-            $where['staff_no|staff_name'] = array('like','%'.$params['search'].'%');
-        }
-        if (!empty($params['department_code'])){
-            $where['big_process|dept_name'] = $params['department_code'];
-        }
-
-        $limit = $params['limit'];
-        if (empty($limit)){
-            $limit = 15;
-        }
-        $pages = $params['page'];
-        if (empty($pages)){
-            $pages = 1;
-        }
-        $list = db('人员_基本资料')
-            ->field('
-                id,
-                staff_no as 员工编号,
-                staff_name as 员工姓名,
-                gender as 性别,
-                big_process as 生产工序,
-                dept_name as 所在部门,
-                status as 状态,
-                sys_rq as 创建日期,
-                sys_id as 创建人员
-            ')
-            ->where($where)->page($pages)->limit($limit)->order('id desc')->whereNull('mod_rq')->select();
-        $total = db('人员_基本资料')->where($where)->whereNull('mod_rq')->count();
-        $data['list'] = $list;
-        $data['total'] = $total;
-        $this->success('获取员工列表信息',$data);
-    }
-
-
-
-    /**
-     * 获取员工资料
-     * @ApiMethod GET
-     * @params string code
-    */
-    public function getStaffInfo(){
-        if (Request::instance()->isGet() == false){
-            $this->error('非法请求');
-        }
-
-        $params = Request::instance()->param();
-        $where = [];
-        if (isset($params['id'])){
-            $where['id'] = $params['id'];
-        }
-
-        // 1. 查询人员基础信息
-        $staffList = db('人员_基本资料')
-            ->field('
-                id,
-                staff_no as 员工编号,
-                staff_name as 员工姓名,
-                gender as 性别,
-                big_process as 生产工序,
-                dept_name as 所在部门,
-                status as 状态,
-                sys_rq as 创建日期,
-                sys_id as 创建人员
-            ')
-            ->where($where)
-            ->whereNull('mod_rq')
-            ->select();
-
-        $this->success('获取员工资料', $staffList);
-    }
-
-    /**
-     * 新增员工资料
-     */
-    public function PostStaffAdd(){
-        if (Request::instance()->isPost() == false){
-            $this->error('非法请求');
-        }
-
-        $params = Request::instance()->param();
-
-        $staff_no = $params['员工编号'] ?? '';
-        if(empty($staff_no)){
-            $this->error('员工编号不能为空');
-        }
-
-        // 查询数据库是否已存在
-        $Find_list = Db::name('人员_基本资料')->where('staff_no', $staff_no)->whereNull('mod_rq')->find();
-        if($Find_list){
-            $this->error('员工编号已存在');
-        }
-
-        $data = [];
-        $data['staff_no'] = $params['员工编号'];
-        $data['staff_name'] = $params['员工姓名'];
-        $data['gender'] = $params['性别'];
-        $data['big_process'] = $params['生产工序'];
-        $data['dept_name'] = '生产部';
-        $data['status'] = 1;
-        $data['sys_rq'] = date('Y-m-d H:i:s');
-        $data['sys_id'] = $params['sys_id'];
-
-        $res = Db::name('人员_基本资料')->insert($data);
-
-        if ($res !== false){
-            $this->success('新增成功');
-        }else{
-            $this->error('新增失败');
-        }
-    }
-
-
-    /**
-     * 修改员工资料
-     * @ApiMethod POST
-     *
-    */
-    public function PostStaffEdit(){
-        if (!Request::instance()->isPost()){
-            $this->error('非法请求');
-        }
-
-        $params = Request::instance()->param();
-
-        //必传参数校验
-        if (empty($params['id'])){
-            $this->error('参数不能为空');
-        }
-        if (empty($params['员工编号'])){
-            $this->error('员工编号不能为空');
-        }
-
-        //查询员工是否存在(未删除)
-        $info = db('人员_基本资料')
-            ->where('id', $params['id'])
-            ->whereNull('mod_rq')
-            ->find();
-        if (!$info){
-            $this->error('员工不存在');
-        }
-
-        //员工编号重复判断(排除自身)
-        $exist = db('人员_基本资料')
-            ->where('staff_no', $params['员工编号'])
-            ->where('id', '<>', $params['id'])  // 排除自己
-            ->whereNull('mod_rq')
-            ->find();
-        if ($exist) {
-            $this->error('员工编号已存在');
-        }
-
-        //修改数据
-        $data = [];
-        $data['staff_no']      = $params['员工编号'];
-        $data['staff_name']    = $params['员工姓名'];
-        $data['gender']        = $params['性别'];
-        $data['big_process']   = $params['生产工序'];
-        $data['dept_name']     = '生产部';
-        $data['updatetime']    = date('Y-m-d H:i:s');
-        $data['sys_id']        = $params['sys_id'];
-
-        $res = db('人员_基本资料')
-            ->where('id', $params['id'])
-            ->update($data);
-        if ($res !== false){
-            $this->success('更新成功');
-        }else{
-            $this->error('更新失败');
-        }
-    }
-
-    /**
-     * 删除员工资料
-     */
-    public function PostStaffDelete()
-    {
-        if (Request::instance()->isPost() == false){
-            $this->error('非法请求');
-        }
-        $params = Request::instance()->param();
-        if (empty($params['id'])){
-            $this->error('参数不能为空');
-        }
-        $staffCode = $params['id'];
-        $sql = db('人员_基本资料')->where('id',$staffCode)->fetchSql(true)->update(['mod_rq'=>date('Y-m-d H:i:s')]);
-        $res = Db::query($sql);
-        if ($res !== false){
-            $this->success('删除成功');
-        }else{
-            $this->error('删除失败');
-        }
-    }
-
-    /**
-     * 获取小组
-     */
-    public function GetDeviceNameList(){
-        if (!Request::instance()->isGet()){
-            $this->error('非法请求');
-        }
-
-        // 定义需要过滤、不返回给前端的设备编组
-        $filterGroups = [
-            '自动裁床01组',
-            '自动裁床02组',
-            '模板机01组',
-            '模板机02组',
-            '上袖机01组',
-            '上袖机02组',
-            '裁床1',
-            '裁床2',
-            '模版机'
-        ];
-
-        $list = Db::name('设备_基本资料')
-            ->field('UniqId,设备编组,生产工序,工序')
-            ->where('设备名称', 1)
-            ->whereNull('mod_rq')
-            ->whereNotIn('设备编组', $filterGroups) // 过滤不想要的数据
-            ->group('设备编组')
-            ->order('工序,UniqId asc')
-            ->select();
-
-        $this->success('获取设备编组成功', $list);
-    }
-
-    /**
-     * 获取小组及人员列表
-     * 支持按工序/小组名称搜索,返回每个小组下的成员信息
-     */
-    public function GetTeamStaffList()
-    {
-        if (Request::instance()->isGet() == false) {
-            $this->error('非法请求');
-        }
-        $params = Request::instance()->param();
-
-        $where = [];
-        if (!empty($params['search'])) {
-            $where['b.staff_no|b.staff_name|a.team_name|b.big_process'] = array('like', '%' . $params['search'] . '%');
-        }
-
-        $list = Db::name('人员_小组资料')->alias('a')
-            ->field('
-            b.staff_no as 员工编号,
-            b.staff_name as 员工姓名,
-            a.*
-        ')
-            ->join('人员_基本资料 b', 'a.staff_no = b.staff_no')
-            ->where($where)
-            ->whereNull('a.mod_rq')
-            ->whereNull('b.mod_rq')
-            ->order('a.id asc')
-            ->select();
-
-        // 整理成"小组+成员"的树形结构
-        $result = [];
-        foreach ($list as $item) {
-            $teamId = $item['team_id'];
-            if (!isset($result[$teamId])) {
-                $result[$teamId] = [
-                    '小组ID' => $teamId,
-                    '小组名称' => $item['team_name'],
-                    '生产工序' => $item['big_process'],
-                    'staff_list' => []
-                ];
-            }
-
-            $member = [
-                'id' => $item['id'],
-                '员工编号' => $item['员工编号'],
-                '员工姓名' => $item['员工姓名'],
-                '小组名称' => $item['team_name'],
-                '职位' => $item['position']
-            ];
-
-            // 组长排第一个
-            if ($item['position'] == '组长') {
-                array_unshift($result[$teamId]['staff_list'], $member);
-            } else {
-                $result[$teamId]['staff_list'][] = $member;
-            }
-        }
-
-        // 转为索引数组返回
-        $data = array_values($result);
-        $this->success('获取成功', $data);
-    }
-
-    /**
-     * 新增小组人员
-     */
-    public function AddTeamStaff()
-    {
-        if (!Request::instance()->isPost()) {
-            $this->error('非法请求');
-        }
-
-        $params = Request::instance()->param();
-
-        // 1. 校验员工是否存在
-        $staffInfo = Db::name('人员_基本资料')
-            ->where('staff_no', $params['staff_no'])
-            ->whereNull('mod_rq')
-            ->find();
-        if (!$staffInfo) {
-            $this->error('员工不存在或已删除');
-        }
-
-        // 2. 校验该员工是否已在小组中
-        $exist = Db::name('人员_小组资料')
-            ->where('staff_no', $params['staff_no'])
-            ->whereNull('mod_rq')
-            ->find();
-        if ($exist) {
-            $this->error('该员工已在'.$exist['team_name'].'小组中');
-        }
-
-        // 3. 组装数据
-        $data = [
-            'big_process' => $params['big_process'],
-            'team_id' => $params['team_id'],
-            'team_name' => $params['team_name'],
-            'staff_no' => $params['staff_no'],
-            'position' => $params['position'],
-            'status' => 1,
-            'sys_id' => $params['sys_id'],
-            'createtime' => date('Y-m-d H:i:s')
-        ];
-
-        $res = Db::name('人员_小组资料')->insert($data);
-        if ($res) {
-            $this->success('新增成功');
-        } else {
-            $this->error('新增失败');
-        }
-    }
-
-    /**
-     * 修改小组人员
-     */
-    public function EditTeamStaff()
-    {
-        if (!Request::instance()->isPost()) {
-            $this->error('非法请求');
-        }
-
-        $params = Request::instance()->param();
-
-        // 必传参数校验
-        if (empty($params['id'])) {
-            $this->error('参数不完整');
-        }
-
-        // 1. 校验该记录是否存在
-        $info = Db::name('人员_小组资料')
-            ->where('id', $params['id'])
-            ->whereNull('mod_rq')
-            ->find();
-        if (!$info) {
-            $this->error('记录不存在或已删除');
-        }
-
-        // 2. 如果修改了员工ID,校验新员工是否已在该小组中(排除自身)
-        if (!empty($params['staff_id']) && $params['staff_id'] != $info['staff_id']) {
-            $exist = Db::name('人员_小组资料')
-                ->where('team_id', $params['team_id'] ?? $info['team_id'])
-                ->where('staff_id', $params['staff_id'])
-                ->where('id', '<>', $params['id'])
-                ->whereNull('mod_rq')
-                ->find();
-            if ($exist) {
-                $this->error('该员工已在此小组中');
-            }
-        }
-
-        // 3. 组装更新数据
-        $data = [];
-        if (isset($params['big_process'])) $data['big_process'] = $params['big_process'];
-        if (isset($params['team_id'])) $data['team_id'] = $params['team_id'];
-        if (isset($params['team_name'])) $data['team_name'] = $params['team_name'];
-        if (isset($params['position'])) $data['position'] = $params['position'];
-        if (isset($params['staff_id'])) {
-            $data['staff_id'] = $params['staff_id'];
-            // 更新员工姓名
-            $staffInfo = Db::name('人员_基本资料')->where('id', $params['staff_id'])->find();
-            if ($staffInfo) {
-                $data['staff_name'] = $staffInfo['staff_name'];
-            }
-        }
-        if (isset($params['position'])) $data['position'] = $params['position'];
-        $data['updatetime'] = date('Y-m-d H:i:s');
-
-        $res = Db::name('人员_小组资料')
-            ->where('id', $params['id'])
-            ->update($data);
-
-        if ($res !== false) {
-            $this->success('小组更换成功');
-        } else {
-            $this->error('小组更换失败');
-        }
-    }
-
-    /**
-     * 删除小组人员
-     */
-    public function DelTeamStaff()
-    {
-        if (!Request::instance()->isPost()) {
-            $this->error('非法请求');
-        }
-
-        $params = Request::instance()->param();
-
-        $res = Db::name('人员_小组资料')
-            ->where('id', $params['id'])
-            ->update([
-                'mod_rq' => date('Y-m-d H:i:s')
-            ]);
-
-        if ($res !== false) {
-            $this->success('删除成功');
-        } else {
-            $this->error('删除失败');
-        }
-    }
-
-}
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use \think\Request;
+use \think\Db;
+use Overtrue\Pinyin\Pinyin;
+/**
+ * 员工资料接口
+ */
+class Staff extends Api
+{
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+    /**
+     * 首页
+     */
+    public function index()
+    {
+        $this->success('员工资料接口');
+    }
+
+    /**
+     * 获取部门列表
+     * 一级:部门 dept_name
+     * 二级:工序 big_process(按固定顺序:裁剪、车缝、手工、大烫、总检、包装、其他)
+     * 空工序 = 其他
+     */
+    public function getDepartment()
+    {
+        if (Request::instance()->isGet() == false) {
+            $this->error('非法请求');
+        }
+
+        // 1. 查询数据,空工序转为"其他"
+        $list = db('人员_基本资料')
+            ->field('dept_name, IF(big_process IS NULL OR big_process = "", "其他", big_process) as big_process, COUNT(*) AS count')
+            ->whereNull('mod_rq')
+            ->group('dept_name, big_process')
+            ->select();
+
+        // 2. 构建树形结构(一级:部门,二级:工序)
+        $tree = [];
+        foreach ($list as $item) {
+            $dept = $item['dept_name'];
+            $process = $item['big_process'];
+            $count = $item['count'];
+
+            // 初始化部门节点
+            if (!isset($tree[$dept])) {
+                $tree[$dept] = [
+                    'label' => $dept,
+                    'count' => 0,
+                    'children' => []
+                ];
+            }
+
+            // 部门总人数累加
+            $tree[$dept]['count'] += $count;
+
+            // 工序加入children
+            $tree[$dept]['children'][] = [
+                'label' => $process,
+                'count' => $count
+            ];
+        }
+
+        // 3. 对每个部门下的工序,按固定顺序排序
+        $sortList = ['裁剪', '车缝', '手工', '大烫', '总检', '包装', '其他'];
+        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);
+//    }
+
+    /**
+     * 获取员工列表信息
+    */
+    public function getStaffList(){
+        if (Request::instance()->isGet() == false){
+            $this->error('非法请求');
+        }
+        $params = Request::instance()->param();
+        $where = [];
+
+        if (!empty($params['search'])){
+            $where['staff_no|staff_name|pinyin'] = array('like','%'.$params['search'].'%');
+        }
+        if (!empty($params['department_code'])){
+            $where['big_process|dept_name'] = $params['department_code'];
+        }
+
+        $limit = $params['limit'];
+        if (empty($limit)){
+            $limit = 15;
+        }
+        $pages = $params['page'];
+        if (empty($pages)){
+            $pages = 1;
+        }
+        $list = db('人员_基本资料')
+            ->field('
+                id,
+                staff_no as 员工编号,
+                staff_name as 员工姓名,
+                gender as 性别,
+                big_process as 生产工序,
+                dept_name as 所在部门,
+                status as 状态,
+                sys_rq as 创建日期,
+                pinyin as pinyin,
+                sys_id as 创建人员
+            ')
+            ->where($where)->page($pages)->limit($limit)->order('id desc')->whereNull('mod_rq')->select();
+        $total = db('人员_基本资料')->where($where)->whereNull('mod_rq')->count();
+        $data['list'] = $list;
+        $data['total'] = $total;
+        $this->success('获取员工列表信息',$data);
+    }
+
+
+
+    /**
+     * 获取员工资料
+     * @ApiMethod GET
+     * @params string code
+    */
+    public function getStaffInfo(){
+        if (Request::instance()->isGet() == false){
+            $this->error('非法请求');
+        }
+
+        $params = Request::instance()->param();
+        $where = [];
+        if (isset($params['id'])){
+            $where['id'] = $params['id'];
+        }
+
+        // 1. 查询人员基础信息
+        $staffList = db('人员_基本资料')
+            ->field('
+                id,
+                staff_no as 员工编号,
+                staff_name as 员工姓名,
+                gender as 性别,
+                big_process as 生产工序,
+                dept_name as 所在部门,
+                status as 状态,
+                sys_rq as 创建日期,
+                sys_id as 创建人员
+            ')
+            ->where($where)
+            ->whereNull('mod_rq')
+            ->select();
+
+        $this->success('获取员工资料', $staffList);
+    }
+
+    /**
+     * 新增员工资料
+     */
+    public function PostStaffAdd(){
+        if (Request::instance()->isPost() == false){
+            $this->error('非法请求');
+        }
+
+        $params = Request::instance()->param();
+
+        $staff_no = $params['员工编号'] ?? '';
+        if(empty($staff_no)){
+            $this->error('员工编号不能为空');
+        }
+
+        // 查询数据库是否已存在
+        $Find_list = Db::name('人员_基本资料')->where('staff_no', $staff_no)->whereNull('mod_rq')->find();
+        if($Find_list){
+            $this->error('员工编号已存在');
+        }
+
+        $data = [];
+        $data['staff_no'] = $params['员工编号'];
+        $data['staff_name'] = $params['员工姓名'];
+        $data['gender'] = $params['性别'];
+        $data['big_process'] = $params['生产工序'];
+        $data['dept_name'] = '生产部';
+        $data['status'] = 1;
+        $data['sys_rq'] = date('Y-m-d H:i:s');
+        $data['sys_id'] = $params['sys_id'];
+
+        $res = Db::name('人员_基本资料')->insert($data);
+
+        if ($res !== false){
+            $this->success('新增成功');
+        }else{
+            $this->error('新增失败');
+        }
+    }
+
+
+    /**
+     * 修改员工资料
+     * @ApiMethod POST
+     *
+    */
+    public function PostStaffEdit(){
+        if (!Request::instance()->isPost()){
+            $this->error('非法请求');
+        }
+
+        $params = Request::instance()->param();
+
+        //必传参数校验
+        if (empty($params['id'])){
+            $this->error('参数不能为空');
+        }
+        if (empty($params['员工编号'])){
+            $this->error('员工编号不能为空');
+        }
+
+        //查询员工是否存在(未删除)
+        $info = db('人员_基本资料')
+            ->where('id', $params['id'])
+            ->whereNull('mod_rq')
+            ->find();
+        if (!$info){
+            $this->error('员工不存在');
+        }
+
+        //员工编号重复判断(排除自身)
+        $exist = db('人员_基本资料')
+            ->where('staff_no', $params['员工编号'])
+            ->where('id', '<>', $params['id'])  // 排除自己
+            ->whereNull('mod_rq')
+            ->find();
+        if ($exist) {
+            $this->error('员工编号已存在');
+        }
+
+        //修改数据
+        $data = [];
+        $data['staff_no']      = $params['员工编号'];
+        $data['staff_name']    = $params['员工姓名'];
+        $data['gender']        = $params['性别'];
+        $data['big_process']   = $params['生产工序'];
+        $data['dept_name']     = '生产部';
+        $data['updatetime']    = date('Y-m-d H:i:s');
+        $data['sys_id']        = $params['sys_id'];
+
+        $res = db('人员_基本资料')
+            ->where('id', $params['id'])
+            ->update($data);
+        if ($res !== false){
+            $this->success('更新成功');
+        }else{
+            $this->error('更新失败');
+        }
+    }
+
+    /**
+     * 删除员工资料
+     */
+    public function PostStaffDelete()
+    {
+        if (Request::instance()->isPost() == false){
+            $this->error('非法请求');
+        }
+        $params = Request::instance()->param();
+        if (empty($params['id'])){
+            $this->error('参数不能为空');
+        }
+        $staffCode = $params['id'];
+        $sql = db('人员_基本资料')->where('id',$staffCode)->fetchSql(true)->update(['mod_rq'=>date('Y-m-d H:i:s')]);
+        $res = Db::query($sql);
+        if ($res !== false){
+            $this->success('删除成功');
+        }else{
+            $this->error('删除失败');
+        }
+    }
+
+    /**
+     * 获取小组
+     */
+    public function GetDeviceNameList(){
+        if (!Request::instance()->isGet()){
+            $this->error('非法请求');
+        }
+
+        // 定义需要过滤、不返回给前端的设备编组
+        $filterGroups = [
+            '自动裁床01组',
+            '自动裁床02组',
+            '模板机01组',
+            '模板机02组',
+            '上袖机01组',
+            '上袖机02组',
+            '裁床1',
+            '裁床2',
+            '模版机'
+        ];
+
+        $list = Db::name('设备_基本资料')
+            ->field('UniqId,设备编组,生产工序,工序')
+            ->where('设备名称', 1)
+            ->whereNull('mod_rq')
+            ->whereNotIn('设备编组', $filterGroups) // 过滤不想要的数据
+            ->group('设备编组')
+            ->order('工序,UniqId asc')
+            ->select();
+
+        $this->success('获取设备编组成功', $list);
+    }
+
+    /**
+     * 获取小组及人员列表
+     * 支持按工序/小组名称搜索,返回每个小组下的成员信息
+     */
+    public function GetTeamStaffList()
+    {
+        if (Request::instance()->isGet() == false) {
+            $this->error('非法请求');
+        }
+        $params = Request::instance()->param();
+
+        $where = [];
+        if (!empty($params['search'])) {
+            $where['b.staff_no|b.staff_name|a.team_name|b.big_process'] = array('like', '%' . $params['search'] . '%');
+        }
+
+        $list = Db::name('人员_小组资料')->alias('a')
+            ->field('
+            b.staff_no as 员工编号,
+            b.staff_name as 员工姓名,
+            a.*
+        ')
+            ->join('人员_基本资料 b', 'a.staff_no = b.staff_no')
+            ->where($where)
+            ->whereNull('a.mod_rq')
+            ->whereNull('b.mod_rq')
+            ->order('a.id asc')
+            ->select();
+
+        // 整理成"小组+成员"的树形结构
+        $result = [];
+        foreach ($list as $item) {
+            $teamId = $item['team_id'];
+            if (!isset($result[$teamId])) {
+                $result[$teamId] = [
+                    '小组ID' => $teamId,
+                    '小组名称' => $item['team_name'],
+                    '生产工序' => $item['big_process'],
+                    'staff_list' => []
+                ];
+            }
+
+            $member = [
+                'id' => $item['id'],
+                '员工编号' => $item['员工编号'],
+                '员工姓名' => $item['员工姓名'],
+                '小组名称' => $item['team_name'],
+                '职位' => $item['position']
+            ];
+
+            // 组长排第一个
+            if ($item['position'] == '组长') {
+                array_unshift($result[$teamId]['staff_list'], $member);
+            } else {
+                $result[$teamId]['staff_list'][] = $member;
+            }
+        }
+
+        // 转为索引数组返回
+        $data = array_values($result);
+        $this->success('获取成功', $data);
+    }
+
+    /**
+     * 新增小组人员
+     */
+    public function AddTeamStaff()
+    {
+        if (!Request::instance()->isPost()) {
+            $this->error('非法请求');
+        }
+
+        $params = Request::instance()->param();
+
+        // 1. 校验员工是否存在
+        $staffInfo = Db::name('人员_基本资料')
+            ->where('staff_no', $params['staff_no'])
+            ->whereNull('mod_rq')
+            ->find();
+        if (!$staffInfo) {
+            $this->error('员工不存在或已删除');
+        }
+
+        // 2. 校验该员工是否已在小组中
+        $exist = Db::name('人员_小组资料')
+            ->where('staff_no', $params['staff_no'])
+            ->whereNull('mod_rq')
+            ->find();
+        if ($exist) {
+            $this->error('该员工已在'.$exist['team_name'].'小组中');
+        }
+
+        // 3. 组装数据
+        $data = [
+            'big_process' => $params['big_process'],
+            'team_id' => $params['team_id'],
+            'team_name' => $params['team_name'],
+            'staff_no' => $params['staff_no'],
+            'position' => $params['position'],
+            'status' => 1,
+            'sys_id' => $params['sys_id'],
+            'createtime' => date('Y-m-d H:i:s')
+        ];
+
+        $res = Db::name('人员_小组资料')->insert($data);
+        if ($res) {
+            $this->success('新增成功');
+        } else {
+            $this->error('新增失败');
+        }
+    }
+
+    /**
+     * 修改小组人员
+     */
+    public function EditTeamStaff()
+    {
+        if (!Request::instance()->isPost()) {
+            $this->error('非法请求');
+        }
+
+        $params = Request::instance()->param();
+
+        // 必传参数校验
+        if (empty($params['id'])) {
+            $this->error('参数不完整');
+        }
+
+        // 1. 校验该记录是否存在
+        $info = Db::name('人员_小组资料')
+            ->where('id', $params['id'])
+            ->whereNull('mod_rq')
+            ->find();
+        if (!$info) {
+            $this->error('记录不存在或已删除');
+        }
+
+        // 2. 如果修改了员工ID,校验新员工是否已在该小组中(排除自身)
+        if (!empty($params['staff_id']) && $params['staff_id'] != $info['staff_id']) {
+            $exist = Db::name('人员_小组资料')
+                ->where('team_id', $params['team_id'] ?? $info['team_id'])
+                ->where('staff_id', $params['staff_id'])
+                ->where('id', '<>', $params['id'])
+                ->whereNull('mod_rq')
+                ->find();
+            if ($exist) {
+                $this->error('该员工已在此小组中');
+            }
+        }
+
+        // 3. 组装更新数据
+        $data = [];
+        if (isset($params['big_process'])) $data['big_process'] = $params['big_process'];
+        if (isset($params['team_id'])) $data['team_id'] = $params['team_id'];
+        if (isset($params['team_name'])) $data['team_name'] = $params['team_name'];
+        if (isset($params['position'])) $data['position'] = $params['position'];
+        if (isset($params['staff_id'])) {
+            $data['staff_id'] = $params['staff_id'];
+            // 更新员工姓名
+            $staffInfo = Db::name('人员_基本资料')->where('id', $params['staff_id'])->find();
+            if ($staffInfo) {
+                $data['staff_name'] = $staffInfo['staff_name'];
+            }
+        }
+        if (isset($params['position'])) $data['position'] = $params['position'];
+        $data['updatetime'] = date('Y-m-d H:i:s');
+
+        $res = Db::name('人员_小组资料')
+            ->where('id', $params['id'])
+            ->update($data);
+
+        if ($res !== false) {
+            $this->success('小组更换成功');
+        } else {
+            $this->error('小组更换失败');
+        }
+    }
+
+    /**
+     * 删除小组人员
+     */
+    public function DelTeamStaff()
+    {
+        if (!Request::instance()->isPost()) {
+            $this->error('非法请求');
+        }
+
+        $params = Request::instance()->param();
+
+        $res = Db::name('人员_小组资料')
+            ->where('id', $params['id'])
+            ->update([
+                'mod_rq' => date('Y-m-d H:i:s')
+            ]);
+
+        if ($res !== false) {
+            $this->success('删除成功');
+        } else {
+            $this->error('删除失败');
+        }
+    }
+
+}

+ 101 - 58
application/api/controller/WorkOrder.php

@@ -29,11 +29,19 @@ class WorkOrder extends Api
      * - api_key:API 调用密钥(Token)
      * - api_url:对应功能的服务端地址
      */
+    // protected $config = [
+    //     //文生文-gtp-4
+    //     'txttotxtgtp' => [
+    //         'api_key' => 'sk-fxlawqVtbbQbNW0wInR3E4wsLo5JHozDC2XOHzMa711su6ss',
+    //         'api_url' => 'https://chatapi.onechats.top/v1/chat/completions'
+    //     ]
+    // ];
+
     protected $config = [
-        //文生文-gtp-4
+        //文生文-deepseek
         'txttotxtgtp' => [
-            'api_key' => 'sk-fxlawqVtbbQbNW0wInR3E4wsLo5JHozDC2XOHzMa711su6ss',
-            'api_url' => 'https://chatapi.onechats.top/v1/chat/completions'
+            'api_key' => 'sk-e573426fbb244f65b50397bffeae8d79',
+            'api_url' => 'https://api.deepseek.com/chat/completions'
         ]
     ];
 
@@ -985,7 +993,7 @@ class WorkOrder extends Api
             $text = $this->fixPdfText($text);
 
 
-            $apiKey = 'sk-fxlawqVtbbQbNW0wInR3E4wsLo5JHozDC2XOHzMa711su6ss';
+            $apiKey = 'sk-e573426fbb244f65b50397bffeae8d79';
 
             // 构建订单数据字符串
             $order_data_str = "工单_基本资料:\n";
@@ -1610,7 +1618,7 @@ class WorkOrder extends Api
             $this->error('参数错误');
         }
 
-            //获取当前订单PO款号
+        //获取当前订单PO款号
         $res = \db('工单_印件资料')
             ->field('款号')
             ->where('订单编号',$param['order'])
@@ -2237,28 +2245,28 @@ class WorkOrder extends Api
             if (is_array($mianliao)) {
                 foreach ($mianliao as $value) {
                     if (!empty($value) && $value !== '粘衬') { // 排除空值和粘衬
-                    $category = '';
+                        $category = '';
 
-                    // 查找物料对应的分类
-                    foreach ($materialCategories as $cat => $materials) {
-                        if (in_array($value, $materials)) {
-                            $category = $cat;
-                            break;
+                        // 查找物料对应的分类
+                        foreach ($materialCategories as $cat => $materials) {
+                            if (in_array($value, $materials)) {
+                                $category = $cat;
+                                break;
+                            }
                         }
-                    }
 
-                    // 如果找到分类,将数据存入BOM
-                    $data[] = [
-                        'BOM_工单编号' => $list['订单编号'],
-                        'BOM_物料名称' => $value,
-                        'BOM_desc' => '',
-                        '物料分类' => $category ? $category : '',
-                        'Sys_rq' => date('Y-m-d H:i:s'),
-                        'Sys_ID' => $list['Sys_id']
-                    ];
+                        // 如果找到分类,将数据存入BOM
+                        $data[] = [
+                            'BOM_工单编号' => $list['订单编号'],
+                            'BOM_物料名称' => $value,
+                            'BOM_desc' => '',
+                            '物料分类' => $category ? $category : '',
+                            'Sys_rq' => date('Y-m-d H:i:s'),
+                            'Sys_ID' => $list['Sys_id']
+                        ];
+                    }
                 }
             }
-            }
             foreach ($data as &$item) {
                 if (empty($item['物料分类'])) {
                     $item['物料分类'] = '';
@@ -2807,57 +2815,92 @@ class WorkOrder extends Api
 
     public function Gpt($massage)
     {
-        $apiKey = 'sk-fxlawqVtbbQbNW0wInR3E4wsLo5JHozDC2XOHzMa711su6ss';
-        $messages = [
-            [
-                'role' => 'user',
-                'content' => '你好,帮我按照:面料1、面料2、面料3……来整理归纳下面的面料信息,我只需要面料信息,不需要其他:'.$massage
-            ]
-        ];
+        $config = $this->config['txttotxtgtp'];
+
         $data = [
-            'model' => 'gpt-4.1',
-            'messages' => $messages,
-            'max_tokens' => 1024,
-            'temperature' => 0.7,
+            'model' => 'deepseek-flash',
+            'messages' => [
+                [
+                    'role' => 'system',
+                    'content' => '你是面料信息整理助手。只整理用户提供的信息,不要编造。'
+                ],
+                [
+                    'role' => 'user',
+                    'content' => '请整理下面的面料信息,每种面料单独一行,'
+                        . '严格按照“面料1:内容”“面料2:内容”的格式输出,'
+                        . '不要使用Markdown,不要输出其他说明:' . $massage
+                ]
+            ],
+            'thinking' => [
+                'type' => 'enabled'
+            ],
+            'reasoning_effort' => 'high',
+            'stream' => false,
+            // 为思考和最终回答预留足够空间
+            'max_tokens' => 8192
         ];
 
-        // 初始化 cURL 请求
-        $ch = curl_init('https://chatapi.onechats.top/v1/chat/completions');
-        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-        curl_setopt($ch, CURLOPT_HTTPHEADER, [
-            'Content-Type: application/json',
-            'Authorization: Bearer ' . $apiKey,
-        ]);
-        curl_setopt($ch, CURLOPT_POST, true);
-        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
+        $json = json_encode($data, JSON_UNESCAPED_UNICODE);
+        if ($json === false) {
+            return '请求数据编码失败:' . json_last_error_msg();
+        }
 
-        // 跳过证书检查可暂时避免 pem 文件配置失败(测试用)
-        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+        $ch = curl_init($config['api_url']);
+
+        curl_setopt_array($ch, [
+            CURLOPT_RETURNTRANSFER => true,
+            CURLOPT_POST => true,
+            CURLOPT_HTTPHEADER => [
+                'Content-Type: application/json',
+                'Authorization: Bearer ' . $config['api_key']
+            ],
+            CURLOPT_POSTFIELDS => $json,
+            CURLOPT_CONNECTTIMEOUT => 15,
+            CURLOPT_TIMEOUT => 180,
+            CURLOPT_SSL_VERIFYPEER => true,
+            CURLOPT_SSL_VERIFYHOST => 2
+        ]);
 
-        // 执行请求
         $response = curl_exec($ch);
+        $curlError = curl_error($ch);
+        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+        curl_close($ch);
 
-        // 错误处理
-        if (curl_errno($ch)) {
-            return '请求失败: ' . curl_error($ch);
+        if ($response === false) {
+            return '请求失败:' . $curlError;
         }
-        curl_close($ch);
 
-        // 解析响应
         $responseData = json_decode($response, true);
 
-        if (isset($responseData['choices'][0]['message']['content'])) {
-            $gptReply = $responseData['choices'][0]['message']['content'];
+        if (json_last_error() !== JSON_ERROR_NONE) {
+            return '响应解析失败:' . json_last_error_msg();
+        }
 
-            // 使用正则提取“面料X:XXX”的内容
-            preg_match_all('/面料\d+[::](.+)/u', $gptReply, $matches);
+        if ($httpCode < 200 || $httpCode >= 300) {
+            $errorMessage = $responseData['error']['message'] ?? '未知错误';
+            return 'DeepSeek 请求失败(HTTP ' . $httpCode . '):' . $errorMessage;
+        }
 
-            return $matches[1] ?? [];
-        } else {
-            return '未能获取 GPT 回复';
+        if (($responseData['choices'][0]['finish_reason'] ?? '') === 'length') {
+            return '回复被截断,请增加 max_tokens 或减少输入内容';
         }
-    }
 
+        // content 是最终回答,reasoning_content 是思考内容
+        $reply = $responseData['choices'][0]['message']['content'] ?? '';
+
+        if (trim($reply) === '') {
+            return '未能获取 DeepSeek 回复';
+        }
+
+        // 提取每行“面料X:”后面的内容,保持原来的数组返回形式
+        preg_match_all('/^\h*面料\h*\d+\h*[::]\h*(.+)$/mu', $reply, $matches);
+
+        if (empty($matches[1])) {
+            return '回复格式不符合预期,未匹配到面料信息';
+        }
+
+        return array_map('trim', $matches[1]);
+    }
     /**
      * 面料库存月份查询
      */

+ 1 - 0
application/api/controller/WorkOrderSpotCheck.php

@@ -2909,6 +2909,7 @@ class WorkOrderSpotCheck extends Api{
                 ->group('c.BOM_物料名称')
                 ->select();
 
+
             foreach ($res as $index => &$item) {
                 $item['物料编码'] = $item['生产款号'] . '-' . $item['颜色备注'] . ($index + 1);
             }