GoodsDeductionShowService.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/12/20
  8. * Time: 11:39
  9. */
  10. namespace app\common\services\plugin\store;
  11. use Yunshop\Integral\Common\Models\IntegralGoodsModel;
  12. use Yunshop\Love\Common\Models\GoodsLove;
  13. use Yunshop\Love\Common\Services\SetService;
  14. use Yunshop\StoreIntegralFees\models\StoreIntegralFeesGoods;
  15. class GoodsDeductionShowService
  16. {
  17. private $goods_ids;
  18. protected $integral_sign;
  19. protected $integral_set;
  20. protected $integral_deduction_set;
  21. protected $love_sign;
  22. protected $love_activation_set;
  23. protected $love_set;
  24. protected $store_integral_fees_sign;
  25. protected $store_integral_fees_goods_set;
  26. protected $store_integral_fees_set;
  27. public function __construct($goodsIds = array())
  28. {
  29. $this->goods_ids = $goodsIds;
  30. //消费积分
  31. $this->integral_sign = false;
  32. if (app('plugins')->isEnabled('integral')) {
  33. //获取消费积分抵扣统一设置
  34. $integralGoods = IntegralGoodsModel::select(['id', 'goods_id', 'deduction_set', 'deduction_set_min', 'deduction_set_max'])->whereIn('goods_id', $goodsIds)->get()->toArray();
  35. $this->integral_sign = true;
  36. $this->integral_deduction_set = array_column($integralGoods, null, 'goods_id');
  37. $this->integral_set = [
  38. 'deduction_set' => \Setting::get('integral.deduction_set'),
  39. 'deduction_set_min' => \Setting::get('integral.deduction_set_min'),
  40. 'deduction_set_max' => \Setting::get('integral.deduction_set_max')
  41. ];
  42. }
  43. //爱心值
  44. $this->love_sign = false;
  45. if (app('plugins')->isEnabled('love')) {
  46. $loveGoods = GoodsLove::select(['id', 'goods_id', 'activation_state', 'love_accelerate'])->whereIn('goods_id', $goodsIds)->get()->toArray();
  47. $this->love_sign = true;
  48. $this->love_activation_set = array_column($loveGoods, null, 'goods_id');
  49. $this->love_set = [
  50. 'activation_state' => SetService::getAcceleratedActivationStatus(),//开关
  51. 'love_accelerate' => SetService::getAcceleratedActivationOfLoveRatio(),//激活比例
  52. ];
  53. }
  54. //手续费
  55. $this->store_integral_fees_sign = false;
  56. if (app('plugins')->isEnabled('store-integral-fees')) {
  57. $store_integral_fees_set = \Setting::get('plugin.store_integral_fees');
  58. $storeIntegralFees = StoreIntegralFeesGoods::select(['id', 'goods_id', 'is_open', 'fees_rate'])->whereIn('goods_id', $goodsIds)->get()->toArray();
  59. $this->store_integral_fees_sign = true;
  60. $this->store_integral_fees_goods_set = array_column($storeIntegralFees, null, 'goods_id');
  61. $this->store_integral_fees_set = [
  62. 'is_open' => $store_integral_fees_set['is_plugin_open'],//开关
  63. 'store_integral_fees' => $store_integral_fees_set['store_integral_fees'],
  64. ];
  65. }
  66. }
  67. /**
  68. * 商品内容显示:消费积分 爱心值 手续费等前端显示
  69. * @param int $showType 1-首页-装修(不受基础设置控制),2-门店商品内容显示(受门店基础设置控制)
  70. * @param int $returnType 返回类型,1-key,name,value形式(前端需要的格式),2-商品详情活动需要格式
  71. * @return array
  72. */
  73. public function getDeductionShow($showType = 1, $returnType = 1): array
  74. {
  75. $res = $frontendData = [];
  76. $show_type = [
  77. 'integral' => true,
  78. 'love' => true,
  79. 'store_integral_fees' => true
  80. ];
  81. //门店商品内容显示
  82. if ($showType != 1) {
  83. $store_set = \Setting::get('plugin.store');
  84. $show_plugins = [
  85. 'integral' => $store_set['goods_show_integral'],
  86. 'love' => $store_set['goods_show_love'],
  87. 'store_integral_fees' => $store_set['goods_show_store_integral_fees']
  88. ];
  89. foreach ($show_plugins as $key => $value) {
  90. //未开启
  91. if ($value != 1) {
  92. $show_type[$key] = false;//不显示
  93. }
  94. }
  95. }
  96. foreach ($this->goods_ids as $key => $item) {
  97. //消费积分抵扣显示
  98. if ($this->integral_sign && $show_type['integral']) {
  99. $integral_name = \Yunshop\Integral\Common\CommonService::getPluginName();
  100. $res[$item]['integral'] = $this->getIntegralData($item);
  101. if ($res[$item]['integral']['max_deduction'] > 0) {
  102. $frontendData[$item][] = $this->getFrontendData('integral', $integral_name.'抵扣', $res[$item]['integral']['max_deduction']);
  103. }
  104. }
  105. //爱心值激活比例显示
  106. if ($this->love_sign && $show_type['love']) {
  107. $love_name = \Setting::get('love.name') ?: '爱心值';
  108. $res[$item]['love'] = $this->getLoveData($item);
  109. if ($res[$item]['love']['love_accelerate'] > 0) {
  110. $frontendData[$item][] = $this->getFrontendData('love', $love_name.'激活', $res[$item]['love']['love_accelerate']);
  111. }
  112. }
  113. //消费积分手续费显示
  114. if ($this->store_integral_fees_sign && $show_type['store_integral_fees']) {
  115. $res[$item]['store_integral_fees'] = $this->getStoreIntegralFeesData($item);
  116. if ($res[$item]['store_integral_fees']['fees_rate'] > 0) {
  117. $frontendData[$item][] = $this->getFrontendData('store-integral-fees', '手续费', $res[$item]['store_integral_fees']['fees_rate']);
  118. }
  119. }
  120. }
  121. if ($returnType == 1) {
  122. return $frontendData;
  123. } elseif ($returnType == 2) {
  124. return $res;
  125. } else {
  126. return [];
  127. }
  128. }
  129. //消费积分
  130. protected function getIntegralData($goodsId): array
  131. {
  132. $deduction_set_min = $deduction_set_max = 0;
  133. //独立商品 -- 开启
  134. if ($this->integral_set['deduction_set'] && $this->integral_deduction_set[$goodsId]['deduction_set']) {
  135. $deduction_set_min = $this->integral_deduction_set[$goodsId]['deduction_set_min'] ?: 0;//商品-最低
  136. $deduction_set_max = $this->integral_deduction_set[$goodsId]['deduction_set_max'] ?: 0;//商品-最高
  137. if (empty($deduction_set_min)) {
  138. $deduction_set_min = $this->integral_set['deduction_set_min'] ?: 0;//统一最低
  139. }
  140. if (empty($deduction_set_max)) {
  141. $deduction_set_max = $this->integral_set['deduction_set_max'] ?: 0;//统一最高
  142. }
  143. }
  144. $max_deduction = $deduction_set_max;
  145. if ($deduction_set_min > $deduction_set_max) {
  146. $max_deduction = $deduction_set_min;
  147. }
  148. return [
  149. 'max_deduction' => (float)$max_deduction,//最高的抵扣
  150. 'deduction_set_min' => (float)$deduction_set_min,
  151. 'deduction_set_max' => (float)$deduction_set_max
  152. ];
  153. }
  154. //爱心值
  155. protected function getLoveData($goodsId): array
  156. {
  157. $love_accelerate = 0;
  158. //独立商品 -- 开启
  159. if ($this->love_set['activation_state'] && $this->love_activation_set[$goodsId]['activation_state']) {
  160. $love_accelerate = $this->love_activation_set[$goodsId]['love_accelerate'] ?: 0;//商品-激活爱心值比例
  161. if (empty($love_accelerate)) {
  162. $love_accelerate = $this->love_set['love_accelerate'] ?: 0;//统一激活爱心值比例
  163. }
  164. }
  165. return [
  166. 'love_accelerate' => (float)$love_accelerate
  167. ];
  168. }
  169. //消费积分手续费
  170. protected function getStoreIntegralFeesData($goodsId): array
  171. {
  172. $fees_rate = 0;
  173. //独立商品 -- 开启
  174. if ($this->store_integral_fees_set['is_plugin_open'] && $this->store_integral_fees_goods_set[$goodsId]['is_open']) {
  175. $fees_rate = $this->store_integral_fees_goods_set[$goodsId]['fees_rate'] ?: 0;//商品-抵扣收取手续费比例
  176. if (empty($fees_rate)) {
  177. $fees_rate = $this->store_integral_fees_set['fees_rate'] ?: 0;//统一抵扣手续费比例
  178. }
  179. }
  180. return [
  181. 'fees_rate' => (float)$fees_rate
  182. ];
  183. }
  184. private function getFrontendData($key, $name, $value)
  185. {
  186. return [
  187. 'key' => $key,
  188. 'name' => $name,
  189. 'value' => $value
  190. ];
  191. }
  192. }