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 = ['成品编码' => ['in', $gdbhList],'jjcp_gdbh' => ['in', $Gd_gdbhList], 'jjcp_smb' => '末 板']; // $where = ['成品编码' => ['in', $gdbhList], 'jjcp_smb' => '末 板']; if (!empty($params['search'])) { $where['成品编码|成品名称|jjcp_gdbh|订单编号'] = new \MongoDB\BSON\Regex($params['search'], 'i'); } // 查询符合条件的成品数据 $al_products = $mongo->name('finished_products') ->where($where) ->order('Sys_rq', 'desc') ->select(); // 查询 texts = 新增产品 的数据 $xz_products = $mongo->name('finished_products') ->where('texts', '新增产品') ->order('Sys_rq', 'desc') ->select(); // 合并数据 $all_products = array_merge($al_products, $xz_products); // 分组聚合处理 $grouped_products = []; foreach ($all_products as $item) { // 转换日期格式(如果是 d/m/Y H:i:s) $date = \DateTime::createFromFormat('d/m/Y H:i:s', $item['Sys_rq']); $item['Sys_rq'] = $date ? $date->format('Y-m-d H:i:s') : $item['Sys_rq']; $group_key = $item['订单编号'] . '_' . $item['jjcp_gdbh']; if (!isset($grouped_products[$group_key])) { $grouped_products[$group_key] = [ 'Sys_rq' => $item['Sys_rq'], 'order_ddbh' => $item['订单编号'], 'gdbh' => $item['jjcp_gdbh'], 'cpbm' => $item['成品编码'], 'cpmc' => $item['成品名称'], 'sl' => 0, 'cpdh_list' => [], 'cpbm_list' => [] ]; } $grouped_products[$group_key]['sl'] += (int)$item['jjcp_sl']; 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']); } // 聚合结果排序(按时间降序) $all_grouped = array_values($grouped_products); usort($all_grouped, function ($a, $b) { $timeA = strtotime($a['Sys_rq']) ?: 0; $timeB = strtotime($b['Sys_rq']) ?: 0; return $timeB <=> $timeA; }); // 再分页 $total = count($all_grouped); $paged_grouped = array_slice($all_grouped, $offset, $limit); // 查询库存并计算剩余数 foreach ($paged_grouped as &$prod) { $inventory = $mongo->name('inventory_summary')->where([ 'order_ddbh' => $prod['order_ddbh'], 'gdbh' => $prod['gdbh'], 'cpmc' => $prod['cpmc'], ])->find(); $total_chu_quantity = isset($inventory['total_chu_quantity']) ? (int)$inventory['total_chu_quantity'] : 0; $prod['total_chu_quantity'] = $total_chu_quantity; $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[] = [ 'jjcp_num' => '', 'jjcp_yjno' => '', 'jjcp_cpmc' => '', '客户料号' => '', '仓库编号' => '', '仓库名称' => '', 'jjcp_desc' => $item['remark'], '入仓类型' => '', '机型备注' => '', 'Sys_id' => '', 'UniqId' => '', '成品名称' => $item['cpmc'], 'jjcp_cpdh' => $item['cpbm'], '成品编码' => $item['cpbm'], '订单编号' => $item['order_ddbh'], 'jjcp_gdbh' => $item['gdbh'], 'jjcp_sl' => intval($item['sl']), 'jjcp_smb' => '末 板', 'jjcp_dw' => '张', 'Mod_rq' => '', 'texts' => '新增产品', '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); $this->success('成功插入'); // } 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(); } }