request->get('name'))) { $name = $this->request->get('name'); $where[] = ['name', 'like', '%' . $name . '%']; } // 状态筛选 if (!empty($this->request->get('status'))) { $status = $this->request->get('status'); $where['status'] = $status; } // 排序 $order = []; if (!empty($this->request->get('order'))) { $order_a = $this->request->get('order'); $order_a = explode(',', $order_a); $order[$order_a[0]] = $order_a[1]; } else { $order['id'] = 'desc'; } // 分页 $page = $this->request->get('page', 1); $limit = $this->request->get('limit', 20); $companyList = Db::name('company') ->where($where) ->order($order) ->paginate([ 'list_rows' => $limit, 'page' => $page ]); $this->success('查询成功', [ 'list' => $companyList->items(), 'total' => $companyList->total() ]); } /** * 新增/编辑企业 * @return void */ public function edit():void { $id = $this->request->post('id'); $name = $this->request->post('name'); $status = $this->request->post('status', 1); if (empty($name)) { $this->error(__('企业名称不能为空')); } // 验证企业名称是否已存在(编辑时排除自己) $checkName = Db::name('company') ->where('name', $name); if (!empty($id)) { $checkName->where('id', '<>', $id); } if ($checkName->find()) { $this->error(__('企业名称已存在')); } $data = [ 'name' => $name, 'status' => $status, 'update_time' => date('Y-m-d H:i:s') ]; $adminInfo = $this->auth->getInfo(); try { if (!empty($id)) { // 编辑 $result = Db::name('company') ->where('id', $id) ->update($data); Db::name('user_on') ->where('company_id', $id) ->update(['company'=>$name]); $message = '编辑成功'; } else { // 新增 $data['creator_name'] = $adminInfo['nickname']; // 可以从登录信息获取 $data['create_time'] = date('Y-m-d H:i:s'); $result = Db::name('company') ->insert($data); $id = Db::name('company')->getLastInsID(); $message = '新增成功'; } } catch (\Exception $e) { $this->error('操作失败: ' . $e->getMessage()); } if ($result) { $this->success($message, ['id' => $id]); } else { $this->error(__('操作失败')); } } /** * 删除企业 * @return void */ public function delete():void { $id = $this->request->post('id'); if (empty($id)) { $this->error(__('参数错误')); } try { $result = Db::name('company') ->where('id', $id) ->delete(); } catch (\Exception $e) { $this->error('删除失败: ' . $e->getMessage()); } if ($result) { $this->success('删除成功'); } else { $this->error(__('删除失败')); } } /** * 获取启用企业选项(用于下拉选择) * @return void */ public function selectOptions():void { $packageList = Db::name('company') ->where('status', 1) ->field('id,name') ->select() ->toArray(); $this->success('获取成功', $packageList); } /** * 禁用/启用企业 * @return void */ public function toggleStatus():void { $id = $this->request->post('id'); if (empty($id)) { $this->error(__('参数错误')); } try { $companyInfo = Db::name('company') ->where('id', $id) ->find(); if (!$companyInfo) { $this->error(__('企业不存在')); } $newStatus = $companyInfo['status'] == 1 ? 2 : 1; $statusText = $newStatus == 2 ? '禁用' : '启用'; $result = Db::name('company') ->where('id', $id) ->update([ 'status' => $newStatus, 'update_time' => date('Y-m-d H:i:s') ]); } catch (\Exception $e) { $this->error('操作失败: ' . $e->getMessage()); } if ($result) { $this->success($statusText . '成功'); } else { $this->error(__('操作失败')); } } /** * 获取企业详情 * @return void */ public function detail():void { $id = $this->request->get('id'); if (empty($id)) { $this->error(__('参数错误')); } $companyInfo = Db::name('company') ->where('id', $id) ->find(); if (!$companyInfo) { $this->error(__('企业不存在')); } $this->success('查询成功', $companyInfo); } /** * 获取企业状态选项 * @return void */ public function statusOptions():void { $options = [ ['value' => 1, 'label' => '正常'], ['value' => 2, 'label' => '禁用'] ]; $this->success('获取成功', $options); } }