StorePromotions.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\v2\activity;
  12. use app\Request;
  13. use app\services\activity\promotions\StorePromotionsServices;
  14. use app\services\activity\promotions\StorePromotionsAuxiliaryServices;
  15. use app\services\product\product\StoreProductRelationServices;
  16. use app\services\product\product\StoreProductServices;
  17. /**
  18. * 优惠活动
  19. */
  20. class StorePromotions
  21. {
  22. protected $services;
  23. public function __construct(StorePromotionsServices $services)
  24. {
  25. $this->services = $services;
  26. }
  27. /**
  28. * 某个优惠活动商品列表
  29. * @param $type
  30. * @return mixed
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function productList($type)
  36. {
  37. $type = (int)($type ?? 0);
  38. if ($type) {
  39. $where['promotions_type'] = $type;
  40. }
  41. $where['type'] = 1;
  42. $where['store_id'] = 0;
  43. $where['pid'] = 0;
  44. $where['is_del'] = 0;
  45. $where['status'] = 1;
  46. $where['promotionsTime'] = true;
  47. //存在一个全部商品折扣优惠活动 直接返回商品
  48. if ($this->services->count($where + ['product_partake_type' => 1])) {
  49. $product_where = [];
  50. } else {
  51. /** @var StoreProductRelationServices $storeProductRelationServices */
  52. $storeProductRelationServices = app()->make(StoreProductRelationServices::class);
  53. /** @var StorePromotionsAuxiliaryServices $promotionsAuxiliaryServices */
  54. $promotionsAuxiliaryServices = app()->make(StorePromotionsAuxiliaryServices::class);
  55. $promotionsIds = $this->services->getAllShowActivityIds([$type], 'id,promotions_type,product_partake_type');
  56. $product_ids = [];
  57. $product_where = ['ids' => []];
  58. foreach ($promotionsIds as $item) {
  59. $promotionsAuxiliaryData = $promotionsAuxiliaryServices->getPromotionsAuxiliaryCache($item['id']);
  60. switch ($item['product_partake_type']) {
  61. case 1://所有商品
  62. break;
  63. case 2://选中商品参与
  64. $product_ids = $promotionsAuxiliaryData;
  65. break;
  66. case 3:
  67. $item['product_count'] = 0;
  68. break;
  69. case 4://品牌
  70. $product_ids = $promotionsAuxiliaryData ? $storeProductRelationServices->getIdsByWhere(['type' => 2, 'relation_id' => $promotionsAuxiliaryData]) : [];
  71. break;
  72. case 5://商品标签
  73. $product_ids = $promotionsAuxiliaryData ? $storeProductRelationServices->getIdsByWhere(['type' => 3, 'relation_id' => $promotionsAuxiliaryData]) : [];
  74. break;
  75. }
  76. $product_where['ids'] = array_merge($product_where['ids'], $product_ids ? $product_ids : []);
  77. }
  78. }
  79. $list = [];
  80. if (!$product_where || (isset($product_where['ids']) && $product_where['ids'])) {
  81. /** @var StoreProductServices $productServices */
  82. $productServices = app()->make(StoreProductServices::class);
  83. $product_where['type'] = [0, 2];
  84. $list = $productServices->getGoodsList($product_where, 0, $type);
  85. if ($list) {
  86. foreach ($list as $key => &$item) {
  87. if (isset($item['promotions']['promotions_type']) && $item['promotions']['promotions_type'] == 1) {
  88. $item['price'] = floatval(bcmul((string)$item['price'], (string)bcdiv((string)$item['promotions']['discount'] ?? '100', '100', 2), 2));
  89. }
  90. }
  91. }
  92. }
  93. return app('json')->success(compact('list'));
  94. }
  95. /**
  96. * 获取凑单商品列表
  97. * @param Request $request
  98. * @return mixed
  99. */
  100. public function collectOrderProduct(Request $request, StoreProductServices $productServices, StorePromotionsAuxiliaryServices $auxiliaryService)
  101. {
  102. [$promotions_id] = $request->getMore([
  103. [['promotions_id', 'd'], 0]
  104. ], true);
  105. $promotions = $this->services->get($promotions_id, ['*'], ['promotions']);
  106. if (!$promotions) {
  107. return app('json')->fail('活动已失效,请刷新页面');
  108. }
  109. $promotions = $promotions->toArray();
  110. $product_where = [];
  111. /** @var StoreProductRelationServices $storeProductRelationServices */
  112. $storeProductRelationServices = app()->make(StoreProductRelationServices::class);
  113. /** @var StorePromotionsAuxiliaryServices $promotionsAuxiliaryServices */
  114. $promotionsAuxiliaryServices = app()->make(StorePromotionsAuxiliaryServices::class);
  115. $promotionsAuxiliaryData = $promotionsAuxiliaryServices->getPromotionsAuxiliaryCache((int)$promotions['id']);
  116. switch ($promotions['product_partake_type']) {
  117. case 1:
  118. break;
  119. case 2:
  120. $product_where['ids'] = is_string($promotions['product_id']) ? explode(',', $promotions['product_id']) : $promotions['product_id'];
  121. break;
  122. case 3:
  123. $ids = is_string($promotions['product_id']) ? explode(',', $promotions['product_id']) : $promotions['product_id'];
  124. if ($ids) {//商品全部规格 不参与 才不显示该商品
  125. $ids = $auxiliaryService->getColumn(['promotions_id' => $promotions['id'], 'type' => 1, 'is_all' => 1, 'product_id' => $ids], 'product_id', '', true);
  126. }
  127. $product_where['not_ids'] = $ids;
  128. break;
  129. case 4://品牌
  130. $product_where['ids'] = $product_ids = $promotionsAuxiliaryData ? $storeProductRelationServices->getIdsByWhere(['type' => 2, 'relation_id' => $promotionsAuxiliaryData]) : [];
  131. break;
  132. case 5://商品标签
  133. $product_where['ids'] = $product_ids = $promotionsAuxiliaryData ? $storeProductRelationServices->getIdsByWhere(['type' => 3, 'relation_id' => $promotionsAuxiliaryData]) : [];
  134. break;
  135. }
  136. $product_where['type'] = [0, 2];
  137. $list = $productServices->getGoodsList($product_where, (int)$request->uid());
  138. return app('json')->success(compact('promotions','list'));
  139. }
  140. /**
  141. * 获取优惠活动赠品
  142. * @param $id
  143. * @return mixed
  144. */
  145. public function getPromotionsGive($id)
  146. {
  147. $result = [];
  148. if($id) {
  149. $promotionsInfo= $this->services->getInfo((int)$id);
  150. if ($promotionsInfo && $promotionsInfo['promotions_type'] == 4) {
  151. $giveIntegral = $giveCoupon = $giveProducts = [];
  152. $promotions_cate = $promotionsInfo['promotions_cate'];
  153. foreach ($promotionsInfo['promotions'] as $p) {
  154. if ($promotions_cate == 2) {
  155. $base = '每满' . floatval($p['threshold'] ?? 0);
  156. } else {
  157. $base = '满' . floatval($p['threshold'] ?? 0);
  158. }
  159. $base .= $p['threshold_type'] == 1 ? '元可领取' : '件可领取';
  160. if (isset($p['give_integral']) && $p['give_integral']) {
  161. $giveIntegral[] = ['threshold_title' => $base, 'give_integral' => intval($p['give_integral'])];
  162. }
  163. if (isset($p['giveCoupon']) && $p['giveCoupon']) {
  164. foreach ($p['giveCoupon'] as &$coupon) {
  165. $coupon['threshold_title'] = $base;
  166. }
  167. $giveCoupon = array_merge($giveCoupon, $p['giveCoupon']);
  168. }
  169. if (isset($p['giveProducts']) && $p['giveProducts']) {
  170. foreach ($p['giveProducts'] as &$product) {
  171. $product['threshold_title'] = $base;
  172. }
  173. $giveProducts = array_merge($giveProducts, $p['giveProducts']);
  174. }
  175. }
  176. $result = compact('giveIntegral', 'giveCoupon', 'giveProducts');
  177. }
  178. }
  179. return app('json')->success($result);
  180. }
  181. }