| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
- <?php
- /**
- * 产品管理控制器
- */
- namespace app\admin\controller\package;
- use app\common\controller\Backend;
- use ba\Filesystem;
- use think\facade\Db;
- class Product extends Backend
- {
- protected array $noNeedLogin = ['*'];
- /**
- * 产品列表
- * @return void
- */
- public function index():void
- {
- $where = [];
- // 产品名称搜索
- if (!empty($this->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);
- }
- }
|