Product.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace app\controller\merchant\alibaba;
  3. use think\facade\Db;
  4. use app\services\ThirdParty\AlibabaAgent\ProductService;
  5. class Product
  6. {
  7. /**
  8. * 已入库的1688商品列表
  9. *
  10. * 展示 rrx_alibaba_import_goods 表中的商品数据
  11. * GET /merchant/alibaba/product/lst?page=1&limit=20&keyword=&status=
  12. *
  13. * @return \think\Response
  14. */
  15. public function lst()
  16. {
  17. $request = app('request');
  18. $page = (int)($request->param('page', 1));
  19. $limit = (int)($request->param('limit', 20));
  20. $keyword = $request->param('keyword', '');
  21. $status = $request->param('status', '');
  22. $query = Db::name('alibaba_import_goods');
  23. // 关键词搜索(商品名称)
  24. if (!empty($keyword)) {
  25. $query->where('product_name', 'like', '%' . $keyword . '%');
  26. }
  27. // 状态筛选
  28. if ($status !== '') {
  29. $query->where('status', (int)$status);
  30. }
  31. $count = $query->count('id');
  32. $list = $query
  33. ->order('create_time', 'DESC')
  34. ->page($page, $limit)
  35. ->select()
  36. ->toArray();
  37. // 格式化返回数据
  38. foreach ($list as &$item) {
  39. $item['id'] = (int)$item['id'];
  40. $item['product_id'] = (int)$item['product_id'];
  41. $item['status'] = (int)$item['status'];
  42. $item['create_time'] = (string)$item['create_time'];
  43. // 解析 product_info JSON
  44. $productInfo = [];
  45. if (!empty($item['product_info'])) {
  46. $productInfo = json_decode($item['product_info'], true) ?: [];
  47. }
  48. $item['product_info'] = $productInfo;
  49. // 提取关键展示字段
  50. $item['price'] = $productInfo['price'] ?? 0;
  51. $item['minPrice'] = $productInfo['minPrice'] ?? 0;
  52. $item['maxPrice'] = $productInfo['maxPrice'] ?? 0;
  53. $item['stock'] = $productInfo['stock'] ?? 0;
  54. $item['unit'] = $productInfo['unit'] ?? '件';
  55. $item['sku_count'] = isset($productInfo['skuList']) ? count($productInfo['skuList']) : 0;
  56. $item['image_list'] = $productInfo['imageList'] ?? [];
  57. $item['shop_name'] = $productInfo['shopName'] ?? '';
  58. $item['category_name'] = $productInfo['categoryName'] ?? '';
  59. // 加价标记
  60. $item['markup_rate'] = $productInfo['markupRate'] ?? 0;
  61. $item['original_min_price'] = $productInfo['originalMinPrice'] ?? 0;
  62. $item['original_max_price'] = $productInfo['originalMaxPrice'] ?? 0;
  63. }
  64. unset($item);
  65. return json([
  66. 'code' => 200,
  67. 'message' => 'success',
  68. 'data' => [
  69. 'list' => $list,
  70. 'count' => $count,
  71. 'page' => $page,
  72. 'limit' => $limit,
  73. ],
  74. ]);
  75. }
  76. /**
  77. * 获取单个入库商品的详细信息
  78. *
  79. * GET /merchant/alibaba/product/detail?id=123
  80. *
  81. * @return \think\Response
  82. */
  83. public function detail()
  84. {
  85. $request = app('request');
  86. $id = (int)$request->param('id', 0);
  87. if ($id <= 0) {
  88. return json(['code' => 400, 'message' => '参数错误:id不能为空']);
  89. }
  90. $item = Db::name('alibaba_import_goods')
  91. ->where('id', $id)
  92. ->find();
  93. if (!$item) {
  94. return json(['code' => 404, 'message' => '商品不存在']);
  95. }
  96. $item['id'] = (int)$item['id'];
  97. $item['product_id'] = (int)$item['product_id'];
  98. $item['status'] = (int)$item['status'];
  99. // 解析 product_info JSON
  100. if (!empty($item['product_info'])) {
  101. $item['product_info'] = json_decode($item['product_info'], true) ?: [];
  102. }
  103. return json([
  104. 'code' => 200,
  105. 'message' => 'success',
  106. 'data' => $item,
  107. ]);
  108. }
  109. /**
  110. * 删除入库商品(软删除:修改status为0)
  111. *
  112. * POST /merchant/alibaba/product/delete
  113. * Body: id=123
  114. *
  115. * @return \think\Response
  116. */
  117. public function delete()
  118. {
  119. $request = app('request');
  120. $id = (int)$request->param('id', 0);
  121. if ($id <= 0) {
  122. return json(['code' => 400, 'message' => '参数错误:id不能为空']);
  123. }
  124. $exists = Db::name('alibaba_import_goods')->where('id', $id)->find();
  125. if (!$exists) {
  126. return json(['code' => 404, 'message' => '商品不存在']);
  127. }
  128. Db::name('alibaba_import_goods')->where('id', $id)->update(['status' => 0]);
  129. return json(['code' => 200, 'message' => '删除成功']);
  130. }
  131. /**
  132. * 正式入库:将 alibaba_import_goods 表中的商品写入 store_product 系列表
  133. *
  134. * 业务场景:从 alibaba_import_goods 表中选择已导入的1688商品,
  135. * 正式写入到 rrx_store_product、rrx_store_product_attr、rrx_store_product_attr_value 表中
  136. *
  137. * POST /merchant/alibaba/product/importStore
  138. * Body: id=123 (alibaba_import_goods 表的主键ID)
  139. * 或 Body: ids=123,456 (批量入库,逗号分隔)
  140. *
  141. * @param ProductService $productService
  142. * @return \think\Response
  143. */
  144. public function importStore(ProductService $productService)
  145. {
  146. $request = app('request');
  147. $id = $request->param('id', '');
  148. $ids = $request->param('ids', '');
  149. // 解析要入库的ID列表
  150. $importIds = [];
  151. if (!empty($ids)) {
  152. $parts = explode(',', $ids);
  153. foreach ($parts as $part) {
  154. $part = (int)trim($part);
  155. if ($part > 0) $importIds[] = $part;
  156. }
  157. } elseif (!empty($id)) {
  158. $importIds[] = (int)$id;
  159. }
  160. if (empty($importIds)) {
  161. return json(['code' => 400, 'message' => '参数错误:请传入id或ids']);
  162. }
  163. // mer_id 写死 3447
  164. $merId = 3447;
  165. $successList = [];
  166. $failList = [];
  167. foreach ($importIds as $importId) {
  168. $result = $productService->importToStoreProduct($importId, $merId);
  169. if ($result['code'] === 200) {
  170. $successList[] = [
  171. 'import_goods_id' => $importId,
  172. 'product_id' => $result['product_id'],
  173. 'msg' => $result['msg'],
  174. ];
  175. } else {
  176. $failList[] = [
  177. 'import_goods_id' => $importId,
  178. 'msg' => $result['msg'],
  179. ];
  180. }
  181. }
  182. $message = count($successList) . '个商品入库成功';
  183. if (!empty($failList)) {
  184. $message .= ',' . count($failList) . '个商品入库失败';
  185. }
  186. return json([
  187. 'code' => 200,
  188. 'message' => $message,
  189. 'data' => [
  190. 'success' => $successList,
  191. 'fail' => $failList,
  192. ],
  193. ]);
  194. }
  195. }