Product.php 7.4 KB

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