Browse Source

first commit

liuhairui 9 tháng trước cách đây
mục cha
commit
bd378180fa

+ 120 - 1
application/api/controller/Index.php

@@ -3,6 +3,8 @@ namespace app\api\controller;
 use app\common\controller\Api;
 use think\Controller;
 use think\Db;
+use think\Request;
+
 /**
  * 达成大屏数据接口
  */
@@ -617,14 +619,131 @@ class Index extends Controller{
         // 返回JSON格式的结果
         return json($res);
     }
+    public function jtClDbToRedis(){
+        //本站API接口地址和OpenAI接口格式完全一致,请自行替换。
+        //API地址: https://api.julianapi.com
+        $url = "https://api.julianapi.com";
+    }
 
 
 
 
+    public function GtpAiOrder()
+    {
+        $param = Request::instance()->get();
+        $maxOrder = \db('工单_基本资料')
+            ->where('订单编号', 'like', "{$param['订单编号']}%")
+            ->order('订单编号', 'desc')
+            ->limit(1)
+            ->value('订单编号');
+
+        $list = \db('工单_基本资料')
+            ->where('订单编号', 'like', "{$param['订单编号']}%")
+            ->order('订单编号', 'desc')
+            ->limit(1)
+            ->find();
+        if($list['面料'] == ''){
+            $this->error('面料数据为空无法定义BOM');
+        }
+        if ($maxOrder) {
+            $numericPart = substr($maxOrder, 2);
+            $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT);
+            $param['订单编号'] = $param['订单编号'] . $newNumericPart;
+        } else {
+            $param['订单编号'] = $param['订单编号'];
+        }
+        if($list['粘衬'] == '' || $list['粘衬'] == '无'){
+            $massage = $list['面料'];
+        }else{
+            $massage = $list['面料'].',粘衬:'.$list['粘衬'];
+        }
+        $mianliao = $this->Gpt($massage);
+        $data = [];
+
+        foreach ($mianliao as $key => $value){
+            $data[$key] = [
+                'BOM_工单编号' => $list['订单编号'],
+                'BOM_物料名称' => $value,
+                'Sys_rq' => date('Y-m-d H:i:s',time()),
+                'Sys_ID' => '超级管理员'
+            ];
+        }
 
-    public function jtClDbToRedis(){
+        //插入订单物料信息
+        $BomSql = \db('工单_bom资料')->fetchSql(true)->insertAll($data);
+        $BomRes = \db()->query($BomSql);
+
+        $this->success('成功');
 
     }
 
+    //GPT
+    public function Gpt($massage)
+    {
+
+
+        // 设置 API 密钥
+        $apiKey = 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY'; // 替换为您的 API 密钥
+
+        // 要发送给 GPT 的消息
+        $messages = [
+            [
+                'role' => 'user',
+                'content' => '你好,帮我按照:面料1、面料2、面料3……来整理归纳下面的面料信息,我只需要面料信息,不需要其他:'.$massage
+            ]
+        ];
+
+        // 创建请求数据
+        $data = [
+            'model' => 'gpt-3.5-turbo', // 使用的模型
+            'messages' => $messages,
+            'max_tokens' => 100, // 设置最大 token 数
+        ];
+
+        // 初始化 cURL
+        $ch = curl_init('https://niubi.zeabur.app/v1/chat/completions');
+
+        // 设置 cURL 选项
+        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));
+        curl_setopt($ch, CURLOPT_CAINFO, ROOT_PATH . '/public/uploads/cacert.pem');
+
+        // 执行请求
+        $response = curl_exec($ch);
+
+        // 检查错误
+        if (curl_errno($ch)) {
+            echo 'Error:' . curl_error($ch);
+        }
+
+        // 关闭 cURL
+        curl_close($ch);
+
+        // 解析和输出响应
+        $responseData = json_decode($response, true);
+
+        // 获取 GPT 的回复
+        if (isset($responseData['choices'][0]['message']['content'])) {
+            //获取返回内容
+            $gptReply = $responseData['choices'][0]['message']['content'];
+//            halt($gptReply);
+            //返回面料信息
+            $gptArray = explode('面料',$gptReply);
+            array_shift($gptArray);
+            foreach ($gptArray as $key=>$value){
+                $gptArray[$key] = preg_replace('/\s+/', '', substr($value,4));
+            }
+            return $gptArray;
+        } else {
+            echo "未能获取 GPT 的回复。";
+        }
+    }
+
+
 }
 

+ 94 - 23
application/api/controller/Manufacture.php

