GoodsDetailService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace app\frontend\modules\goods\services;
  3. use app\common\facades\Setting;
  4. use Yunshop\StoreCashier\common\models\StoreSetting;
  5. use Yunshop\StoreCashier\store\models\StoreGoods;
  6. class GoodsDetailService extends GoodsDetailBaseService
  7. {
  8. public $plugin_id = 0;
  9. public $goods_type = 'goods';
  10. public function getGoodsSale()
  11. {
  12. //获取商城设置: 判断 积分、余额 是否有自定义名称
  13. $shopSet = \Setting::get('shop.shop');
  14. $sale = [];
  15. $sale[] = $this->getFreightSale();
  16. $sale[] = $this->getPointAllSale($shopSet);
  17. $sale[] = $this->getFullSale();
  18. $sale[] = $this->getBalanceSale($shopSet);
  19. $sale[] = $this->getPointSale($shopSet);
  20. $sale[] = $this->getCouponSale();
  21. $sale[] = $this->getFullPieceSale();
  22. return array_filter($sale);
  23. }
  24. private function getFreightSale()
  25. {
  26. $data = [];
  27. if ($this->goods_model->hasOneSale->ed_num || $this->goods_model->hasOneSale->ed_money) {
  28. $data['name'] = '包邮';
  29. $data['key'] = 'ed_num';
  30. $data['type'] = 'array';
  31. if ($this->goods_model->hasOneSale->ed_num) {
  32. $data['value'][] = '本商品满' . $this->goods_model->hasOneSale->ed_num . '件包邮';
  33. }
  34. if ($this->goods_model->hasOneSale->ed_money) {
  35. $data['value'][] = '本商品满¥' . $this->goods_model->hasOneSale->ed_money . '包邮';
  36. }
  37. }
  38. return $data;
  39. }
  40. private function getFullPieceSale()
  41. {
  42. $data = [];
  43. $settings = Setting::get('shop.fullPieceNew');
  44. foreach ($settings['fullPiece'] as $k=>$v) {
  45. if (in_array($this->goods_model->id,$v['goods'])) {
  46. $data['name'] = '满件优惠';
  47. $data['key'] = 'full_piece';
  48. $data['type'] = 'array';
  49. $data['value'] = [];
  50. $rules = collect($v['rules']);
  51. $rules = $rules->sortBy(function ($setting) {
  52. return $setting['enough'];
  53. });
  54. $rules->map(function ($item) use (&$data,$v) {
  55. if ($v['discount_type']) {//折扣
  56. $discount = '打'.$item['reduce'].'折';
  57. } else {//立减
  58. $discount = '立减'.$item['reduce'].'元';
  59. }
  60. $data['value'][] = '满'.$item['enough'].'件,'.$discount;
  61. });
  62. if (!$data['value']) {
  63. return [];
  64. }
  65. }
  66. }
  67. return $data;
  68. }
  69. private function getPointAllSale($shopSet)
  70. {
  71. $data = [];
  72. if($this->goods_model->hasOneSale->all_point_deduct && $this->goods_model->hasOneSale->has_all_point_deduct){//商品设置
  73. $data['name'] = $shopSet['credit1'] ? $shopSet['credit1'].'全额抵扣':'积分全额抵扣';
  74. $data['key'] = 'all_point_deduct';
  75. $data['type'] = 'string';
  76. $data['value'] = '可使用' . $this->goods_model->hasOneSale->all_point_deduct .'个'.($shopSet['credit1'] ? $shopSet['credit1'] .'全额抵扣购买' : '积分全额抵扣购买');
  77. }
  78. return $data;
  79. }
  80. private function getFullSale()
  81. {
  82. $data = [];
  83. if ((bccomp($this->goods_model->hasOneSale->ed_full, 0.00, 2) == 1) && (bccomp($this->goods_model->hasOneSale->ed_reduction, 0.00, 2) == 1)) {
  84. $data['name'] = '满减';
  85. $data['key'] = 'ed_full';
  86. $data['type'] = 'string';
  87. $data['value'] = '本商品满¥' . $this->goods_model->hasOneSale->ed_full . '立减¥' . $this->goods_model->hasOneSale->ed_reduction;
  88. }
  89. return $data;
  90. }
  91. private function getBalanceSale($shopSet)
  92. {
  93. $data = [];
  94. if ($this->goods_model->hasOneSale->award_balance) {
  95. $data['name'] = $shopSet['credit'] ?: '余额';
  96. $data['key'] = 'award_balance';
  97. $data['type'] = 'string';
  98. $data['value'] = '购买赠送' . $this->goods_model->hasOneSale->award_balance . $data['name'];
  99. }
  100. return $data;
  101. }
  102. private function getPointSale($shopSet)
  103. {
  104. $set = \Setting::get('point.set');
  105. $point = app('GoodsDetail')->make('GoodsDetailInstance')->getPointSet()?:[];
  106. $data['name'] = $shopSet['credit1'] ?: '积分';
  107. $data['key'] = 'point';
  108. $data['type'] = 'array';
  109. if ((strlen($this->goods_model->hasOneSale->point) === 0) || $this->goods_model->hasOneSale->point != 0) {
  110. if ($this->goods_model->hasOneSale->point) {
  111. $points = $this->goods_model->hasOneSale->point;
  112. } elseif (!empty($point['value']['set']['give_point']) && $point['value']['set']['give_point'] != 0) {//门店抵扣设置
  113. $points = $point['value']['set']['give_point'].'%';
  114. } else {
  115. $points = $set['give_point'] ?: 0;
  116. }
  117. $tradeGoodsPointsServer = new TradeGoodsPointsServer;
  118. if (!empty($points)) {
  119. $points = $tradeGoodsPointsServer->getPoint($points, $this->goods_model->price, $this->goods_model->cost_price);
  120. $data['value'][] = '购买赠送' . $points . $data['name'];
  121. // 后台设置 积分-> 商品详情页 开启时 显示积分数据,否则显示空
  122. $data['points'] = $set['goods_point']['goods_page'] ? $points : '' ;
  123. }
  124. $tradeGoodsPointsServer->setSingletonGoodsModel( $this->goods_model);
  125. $this->goods_model->hasManyOptions->each(function ($option) {
  126. $option->append('points');
  127. });
  128. }
  129. //是否开启积分抵扣显示
  130. if ($set['goods_page_deduct_show'] != 1) {
  131. //设置不等于0,支持积分抵扣
  132. //积分抵扣优先级 商品独立设置 ---> 门店设置 ---> 积分统一设置
  133. if ($set['point_deduct'] && (strlen($this->goods_model->hasOneSale->max_point_deduct) === 0 || $this->goods_model->hasOneSale->max_point_deduct != 0)) {
  134. $goods_point_deduct_unit = $this->goods_model->hasOneSale->point_deduct_type ? '' : '元';
  135. if ($this->goods_model->hasOneSale->max_point_deduct) {
  136. $max_point_deduct = $this->goods_model->hasOneSale->max_point_deduct . $goods_point_deduct_unit;
  137. } elseif (strlen($point['value']['set']['money_max']) !== 0) {
  138. if (!($point['value']['set']['money_max'] === 0 || $point['value']['set']['money_max'] === '0')) {
  139. $max_point_deduct = $point['value']['set']['money_max'] . '%';
  140. }
  141. } else {
  142. $max_point_deduct = $set['money_max'] ? $set['money_max'] . '%': 0;
  143. }
  144. if (!empty(mb_substr($max_point_deduct, 0,-1))) {
  145. $data['value'][] = '最高抵扣' . $max_point_deduct;
  146. }
  147. }
  148. if ($set['point_deduct'] && (strlen($this->goods_model->hasOneSale->min_point_deduct) === 0 || $this->goods_model->hasOneSale->min_point_deduct != 0)) {
  149. $goods_point_deduct_unit = $this->goods_model->hasOneSale->point_deduct_type ? '' : '元';
  150. if ($this->goods_model->hasOneSale->min_point_deduct) {
  151. $min_point_deduct = $this->goods_model->hasOneSale->min_point_deduct . $goods_point_deduct_unit;
  152. } else {
  153. $min_point_deduct = $set['money_min'] ? $set['money_min'] . '%' : 0;
  154. }
  155. if (!empty(mb_substr($min_point_deduct, 0,-1))) {
  156. $data['value'][] = '最少抵扣' . $min_point_deduct;
  157. }
  158. }
  159. }
  160. if (!empty($data['value'])) {
  161. return $data;
  162. }
  163. return [];
  164. }
  165. private function getCouponSale()
  166. {
  167. $data = [];
  168. if ($this->goods_model->hasOneGoodsCoupon->is_give) {
  169. $data['name'] = '购买返券';
  170. $data['key'] = 'coupon';
  171. $data['type'] = 'string';
  172. $data['value'] = $this->goods_model->hasOneGoodsCoupon->send_type ? '商品订单完成返优惠券' : '每月一号返优惠券';
  173. }
  174. return $data;
  175. }
  176. }