StoreCouponUserServices.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\BaseServices;
  14. use app\dao\activity\coupon\StoreCouponUserDao;
  15. use app\services\product\product\StoreCategoryServices;
  16. use app\services\product\product\StoreProductCateServices;
  17. use app\services\user\UserServices;
  18. use crmeb\utils\Arr;
  19. /**
  20. *
  21. * Class StoreCouponUserServices
  22. * @package app\services\coupon
  23. * @method useCoupon(int $id) 使用优惠券修改优惠券状态
  24. * @method delUserCoupon(array $where)
  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. /** @var StoreCouponUserUserServices $storeCouponUserUserService */
  62. $storeCouponUserUserService = app()->make(StoreCouponUserUserServices::class);
  63. return $storeCouponUserUserService->getList($where);
  64. }
  65. /**
  66. * 获取用户优惠券
  67. * @param int $id
  68. */
  69. public function getUserCouponList(int $id)
  70. {
  71. [$page, $limit] = $this->getPageValue();
  72. $list = $this->dao->getList(['uid' => $id], '*', ['issue'], $page, $limit);
  73. foreach ($list as &$item) {
  74. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  75. $item['_end_time'] = date('Y-m-d H:i:s', $item['end_time']);
  76. if (!$item['coupon_time']) {
  77. $item['coupon_time'] = bcdiv((string)($item['end_use_time'] - $item['start_use_time']), '86400', 0);
  78. }
  79. }
  80. $count = $this->dao->count(['uid' => $id]);
  81. return compact('list', 'count');
  82. }
  83. /**
  84. * 恢复优惠券
  85. * @param int $id
  86. * @return bool|mixed
  87. */
  88. public function recoverCoupon(int $id)
  89. {
  90. $status = $this->dao->value(['id' => $id], 'status');
  91. if ($status) return $this->dao->update($id, ['status' => 0, 'use_time' => '']);
  92. else return true;
  93. }
  94. /**
  95. * 过期优惠卷失效
  96. */
  97. public function checkInvalidCoupon()
  98. {
  99. $this->dao->update([['end_time', '<', time()], ['status', '=', '0']], ['status' => 2]);
  100. }
  101. /**
  102. * 获取用户有效优惠劵数量
  103. * @param int $uid
  104. * @return int
  105. */
  106. public function getUserValidCouponCount(int $uid)
  107. {
  108. $this->checkInvalidCoupon();
  109. return $this->dao->getCount(['uid' => $uid, 'status' => 0]);
  110. }
  111. /**
  112. * 下单页面显示可用优惠券
  113. * @param $uid
  114. * @param $cartGroup
  115. * @param $price
  116. * @return array
  117. */
  118. public function getUsableCouponList(int $uid, array $cartGroup)
  119. {
  120. $userCoupons = $this->dao->getUserAllCoupon($uid);
  121. $result = [];
  122. if ($userCoupons) {
  123. $cartInfo = $cartGroup['valid'];
  124. foreach ($userCoupons as $coupon) {
  125. $price = 0;
  126. $count = 0;
  127. switch ($coupon['applicable_type']) {
  128. case 0:
  129. case 3:
  130. foreach ($cartInfo as $cart) {
  131. $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  132. $count++;
  133. }
  134. break;
  135. case 1://品类券
  136. /** @var StoreCategoryServices $storeCategoryServices */
  137. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  138. $coupon_category = explode(',', (string)$coupon['category_id']);
  139. $category_ids = $storeCategoryServices->getAllById($coupon_category);
  140. if ($category_ids) {
  141. $cateIds = array_column($category_ids, 'id');
  142. foreach ($cartInfo as $cart) {
  143. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  144. $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  145. $count++;
  146. }
  147. }
  148. }
  149. break;
  150. case 2:
  151. foreach ($cartInfo as $cart) {
  152. if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $coupon['product_id']))) {
  153. $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  154. $count++;
  155. }
  156. }
  157. break;
  158. }
  159. if ($count && $coupon['use_min_price'] <= $price) {
  160. $coupon['start_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
  161. $coupon['add_time'] = date('Y/m/d', $coupon['add_time']);
  162. $coupon['end_time'] = date('Y/m/d', $coupon['end_time']);
  163. $coupon['start_use_time'] = $coupon['start_use_time'] > 0 ? date('Y/m/d', $coupon['start_use_time']) : 0;
  164. $coupon['end_use_time'] = $coupon['start_use_time'] > 0 ? date('Y/m/d', $coupon['end_use_time']) : 0;
  165. $coupon['title'] = $coupon['coupon_title'];
  166. $coupon['type'] = $coupon['applicable_type'];
  167. $coupon['use_min_price'] = floatval($coupon['use_min_price']);
  168. $coupon['coupon_price'] = floatval($coupon['coupon_price']);
  169. $result[] = $coupon;
  170. }
  171. }
  172. }
  173. return $result;
  174. }
  175. /**
  176. * 下单页面显示可用优惠券
  177. * @param $uid
  178. * @param $cartGroup
  179. * @param $price
  180. * @return array
  181. */
  182. public function getOldUsableCouponList(int $uid, array $cartGroup)
  183. {
  184. $cartPrice = $cateIds = [];
  185. $productId = Arr::getUniqueKey($cartGroup['valid'], 'product_id');
  186. foreach ($cartGroup['valid'] as $value) {
  187. $cartPrice[] = bcmul((string)$value['truePrice'], (string)$value['cart_num'], 2);
  188. }
  189. $maxPrice = count($cartPrice) ? max($cartPrice) : 0;
  190. if ($productId) {
  191. /** @var StoreProductCateServices $productCateServices */
  192. $productCateServices = app()->make(StoreProductCateServices::class);
  193. $cateId = $productCateServices->productIdByCateId($productId);
  194. if ($cateId) {
  195. /** @var StoreCategoryServices $cateServices */
  196. $cateServices = app()->make(StoreCategoryServices::class);
  197. $catePids = $cateServices->cateIdByPid($cateId);
  198. $cateIds = array_merge($cateId, $catePids);
  199. } else {
  200. $cateIds = $cateId;
  201. }
  202. }
  203. $productCouponList = $this->dao->productIdsByCoupon($productId, $uid, (string)$maxPrice);
  204. $cateCouponList = $this->dao->cateIdsByCoupon($cateIds, $uid, (string)$maxPrice);
  205. $list = array_merge($productCouponList, $cateCouponList);
  206. $couponIds = Arr::getUniqueKey($list, 'id');
  207. $sumCartPrice = array_sum($cartPrice);
  208. $list1 = $this->dao->getUserCoupon($couponIds, $uid, (string)$sumCartPrice);
  209. $list = array_merge($list, $list1);
  210. foreach ($list as &$item) {
  211. $item['add_time'] = date('Y/m/d', $item['add_time']);
  212. $item['end_time'] = date('Y/m/d', $item['end_time']);
  213. $item['title'] = $item['coupon_title'];
  214. $item['type'] = $item['applicable_type'] ?? 0;
  215. }
  216. return $list;
  217. }
  218. /**
  219. * 用户领取优惠券
  220. * @param $uid
  221. * @param $issueCouponInfo
  222. * @param string $type
  223. * @return mixed
  224. */
  225. public function addUserCoupon($uid, $issueCouponInfo, $type = 'get')
  226. {
  227. $data = [];
  228. $data['cid'] = $issueCouponInfo['id'];
  229. $data['uid'] = $uid;
  230. $data['coupon_title'] = $issueCouponInfo['title'];
  231. $data['coupon_price'] = $issueCouponInfo['coupon_price'];
  232. $data['use_min_price'] = $issueCouponInfo['use_min_price'];
  233. $data['add_time'] = time();
  234. if ($issueCouponInfo['coupon_time']) {
  235. $data['start_time'] = $data['add_time'];
  236. $data['end_time'] = $data['add_time'] + $issueCouponInfo['coupon_time'] * 86400;
  237. } else {
  238. $data['start_time'] = $issueCouponInfo['start_use_time'];
  239. $data['end_time'] = $issueCouponInfo['end_use_time'];
  240. }
  241. $data['type'] = $type;
  242. return $this->dao->save($data);
  243. }
  244. /**会员领取优惠券
  245. * @param $uid
  246. * @param $issueCouponInfo
  247. * @param string $type
  248. * @return mixed
  249. */
  250. public function addMemberUserCoupon($uid, $issueCouponInfo, $type = 'get')
  251. {
  252. $data = [];
  253. $data['cid'] = $issueCouponInfo['id'];
  254. $data['uid'] = $uid;
  255. $data['coupon_title'] = $issueCouponInfo['title'];
  256. $data['coupon_price'] = $issueCouponInfo['coupon_price'];
  257. $data['use_min_price'] = $issueCouponInfo['use_min_price'];
  258. $data['add_time'] = time();
  259. $data['start_time'] = strtotime(date('Y-m-d 00:00:00', time()));
  260. $data['end_time'] = strtotime(date('Y-m-d 23:59:59', strtotime('+30 day')));
  261. $data['type'] = $type;
  262. return $this->dao->save($data);
  263. }
  264. /**
  265. * 获取用户已领取的优惠卷
  266. * @param int $uid
  267. * @param $type
  268. * @return array
  269. * @throws \think\db\exception\DataNotFoundException
  270. * @throws \think\db\exception\DbException
  271. * @throws \think\db\exception\ModelNotFoundException
  272. */
  273. public function getUserCounpon(int $uid, $type)
  274. {
  275. $where = [];
  276. $where['uid'] = $uid;
  277. switch ($type) {
  278. case 0:
  279. case '':
  280. break;
  281. case 1:
  282. $where['status'] = 0;
  283. break;
  284. case 2:
  285. $where['status'] = 1;
  286. break;
  287. default:
  288. $where['status'] = 1;
  289. break;
  290. }
  291. [$page, $limit] = $this->getPageValue();
  292. $list = $this->dao->getCouponListByOrder($where, 'status ASC,add_time DESC', $page, $limit);
  293. return $list ? $this->tidyCouponList($list) : [];
  294. }
  295. /**
  296. * 格式化优惠券
  297. * @param $couponList
  298. * @return mixed
  299. */
  300. public function tidyCouponList($couponList)
  301. {
  302. $time = time();
  303. foreach ($couponList as &$coupon) {
  304. if ($coupon['status'] == '已使用') {
  305. $coupon['_type'] = 0;
  306. $coupon['_msg'] = '已使用';
  307. $coupon['pc_type'] = 0;
  308. $coupon['pc_msg'] = '已使用';
  309. } else if ($coupon['status'] == '已过期') {
  310. $coupon['is_fail'] = 1;
  311. $coupon['_type'] = 0;
  312. $coupon['_msg'] = '已过期';
  313. $coupon['pc_type'] = 0;
  314. $coupon['pc_msg'] = '已过期';
  315. } else if ($coupon['end_time'] < $time) {
  316. $coupon['is_fail'] = 1;
  317. $coupon['_type'] = 0;
  318. $coupon['_msg'] = '已过期';
  319. $coupon['pc_type'] = 0;
  320. $coupon['pc_msg'] = '已过期';
  321. } else if ($coupon['start_time'] > $time) {
  322. $coupon['_type'] = 0;
  323. $coupon['_msg'] = '未开始';
  324. $coupon['pc_type'] = 1;
  325. $coupon['pc_msg'] = '未开始';
  326. } else {
  327. if ($coupon['start_time'] + 3600 * 24 > $time) {
  328. $coupon['_type'] = 2;
  329. $coupon['_msg'] = '立即使用';
  330. $coupon['pc_type'] = 1;
  331. $coupon['pc_msg'] = '可使用';
  332. } else {
  333. $coupon['_type'] = 1;
  334. $coupon['_msg'] = '立即使用';
  335. $coupon['pc_type'] = 1;
  336. $coupon['pc_msg'] = '可使用';
  337. }
  338. }
  339. $coupon['add_time'] = $coupon['_add_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
  340. $coupon['end_time'] = $coupon['_end_time'] = date('Y/m/d', $coupon['end_time']);
  341. $coupon['use_min_price'] = floatval($coupon['use_min_price']);
  342. $coupon['coupon_price'] = floatval($coupon['coupon_price']);
  343. }
  344. return $couponList;
  345. }
  346. /** 获取会员优惠券列表
  347. * @param array $where
  348. * @return array
  349. * @throws \think\db\exception\DataNotFoundException
  350. * @throws \think\db\exception\DbException
  351. * @throws \think\db\exception\ModelNotFoundException
  352. */
  353. public function getMemberCoupon($uid)
  354. {
  355. if (!$uid) return [];
  356. /** @var StoreCouponIssueServices $couponIssueService */
  357. $couponIssueService = app()->make(StoreCouponIssueServices::class);
  358. $couponWhere['receive_type'] = 4;
  359. $couponInfo = $couponIssueService->getMemberCouponIssueList($couponWhere);
  360. $couponList = [];
  361. if ($couponInfo) {
  362. $couponIds = array_column($couponInfo, 'id');
  363. $couponType = array_column($couponInfo, 'type', 'id');
  364. $couponList = $this->dao->getCouponListByOrder(['uid' => $uid, 'coupon_ids' => $couponIds], 'add_time desc');
  365. if ($couponList) {
  366. foreach ($couponList as $k => $v) {
  367. $couponList[$k]['coupon_type'] = $couponIssueService->_couponType[$couponType[$v['cid']]];
  368. }
  369. }
  370. }
  371. return $couponList ? $this->tidyCouponList($couponList) : [];
  372. }
  373. /**根据月分组看会员发放优惠券情况
  374. * @param array $where
  375. * @return array
  376. * @throws \think\db\exception\DataNotFoundException
  377. * @throws \think\db\exception\DbException
  378. * @throws \think\db\exception\ModelNotFoundException
  379. */
  380. public function memberCouponUserGroupBymonth(array $where)
  381. {
  382. return $this->dao->memberCouponUserGroupBymonth($where);
  383. }
  384. /**会员券失效
  385. * @param $coupon_user_id
  386. * @return bool|mixed
  387. */
  388. public function memberCouponIsFail($coupon_user)
  389. {
  390. if (!$coupon_user) return false;
  391. if ($coupon_user['use_time'] == 0) {
  392. return $this->dao->update($coupon_user['id'], ['is_fail' => 1, 'status' => 2]);
  393. }
  394. }
  395. /**根据id查询会员优惠劵
  396. * @param $id
  397. * @return array|bool|\think\Model|null
  398. * @throws \think\db\exception\DataNotFoundException
  399. * @throws \think\db\exception\DbException
  400. * @throws \think\db\exception\ModelNotFoundException
  401. */
  402. public function getCouponUserOne($id)
  403. {
  404. if (!$id) return false;
  405. return $this->dao->getOne(['id' => $id]);
  406. }
  407. /**
  408. * 根据时间查询用户优惠券
  409. * @param array $where
  410. * @return array|bool|\think\Model|null
  411. * @throws \think\db\exception\DataNotFoundException
  412. * @throws \think\db\exception\DbException
  413. * @throws \think\db\exception\ModelNotFoundException
  414. */
  415. public function getUserCounponByMonth(array $where, string $field = '*')
  416. {
  417. if (!$where) return [];
  418. return $this->dao->getUserCounponByMonth($where, $field);
  419. }
  420. /**
  421. * 检查付费会员是否领取了会员券
  422. * @param $uid
  423. * @param $vipCouponIds
  424. * @return array
  425. * @throws \think\db\exception\DataNotFoundException
  426. * @throws \think\db\exception\DbException
  427. * @throws \think\db\exception\ModelNotFoundException
  428. */
  429. public function checkHave($uid, $vipCouponIds)
  430. {
  431. $list = $this->dao->getVipCouponList($uid);
  432. $have = [];
  433. foreach ($list as $item) {
  434. if ($vipCouponIds && in_array($item['cid'], $vipCouponIds)) {
  435. $have[$item['cid']] = true;
  436. } else {
  437. $have[$item['cid']] = false;
  438. }
  439. }
  440. return $have;
  441. }
  442. public function autoReceiveCoupon($uid, $cartGroup)
  443. {
  444. $isMember = boolval(app()->make(UserServices::class)->value(['uid' => $uid], 'is_money_level'));
  445. $canReceiveCoupons = app()->make(StoreCouponIssueServices::class)->canReceiveCoupons($uid, $isMember);
  446. $cartInfo = $cartGroup['valid'];
  447. $canReceiveCoupon = [];
  448. if ($canReceiveCoupons) {
  449. foreach ($canReceiveCoupons as $canReceive) {
  450. $price = 0;
  451. $count = 0;
  452. switch ($canReceive['type']) {
  453. case 0:
  454. case 3:
  455. foreach ($cartInfo as $cart) {
  456. $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  457. $count++;
  458. }
  459. break;
  460. case 1://品类券
  461. /** @var StoreCategoryServices $storeCategoryServices */
  462. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  463. $coupon_category = explode(',', (string)$canReceive['category_id']);
  464. $category_ids = $storeCategoryServices->getAllById($coupon_category);
  465. if ($category_ids) {
  466. $cateIds = array_column($category_ids, 'id');
  467. foreach ($cartInfo as $cart) {
  468. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  469. $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  470. $count++;
  471. }
  472. }
  473. }
  474. break;
  475. case 2:
  476. foreach ($cartInfo as $cart) {
  477. if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $canReceive['product_id']))) {
  478. $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  479. $count++;
  480. }
  481. }
  482. break;
  483. }
  484. if ($count && $canReceive['use_min_price'] <= $price && !count($canReceive['used'])) {
  485. $canReceiveCoupon = $canReceive;
  486. break;
  487. }
  488. }
  489. }
  490. if ($canReceiveCoupon) {
  491. $data = [];
  492. $issueData = [];
  493. /** @var StoreCouponIssueUserServices $storeCouponIssueUser */
  494. $storeCouponIssueUser = app()->make(StoreCouponIssueUserServices::class);
  495. $data['cid'] = $canReceiveCoupon['id'];
  496. $data['uid'] = $uid;
  497. $data['coupon_title'] = $canReceiveCoupon['title'];
  498. $data['coupon_price'] = $canReceiveCoupon['coupon_price'];
  499. $data['use_min_price'] = $canReceiveCoupon['use_min_price'];
  500. $data['add_time'] = time();
  501. if ($canReceiveCoupon['coupon_time']) {
  502. $data['start_time'] = $data['add_time'];
  503. $data['end_time'] = $data['add_time'] + $canReceiveCoupon['coupon_time'] * 86400;
  504. } else {
  505. $data['start_time'] = $canReceiveCoupon['start_use_time'];
  506. $data['end_time'] = $canReceiveCoupon['end_use_time'];
  507. }
  508. $data['type'] = 'get';
  509. $issueData['uid'] = $uid;
  510. $issueData['issue_coupon_id'] = $canReceiveCoupon['id'];
  511. $issueData['add_time'] = time();
  512. $this->dao->save($data);
  513. $storeCouponIssueUser->save($issueData);
  514. }
  515. return true;
  516. }
  517. }