model = new \app\admin\model\Finishedproduct; } public function index() { $params = input(''); $page = input('get.page', 1); $limit = input('get.limit', 10); $offset = ($page - 1) * $limit; // 获取工单编号列表 $db3 = Db::connect(config('database.db3')); $gdRows = $db3->query("SELECT DISTINCT `Gd_gdbh`, `Gd_客户代号`, `Gd_cpdh` FROM `工单_基本资料` WHERE `Gd_客户代号` = ?", ['J0031']); $Gd_gdbhList = array_column($gdRows, 'Gd_gdbh'); $gdbhList = array_column($gdRows, 'Gd_cpdh'); if (empty($gdbhList)) { $this->assign([ 'data' => '', 'total' =>'', ]); } // Mongo 查询 $mongo = \think\Db::connect('mongodb'); $where = ['jjcp_cpdh' => ['in', $gdbhList],'jjcp_gdbh' => ['in', $Gd_gdbhList], 'jjcp_smb' => '末 板']; if (!empty($params['search'])) { $where['成品编码|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($params['search'], 'i'); } // 1. 查询所有符合条件的成品数据(不分页) $all_products = $mongo->name('finished_products') ->where($where) ->order('UniqId', 'desc') ->select(); // 2. 按订单编号 + 工单编号分组聚合,并处理多个cpdh与成品编码 $grouped_products = []; foreach ($all_products as $item) { $originDateStr = $item['Sys_rq']; // 例如:'20/6/2025 16:01:43' $dt = \DateTime::createFromFormat('d/m/Y H:i:s', $originDateStr); $standardDate = $dt ? $dt->format('Y-m-d H:i:s') : null; $group_key = $item['订单编号'] . '_' . $item['jjcp_gdbh']; if (!isset($grouped_products[$group_key])) { $grouped_products[$group_key] = [ 'order_ddbh' => $item['订单编号'], 'gdbh' => $item['jjcp_gdbh'], 'cpbm' => $item['成品编码'], 'cpmc' => $item['成品名称'], 'Sys_rq' => $standardDate, 'sl' => 0, // 库存总数 'cpdh_list' => [], 'cpbm_list' => [] ]; } $grouped_products[$group_key]['sl'] += (int)$item['jjcp_sl']; // 累计库存数量 // 收集不同的cpdh和cpbm(成品编码) if (!in_array($item['jjcp_cpdh'], $grouped_products[$group_key]['cpdh_list'])) { $grouped_products[$group_key]['cpdh_list'][] = $item['jjcp_cpdh']; } if (!in_array($item['成品编码'], $grouped_products[$group_key]['cpbm_list'])) { $grouped_products[$group_key]['cpbm_list'][] = $item['成品编码']; } } // 格式化成用逗号连接的字符串 foreach ($grouped_products as &$group) { $group['cpdh'] = implode(',', $group['cpdh_list']); $group['cpbm'] = implode(',', $group['cpbm_list']); unset($group['cpdh_list'], $group['cpbm_list']); // 清理临时字段 } // 3. 将聚合后结果分页(稳定分页) $all_grouped = array_values($grouped_products); $total = count($all_grouped); $paged_grouped = array_slice($all_grouped, $offset, $limit); // 4. 获取库存出货数据并计算剩余数 foreach ($paged_grouped as &$prod) { $inventory = $mongo->name('inventory_summary')->where([ 'order_ddbh' => $prod['order_ddbh'], 'gdbh' => $prod['gdbh'], 'cpmc' => $prod['cpmc'], ])->find(); // 出货数量(若无数据则为 0) $total_chu_quantity = isset($inventory['total_chu_quantity']) ? (int)$inventory['total_chu_quantity'] : 0; $prod['total_chu_quantity'] = $total_chu_quantity; // 计算剩余数 = sl - 出货数 $prod['remaining_quantity'] = $prod['sl'] - $total_chu_quantity; } unset($prod); if (request()->isAjax()) { return json([ 'code' => 1, 'data' => $paged_grouped, 'total' => $total, 'page' => $page, 'limit' => $limit ]); } $this->assign('data', $paged_grouped); return $this->fetch(); } // public function index() // { // $search = input(''); // $page = input('get.page', 1); // $limit = input('get.limit', 10); // $mongo = \think\Db::connect('mongodb'); // // // 初始化查询条件 // $where = []; // // if (!empty($search['search'])) { // // 使用正则表达式来实现模糊匹配,'i' 表示忽略大小写 // $where['成品编码|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($search['search'], 'i'); // } // // if($where){ // $data = $mongo->name('finished_products') // ->limit($page, $limit) // ->where($where) // ->order('UniqId','desc') // ->where('jjcp_smb','末 板') // ->select(); // }else{ // $data = $mongo->name('finished_products') // ->limit($page, $limit) // ->where($where) // ->order('UniqId','desc') // ->where('jjcp_smb','末 板') // ->select(); // } // // $filtered = []; // // foreach ($data as $item) { // if (isset($item['jjcp_cpdh'], $item['成品编码']) && $item['jjcp_cpdh'] === $item['成品编码']) { // $filtered[] = $item; // } // } // // $count = $mongo->name('finished_products') // ->where($where) // ->order('UniqId','desc') // ->where('jjcp_smb','末 板') // ->select(); // // if (request()->isAjax()) { // return json([ // 'data' => $filtered, // 'total' => count($count), // 'page' => $page, // 'limit' => $limit // ]); // } // // $this->assign('data', $data); // return $this->fetch(); // } public function finished() { // 获取 JSON 格式的请求体 $jsonData = file_get_contents("php://input"); // 解析为 PHP 数组 $data = json_decode($jsonData, true); if (!isset($data['data']) || !is_array($data['data'])) { return json(['status' => 'error', 'msg' => '无效数据']); } // 连接 MongoDB 数据库(你在 config/database.php 中的 mongodb 配置名) $mongo = \think\Db::connect('mongodb'); $userinfo = Session::get('admin'); $insertList = []; foreach ($data['data'] as $item) { $insertList[] = [ '成品名称' => $item['jjcp_cpmc'], '成品编码' => $item['jjcp_cpbm'], '订单编号' => $item['order_ddbh'], 'jjcp_gdbh' => $item['jjcp_gdbh'], 'jjcp_sl' => intval($item['jjcp_sl']), 'jjcp_smb' => '末 板', 'Mod_rq' => '', 'company' => $userinfo['company'], 'jjcp_sj' => date('Y-m-d H:i:s'), 'Sys_rq' => date('Y-m-d H:i:s') ]; } try { $mongo->name('Finished_products')->insertAll($insertList); return json(['status' => 'success', 'msg' => '成功插入 MongoDB']); } catch (\Exception $e) { return json(['status' => 'error', 'msg' => '插入失败:' . $e->getMessage()]); } } //左侧 右侧页面 public function product_summary() { // 连接到 MongoDB 数据库 $mongo = \think\Db::connect('mongodb'); // 获取库存汇总表的数据 $list = $mongo->name('inventory_summary')->select(); // 用于存储分组后的结果 $grouped_data = []; // 遍历查询到的数据并进行分组 foreach ($list as $item) { // 获取创建时间的年份和月份 $year = date('Y', strtotime($item['created_at'])); // 获取年份 $month = date('Ym', strtotime($item['created_at'])); // 获取年月(例如 202506) // 获取产品名称 $product_name = $item['cpmc']; // 获取工单编号 $gdbh = $item['gdbh']; $order_ddbh = $item['order_ddbh']; // 构建多级分组结构 if (!isset($grouped_data[$year])) { $grouped_data[$year] = []; } if (!isset($grouped_data[$year][$month])) { $grouped_data[$year][$month] = []; } if (!isset($grouped_data[$year][$month][$product_name])) { $grouped_data[$year][$month][$product_name] = []; } // 将工单编号添加到对应的位置,保留 'order_ddbh' $grouped_data[$year][$month][$product_name][$order_ddbh] = [ 'order_ddbh' => $item['order_ddbh'], ]; } // 输出结果查看结构 // echo "
"; // print_r($grouped_data); // 或者可以使用 var_export($grouped_data, true) // echo ""; return $this->fetch(); } }