model = new \app\admin\model\User; } /** * 查看 */ public function index() { $mongo = \think\Db::connect('mongodb'); $this->model = $mongo->name('inventory_summary'); //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } //list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $this->model = $this->model; //->where($where); $yearMonth = $this->request->param("yearMonth"); if(!empty($yearMonth)){ // 获取月份的第一天 00:00:00 $startTime = $yearMonth . '-01 00:00:00'; // 获取下个月的第一天,然后减去1秒,得到本月最后一秒 $endTime = date('Y-m-t 23:59:59', strtotime($yearMonth . '-01')); $this->model = $this->model->whereBetween('created_at',[$startTime,$endTime] ); } $test2 = $this->request->param("test2"); if(!empty($test2)){ $this->model = $this->model->where('cpmc',$test2 ); } $test3 = $this->request->param("test3"); if(!empty($test2)){ $this->model = $this->model->where('order_ddbh',$test3 ); } if(empty($yearMonth)){ $list = []; $result = array("total" => count($list), "rows" => $list); return json($result); } $list = $this->model ->select(); $result = array("total" => count($list), "rows" => $list); return json($result); } $tree = $this->getTreeDataOptimized(); $this->assign("tree",$tree); return $this->view->fetch(); } /** * 方法二:分级查询(推荐) */ public function getTreeDataOptimized() { $mongo = \think\Db::connect('mongodb'); $this->model = $mongo->name('inventory_summary'); // 1. 查询所有一级分类 $level1 = $this->model ->field('created_at') ->order('created_at DESC') ->select(); $tree = []; foreach ($level1 as $k =>$v){ $yearMonth = substr($v['created_at'], 0, 7); // 2025-06-20 -> 2025-06 $level1_news[$yearMonth]=$v; } $level1 = $level1_news; unset($level1_news); foreach ($level1 as $item1) { $yearMonth = substr($item1['created_at'], 0, 7); // 2025-06-20 -> 2025-06 $node1 = [ 'id' => $yearMonth, 'name' => $yearMonth, 'level' => 1, 'children' => [] ]; // 获取月份的第一天 00:00:00 $startTime = $yearMonth . '-01 00:00:00'; // 获取下个月的第一天,然后减去1秒,得到本月最后一秒 $endTime = date('Y-m-t 23:59:59', strtotime($yearMonth . '-01')); // 2. 查询当前一级下的所有二级分类 $level2 = $this->model ->field('cpmc') ->whereBetween('created_at',[$startTime,$endTime] ) ->where('cpmc', '<>', '') ->order('cpmc asc') ->select(); foreach ($level2 as $k=>$v){ $level2_new[$v["cpmc"]] = $v; } $level2 = $level2_new; unset($level2_new); foreach ($level2 as $item2) { $node2 = [ 'id' => $yearMonth . '_' . $item2['cpmc'], 'name' => $item2['cpmc'], 'level' => 2, 'parent' => $yearMonth, 'children' => [] ]; // 3. 查询当前二级下的所有三级分类 $level3 = $this->model // ->field('*') ->whereBetween('created_at',[$startTime,$endTime] ) ->where('cpmc', $item2['cpmc']) ->where('order_ddbh', '<>', '') ->order('order_ddbh asc') ->select(); foreach ($level3 as $k=>$v){ $level3_news[$v["order_ddbh"]] = $v; } $level3 = $level3_news; unset($level3_news); foreach ($level3 as $item3) { $node2['children'][] = [ 'id' => $yearMonth . '_' . $item2['cpmc'] . '_' . $item3['order_ddbh'], 'name' => $item3['order_ddbh'], 'level' => 3, 'parent' => $yearMonth . '_' . $item2['cpmc'] ]; } $node1['children'][] = $node2; } $tree[$yearMonth] = $node1; } return $tree; } public function ruku() { $order_ddbh = $this->request->get('order_ddbh', ''); $gdbh = $this->request->get('gdbh', ''); $cpmc = $this->request->get('cpmc', ''); $limit = $this->request->get('limit', 20); $offset = $this->request->get('offset', 0); $where = []; if ($order_ddbh) $where['订单编号'] = $order_ddbh; if ($gdbh) $where['jjcp_gdbh'] = $gdbh; if ($cpmc) $where['成品名称'] = $cpmc; // 获取工单编号列表 $db3 = Db::connect(config('database.db3')); $gdRows = $db3->query("SELECT DISTINCT `Gd_gdbh`, `Gd_客户代号`, `Gd_cpdh` FROM `工单_基本资料` WHERE `Gd_客户代号` = ? AND `Gd_gdbh` = ?", ['J0031', $gdbh]); $Gd_gdbhList = array_column($gdRows, 'Gd_gdbh'); $gdbhList = array_column($gdRows, 'Gd_cpdh'); if (empty($gdbhList)) { $this->assign([ 'list' => '', 'total' =>'', 'total_sl' => '', ]); } // Mongo 查询 $mongo = \think\Db::connect('mongodb'); $where = ['jjcp_cpdh' => ['in', $gdbhList],'jjcp_gdbh' => ['in', $Gd_gdbhList], 'jjcp_smb' => '末 板']; $total = $mongo->name('finished_products')->where($where)->count(); // 查询原始数据 $rawList = $mongo->name('finished_products') ->field('订单编号, 成品名称, jjcp_gdbh, jjcp_sl, jjcp_sj') ->where($where) ->order('jjcp_sj','desc') ->limit($offset, $limit) ->select(); // 转换字段名并格式化时间 $list = []; $total_sl = 0; // 初始化合计值 foreach ($rawList as $item) { // 处理时间格式转换 $originalDate = $item['jjcp_sj'] ?? null; $formattedDate = null; if ($originalDate) { try { $date = \DateTime::createFromFormat('d/m/Y H:i:s', $originalDate); if ($date) { $formattedDate = $date->format('Y-m-d H:i:s'); } else { $date = new \DateTime($originalDate); $formattedDate = $date->format('Y-m-d H:i:s'); } } catch (Exception $e) { $formattedDate = $originalDate; } } $sl = $item['jjcp_sl'] ?? 0; $total_sl += (int)$sl; // 累加数量 $list[] = [ 'order_ddbh' => $item['订单编号'] ?? null, 'cpmc' => $item['成品名称'] ?? null, 'gdbh' => $item['jjcp_gdbh'] ?? null, 'sl' => $sl, 'sj' => $formattedDate ]; } $this->assign([ 'list' => $list, 'total' => $total, 'total_sl' => $total_sl ]); return $this->fetch(); } public function chuku() { $order_ddbh = $this->request->get('order_ddbh', ''); $gdbh = $this->request->get('gdbh', ''); $cpmc = $this->request->get('cpmc', ''); $limit = $this->request->get('limit', 20); $offset = $this->request->get('offset', 0); $where = []; if ($order_ddbh) $where['order_ddbh'] = $order_ddbh; if ($gdbh) $where['gdbh'] = $gdbh; if ($cpmc) $where['cpmc'] = $cpmc; $mongo = \think\Db::connect('mongodb'); $total = $mongo->name('inventory_records')->where($where)->count(); $list = $mongo->name('inventory_records') ->where($where) ->limit($offset, $limit) ->select(); // 计算出库数量合计 $total_sl = 0; foreach ($list as &$item) { $sl = $item['total_chu_quantity'] ?? 0; $total_sl += (int)$sl; $item['total_chu_quantity'] = $sl; // 确保sl字段存在 } $this->assign([ 'list' => $list, 'total' => $total, 'total_sl' => $total_sl // 传递合计值到视图 ]); return $this->fetch(); } /** * 添加 */ public function add() { if ($this->request->isPost()) { $this->token(); } return parent::add(); } /** * 编辑 */ public function edit($ids = null) { if ($this->request->isPost()) { $this->token(); } $row = $this->model->get($ids); $this->modelValidate = true; if (!$row) { $this->error(__('No Results were found')); } $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker'])); return parent::edit($ids); } /** * 删除 */ public function del($ids = "") { if (!$this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ? $ids : $this->request->post("ids"); $row = $this->model->get($ids); $this->modelValidate = true; if (!$row) { $this->error(__('No Results were found')); } Auth::instance()->delete($row['id']); $this->success(); } }