request->get('name'))) { $name = $this->request->get('name'); $where[] = ['p.name', 'like', '%' . $name . '%']; } // 产品编码搜索 if (!empty($this->request->get('code'))) { $code = $this->request->get('code'); $where[] = ['p.code', 'like', '%' . $code . '%']; } // 产品类型筛选 if (!empty($this->request->get('type'))) { $type = $this->request->get('type'); $where['p.type'] = $type; } // 分类筛选 if (!empty($this->request->get('category_id'))) { $categoryId = $this->request->get('category_id'); $where['p.category_id'] = $categoryId; } // 库存状态筛选 if (!empty($this->request->get('stock_status'))) { $stockStatus = $this->request->get('stock_status'); $where['p.stock_status'] = $stockStatus; } // 产品状态筛选 if (!empty($this->request->get('status'))) { $status = $this->request->get('status'); $where['p.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['p.sort'] = 'asc'; $order['p.id'] = 'desc'; } // 分页 $page = $this->request->param('page', 1); $limit = $this->request->param('limit', 20); // 查询数据 $productList = Db::name('product') ->alias('p') ->leftJoin('package pk', 'p.package_id = pk.id') ->field('p.*, pk.name as package_name') ->where($where) ->order($order) ->paginate([ 'list_rows' => $limit, 'page' => $page ]); // 转换图片字段 $list = $productList->items(); // foreach ($list as &$item) { // if (!empty($item['image'])) { // $item['image'] = json_decode($item['image'], true); // } else { // $item['image'] = []; // } // // if (!empty($item['key_attributes'])) { // $item['key_attributes'] = json_decode($item['key_attributes'], true); // } else { // $item['key_attributes'] = []; // } // } $this->success('查询成功', [ 'list' => $list, 'total' => $productList->total() ]); } /** * 新增/编辑产品 * @return void */ public function edit():void { $id = $this->request->post('id'); $name = $this->request->post('name'); // $code = $this->request->post('code'); $packageId = $this->request->post('package_id'); $description = $this->request->post('description', ''); $detail = $this->request->post('detail', ''); $keyAttributes = $this->request->post('key_attributes', []); $image = $this->request->post('image'); $stockStatus = $this->request->post('stock_status', 1); $status = $this->request->post('status', 1); $sort = $this->request->post('sort', 0); $type = $this->request->post('type', 0); $link = $this->request->post('link', 0); if (empty($name)) { $this->error(__('产品名称不能为空')); } if (empty($link)) { $this->error(__('产品链接不能为空')); } if (empty($packageId)) { $this->error(__('请选择套餐')); } // 验证套餐是否存在 $packageInfo = Db::name('package') ->where('id', $packageId) ->find(); if (!$packageInfo) { $this->error(__('选择的套餐不存在')); } // 处理图片和属性字段 if (empty($image)) { $image = ""; } if (!is_array($keyAttributes)) { $keyAttributes = []; } $data = [ 'name' => $name, // 'code' => $code, 'package_id' => $packageId ?: null, 'description' => $description, 'detail' => $detail, 'key_attributes' => json_encode($keyAttributes, JSON_UNESCAPED_UNICODE), 'image' => $image, 'stock_status' => $stockStatus, 'status' => $status, 'sort' => $sort, 'type' => $type, 'link' =>$link, 'update_time' => date('Y-m-d H:i:s') ]; try { if (!empty($id)) { // 编辑 $productInfo = Db::name('product') ->where('id', $id) ->find(); if (!$productInfo) { $this->error(__('产品不存在')); } $result = Db::name('product') ->where('id', $id) ->update($data); $message = '编辑成功'; } else { // 新增 $data['create_time'] = date('Y-m-d H:i:s'); $result = Db::name('product') ->insert($data); $id = Db::name('product')->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(__('参数错误')); } // 检查是否有订单关联 $hasOrder = Db::name('order_item') ->where('product_id', $id) ->count(); if ($hasOrder > 0) { $this->error(__('该产品已有订单记录,不能删除')); } try { $result = Db::name('product') ->where('id', $id) ->delete(); } catch (\Exception $e) { $this->error('删除失败: ' . $e->getMessage()); } if ($result) { $this->success('删除成功'); } else { $this->error(__('删除失败')); } } /** * 产品详情 * @return void */ public function detail():void { $id = $this->request->get('id'); if (empty($id)) { $this->error(__('参数错误')); } // 获取产品基础信息 $productInfo = Db::name('product') ->alias('p') ->leftJoin('package pk', 'p.package_id = pk.id') ->where('p.id', $id) ->field('p.*, pk.name as package_name, pk.type as package_type') ->find(); if (!$productInfo) { $this->error(__('产品不存在')); } // 获取所属套餐下的其他产品 $samePackageProducts = Db::name('product') ->where('package_id', $productInfo['package_id']) ->where('id', '<>', $id) ->field('id, name, code') ->order('sort', 'asc') ->select() ->toArray(); // 获取订单数量 $orderCount = Db::name('order_item') ->where('product_id', $id) ->count(); $result = [ 'product_info' => $productInfo, 'same_package_products' => $samePackageProducts, 'order_count' => $orderCount ]; $this->success('查询成功', $result); } /** * 批量更新产品状态 * @return void */ public function batchStatus():void { $ids = $this->request->post('ids'); $status = $this->request->post('status'); if (empty($ids) || empty($status)) { $this->error(__('参数错误')); } if (!is_array($ids)) { $ids = explode(',', $ids); } try { $result = Db::name('product') ->whereIn('id', $ids) ->update([ 'status' => $status, 'update_time' => date('Y-m-d H:i:s') ]); } catch (\Exception $e) { $this->error('操作失败: ' . $e->getMessage()); } if ($result) { $statusText = $status == 1 ? '上架' : '下架'; $this->success("成功{$statusText}{$result}个产品"); } else { $this->error(__('操作失败')); } } /** * 批量更新库存状态 * @return void */ public function batchStockStatus():void { $ids = $this->request->post('ids'); $stockStatus = $this->request->post('stock_status'); if (empty($ids) || empty($stockStatus)) { $this->error(__('参数错误')); } if (!is_array($ids)) { $ids = explode(',', $ids); } try { $result = Db::name('product') ->whereIn('id', $ids) ->update([ 'stock_status' => $stockStatus, 'update_time' => date('Y-m-d H:i:s') ]); } catch (\Exception $e) { $this->error('操作失败: ' . $e->getMessage()); } if ($result) { $statusText = [ 1 => '充足', 2 => '缺货', 3 => '下架' ][$stockStatus] ?? '未知'; $this->success("成功更新{$result}个产品的库存状态为{$statusText}"); } else { $this->error(__('操作失败')); } } /** * 添加/编辑产品规格 * @return void */ public function editSpec():void { $id = $this->request->post('id'); $productId = $this->request->post('product_id'); $name = $this->request->post('name'); $value = $this->request->post('value', []); $sort = $this->request->post('sort', 0); if (empty($productId) || empty($name)) { $this->error(__('参数不完整')); } // 检查产品是否存在 $productInfo = Db::name('product') ->where('id', $productId) ->find(); if (!$productInfo) { $this->error(__('产品不存在')); } // 处理规格值 if (!is_array($value)) { $value = []; } $data = [ 'product_id' => $productId, 'name' => $name, 'value' => json_encode($value, JSON_UNESCAPED_UNICODE), 'sort' => $sort ]; try { if (!empty($id)) { // 编辑 $result = Db::name('product_spec') ->where('id', $id) ->update($data); $message = '编辑成功'; } else { // 新增 $data['create_time'] = date('Y-m-d H:i:s'); $result = Db::name('product_spec') ->insert($data); $id = Db::name('product_spec')->getLastInsID(); $message = '新增成功'; } } catch (\Exception $e) { $this->error('操作失败: ' . $e->getMessage()); } if ($result) { $this->success($message, ['id' => $id]); } else { $this->error(__('操作失败')); } } /** * 删除产品规格 * @return void */ public function deleteSpec():void { $id = $this->request->post('id'); if (empty($id)) { $this->error(__('参数错误')); } try { $result = Db::name('product_spec') ->where('id', $id) ->delete(); } catch (\Exception $e) { $this->error('删除失败: ' . $e->getMessage()); } if ($result) { $this->success('删除成功'); } else { $this->error(__('删除失败')); } } /** * 获取产品规格列表 * @return void */ public function specList():void { $productId = $this->request->get('product_id'); if (empty($productId)) { $this->error(__('参数错误')); } $specList = Db::name('product_spec') ->where('product_id', $productId) ->order('sort', 'asc') ->select() ->toArray(); foreach ($specList as &$spec) { if (!empty($spec['value'])) { $spec['value'] = json_decode($spec['value'], true); } else { $spec['value'] = []; } } $this->success('查询成功', $specList); } /** * 获取产品状态选项 * @return void */ public function statusOptions():void { $options = [ ['value' => 1, 'label' => '上架'], ['value' => 2, 'label' => '下架'] ]; $this->success('获取成功', $options); } /** * 获取库存状态选项 * @return void */ public function stockStatusOptions():void { $options = [ ['value' => 1, 'label' => '充足'], ['value' => 2, 'label' => '缺货'], ['value' => 3, 'label' => '下架'] ]; $this->success('获取成功', $options); } /** * 导出产品列表 * @return void */ public function export():void { $where = []; // 根据筛选条件构建查询条件 if (!empty($this->request->get('name'))) { $name = $this->request->get('name'); $where[] = ['p.name', 'like', '%' . $name . '%']; } if (!empty($this->request->get('status'))) { $status = $this->request->get('status'); $where['p.status'] = $status; } // 查询数据 $productList = Db::name('product') ->alias('p') ->leftJoin('product_category pc', 'p.category_id = pc.id') ->field('p.id, p.name, p.code, pc.name as category_name, p.stock_status, p.status, p.sort, p.create_time') ->where($where) ->order('p.sort', 'asc') ->order('p.id', 'desc') ->select() ->toArray(); // 状态映射 $stockStatusMap = [1 => '充足', 2 => '缺货', 3 => '下架']; $statusMap = [1 => '上架', 2 => '下架']; foreach ($productList as &$item) { $item['stock_status'] = $stockStatusMap[$item['stock_status']] ?? '未知'; $item['status'] = $statusMap[$item['status']] ?? '未知'; } $this->success('查询成功', $productList); } }