StoreCouponUserServices.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. declare (strict_types=1);
  12. namespace app\services\activity\coupon;
  13. use app\services\activity\promotions\StorePromotionsServices;
  14. use app\services\BaseServices;
  15. use app\services\product\brand\StoreBrandServices;
  16. use app\services\product\product\StoreProductRelationServices;
  17. use app\services\user\UserServices;
  18. use app\dao\activity\coupon\StoreCouponUserDao;
  19. use app\services\product\category\StoreProductCategoryServices;
  20. use crmeb\utils\Arr;
  21. /**
  22. * Class StoreCouponUserServices
  23. * @package app\services\activity\coupon
  24. * @mixin StoreCouponUserDao
  25. */
  26. class StoreCouponUserServices extends BaseServices
  27. {
  28. /**
  29. * StoreCouponUserServices constructor.
  30. * @param StoreCouponUserDao $dao
  31. */
  32. public function __construct(StoreCouponUserDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 获取列表
  38. * @param array $where
  39. * @return array
  40. */
  41. public function issueLog(array $where)
  42. {
  43. [$page, $limit] = $this->getPageValue();
  44. $list = $this->dao->getList($where, 'uid,add_time', ['userInfo'], $page, $limit);
  45. foreach ($list as &$item) {
  46. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  47. }
  48. $count = $this->dao->count($where);
  49. return compact('list', 'count');
  50. }
  51. /**
  52. * 获取列表
  53. * @param array $where
  54. * @return array
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public function systemPage(array $where)
  60. {
  61. [$page, $limit] = $this->getPageValue();
  62. $list = $this->dao->getList($where, '*', ['issue'], $page, $limit);
  63. $count = 0;
  64. if ($list) {
  65. /** @var UserServices $userServices */
  66. $userServices = app()->make(UserServices::class);
  67. $userAll = $userServices->getColumn([['uid', 'IN', array_column($list, 'uid')]], 'uid,nickname', 'uid');
  68. foreach ($list as &$item) {
  69. $item['nickname'] = $userAll[$item['uid']]['nickname'] ?? '';
  70. }
  71. $count = $this->dao->count($where);
  72. }
  73. return compact('list', 'count');
  74. }
  75. /**
  76. * 获取用户优惠券
  77. * @param int $id
  78. * @param int $status
  79. * @return array
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function getUserCouponList(int $id, int $status = -1)
  85. {
  86. [$page, $limit] = $this->getPageValue();
  87. $where = ['uid' => $id];
  88. if ($status != -1) $where['status'] = $status;
  89. $list = $this->dao->getList($where, '*', ['issue'], $page, $limit);
  90. foreach ($list as &$item) {
  91. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  92. $item['_end_time'] = date('Y-m-d H:i:s', $item['end_time']);
  93. if (!$item['coupon_time']) {
  94. $item['coupon_time'] = ceil(($item['end_use_time'] - $item['start_use_time']) / '86400');
  95. }
  96. }
  97. $count = $this->dao->count($where);
  98. return compact('list', 'count');
  99. }
  100. /**
  101. * 恢复优惠券
  102. * @param int $id
  103. * @return bool|mixed
  104. */
  105. public function recoverCoupon(int $id)
  106. {
  107. $status = $this->dao->value(['id' => $id], 'status');
  108. if ($status) return $this->dao->update($id, ['status' => 0, 'use_time' => 0]);
  109. else return true;
  110. }
  111. /**
  112. * 过期优惠卷失效
  113. */
  114. public function checkInvalidCoupon()
  115. {
  116. $this->dao->update([['end_time', '<', time()], ['status', '=', '0']], ['status' => 2]);
  117. }
  118. /**
  119. * 获取用户有效优惠劵数量
  120. * @param int $uid
  121. * @return int
  122. */
  123. public function getUserValidCouponCount(int $uid)
  124. {
  125. $this->checkInvalidCoupon();
  126. return $this->dao->getCount(['uid' => $uid, 'status' => 0]);
  127. }
  128. /**
  129. * 下单页面显示可用优惠券
  130. * @param int $uid
  131. * @param array $cartGroup
  132. * @param int $store_id
  133. * @return array
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function getUsableCouponList(int $uid, array $cartGroup, int $store_id = 0)
  139. {
  140. $userCoupons = $this->dao->getUserAllCoupon($uid);
  141. $result = [];
  142. if ($userCoupons) {
  143. $cartInfo = $cartGroup['valid'];
  144. $promotions = $cartGroup['promotions'] ?? [];
  145. $promotionsList = [];
  146. if($promotions){
  147. $promotionsList = array_combine(array_column($promotions, 'id'), $promotions);
  148. }
  149. //验证是否适用门店
  150. $isApplicableStore = function ($couponInfo) use ($store_id) {
  151. if ($store_id && isset($couponInfo['applicable_type']) && isset($couponInfo['applicable_store_id'])) {
  152. $applicable_store_id = is_array($couponInfo['applicable_store_id']) ? $couponInfo['applicable_store_id'] : explode(',', $couponInfo['applicable_store_id']);
  153. //活动不适用该门店
  154. if ($couponInfo['applicable_type'] == 0 || ($couponInfo['applicable_type'] == 2 && !in_array($store_id, $applicable_store_id))) {
  155. return false;
  156. }
  157. }
  158. return true;
  159. };
  160. $isOverlay = function ($cart) use ($promotionsList) {
  161. $productInfo = $cart['productInfo'] ?? [];
  162. if (!$productInfo) {
  163. return false;
  164. }
  165. //门店独立商品 不使用优惠券
  166. $isBranchProduct = isset($productInfo['type']) && isset($productInfo['pid']) && $productInfo['type'] == 1 && !$productInfo['pid'];
  167. if ($isBranchProduct) {
  168. return false;
  169. }
  170. if (isset($cart['promotions_id']) && $cart['promotions_id']) {
  171. foreach ($cart['promotions_id'] as $key => $promotions_id) {
  172. $promotions = $promotionsList[$promotions_id] ?? [];
  173. if ($promotions && $promotions['promotions_type'] != 4){
  174. $overlay = is_string($promotions['overlay']) ? explode(',', $promotions['overlay']) : $promotions['overlay'];
  175. if (!in_array(5, $overlay)) {
  176. return false;
  177. }
  178. }
  179. }
  180. }
  181. return true;
  182. };
  183. /** @var StoreProductCategoryServices $storeCategoryServices */
  184. $storeCategoryServices = app()->make(StoreProductCategoryServices::class);
  185. /** @var StoreBrandServices $storeBrandServices */
  186. $storeBrandServices = app()->make(StoreBrandServices::class);
  187. foreach ($userCoupons as $coupon) {
  188. if (!$isApplicableStore($coupon)) {//不适用门店跳过
  189. continue;
  190. }
  191. $price = 0;
  192. $count = 0;
  193. switch ($coupon['coupon_applicable_type']) {
  194. case 0:
  195. foreach ($cartInfo as $cart) {
  196. if (!$isOverlay($cart)) continue;
  197. $price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
  198. $count++;
  199. }
  200. break;
  201. case 1://品类券
  202. $cateGorys = $storeCategoryServices->getAllById((int)$coupon['category_id']);
  203. if ($cateGorys) {
  204. $cateIds = array_column($cateGorys, 'id');
  205. foreach ($cartInfo as $cart) {
  206. if (!$isOverlay($cart)) continue;
  207. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  208. $price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
  209. $count++;
  210. }
  211. }
  212. }
  213. break;
  214. case 2://商品
  215. foreach ($cartInfo as $cart) {
  216. if (!$isOverlay($cart)) continue;
  217. $product_id = isset($cart['productInfo']['pid']) && $cart['productInfo']['pid'] ? $cart['productInfo']['pid'] : ($cart['product_id'] ?? 0);
  218. if ($product_id && in_array($product_id, explode(',', $coupon['product_id']))) {
  219. $price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
  220. $count++;
  221. }
  222. }
  223. break;
  224. case 3://品牌
  225. $brands = $storeBrandServices->getAllById((int)$coupon['brand_id']);
  226. if ($brands) {
  227. $brandIds = array_column($brands, 'id');
  228. foreach ($cartInfo as $cart) {
  229. if (!$isOverlay($cart)) continue;
  230. if (isset($cart['productInfo']['brand_id']) && in_array($cart['productInfo']['brand_id'], $brandIds)) {
  231. $price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
  232. $count++;
  233. }
  234. }
  235. }
  236. break;
  237. }
  238. if ($count && $coupon['use_min_price'] <= $price) {
  239. $coupon['start_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
  240. $coupon['add_time'] = date('Y/m/d', $coupon['add_time']);
  241. $coupon['end_time'] = date('Y/m/d', $coupon['end_time']);
  242. $coupon['title'] = $coupon['coupon_title'];
  243. $coupon['type'] = $coupon['coupon_applicable_type'];
  244. $coupon['use_min_price'] = floatval($coupon['use_min_price']);
  245. $coupon['coupon_price'] = floatval($coupon['coupon_price']);
  246. $result[] = $coupon;
  247. }
  248. }
  249. }
  250. return $result;
  251. }
  252. /**
  253. * 下单页面显示可用优惠券
  254. * @param $uid
  255. * @param $cartGroup
  256. * @param $price
  257. * @return array
  258. */
  259. public function getOldUsableCouponList(int $uid, array $cartGroup)
  260. {
  261. $cartPrice = $cateIds = [];
  262. $productId = Arr::getUniqueKey($cartGroup['valid'], 'product_id');
  263. foreach ($cartGroup['valid'] as $value) {
  264. $cartPrice[] = bcmul((string)$value['truePrice'], (string)$value['cart_num'], 2);
  265. }
  266. $maxPrice = count($cartPrice) ? max($cartPrice) : 0;
  267. if ($productId) {
  268. /** @var StoreProductRelationServices $storeProductRelationServices */
  269. $storeProductRelationServices = app()->make(StoreProductRelationServices::class);
  270. $cateId = $storeProductRelationServices->productIdByCateId($productId);
  271. if ($cateId) {
  272. /** @var StoreProductCategoryServices $cateServices */
  273. $cateServices = app()->make(StoreProductCategoryServices::class);
  274. $catePids = $cateServices->cateIdByPid($cateId);
  275. $cateIds = array_merge($cateId, $catePids);
  276. } else {
  277. $cateIds = $cateId;
  278. }
  279. }
  280. $productCouponList = $this->dao->productIdsByCoupon($productId, $uid, (string)$maxPrice);
  281. $cateCouponList = $this->dao->cateIdsByCoupon($cateIds, $uid, (string)$maxPrice);
  282. $list = array_merge($productCouponList, $cateCouponList);
  283. $couponIds = Arr::getUniqueKey($list, 'id');
  284. $sumCartPrice = array_sum($cartPrice);
  285. $list1 = $this->dao->getUserCoupon($couponIds, $uid, (string)$sumCartPrice);
  286. $list = array_merge($list, $list1);
  287. foreach ($list as &$item) {
  288. $item['add_time'] = date('Y/m/d', $item['add_time']);
  289. $item['end_time'] = date('Y/m/d', $item['end_time']);
  290. $item['title'] = $item['coupon_title'];
  291. $item['type'] = $item['coupon_applicable_type'] ?? 0;
  292. }
  293. return $list;
  294. }
  295. /**
  296. * 用户领取优惠券
  297. * @param $uid
  298. * @param $issueCouponInfo
  299. * @param string $type
  300. * @return mixed
  301. */
  302. public function addUserCoupon($uid, $issueCouponInfo, string $type = 'get')
  303. {
  304. $data = [];
  305. $data['cid'] = $issueCouponInfo['id'];
  306. $data['uid'] = $uid;
  307. $data['coupon_title'] = $issueCouponInfo['title'];
  308. $data['coupon_price'] = $issueCouponInfo['coupon_price'];
  309. $data['use_min_price'] = $issueCouponInfo['use_min_price'];
  310. $data['add_time'] = time();
  311. if ($issueCouponInfo['coupon_time']) {
  312. $data['start_time'] = $data['add_time'];
  313. $data['end_time'] = $data['add_time'] + $issueCouponInfo['coupon_time'] * 86400;
  314. } else {
  315. $data['start_time'] = $issueCouponInfo['start_use_time'];
  316. $data['end_time'] = $issueCouponInfo['end_use_time'];
  317. }
  318. $data['type'] = $type;
  319. return $this->dao->save($data);
  320. }
  321. /**会员领取优惠券
  322. * @param $uid
  323. * @param $issueCouponInfo
  324. * @param string $type
  325. * @return mixed
  326. */
  327. public function addMemberUserCoupon($uid, $issueCouponInfo, $type = 'get')
  328. {
  329. $data = [];
  330. $data['cid'] = $issueCouponInfo['id'];
  331. $data['uid'] = $uid;
  332. $data['coupon_title'] = $issueCouponInfo['title'];
  333. $data['coupon_price'] = $issueCouponInfo['coupon_price'];
  334. $data['use_min_price'] = $issueCouponInfo['use_min_price'];
  335. $data['add_time'] = time();
  336. /* if ($issueCouponInfo['coupon_time']) {
  337. $data['start_time'] = $data['add_time'];
  338. $data['end_time'] = $data['add_time'] + $issueCouponInfo['coupon_time'] * 86400;
  339. } else {
  340. $data['start_time'] = $issueCouponInfo['start_use_time'];
  341. $data['end_time'] = $issueCouponInfo['end_use_time'];
  342. }*/
  343. $data['start_time'] = strtotime(date('Y-m-d 00:00:00', time()));
  344. $data['end_time'] = strtotime(date('Y-m-d 23:59:59', strtotime('+30 day')));
  345. $data['type'] = $type;
  346. return $this->dao->save($data);
  347. }
  348. /**
  349. * 获取用户已领取的优惠卷
  350. * @param int $uid
  351. * @param $type
  352. * @return array
  353. * @throws \think\db\exception\DataNotFoundException
  354. * @throws \think\db\exception\DbException
  355. * @throws \think\db\exception\ModelNotFoundException
  356. */
  357. public function getUserCounpon(int $uid, $type)
  358. {
  359. $where = [];
  360. $where['uid'] = $uid;
  361. switch ($type) {
  362. case 0:
  363. case '':
  364. break;
  365. case 1:
  366. $where['status'] = 0;
  367. break;
  368. case 2:
  369. $where['status'] = 1;
  370. break;
  371. default:
  372. $where['status'] = 1;
  373. break;
  374. }
  375. [$page, $limit] = $this->getPageValue();
  376. $list = $this->dao->getCouponListByOrder($where, 'status ASC,add_time DESC', $page, $limit);
  377. /** @var StoreProductCategoryServices $categoryServices */
  378. $categoryServices = app()->make(StoreProductCategoryServices::class);
  379. $category = $categoryServices->getColumn([], 'pid,cate_name', 'id');
  380. /** @var StoreBrandServices $storeBrandServices */
  381. $storeBrandServices = app()->make(StoreBrandServices::class);
  382. $brand = $storeBrandServices->getColumn([], 'id,pid,brand_name', 'id');
  383. foreach ($list as &$item) {
  384. $item['applicable_type'] = $item['coupon_applicable_type'];
  385. if ($item['category_id'] && isset($category[$item['category_id']])) {
  386. $item['category_type'] = $category[$item['category_id']]['pid'] == 0 ? 1 : 2;
  387. $item['category_name'] = $category[$item['category_id']]['cate_name'];
  388. } else {
  389. $item['category_type'] = '';
  390. $item['category_name'] = '';
  391. }
  392. if ($item['brand_id'] && isset($brand[$item['brand_id']])) {
  393. $item['category_name'] = $brand[$item['brand_id']]['brand_name'];
  394. } else {
  395. $item['brand_name'] = '';
  396. }
  397. }
  398. return $list ? $this->tidyCouponList($list) : [];
  399. }
  400. /**
  401. * 格式化优惠券
  402. * @param $couponList
  403. * @return mixed
  404. */
  405. public function tidyCouponList($couponList)
  406. {
  407. $time = time();
  408. foreach ($couponList as &$coupon) {
  409. if ($coupon['status'] == '已使用') {
  410. $coupon['_type'] = 0;
  411. $coupon['_msg'] = '已使用';
  412. $coupon['pc_type'] = 0;
  413. $coupon['pc_msg'] = '已使用';
  414. } else if ($coupon['status'] == '已过期') {
  415. $coupon['is_fail'] = 1;
  416. $coupon['_type'] = 0;
  417. $coupon['_msg'] = '已过期';
  418. $coupon['pc_type'] = 0;
  419. $coupon['pc_msg'] = '已过期';
  420. } else if ($coupon['end_time'] < $time) {
  421. $coupon['is_fail'] = 1;
  422. $coupon['_type'] = 0;
  423. $coupon['_msg'] = '已过期';
  424. $coupon['pc_type'] = 0;
  425. $coupon['pc_msg'] = '已过期';
  426. } else if ($coupon['start_time'] > $time) {
  427. $coupon['_type'] = 0;
  428. $coupon['_msg'] = '未开始';
  429. $coupon['pc_type'] = 1;
  430. $coupon['pc_msg'] = '未开始';
  431. } else {
  432. if ($coupon['start_time'] + 3600 * 24 > $time) {
  433. $coupon['_type'] = 2;
  434. $coupon['_msg'] = '立即使用';
  435. $coupon['pc_type'] = 1;
  436. $coupon['pc_msg'] = '可使用';
  437. } else {
  438. $coupon['_type'] = 1;
  439. $coupon['_msg'] = '立即使用';
  440. $coupon['pc_type'] = 1;
  441. $coupon['pc_msg'] = '可使用';
  442. }
  443. }
  444. $coupon['add_time'] = $coupon['_add_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
  445. $coupon['end_time'] = $coupon['_end_time'] = date('Y/m/d', $coupon['end_time']);
  446. $coupon['use_min_price'] = floatval($coupon['use_min_price']);
  447. $coupon['coupon_price'] = floatval($coupon['coupon_price']);
  448. }
  449. return $couponList;
  450. }
  451. /**
  452. * 获取会员优惠券列表
  453. * @param $uid
  454. * @return array|mixed
  455. * @throws \think\db\exception\DataNotFoundException
  456. * @throws \think\db\exception\DbException
  457. * @throws \think\db\exception\ModelNotFoundException
  458. */
  459. public function getMemberCoupon($uid)
  460. {
  461. if (!$uid) return [];
  462. /** @var StoreCouponIssueServices $couponIssueService */
  463. $couponIssueService = app()->make(StoreCouponIssueServices::class);
  464. //$couponWhere['status'] = 1;
  465. $couponWhere['receive_type'] = 4;
  466. //$couponWhere['is_del'] = 0;
  467. $couponInfo = $couponIssueService->getMemberCouponIssueList($couponWhere);
  468. $couponList = [];
  469. if ($couponInfo) {
  470. $couponIds = array_column($couponInfo, 'id');
  471. $couponType = array_column($couponInfo, 'type', 'id');
  472. $couponList = $this->dao->getCouponListByOrder(['uid' => $uid, 'coupon_ids' => $couponIds], 'add_time desc');
  473. if ($couponList) {
  474. foreach ($couponList as $k => $v) {
  475. $couponList[$k]['type_name'] = $couponIssueService->_couponType[$couponType[$v['cid']]];
  476. }
  477. }
  478. }
  479. return $couponList ? $this->tidyCouponList($couponList) : [];
  480. }
  481. /**
  482. * 根据月分组看会员发放优惠券情况
  483. * @param array $where
  484. * @return array
  485. * @throws \think\db\exception\DataNotFoundException
  486. * @throws \think\db\exception\DbException
  487. * @throws \think\db\exception\ModelNotFoundException
  488. */
  489. public function memberCouponUserGroupBymonth(array $where)
  490. {
  491. return $this->dao->memberCouponUserGroupBymonth($where);
  492. }
  493. /**
  494. * 会员券失效
  495. * @param $coupon_user
  496. * @return false|mixed
  497. */
  498. public function memberCouponIsFail($coupon_user)
  499. {
  500. if (!$coupon_user) return false;
  501. if ($coupon_user['use_time'] == 0) {
  502. return $this->dao->update($coupon_user['id'], ['is_fail' => 1, 'status' => 2]);
  503. }
  504. }
  505. /**
  506. * 根据id查询会员优惠劵
  507. * @param int $id
  508. * @param string $filed
  509. * @param array $with
  510. * @return array|false|\think\Model|null
  511. * @throws \think\db\exception\DataNotFoundException
  512. * @throws \think\db\exception\DbException
  513. * @throws \think\db\exception\ModelNotFoundException
  514. */
  515. public function getCouponUserOne(int $id, string $filed = '', array $with = [])
  516. {
  517. if (!$id) return false;
  518. return $this->dao->getOne(['id' => $id], $filed, $with);
  519. }
  520. /**根据时间查询用户优惠券
  521. * @param array $where
  522. * @return array|bool|\think\Model|null
  523. * @throws \think\db\exception\DataNotFoundException
  524. * @throws \think\db\exception\DbException
  525. * @throws \think\db\exception\ModelNotFoundException
  526. */
  527. public function getUserCounponByMonth(array $where)
  528. {
  529. if (!$where) return false;
  530. return $this->dao->getUserCounponByMonth($where);
  531. }
  532. /**
  533. * 检查付费会员是否领取了会员券
  534. * @param $uid
  535. * @param $vipCouponIds
  536. * @return array
  537. * @throws \think\db\exception\DataNotFoundException
  538. * @throws \think\db\exception\DbException
  539. * @throws \think\db\exception\ModelNotFoundException
  540. */
  541. public function checkHave($uid, $vipCouponIds)
  542. {
  543. $list = $this->dao->getVipCouponList($uid);
  544. $have = [];
  545. foreach ($list as $item) {
  546. if ($vipCouponIds && in_array($item['cid'], $vipCouponIds)) {
  547. $have[$item['cid']] = true;
  548. } else {
  549. $have[$item['cid']] = false;
  550. }
  551. }
  552. return $have;
  553. }
  554. /**
  555. * 使用优惠券验证
  556. * @param int $couponId
  557. * @param int $uid
  558. * @param array $cartInfo
  559. * @param array $promotions
  560. * @param int $store_id
  561. * @return bool
  562. */
  563. public function useCoupon(int $couponId, int $uid, array $cartInfo, array $promotions = [], int $store_id = 0)
  564. {
  565. if (!$couponId || !$uid || !$cartInfo) {
  566. return true;
  567. }
  568. /** @var StorePromotionsServices $promotionsServices */
  569. $promotionsServices = app()->make(StorePromotionsServices::class);
  570. try {
  571. [$couponInfo, $couponPrice] = $promotionsServices->useCoupon($couponId, $uid, $cartInfo, $promotions, $store_id);
  572. } catch (\Throwable $e) {
  573. $couponInfo = [];
  574. $couponPrice = 0;
  575. }
  576. if ($couponInfo) {
  577. $this->dao->useCoupon($couponId);
  578. }
  579. return true;
  580. }
  581. }