Product.php 7.3 KB

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