Promotions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\cashier;
  12. use app\Request;
  13. use app\services\activity\seckill\StoreSeckillTimeServices;
  14. use app\services\user\UserServices;
  15. use app\services\activity\discounts\StoreDiscountsServices;
  16. use app\services\activity\promotions\StorePromotionsServices;
  17. use app\services\activity\promotions\StorePromotionsAuxiliaryServices;
  18. use app\services\activity\seckill\StoreSeckillServices;
  19. use app\services\product\product\StoreProductServices;
  20. use app\services\product\branch\StoreBranchProductServices;
  21. use think\facade\App;
  22. /**
  23. * 收银台优惠活动
  24. */
  25. class Promotions extends AuthController
  26. {
  27. protected $services;
  28. /**
  29. * StoreOrder constructor.
  30. * @param App $app
  31. * @param StorePromotionsServices $service
  32. */
  33. public function __construct(App $app, StorePromotionsServices $service)
  34. {
  35. parent::__construct($app);
  36. $this->services = $service;
  37. }
  38. /** 获取门店适用的活动
  39. * @param $type
  40. * @return mixed
  41. */
  42. public function getPromotionInfo($type)
  43. {
  44. $where['promotions_type'] = $type;
  45. $where['type'] = 1;
  46. $where['status'] = 1;
  47. $where['applicable_type'] = '1,2';
  48. $where['store_id'] = 0;
  49. $where['pid'] = 0;
  50. $where['is_del'] = 0;
  51. return $this->success($this->services->getgetPromotionListInfo($where,$this->storeId));
  52. }
  53. /**
  54. * 获取活动商品数量信息
  55. * @return mixed
  56. */
  57. public function promotionsCount(StoreSeckillTimeServices $seckillTimeServices, StoreSeckillServices $seckillServices, StoreDiscountsServices $discountsServices, $uid)
  58. {
  59. $typeArr = [1 => 'time_discount', 2 => 'n_piece_n_discount', 3 => 'full_discount', 4 => 'full_give'];
  60. $where[] = [];
  61. $where['type'] = 1;
  62. $where['store_id'] = 0;
  63. $where['pid'] = 0;
  64. $where['is_del'] = 0;
  65. $where['status'] = 1;
  66. $where['promotionsTime'] = true;
  67. /** @var StoreProductServices $productServices */
  68. $productServices = app()->make(StoreProductServices::class);
  69. /** @var StoreBranchProductServices $branchProductServices */
  70. $branchProductServices = app()->make(StoreBranchProductServices::class);
  71. $storeProducts = $branchProductServices->getSearchList(['type' => 1, 'relation_id' => $this->storeId, 'status' => 7, 'pid' => -1], 0, 0, ['pid']);
  72. $not_ids = [];
  73. if ($storeProducts) {
  74. $not_ids = array_column($storeProducts, 'pid');
  75. }
  76. $storeProducts = $branchProductServices->getSearchList(['type' => 1, 'relation_id' => $this->storeId, 'status' => 1, 'pid' => -1], 0, 0, ['pid']);
  77. $ids = [];
  78. if ($storeProducts) {
  79. $ids = array_column($storeProducts, 'pid');
  80. }
  81. $result = [];
  82. foreach ($typeArr as $type => $key) {
  83. $where['promotions_type'] = $type;
  84. $product_where = [];
  85. $product_where['type'] = 1;
  86. $product_where['relation_id'] = $this->storeId;
  87. $product_where['is_verify'] = 1;
  88. $product_where['pids'] = $ids;
  89. $product_where['not_pids'] = $not_ids;
  90. //门店不展示卡密商品
  91. $product_where['product_type'] = [0, 2, 4];
  92. $product_where['is_show'] = 1;
  93. $product_where['is_del'] = 0;
  94. //存在一个全部商品折扣优惠活动 直接返回商品
  95. if ($ids && !$this->services->count($where + ['product_partake_type' => 1])) {
  96. //正选并集
  97. $mergeIds = function ($promotions) {
  98. $data = [];
  99. foreach ($promotions as $item) {
  100. $productIds = is_string($item['product_id']) ? explode(',', $item['product_id']) : $item['product_id'];
  101. $data = array_merge($data, $productIds);
  102. }
  103. return $data;
  104. };
  105. $promotions = $this->services->getList($where + ['product_partake_type' => 2], 'id,product_id');
  106. $product_where['pids'] = $promotions ? $mergeIds($promotions) : [];
  107. $notPromotions = $this->services->getList($where + ['product_partake_type' => 3], 'id,product_id');
  108. //反选交集
  109. /** @var StorePromotionsAuxiliaryServices $auxiliaryService */
  110. $auxiliaryService = app()->make(StorePromotionsAuxiliaryServices::class);
  111. $intersectIds = function ($promotions) use ($auxiliaryService) {
  112. $data = [];
  113. foreach ($promotions as $item) {
  114. $productIds = is_string($item['product_id']) ? explode(',', $item['product_id']) : $item['product_id'];
  115. $productIds = $auxiliaryService->getColumn(['promotions_id' => $item['id'], 'type' => 1, 'is_all' => 1, 'product_id' => $productIds], 'product_id', '', true);
  116. if(!$productIds) {
  117. continue;
  118. }
  119. if ($data) {
  120. $data = array_intersect($data, $productIds);
  121. } else {
  122. $data = $productIds;
  123. }
  124. }
  125. return $data;
  126. };
  127. $product_where['not_pids'] = array_merge($product_where['not_ids'] ?? [], $notPromotions ? $intersectIds($notPromotions) : []);
  128. }
  129. $count = 0;
  130. if ($product_where['pids'] && $this->services->count($where)) {
  131. $product_where['pids'] = array_merge(array_diff($product_where['pids'], $product_where['not_pids']));
  132. unset($product_where['not_pids']);
  133. $product_where['is_vip_product'] = 0;
  134. $product_where['is_presale_product'] = 0;
  135. //门店不展示卡密商品
  136. $product_where['product_type'] = [0, 2, 4];
  137. if ($uid) {
  138. /** @var UserServices $user */
  139. $user = app()->make(UserServices::class);
  140. $userInfo = $user->getUserCacheInfo((int)$uid);
  141. $is_vip = $userInfo['is_money_level'] ?? 0;
  142. $product_where['is_vip_product'] = $is_vip ? -1 : 0;
  143. }
  144. $count = $productServices->getCount($product_where);
  145. }
  146. $result[$key] = ['type' => $type, 'count' => $count];
  147. }
  148. $result['seckill'] = ['type' => '5', 'count' => $seckillServices->getCountByTime($seckillTimeServices->getSeckillTime(), $ids, $not_ids)];
  149. $result['discount'] = ['type' => '6', 'count' => $discountsServices->getDiscountsCount()];
  150. return $this->success($result);
  151. }
  152. /**
  153. * 收银台获取活动商品列表
  154. * @param $type
  155. * @return mixed
  156. * @throws \think\db\exception\DataNotFoundException
  157. * @throws \think\db\exception\DbException
  158. * @throws \think\db\exception\ModelNotFoundException
  159. */
  160. public function activityList(Request $request, $type, $uid)
  161. {
  162. $data = [];
  163. /** @var StoreBranchProductServices $branchProductServices */
  164. $branchProductServices = app()->make(StoreBranchProductServices::class);
  165. $storeProducts = $branchProductServices->getSearchList(['type' => 1, 'relation_id' => $this->storeId, 'status' => 7, 'pid' => -1], 0, 0, ['pid']);
  166. $not_ids = [];
  167. if ($storeProducts) {
  168. $not_ids = array_column($storeProducts, 'pid');
  169. }
  170. $storeProducts = $branchProductServices->getSearchList(['type' => 1, 'relation_id' => $this->storeId, 'status' => 1, 'pid' => -1], 0, 0, ['pid']);
  171. $ids = [];
  172. if ($storeProducts) {
  173. $ids = array_column($storeProducts, 'pid');
  174. }
  175. $data['list'] = [];
  176. $data['count'] = 0;
  177. switch ($type) {
  178. case 1:
  179. case 2:
  180. case 3:
  181. case 4:
  182. [$staff_id, $promotions_id, $tourist_uid, $store_name] = $request->getMore([
  183. ['staff_id', ''],
  184. ['promotions_id', 0],
  185. ['tourist_uid', ''],//虚拟用户uid
  186. ['store_name', '']
  187. ], true);
  188. $this->services->setItem('store_id', $this->storeId)
  189. ->setItem('tourist_uid', $tourist_uid)
  190. ->setItem('staff_id', $staff_id)
  191. ->setItem('ids', $ids)
  192. ->setItem('not_ids', $not_ids)
  193. ->setItem('store_name', $store_name);
  194. $data = $this->services->getTypeList((int)$type, (int)$uid, (int)$promotions_id);
  195. $this->services->reset();
  196. break;
  197. case 5:
  198. /** @var StoreSeckillTimeServices $seckillTimeServices */
  199. $seckillTimeServices = app()->make(StoreSeckillTimeServices::class);
  200. /** @var StoreSeckillServices $seckillServices */
  201. $seckillServices = app()->make(StoreSeckillServices::class);
  202. $timeId = (int)$seckillTimeServices->getSeckillTime();
  203. $data['list'] = $seckillServices->getListByTime($timeId, $ids, true);
  204. $data['count'] = $seckillServices->getCountByTime($timeId, $ids);
  205. break;
  206. case 6:
  207. $where['is_del'] = 0;
  208. $where['status'] = 1;
  209. $where['is_time'] = 1;
  210. /** @var StoreDiscountsServices $discountsServices */
  211. $discountsServices = app()->make(StoreDiscountsServices::class);
  212. $data = $discountsServices->getList($where);
  213. break;
  214. }
  215. return $this->success($data);
  216. }
  217. }