Product.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\api\model\plus\assemble;
  3. use app\common\exception\BaseException;
  4. use app\common\model\plus\assemble\Product as AssembleProductModel;
  5. use app\api\model\product\Product as ProductModel;
  6. /**
  7. * 限时拼团模型
  8. */
  9. class Product extends AssembleProductModel
  10. {
  11. /**
  12. * 隐藏字段
  13. */
  14. protected $hidden = [
  15. 'sales_initial',
  16. 'total_sales',
  17. 'is_delete',
  18. 'app_id',
  19. 'create_time',
  20. 'update_time'
  21. ];
  22. /**
  23. * 获取首页拼团商品显示
  24. */
  25. public function getProductList($assemble_activity_id, $limit)
  26. {
  27. // 获取列表数据
  28. $list = $this->with(['product.image.file', 'assembleSku'])
  29. ->where('assemble_activity_id', '=', $assemble_activity_id)
  30. ->where('is_delete', '=', 0)
  31. ->limit($limit)
  32. ->visible(['product.product_id','product.product_name','product.file_path'])
  33. ->select();
  34. foreach ($list as $product) {
  35. $assemble_arr = array_column($product['assembleSku']->toArray(), 'assemble_price');
  36. $product_arr = array_column($product['assembleSku']->toArray(), 'product_price');
  37. sort($assemble_arr);
  38. sort($product_arr);
  39. $product['assemble_price'] = current($assemble_arr);
  40. $product['product_price'] = current($product_arr);
  41. $real_product = $product['product'];
  42. $real_product['file_path'] = $product['product']['image'][0]['file_path'];
  43. unset($product['assembleSku']);
  44. unset($real_product['image']);
  45. }
  46. return $list;
  47. }
  48. /**
  49. * 获取列表页拼团数据
  50. * 目前未分页,后续有可能会分页
  51. */
  52. public function getActivityList($assemble_activity_id)
  53. {
  54. // 获取列表数据
  55. $list = $this->with(['product.image.file', 'assembleSku'])
  56. ->where('assemble_activity_id', '=', $assemble_activity_id)
  57. ->where('is_delete', '=', 0)
  58. ->visible(['product.product_id','product.product_name','product.file_path'])
  59. ->select();
  60. foreach ($list as $product) {
  61. $assemble_arr = array_column($product['assembleSku']->toArray(), 'assemble_price');
  62. $product_arr = array_column($product['assembleSku']->toArray(), 'product_price');
  63. sort($assemble_arr);
  64. sort($product_arr);
  65. $product['assemble_price'] = current($assemble_arr);
  66. $product['product_price'] = current($product_arr);
  67. $product['product']['file_path'] = $product['product']['image'][0]['file_path'];
  68. unset($product['assembleSku']);
  69. unset($product['product']['image']);
  70. }
  71. return $list;
  72. }
  73. /**
  74. * 获取拼团商品列表
  75. */
  76. public static function getAssembleProduct($params)
  77. {
  78. // 拼团详情
  79. $assemble = self::detail($params['assemble_product_id'], ['assembleSku']);
  80. if (empty($assemble)) {
  81. throw new BaseException(['msg' => '拼团商品不存在或已结束']);
  82. }
  83. // 拼团商品详情
  84. $product = ProductModel::detail($assemble['product_id']);
  85. // 拼团商品sku信息
  86. $assemble_sku = null;
  87. if ($product['spec_type'] == 10) {
  88. $assemble_sku = $assemble['assembleSku'][0];
  89. } else {
  90. //多规格
  91. foreach ($assemble['assembleSku'] as $sku) {
  92. if ($sku['assemble_product_sku_id'] == $params['assemble_product_sku_id']) {
  93. $assemble_sku = $sku;
  94. break;
  95. }
  96. }
  97. }
  98. if ($assemble_sku == null) {
  99. throw new BaseException(['msg' => '拼团商品规格不存在']);
  100. }
  101. // 拼团商品sku信息
  102. $product['product_sku'] = ProductModel::getProductSku($product, $params['product_sku_id']);
  103. $product['assemble_sku'] = $assemble_sku;
  104. // 拼团商品列表
  105. $productList = [$product->hidden(['category', 'content', 'image', 'sku'])];
  106. foreach ($productList as &$item) {
  107. // 商品单价
  108. $item['product_price'] = $assemble_sku['assemble_price'];
  109. // 商品购买数量
  110. $item['total_num'] = $params['product_num'];
  111. $item['spec_sku_id'] = $item['product_sku']['spec_sku_id'];
  112. // 商品购买总金额
  113. $item['total_price'] = $assemble_sku['assemble_price'] * $item['total_num'];
  114. $item['point_num'] = $assemble_sku['point_num'];
  115. $item['assemble_product_sku_id'] = $assemble_sku['assemble_product_sku_id'];
  116. $item['product_sku_id'] = $params['product_sku_id'];
  117. $item['product_source_id'] = $assemble_sku['assemble_product_id'];
  118. $item['sku_source_id'] = $assemble_sku['assemble_product_sku_id'];
  119. // 拼团活动id
  120. $item['activity_id'] = $assemble['assemble_activity_id'];
  121. // 拼团订单id
  122. $item['bill_source_id'] = $params['assemble_bill_id'];
  123. // 拼团最大购买数
  124. $item['assemble_product'] = [
  125. 'limit_num' => $assemble['limit_num']
  126. ];
  127. }
  128. return $productList;
  129. }
  130. /**
  131. * 拼团商品详情
  132. */
  133. public function getAssembleDetail($assemble_product_id)
  134. {
  135. $result = $this->with(['product.image.file', 'assembleSku.productSku.image'])
  136. ->where('assemble_product_id', '=', $assemble_product_id)->find();
  137. if (!empty($result)) {
  138. $assemble_arr = array_column($result->toArray()['assembleSku'], 'assemble_price');
  139. $product_arr = array_column($result->toArray()['assembleSku'], 'product_price');
  140. sort($assemble_arr);
  141. sort($product_arr);
  142. $result['assemble_price'] = current($assemble_arr);
  143. $result['line_price'] = current($product_arr);
  144. if (count($assemble_arr) > 1) {
  145. $res['assemble_high_price'] = end($assemble_arr);
  146. $res['line_high_price'] = end($product_arr);
  147. }
  148. }
  149. return $result;
  150. }
  151. }