| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace app\controller\merchant\alibaba;
- use think\facade\Db;
- use app\services\ThirdParty\AlibabaAgent\ProductService;
- use crmeb\basic\BaseController;
- class Product extends BaseController
- {
- /**
- * 已入库的1688商品列表
- *
- * 展示 rrx_alibaba_import_goods 表中的商品数据
- * GET /merchant/alibaba/product/lst?page=1&limit=20&keyword=&status=
- *
- * @return \think\Response
- */
- public function lst()
- {
- $request = app('request');
- $page = (int)($request->param('page', 1));
- $limit = (int)($request->param('limit', 20));
- $keyword = $request->param('keyword', '');
- $status = $request->param('status', '');
- $merId = $this->request->merId();
- // $merId = 3447;
- $query = Db::name('alibaba_import_goods');
- // 关键词搜索(商品名称)
- if (!empty($keyword)) {
- $query->where('product_name', 'like', '%' . $keyword . '%');
- }
- // 状态筛选
- if ($status !== '') {
- $query->where('status', (int)$status);
- }
- $count = $query->count('id');
- $list = $query
- ->order('create_time', 'DESC')
- ->page($page, $limit)
- ->select()
- ->toArray();
- // 格式化返回数据
- foreach ($list as &$item) {
- $item['id'] = (int)$item['id'];
- $item['product_id'] = (int)$item['product_id'];
- $item['status'] = (int)$item['status'];
- $item['create_time'] = (string)$item['create_time'];
- // 解析 product_info JSON
- $productInfo = [];
- if (!empty($item['product_info'])) {
- $productInfo = json_decode($item['product_info'], true) ?: [];
- }
- $item['product_info'] = $productInfo;
- // 提取关键展示字段
- $item['price'] = $productInfo['price'] ?? 0;
- $item['minPrice'] = $productInfo['minPrice'] ?? 0;
- $item['maxPrice'] = $productInfo['maxPrice'] ?? 0;
- $item['stock'] = $productInfo['stock'] ?? 0;
- $item['unit'] = $productInfo['unit'] ?? '件';
- $item['sku_count'] = isset($productInfo['skuList']) ? count($productInfo['skuList']) : 0;
- $item['image_list'] = $productInfo['imageList'] ?? [];
- $item['shop_name'] = $productInfo['shopName'] ?? '';
- $item['category_name'] = $productInfo['categoryName'] ?? '';
- // 加价标记
- $item['markup_rate'] = $productInfo['markupRate'] ?? 0;
- $item['original_min_price'] = $productInfo['originalMinPrice'] ?? 0;
- $item['original_max_price'] = $productInfo['originalMaxPrice'] ?? 0;
- // 是否已入库
- $item['isStoreProduct'] = 0;
- $storeProduct = Db::name('store_product')->where(['spu_id' => $item['product_id'], 'mer_id' => $merId])->find();
- if ($storeProduct) $item['isStoreProduct'] = 1;
- }
- unset($item);
- return json([
- 'code' => 200,
- 'message' => 'success',
- 'data' => [
- 'list' => $list,
- 'count' => $count,
- 'page' => $page,
- 'limit' => $limit,
- ],
- ]);
- }
- /**
- * 获取单个入库商品的详细信息
- *
- * GET /merchant/alibaba/product/detail?id=123
- *
- * @return \think\Response
- */
- public function detail()
- {
- $request = app('request');
- $id = (int)$request->param('id', 0);
- if ($id <= 0) {
- return json(['code' => 400, 'message' => '参数错误:id不能为空']);
- }
- $item = Db::name('alibaba_import_goods')
- ->where('id', $id)
- ->find();
- if (!$item) {
- return json(['code' => 404, 'message' => '商品不存在']);
- }
- $item['id'] = (int)$item['id'];
- $item['product_id'] = (int)$item['product_id'];
- $item['status'] = (int)$item['status'];
- // 解析 product_info JSON
- if (!empty($item['product_info'])) {
- $item['product_info'] = json_decode($item['product_info'], true) ?: [];
- }
- return json([
- 'code' => 200,
- 'message' => 'success',
- 'data' => $item,
- ]);
- }
- /**
- * 删除入库商品(软删除:修改status为0)
- *
- * POST /merchant/alibaba/product/delete
- * Body: id=123
- *
- * @return \think\Response
- */
- public function delete()
- {
- $request = app('request');
- $id = (int)$request->param('id', 0);
- if ($id <= 0) {
- return json(['code' => 400, 'message' => '参数错误:id不能为空']);
- }
- $exists = Db::name('alibaba_import_goods')->where('id', $id)->find();
- if (!$exists) {
- return json(['code' => 404, 'message' => '商品不存在']);
- }
- Db::name('alibaba_import_goods')->where('id', $id)->update(['status' => 0]);
- return json(['code' => 200, 'message' => '删除成功']);
- }
- /**
- * 正式入库:将 alibaba_import_goods 表中的商品写入 store_product 系列表
- *
- * 业务场景:从 alibaba_import_goods 表中选择已导入的1688商品,
- * 正式写入到 rrx_store_product、rrx_store_product_attr、rrx_store_product_attr_value 表中
- *
- * POST /merchant/alibaba/product/importStore
- * Body: id=123 (alibaba_import_goods 表的主键ID)
- * 或 Body: ids=123,456 (批量入库,逗号分隔)
- *
- * @param ProductService $productService
- * @return \think\Response
- */
- public function importStore(ProductService $productService)
- {
- $request = app('request');
- $idStr = $request->param('id', '');
- if (empty($idStr)) {
- return json(['code' => 400, 'message' => '参数错误:请传入id']);
- }
- // 解析要入库的ID列表(支持单个id或逗号分隔的多个id)
- $importIds = [];
- $parts = explode(',', $idStr);
- foreach ($parts as $part) {
- $part = (int)trim($part);
- if ($part > 0) $importIds[] = $part;
- }
- if (empty($importIds)) {
- return json(['code' => 400, 'message' => '参数错误:id格式不正确']);
- }
- $merId = $this->request->merId();
- // $merId = 3447;
- $successList = [];
- $failList = [];
- foreach ($importIds as $importId) {
- $result = $productService->importToStoreProduct($importId, $merId);
- if ($result['code'] === 200) {
- $successList[] = [
- 'import_goods_id' => $importId,
- 'product_id' => $result['product_id'],
- 'msg' => $result['msg'],
- ];
- } else {
- $failList[] = [
- 'import_goods_id' => $importId,
- 'msg' => $result['msg'],
- ];
- }
- }
- $message = count($successList) . '个商品入库成功';
- if (!empty($failList)) {
- $message .= ',' . count($failList) . '个商品入库失败';
- }
- return json([
- 'code' => 200,
- 'message' => $message,
- 'data' => [
- 'success' => $successList,
- 'fail' => $failList,
- ],
- ]);
- }
- }
|