@@ -1142,27 +1142,33 @@ class Manufacture extends Api
      */
     public function Approval()
     {
-        if ($this->request->isPost() === false){
+        if ($this->request->isPost() === false) {
             $this->error('请求错误');
         }
         $param = Request::instance()->post();
-        if (empty($param)){
+        if (empty($param)) {
             $this->error('参数错误');
         }
-        $data['审核'] = $param['sys_id'];
+        // 判断sys_id是否为多个,多个以逗号分隔
+        $sys_ids = strpos($param['sys_id'], ',') !== false ? explode(',', $param['sys_id']) : [$param['sys_id']];
         $data['审核日期'] = date('Y-m-d H:i:s');
-        $sql = \db('工单_基本资料')
-            ->where('Uniqid',$param['Uniqid'])
-            ->fetchSql(true)
-            ->update($data);
-        $res = \db()->query($sql);
-        if ($res === false){
-            $this->error('审核失败');
-        }else{
-            $this->success('审核成功');
+        // 遍历所有sys_id进行更新
+        foreach ($sys_ids as $sys_id) {
+            $data['审核'] = $sys_id;
+            $sql = \db('工单_基本资料')
+                ->where('Uniqid', $param['Uniqid'])
+                ->fetchSql(true)
+                ->update($data);
+            // 执行SQL语句
+            $res = \db()->query($sql);
+            if ($res === false) {
+                $this->error('审核失败');
+            }
         }
+        $this->success('审核成功');
     }
 
+
     /**
      * 订单未审核列表
      * @return void
@@ -1194,16 +1200,10 @@ class Manufacture extends Api
             ->whereIn('关联编号', $orderIds)
             ->whereIn('附件备注', '技术附件')
             ->column('关联编号');
-//        // 遍历数据,为每个订单设置 status
-//        foreach ($list as &$value) {
-//            if (in_array($value['订单编号'], $relatedOrders)) {
-//                $value['status'] = '';
-//            } else {
-//                $value['status'] = '*';
-//            }
-//        }
-//        $data['total'] = count($list);
-//        $data['table'] = $list;
+        $orderAttachments = \db('工单_相关附件')
+            ->whereIn('关联编号', $orderIds)
+            ->whereIn('附件备注', '订单资料附件')
+            ->column('关联编号');
         // 遍历数据,为每个订单设置 status,并替换字段名
         foreach ($list as &$value) {
             // 设置订单状态
@@ -1212,7 +1212,11 @@ class Manufacture extends Api
             } else {
                 $value['status'] = '*';
             }
-
+            if (in_array($value['订单编号'], $orderAttachments)) {
+                $value['orderstatus'] = '';
+            } else {
+                $value['orderstatus'] = '*';
+            }
             // 将「工单入仓数量」字段替换为「工单完工数量」
             if (isset($value['工单入仓数量'])) {
                 $value['工单完工数量'] = $value['工单入仓数量'];
@@ -1224,6 +1228,73 @@ class Manufacture extends Api
         $this->success('成功',$data);
     }
 
+//    public function UnapprovalList()
+//    {
+//        if ($this->request->isGet() === false){
+//            $this->error('请求错误');
+//        }
+//
+//        $param = $this->request->param();
+//        if (isset($param)){
+//            $where['订单编号|生产款号'] = ['like','%'.$param['search'].'%'];
+//        }
+//
+//        // 其他条件
+//        $where['Mod_rq'] = null;
+//
+//        // 查询工单基本资料
+//        $list = \db('工单_基本资料')
+//            ->where($where)
+//            ->orderRaw("FIELD(gd_statu, '1-计划中', '2-生产中')")
+//            ->order('订单编号 desc')
+//            ->select();
+//
+//        // 提取所有订单编号
+//        $orderIds = array_column($list, '订单编号');
+//
+//        // 查询相关附件(技术附件和订单附件)
+//        $relatedOrders = \db('工单_相关附件')
+//            ->whereIn('关联编号', $orderIds)
+//            ->whereIn('附件备注', ['技术附件', '订单资料附件'])  // 查询技术附件和订单附件
+//            ->select();
+//
+//        $techAttachments = [];  // 存储技术附件的订单编号
+//        $orderAttachments = [];  // 存储订单附件的订单编号
+//        foreach ($relatedOrders as $attachment) {
+//            if ($attachment['附件备注'] == '技术附件') {
+//                $techAttachments[] = $attachment['关联编号'];
+//            } elseif ($attachment['附件备注'] == '订单资料附件') {
+//                $orderAttachments[] = $attachment['关联编号'];
+//            }
+//        }
+//        // 遍历工单基本资料,设置相应的状态
+//        foreach ($list as &$value) {
+//            // 判断是否是技术附件相关的订单
+//            if (in_array($value['订单编号'], $techAttachments)) {
+//                $value['status'] = '';  // 技术附件的状态为空
+//            } else {
+//                $value['status'] = '*';  // 没有技术附件的订单状态为 *
+//            }
+//            // 判断是否是订单附件相关的订单
+//            if (in_array($value['订单编号'], $orderAttachments)) {
+//                $value['orderstatus'] = '';  // 订单附件的状态为空
+//            } else {
+//                $value['orderstatus'] = '*';  // 没有订单附件的订单状态为 *
+//            }
+//            // 将「工单入仓数量」字段替换为「工单完工数量」
+//            if (isset($value['工单入仓数量'])) {
+//                $value['工单完工数量'] = $value['工单入仓数量'];
+//                unset($value['工单入仓数量']);
+//            }
+//        }
+//
+//        // 返回成功的结果
+//        $data['total'] = count($list);
+//        $data['table'] = $list;
+//        $this->success('成功', $data);
+//    }
+
+
     /**
      * 月度客户订单汇总
      */

+ 737 - 13
application/api/controller/WorkOrder.php

@@ -10,6 +10,7 @@ use think\Config;
 use think\Db;
 use think\Request;
 use PhpOffice\PhpSpreadsheet\IOFactory;
+use function fast\e;
 
 
 /**
@@ -864,14 +865,33 @@ class WorkOrder extends Api
         } else {
             $param['订单编号'] = $param['订单编号'];
         }
-
-        $sql= \db('工单_基本资料')->fetchSql(true)->insert($param);
-        $res = \db()->query($sql);
-        if ($res !== false){
-            $this->success('成功');
-        }else{
-            $this->error('失败');
+        $massage = $param['面料'].',粘衬:'.$param['粘衬'];
+        $mianliao = $this->Gpt($massage);
+        $data = [];
+        foreach ($mianliao as $key => $value){
+            $data[$key] = [
+                'BOM_工单编号' => $param['订单编号'],
+                'BOM_物料名称' => $value,
+                'Sys_rq' => date('Y-m-d H:i:s',time()),
+                'Sys_id' => $param['Sys_id']
+            ];
         }
+        Db::startTrans();
+        try {
+            //插入工单基本资料
+            $sql= \db('工单_基本资料')->fetchSql(true)->insert($param);
+            $res = \db()->query($sql);
+            //插入订单物料信息
+            $BomSql = \db('工单_bom资料')->fetchSql(true)->insertAll($data);
+            $BomRes = \db()->query($BomSql);
+
+            //提交
+            Db::commit();
+        }catch (\Exception $e){
+            //回滚
+            Db::rollback();
+        }
+        $this->success('成功');
     }
 
     /**
@@ -1487,7 +1507,7 @@ class WorkOrder extends Api
                 $data['xhdata'] = $arr;
                 $this->success('成功',$data);
             } elseif (strlen($afterDash) == 4) {
-                    //一条子订单编号一个条码,统计颜色
+                //一条子订单编号一个条码,统计颜色
 //                $processlist = \db('工单_印件资料')
 //                    ->where('订单编号', $param['order'])
 //                    ->whereNull('Mod_rq') // 查询未删除数据
@@ -1580,11 +1600,11 @@ class WorkOrder extends Api
                         }
                     }
                 }
-                    $data['scslTotals'] = $scslTotals;
-                    $data['process'] = $table;//汇总数据集
-                    $data['order'] = $list;//表格数据
-                    $data['xhdata'] = $arr;//尺码表头
-                    $this->success('成功', $data);
+                $data['scslTotals'] = $scslTotals;
+                $data['process'] = $table;//汇总数据集
+                $data['order'] = $list;//表格数据
+                $data['xhdata'] = $arr;//尺码表头
+                $this->success('成功', $data);
 //                }
             } else {
                 echo "子订单编号 - 后不是2位也不是4位:$afterDash";
@@ -1928,4 +1948,708 @@ class WorkOrder extends Api
             ->select();
         $this->success('成功',$list);
     }
+
+
+    /**
+     * 订单BOM资料显示
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function OrderBomList()
+    {
+        if ($this->request->isGet() === false){
+            $this->error('请求错误');
+        }
+        $param = $this->request->param();
+        if (empty($param) || !isset($param['order'])){
+            $this->error('参数错误');
+        }
+        $where = ['a.BOM_工单编号'=>$param['order']];
+        $list = \db('工单_bom资料')
+            ->alias('a')
+            ->join('工单_基本资料 b','b.订单编号 = a.BOM_工单编号')
+            ->field('a.BOM_工单编号 as 订单编号,b.生产款号 as 生产款号,b.客户编号 as 客户编号,b.款式 as 款式,
+            a.BOM_物料名称 as 物料名称,a.BOM_投料单位 as 投料单位,a.BOM_计划用量 as 计划用料,a.BOM_标准用量 as 定额用料,
+            a.BOM_实际用量 as 裁床实际用料,a.BOM_领用数量 as 裁床领用面料,a.BOM_退还数量 as 裁床退回仓库面料,
+            a.BOM_desc as 备注,a.UNIQID,a.BOM_库存总量 as 入库总量,a.BOM_门封 as 门封,a.BOM_面料结余 as 面料结余,a.Sys_ID as ID,a.Sys_rq as 日期')
+            ->where($where)
+            ->whereNull('a.Mod_rq')
+            ->select();
+        if (!empty($list)) {
+            $this->success('成功', $list);
+        } else {
+            // 调用其他方法处理逻辑(例如记录日志、执行其他操作等)
+            $this->GdGtpAiOrder($param['order']);
+
+            // 返回空数据的统一处理
+            return $this->success('没有找到相关数据', []);
+        }
+    }
+
+    /**
+     * Bom资料删除
+     * */
+    public function Bomdel()
+    {
+        if ($this->request->isGet() === false) {
+            $this->error('请求错误');
+        }
+
+        // 获取请求参数
+        $param = $this->request->param();
+
+        // 检查参数是否为空或者缺少UNIQID
+        if (empty($param) || !isset($param['UNIQID'])) {
+            $this->error('参数错误');
+        }
+
+        // 判断UNIQID是否是逗号分隔的多个ID,如果是则拆分成数组,否则直接处理为单个ID
+        $uniqids = strpos($param['UNIQID'], ',') !== false ? explode(',', $param['UNIQID']) : [$param['UNIQID']];
+
+        $where = [];
+        $where['Mod_rq'] = date('Y-m-d H:i:s', time());
+
+            // 定义一个标志变量来追踪是否所有更新都成功
+            $allUpdated = true;
+            $failedUniqids = [];
+            // 遍历所有UNIQID并更新数据库
+            foreach ($uniqids as $uniqid) {
+                // 更新指定UNIQID的记录
+                $result = \db('工单_bom资料')
+                    ->where('UNIQID', $uniqid)
+                    ->update($where);
+
+                // 检查更新结果
+                if (!$result) {
+                    // 如果某个UNIQID更新失败,记录失败的ID
+                    $allUpdated = false;
+                    $failedUniqids[] = $uniqid;
+                }
+            }
+
+            // 如果所有更新都成功,返回成功信息
+            if ($allUpdated) {
+                $list = \db('工单_bom资料')
+                    ->whereIn('UNIQID', $uniqids)  // 查询所有传入的UNIQID
+                    ->select();
+                if (!empty($list)) {
+                    $this->success('删除成功');
+                } else {
+                    $this->GdGtpAiOrder($param['order']);
+                    return $this->success('没有找到相关数据', []);
+                }
+            } else {
+                // 如果有更新失败的记录,返回失败的UNIQID
+                $this->error('部分更新失败,无法更新以下UNIQID: ' . implode(', ', $failedUniqids));
+            }
+
+    }
+
+
+
+
+    /**
+     * 前端选择订单时如果BOM资料数据为空则单独调用,重新生成最新
+     */
+    public function GdGtpAiOrder($order){
+        // 判断是否有指定的订单号
+        if (!empty($order)) {
+            // 查询单个订单的最大编号
+            $maxOrder = \db('工单_基本资料')
+                ->where('订单编号', 'like', "{$order}%")
+                ->order('订单编号', 'desc')
+                ->limit(1)
+                ->value('订单编号');
+
+            // 查询该订单的基本资料
+            $list = \db('工单_基本资料')
+                ->where('订单编号', 'like', "{$order}%")
+                ->order('订单编号', 'desc')
+                ->limit(1)
+                ->find();
+
+            // 如果面料数据为空,提示错误
+            if (empty($list['面料'])) {
+                $this->error('面料数据为空无法定义BOM');
+            }
+
+            // 处理订单编号
+            $numericPart = substr($maxOrder, 2);
+            $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT);
+            $param['订单编号'] = $order . $newNumericPart;
+
+            // 处理物料信息
+            $massage = empty($list['粘衬']) || $list['粘衬'] == '无' ? $list['面料'] : $list['面料'] . ',粘衬:' . $list['粘衬'];
+            $mianliao = $this->Gpt($massage);
+
+            // 插入物料数据
+            $data = [];
+            foreach ($mianliao as $key => $value) {
+                if (!empty($value) && $value !== '粘衬') { // 排除空值和粘衬
+                    $data[] = [
+                        'BOM_工单编号' => $list['订单编号'],
+                        'BOM_物料名称' => $value,
+                        'Sys_rq' => date('Y-m-d H:i:s'),
+                        'Sys_ID' => '超级管理员'
+                    ];
+                }
+            }
+            // 批量插入BOM资料
+            if (!empty($data)) {
+                \db('工单_bom资料')->insertAll($data);
+            }
+
+            $this->success('成功',$order);
+        } else {
+            // 如果没有指定订单号,批量查询订单号并处理
+            $has_bom = \db('工单_bom资料')->alias('a')->field('a.BOM_工单编号')->group('a.BOM_工单编号')->select();
+            $all_orders = \db('工单_基本资料')->alias('a')->field('a.订单编号')->where('a.面料', '<>', '')->group('a.订单编号')->select();
+
+            // 提取有BOM资料的订单号
+            $has_bom_orders = array_column($has_bom, 'BOM_工单编号');
+
+            // 筛选出没有对应BOM资料的订单号
+            $no_bom_orders = array_filter($all_orders, function ($order) use ($has_bom_orders) {
+                return !in_array($order['订单编号'], $has_bom_orders);
+            });
+
+            // 遍历没有BOM资料的订单
+            foreach ($no_bom_orders as $orderData) {
+                // 获取该订单号的最大订单编号
+                $maxOrder = \db('工单_基本资料')
+                    ->where('订单编号', 'like', "{$orderData['订单编号']}%")
+                    ->order('订单编号', 'desc')
+                    ->limit(1)
+                    ->value('订单编号');
+
+                // 获取该订单号的具体数据
+                $list = \db('工单_基本资料')
+                    ->where('订单编号', 'like', "{$orderData['订单编号']}%")
+                    ->order('订单编号', 'desc')
+                    ->limit(1)
+                    ->find();
+
+                if (empty($list['面料'])) {
+                    $this->error("订单 {$orderData['订单编号']} 面料数据为空,无法定义BOM");
+                }
+
+                // 处理订单编号
+                $numericPart = substr($maxOrder, 2);
+                $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT);
+                $param['订单编号'] = $order . $newNumericPart;
+
+                // 处理物料信息
+                $massage = empty($list['粘衬']) || $list['粘衬'] == '无' ? $list['面料'] : $list['面料'] . ',粘衬:' . $list['粘衬'];
+                $mianliao = $this->Gpt($massage);
+                // 插入物料数据
+                $data = [];
+                foreach ($mianliao as $key => $value) {
+                    if (!empty($value) && $value !== '粘衬') { // 排除空值和粘衬
+                        $data[] = [
+                            'BOM_工单编号' => $list['订单编号'],
+                            'BOM_物料名称' => $value,
+                            'Sys_rq' => date('Y-m-d H:i:s'),
+                            'Sys_ID' => '超级管理员'
+                        ];
+                    }
+                }
+
+                // 批量插入BOM资料
+                if (!empty($data)) {
+                    \db('工单_bom资料')->insertAll($data);
+                }
+            }
+            $this->success('成功');
+        }
+    }
+
+
+    /**
+     * 订单面料修改接口
+     * @return void
+     * @throws \think\Exception
+     * @throws \think\db\exception\BindParamException
+     * @throws \think\exception\PDOException
+     */
+    public function FabricEdit()
+    {
+        if ($this->request->isPost() === false){
+            $this->error('请求错误');
+        }
+        $param = Request::instance()->post();
+        if (empty($param)){
+            $this->error('请求错误');
+        }
+        foreach ($param as $key=>$value){
+            $data = $value;
+            unset($data['UNIQID']);
+            if ($value['UNIQID'] !== '' || !empty($value['UNIQID'])){
+                $sql = \db('工单_bom资料')
+                    ->where('UNIQID',$value['UNIQID'])
+                    ->fetchSql(true)
+                    ->update($data);
+                $res = \db()->query($sql);
+            }else{
+                $sql = \db('工单_bom资料')
+                    ->fetchSql(true)
+                    ->insert($value);
+                $res = \db()->query($sql);
+            }
+            if ($res === false){
+                $this->error('修改失败');
+            }
+        }
+        $this->success('修改成功');
+    }
+
+    /**
+     * 订单BOM出库、退还详情显示
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function FabricDetail()
+    {
+        if ($this->request->isGet() === false){
+            $this->error('请求错误');
+        }
+        $param = $this->request->param();
+        if (empty($param) || !isset($param['order'])){
+            $this->error('参数错误');
+        }
+        if (isset($param['searchh']) || !empty($param['search'])){
+            $list['入库记录'] = \db('设备_报工日志')
+                ->where('order_id|款号',$param['order'])
+                ->where('name','入库')
+                ->field('id,order_id as 订单编号,款号,sum(number) as 数量,rq as 入库时间,sys_id as 入库人员,recipient as 入仓人员,receipt_number as 单号')
+                ->group('receipt_number')
+                ->order('入库时间 desc')
+                ->whereNull('Mod_rq')
+                ->select();
+        }else{
+            //出库记录查询
+            $list['出库记录'] = \db('设备_报工日志')
+                ->where('order_id|款号',$param['order'])
+                ->where('name','出库')
+                ->field('id,order_id as 订单编号,款号,sum(number) as 数量,rq as 出库时间,sys_id as 出库人员,receipt_number as 出库单据编号,recipient as 出库人员,receipt_number as 单号')
+                ->group('receipt_number')
+                ->order('出库时间 desc')
+                ->whereNull('Mod_rq')
+                ->select();
+            //退还记录查询
+            $list['退还记录'] = \db('设备_报工日志')
+                ->where('order_id|款号',$param['order'])
+                ->where('name','退还')
+                ->field('id,order_id as 订单编号,款号,sum(number) as 数量,rq as 退还时间,sys_id as 退还机台,recipient as 退还人员,receipt_number as 单号')
+                ->group('receipt_number')
+                ->order('退还时间 desc')
+                ->whereNull('Mod_rq')
+                ->select();
+        }
+
+        $this->success('成功',$list);
+    }
+
+    /**
+     * 入库、出库、退还详情数据
+     */
+    public function FabricDetaillist()
+    {
+        if ($this->request->isGet() === false){
+            $this->error('请求错误');
+        }
+        $param = $this->request->param();
+        if (empty($param) || !isset($param['order'])){
+            $this->error('参数错误');
+        }
+        if (isset($param['searchh']) || !empty($param['search'])){
+            $list['入库记录'] = \db('设备_报工日志')
+                ->where('receipt_number',$param['order'])
+                ->where('name','入库')
+                ->field('id,order_id as 订单编号,款号,number as 数量,rq as 入库时间,sys_id as 入库人员,recipient as 入仓人员,receipt_number as 单号,物料名称')
+                ->order('入库时间 desc')
+                ->whereNull('Mod_rq')
+                ->select();
+        }else{
+            //出库记录查询
+            $list['出库记录'] = \db('设备_报工日志')
+                ->where('receipt_number',$param['order'])
+                ->where('name','出库')
+                ->field('id,order_id as 订单编号,款号,number as 数量,rq as 出库时间,sys_id as 出库人员,recipient as 出库人员,receipt_number as 单号,物料名称')
+                ->order('出库时间 desc')
+                ->whereNull('Mod_rq')
+                ->select();
+            //退还记录查询
+            $list['退还记录'] = \db('设备_报工日志')
+                ->where('receipt_number',$param['order'])
+                ->where('name','退还')
+                ->field('id,order_id as 订单编号,款号,number as 数量,rq as 退还时间,sys_id as 退还机台,recipient as 退还人员,receipt_number as 单号,物料名称')
+                ->order('退还时间 desc')
+                ->whereNull('Mod_rq')
+                ->select();
+        }
+        $this->success('成功',$list);
+    }
+
+    /**
+     * 入库、出库、退还删除
+     */
+    public function FabricDetaildel()
+    {
+        if ($this->request->isPost() === false){
+            $this->error('请求错误');
+        }
+        $param = Request::instance()->post();
+        if (empty($param)){
+            $this->error('请求错误');
+        }
+        // 判断UNIQID是否是逗号分隔的多个ID,如果是则拆分成数组,否则直接处理为单个ID
+        $ids = strpos($param['id'], ',') !== false ? explode(',', $param['id']) : [$param['id']];
+
+        $where = [];
+        $where['Mod_id'] = $param['Mod_id'];
+        $where['Mod_rq'] = date('Y-m-d H:i:s', time());
+
+        // 定义一个标志变量来追踪是否所有更新都成功
+        $allUpdated = true;
+        $failedUniqids = [];
+        // 遍历所有UNIQID并更新数据库
+        foreach ($ids as $id) {
+            // 更新指定UNIQID的记录
+            $result = \db('设备_报工日志')
+                ->where('id', $id)
+                ->update($where);
+
+            // 检查更新结果
+            if (!$result) {
+                // 如果某个UNIQID更新失败,记录失败的ID
+                $allUpdated = false;
+                $failedUniqids[] = $id;
+            }
+        }
+
+        // 如果所有更新都成功,返回成功信息
+        if ($allUpdated) {
+            $list = \db('设备_报工日志')
+                ->whereIn('id', $id)  // 查询所有传入的UNIQID
+                ->select();
+            if (!empty($list)) {
+                $this->success('删除成功');
+            } else {
+                $this->GdGtpAiOrder($param['order']);
+                return $this->success('没有找到相关数据', []);
+            }
+        } else {
+            // 如果有更新失败的记录,返回失败的UNIQID
+            $this->error('部分更新失败,无法更新以下UNIQID: ' . implode(', ', $failedUniqids));
+        }
+    }
+
+    //GPT
+    public function Gpt($massage)
+    {
+
+
+        // 设置 API 密钥
+        $apiKey = 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY'; // 替换为您的 API 密钥
+
+        // 要发送给 GPT 的消息
+        $messages = [
+            [
+                'role' => 'user',
+                'content' => '你好,帮我按照:面料1、面料2、面料3……来整理归纳下面的面料信息,我只需要面料信息,不需要其他:'.$massage
+            ]
+        ];
+
+        // 创建请求数据
+        $data = [
+            'model' => 'gpt-3.5-turbo', // 使用的模型
+            'messages' => $messages,
+            'max_tokens' => 100, // 设置最大 token 数
+        ];
+
+        // 初始化 cURL
+        $ch = curl_init('https://niubi.zeabur.app/v1/chat/completions');
+
+        // 设置 cURL 选项
+        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));
+        curl_setopt($ch, CURLOPT_CAINFO, ROOT_PATH . '/public/uploads/cacert.pem');
+
+        // 执行请求
+        $response = curl_exec($ch);
+
+        // 检查错误
+        if (curl_errno($ch)) {
+            echo 'Error:' . curl_error($ch);
+        }
+
+        // 关闭 cURL
+        curl_close($ch);
+
+        // 解析和输出响应
+        $responseData = json_decode($response, true);
+
+        // 获取 GPT 的回复
+        if (isset($responseData['choices'][0]['message']['content'])) {
+            //获取返回内容
+            $gptReply = $responseData['choices'][0]['message']['content'];
+//            halt($gptReply);
+            //返回面料信息
+            $gptArray = explode('面料',$gptReply);
+            array_shift($gptArray);
+            foreach ($gptArray as $key=>$value){
+                $gptArray[$key] = preg_replace('/\s+/', '', substr($value,4));
+            }
+            return $gptArray;
+        } else {
+            echo "未能获取 GPT 的回复。";
+        }
+    }
+
+
+    /**
+     * 面料库存
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function fabricList()
+    {
+        if ($this->request->isGet() === false){
+            $this->error('请求错误');
+        }
+        $param = $this->request->param();
+        $where = [];
+        if (isset($param['order'])){
+            $where = ['a.BOM_工单编号'=>$param['order']];
+        }
+        if (isset($param['mouth'])){
+            $where = ['a.Sys_rq'=>['like',$param['mouth'].'%']];
+        }
+        $list = \db('工单_bom资料')
+            ->alias('a')
+            ->join('工单_基本资料 b','b.订单编号 = a.BOM_工单编号')
+//            ->field('a.BOM_工单编号 as 订单编号,b.生产款号 as 生产款号,b.客户编号 as 客户编号,b.款式 as 款式,
+//            a.BOM_物料名称 as 物料名称,a.BOM_投料单位 as 投料单位,a.BOM_库存总量 as 库存总量,a.BOM_面料结余 as 面料结余,
+//            a.UNIQID')
+            ->field('a.BOM_工单编号 as 订单编号,b.生产款号 as 生产款号,b.客户编号 as 客户编号,b.款式 as 款式,
+            a.BOM_物料名称 as 物料名称,a.BOM_投料单位 as 投料单位,a.BOM_计划用量 as 计划用料,a.BOM_标准用量 as 定额用料,
+            a.BOM_实际用量 as 裁床实际用料,a.BOM_领用数量 as 裁床领用面料,a.BOM_退还数量 as 裁床退回仓库面料,
+            a.BOM_desc as 备注,a.UNIQID,a.BOM_库存总量 as 入库总量,a.BOM_门封 as 门封,a.BOM_面料结余 as 面料结余,a.Sys_ID as ID,a.Sys_rq as 日期')
+            ->where($where)
+            ->select();
+        if (!empty($list)){
+            $this->success('成功',$list);
+        } else {
+            $this->success('没有找到相关数据', []);
+        }
+    }
+
+
+    /**
+     * 单条面料详情
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function oneFabricDetail()
+    {
+        if ($this->request->isGet() === false){
+            $this->error('请求错误');
+        }
+        $param = $this->request->param();
+        if (empty($param)){
+            $this->error('参数错误');
+        }
+        //面料入库记录
+        $list['入库'] = \db('设备_报工日志')
+            ->where('order_id',$param['order'])
+            ->where('物料名称',$param['fabricName'])
+            ->where('name','入库')
+            ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,recipient as 入仓人员')
+            ->select();
+        //面料出库记录
+        $list['出库'] = \db('设备_报工日志')
+            ->where('order_id',$param['order'])
+            ->where('物料名称',$param['fabricName'])
+            ->where('name','出库')
+            ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,receipt_number as 出库单据编号,recipient as 领用人员')
+            ->select();
+        //面料退还记录
+        $list['退还'] = \db('设备_报工日志')
+            ->where('order_id',$param['order'])
+            ->where('物料名称',$param['fabricName'])
+            ->where('name','退还')
+            ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,recipient as 退还人员')
+            ->select();
+        $this->success('成功',$list);
+    }
+
+    /**
+     * 单据号查询数据
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function ReceiptDetail()
+    {
+        if ($this->request->isGet() === false){
+            $this->error('请求错误');
+        }
+        $param = $this->request->param();
+        if (empty($param)){
+            $this->error('单据编号参数错误');
+        }
+        $list = \db('设备_报工日志')
+//            ->where('receipt_number','like','%',$param['receipt'].'%')
+            ->where('receipt_number',$param['receipt'])
+            ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,receipt_number as 出库单据编号,recipient as 领用人员')
+            ->whereNull('Mod_rq')
+            ->select();
+
+        if (empty($list)){
+            $this->error('未找到该出库单');
+        }else{
+            $this->success('成功',$list);
+        }
+    }
+
+    /**
+     * 出库单列表
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function ReceiptList()
+    {
+        if ($this->request->isGet() === false){
+            $this->error('请求错误');
+        }
+        $param = $this->request->param();
+        if (empty($param)){
+            $this->error('参数错误');
+        }
+        $list = \db('设备_报工日志')
+            ->where('rq','like','%'.$param['mouth'].'%')
+            ->field('receipt_number as 出库单,order_id as 订单编号,款号,物料名称,rq as 日期,sys_id as 操作机台,recipient as 领料人员')
+            ->group('出库单')
+            ->order('rq desc')
+            ->select();
+        if (empty($list)){
+            $this->success('未找到数据');
+        }else{
+            $this->success('成功',$list);
+        }
+
+    }
+
+    /**
+     * 出库单左侧菜单
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function getReceiptTab()
+    {
+        if ($this->request->isGet() === false){
+            $this->error('请求错误');
+        }
+        $list = \db('设备_报工日志')
+            ->field([
+                "DATE_FORMAT(rq, '%Y-%m') AS month",
+            ])
+            ->group('month')
+            ->order('month DESC')
+            ->select();
+        if (empty($list)){
+            $this->error('未找到数据');
+        }else{
+            $this->success('成功',$list);
+        }
+    }
+
+    /**
+     * 获取入库单号、出库单号
+     * @return void
+     */
+    public function gitReceiptNumber()
+    {
+        if ($this->request->isGet() === false){
+            $this->error('请求错误');
+        }
+        $param = $this->request->param();
+        if (empty($param)){
+            $this->error('参数错误');
+        }
+        $lastNumber = \db('设备_报工日志')
+            ->where('receipt_number','like',$param['number'].'%')
+            ->order('receipt_number desc')
+            ->limit(1)
+            ->column('receipt_number as 单号');
+        if (empty($lastNumber)){
+            $num = 1;
+        }else{
+            $num = (int)(substr($lastNumber[0],12,3))+1;
+        }
+        if ($num < 10){
+            $num = '00'.$num;
+        }elseif ($num>=10 && $num<100){
+            $num = '0'.$num;
+        }else{
+            $num;
+        }
+        $number = $param['number'].date('Ymd',time()).'-'.$num;
+
+        $lastNumber = \db('设备_报工日志')
+            ->where('name','出库')
+            ->order('recipient desc')
+            ->group('recipient')
+            ->limit(1)
+            ->value('recipient as 人员');
+        $data = [
+            'number' => $number,
+            'username' => $lastNumber
+        ];
+        $this->success('成功', $data);
+    }
+
+    /**
+     * 面料汇总左侧菜单
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function getFabricTab()
+    {
+        if ($this->request->isGet() === false){
+            $this->error('请求错误');
+        }
+        $list = \db('工单_bom资料')
+            ->field([
+                "DATE_FORMAT(Sys_rq, '%Y-%m') AS month",
+            ])
+            ->group('month')
+            ->order('month DESC')
+            ->select();
+        if (empty($list)){
+            $this->error('未找到数据');
+        }else{
+            $this->success('成功',$list);
+        }
+    }
 }

+ 42 - 44
application/api/controller/WorkOrderSpotCheck.php

@@ -118,21 +118,20 @@ class WorkOrderSpotCheck extends Api{
                         ->fetchSql(true)
                         ->update(['ck_rq' => $currentDateTime]);
                     $updateCount1 = \db()->query($sql);
-                    if ($updateCount1 > 0) {
-                        // 批量生成并插入日志数据
-                        $logData = array_map(function ($item) use ($currentDateTime, $params) {
-                            return [
-                                'order_id' => $item['子订单编号'],
-                                'rq' => $currentDateTime,
-                                'name' => $params['code'],
-                                'sys_id' => '裁剪自动出库'
-                            ];
-                        }, $sameColorList);
-                        \db('设备_报工日志')->insertAll($logData);
-                    } else {
-                        return $this->error('网络异常,重新尝试');
-                    }
-
+//                    if ($updateCount1 > 0) {
+//                        // 批量生成并插入日志数据
+//                        $logData = array_map(function ($item) use ($currentDateTime, $params) {
+//                            return [
+//                                'order_id' => $item['子订单编号'],
+//                                'rq' => $currentDateTime,
+//                                'name' => $params['code'],
+//                                'sys_id' => '裁剪自动出库'
+//                            ];
+//                        }, $sameColorList);
+//                        \db('设备_报工日志')->insertAll($logData);
+//                    } else {
+//                        return $this->error('网络异常,重新尝试');
+//                    }
                 }
 
                 //查询子订单编号对应数据【条码查询】
@@ -1908,22 +1907,21 @@ class WorkOrderSpotCheck extends Api{
                                 $updateCount2 = $result2;
                             }
                         }
-
-                        if ($updateCount1 > 0) {
-                            // 插入日志
-                            foreach ($sameColorList as $item) {
-                                $addlist = [
-                                    'order_id' => $item['子订单编号'],
-                                    'rq' => date("Y-m-d H:i:s"),
-                                    'name' => $params['code'],
-                                    'sys_id' => $params['sys_id']
-                                ];
-                                \db('设备_报工日志')->insertGetId($addlist);
-                            }
-                            $this->success("出库成功");
-                        } else {
-                            $this->error('出库失败');
-                        }
+//                        if ($updateCount1 > 0) {
+//                            // 插入日志
+//                            foreach ($sameColorList as $item) {
+//                                $addlist = [
+//                                    'order_id' => $item['子订单编号'],
+//                                    'rq' => date("Y-m-d H:i:s"),
+//                                    'name' => $params['code'],
+//                                    'sys_id' => $params['sys_id']
+//                                ];
+//                                \db('设备_报工日志')->insertGetId($addlist);
+//                            }
+//                            $this->success("出库成功");
+//                        } else {
+//                            $this->error('出库失败');
+//                        }
                     }
                 } else {
                     $this->error('未找到对应的出库订单');
@@ -1955,19 +1953,19 @@ class WorkOrderSpotCheck extends Api{
                     ->update(['出库日期' => null,'gd_statu'=>'1-计划中']);
                 $updateCount2 = \db()->query($result2);
 
-                if ($updateCount1 > 0 && $updateCount2 > 0) {
-                    // 插入日志
-                    $addlist = [
-                        'order_id' => $params['order_id'],
-                        'rq' => date("Y-m-d H:i:s"),
-                        'name' => $params['code'],
-                        'sys_id' => $params['sys_id']
-                    ];
-                    \db('设备_报工日志')->insertGetId($addlist);
-                    $this->success($params['order_id'] . "恢复未出库");
-                } else {
-                    $this->error($params['order_id'] . '该子订单是未出库状态,无需再次恢复');
-                }
+//                if ($updateCount1 > 0 && $updateCount2 > 0) {
+//                    // 插入日志
+//                    $addlist = [
+//                        'order_id' => $params['order_id'],
+//                        'rq' => date("Y-m-d H:i:s"),
+//                        'name' => $params['code'],
+//                        'sys_id' => $params['sys_id']
+//                    ];
+//                    \db('设备_报工日志')->insertGetId($addlist);
+//                    $this->success($params['order_id'] . "恢复未出库");
+//                } else {
+//                    $this->error($params['order_id'] . '该子订单是未出库状态,无需再次恢复');
+//                }
             } else {
                 $this->error('该子订单是未出库状态,无需再次恢复');
             }