Product.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\api\model\plus\bargain;
  3. use app\common\exception\BaseException;
  4. use app\common\library\helper;
  5. use app\common\model\plus\bargain\Product as BargainProductModel;
  6. use app\api\model\product\Product as ProductModel;
  7. /**
  8. * 砍价商品模型
  9. */
  10. class Product extends BargainProductModel
  11. {
  12. /**
  13. * 隐藏字段
  14. */
  15. protected $hidden = [
  16. 'sales_initial',
  17. 'total_sales',
  18. 'is_delete',
  19. 'app_id',
  20. 'create_time',
  21. 'update_time'
  22. ];
  23. /**
  24. * 获取首页砍价商品显示
  25. */
  26. public function getProductList($bargain_activity_id, $limit)
  27. {
  28. // 获取列表数据
  29. $list = $this->with(['product.image.file', 'bargainSku'])
  30. ->where('bargain_activity_id', '=', $bargain_activity_id)
  31. ->where('is_delete', '=', 0)
  32. ->limit($limit)
  33. ->visible(['product.product_id','product.product_name','product.file_path'])
  34. ->select();
  35. foreach ($list as $product) {
  36. $bargain_arr = array_column($product['bargainSku']->toArray(), 'bargain_price');
  37. $product_arr = array_column($product['bargainSku']->toArray(), 'product_price');
  38. sort($bargain_arr);
  39. sort($product_arr);
  40. $product['bargain_price'] = current($bargain_arr);
  41. $product['product_price'] = current($product_arr);
  42. $real_product = $product['product'];
  43. $real_product['file_path'] = $product['product']['image'][0]['file_path'];
  44. unset($product['bargainSku']);
  45. unset($real_product['image']);
  46. }
  47. return $list;
  48. }
  49. /**
  50. * 获取商品列表
  51. */
  52. public static function getBargainProduct($params)
  53. {
  54. // 砍价任务详情
  55. $bargain = self::detail($params['bargain_product_id'], ['bargainSku']);
  56. $task = Task::detail($params['bargain_task_id']);
  57. if (empty($task)) {
  58. throw new BaseException(['msg' => '任务不存在']);
  59. }
  60. if ($task['is_buy'] == 1) {
  61. throw new BaseException(['msg' => '当前砍价任务商品已购买']);
  62. }
  63. if (empty($bargain)) {
  64. throw new BaseException(['msg' => '商品不存在或已结束']);
  65. }
  66. // 商品详情
  67. $product = ProductModel::detail($bargain['product_id']);
  68. // 商品sku信息
  69. $bargain_sku = null;
  70. if ($product['spec_type'] == 10) {
  71. $bargain_sku = $bargain['bargainSku'][0];
  72. } else {
  73. //多规格
  74. foreach ($bargain['bargainSku'] as $sku) {
  75. if ($sku['bargain_product_sku_id'] == $params['bargain_product_sku_id']) {
  76. $bargain_sku = $sku;
  77. break;
  78. }
  79. }
  80. }
  81. if ($bargain_sku == null) {
  82. throw new BaseException(['msg' => '商品规格不存在']);
  83. }
  84. // 商品sku信息
  85. $product['product_sku'] = self::getProductSku($product, $params['product_sku_id']);
  86. $product['bargain_sku'] = $bargain_sku;
  87. // 商品列表
  88. $productList = [$product->hidden(['category', 'content', 'image', 'sku'])];
  89. foreach ($productList as &$item) {
  90. // 商品单价
  91. $item['product_price'] = $task['actual_price'];
  92. // 商品购买数量
  93. $item['total_num'] = 1;
  94. $item['spec_sku_id'] = $item['product_sku']['spec_sku_id'];
  95. // 商品购买总金额
  96. $item['total_price'] = $task['actual_price'];
  97. $item['bargain_product_sku_id'] = $bargain_sku['bargain_product_sku_id'];
  98. $item['product_sku_id'] = $params['product_sku_id'];
  99. $item['product_source_id'] = $bargain_sku['bargain_product_id'];
  100. $item['sku_source_id'] = $bargain_sku['bargain_product_sku_id'];
  101. // 砍价活动id
  102. $item['activity_id'] = $bargain['bargain_activity_id'];
  103. // 砍价订单id
  104. $item['bill_source_id'] = $params['bargain_task_id'];
  105. }
  106. return $productList;
  107. }
  108. /**
  109. * 指定的商品规格信息
  110. */
  111. private static function getProductSku($product, $product_sku_id)
  112. {
  113. // 获取指定的sku
  114. $productSku = [];
  115. foreach ($product['sku'] as $item) {
  116. if ($item['product_sku_id'] == $product_sku_id) {
  117. $productSku = $item;
  118. break;
  119. }
  120. }
  121. if (empty($productSku)) {
  122. return false;
  123. }
  124. // 多规格文字内容
  125. $productSku['product_attr'] = '';
  126. if ($product['spec_type'] == 20) {
  127. $specRelData = helper::arrayColumn2Key($product['spec_rel'], 'spec_value_id');
  128. $attrs = explode('_', $productSku['spec_sku_id']);
  129. foreach ($attrs as $specValueId) {
  130. $productSku['product_attr'] .= $specRelData[$specValueId]['spec']['spec_name'] . ':'
  131. . $specRelData[$specValueId]['spec_value'] . '; ';
  132. }
  133. }
  134. return $productSku;
  135. }
  136. /**
  137. * 获取列表页砍价数据
  138. * 目前未分页,后续有可能会分页
  139. */
  140. public function getActivityList($bargain_activity_id)
  141. {
  142. // 获取列表数据
  143. $list = $this->with(['product.image.file', 'bargainSku'])
  144. ->where('bargain_activity_id', '=', $bargain_activity_id)
  145. ->where('is_delete', '=', 0)
  146. ->visible(['product.product_id','product.product_name','product.file_path'])
  147. ->select();
  148. foreach ($list as $product) {
  149. $bargain_arr = array_column($product['bargainSku']->toArray(), 'bargain_price');
  150. $product_arr = array_column($product['bargainSku']->toArray(), 'product_price');
  151. sort($bargain_arr);
  152. sort($product_arr);
  153. $product['bargain_price'] = current($bargain_arr);
  154. $product['product_price'] = current($product_arr);
  155. $product['product']['file_path'] = $product['product']['image'][0]['file_path'];
  156. unset($product['bargainSku']);
  157. unset($product['product']['image']);
  158. }
  159. return $list;
  160. }
  161. /**
  162. * 拼团商品详情
  163. */
  164. public function getBargainDetail($bargain_product_id)
  165. {
  166. $result = $this->with(['product.image.file', 'bargainSku.productSku.image'])
  167. ->where('bargain_product_id', '=', $bargain_product_id)->find();
  168. if (!empty($result)) {
  169. $bargain_arr = array_column($result->toArray()['bargainSku'], 'bargain_price');
  170. $product_arr = array_column($result->toArray()['bargainSku'], 'product_price');
  171. sort($bargain_arr);
  172. sort($product_arr);
  173. $result['bargain_price'] = current($bargain_arr);
  174. $result['line_price'] = current($product_arr);
  175. if (count($bargain_arr) > 1) {
  176. $result['bargain_high_price'] = end($bargain_arr);
  177. $result['line_high_price'] = end($product_arr);
  178. }
  179. }
  180. return $result;
  181. }
  182. }