StoreCouponUserServices.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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\product\product\StoreProductServices;
  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. * @return array
  116. */
  117. public function getUsableCouponList(int $uid, array $cartGroup)
  118. {
  119. $userCoupons = $this->dao->getUserAllCoupon($uid);
  120. $result = [];
  121. if ($userCoupons) {
  122. $cartInfo = $cartGroup['valid'];
  123. // 获取购物车中所有商品的 is_repeat、is_free 字段
  124. $productIds = array_unique(array_column($cartInfo, 'product_id'));
  125. $hasRepeat = false;
  126. $hasFree = false;
  127. if ($productIds) {
  128. /** @var StoreProductServices $storeProductServices */
  129. $storeProductServices = app()->make(StoreProductServices::class);
  130. $products = $storeProductServices->getColumn([['id', 'in', $productIds]], 'is_repeat,is_free', 'id');
  131. foreach ($products as $product) {
  132. if (!empty($product['is_repeat']) && $product['is_repeat'] == 1) {
  133. $hasRepeat = true;
  134. }
  135. if (!empty($product['is_free']) && $product['is_free'] == 1) {
  136. $hasFree = true;
  137. }
  138. }
  139. }
  140. // 从系统配置获取特殊优惠券 id
  141. $repeatVoucherId = (int)sys_config('repeat_voucher');
  142. $freeVoucherId = (int)sys_config('free_voucher');
  143. foreach ($userCoupons as $coupon) {
  144. $couponId = (int)$coupon['cid'];
  145. // is_repeat 为 1,则只显示 repeat_voucher 对应的优惠券,其他都跳过
  146. if ($hasRepeat && $repeatVoucherId && $couponId !== $repeatVoucherId) {
  147. continue;
  148. }
  149. // is_free 为 1,则只显示 free_voucher 对应的优惠券,其他都跳过
  150. if ($hasFree && $freeVoucherId && $couponId !== $freeVoucherId) {
  151. continue;
  152. }
  153. // is_repeat 和 is_free 都不为 1,则隐藏 repeat_voucher 和 free_voucher 优惠券
  154. if (!$hasRepeat && $repeatVoucherId && $couponId === $repeatVoucherId) {
  155. continue;
  156. }
  157. if (!$hasFree && $freeVoucherId && $couponId === $freeVoucherId) {
  158. continue;
  159. }
  160. $price = 0;
  161. $count = 0;
  162. switch ($coupon['applicable_type']) {
  163. case 0:
  164. case 3:
  165. foreach ($cartInfo as $cart) {
  166. $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  167. $count++;
  168. }
  169. break;
  170. case 1://品类券
  171. /** @var StoreCategoryServices $storeCategoryServices */
  172. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  173. $coupon_category = explode(',', (string)$coupon['category_id']);
  174. $category_ids = $storeCategoryServices->getAllById($coupon_category);
  175. if ($category_ids) {
  176. $cateIds = array_column($category_ids, 'id');
  177. foreach ($cartInfo as $cart) {
  178. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  179. $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  180. $count++;
  181. }
  182. }
  183. }
  184. break;
  185. case 2:
  186. foreach ($cartInfo as $cart) {
  187. if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $coupon['product_id']))) {
  188. $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  189. $count++;
  190. }
  191. }
  192. break;
  193. }
  194. if ($count && $coupon['use_min_price'] <= $price) {
  195. $coupon['start_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
  196. $coupon['add_time'] = date('Y/m/d', $coupon['add_time']);
  197. $coupon['end_time'] = date('Y/m/d', $coupon['end_time']);
  198. $coupon['start_use_time'] = $coupon['start_use_time'] > 0 ? date('Y/m/d', $coupon['start_use_time']) : 0;
  199. $coupon['end_use_time'] = $coupon['start_use_time'] > 0 ? date('Y/m/d', $coupon['end_use_time']) : 0;
  200. $coupon['title'] = $coupon['coupon_title'];
  201. $coupon['type'] = $coupon['applicable_type'];
  202. $coupon['use_min_price'] = floatval($coupon['use_min_price']);
  203. $coupon['coupon_price'] = floatval($coupon['coupon_price']);
  204. $result[] = $coupon;
  205. }
  206. }
  207. }
  208. return $result;
  209. }
  210. /**
  211. * 下单页面显示可用优惠券
  212. * @param $uid
  213. * @param $cartGroup
  214. * @param $price
  215. * @return array
  216. */
  217. public function getOldUsableCouponList(int $uid, array $cartGroup)
  218. {
  219. $cartPrice = $cateIds = [];
  220. $productId = Arr::getUniqueKey($cartGroup['valid'], 'product_id');
  221. foreach ($cartGroup['valid'] as $value) {
  222. $cartPrice[] = bcmul((string)$value['truePrice'], (string)$value['cart_num'], 2);
  223. }
  224. $maxPrice = count($cartPrice) ? max($cartPrice) : 0;
  225. if ($productId) {
  226. /** @var StoreProductCateServices $productCateServices */
  227. $productCateServices = app()->make(StoreProductCateServices::class);
  228. $cateId = $productCateServices->productIdByCateId($productId);
  229. if ($cateId) {
  230. /** @var StoreCategoryServices $cateServices */
  231. $cateServices = app()->make(StoreCategoryServices::class);
  232. $catePids = $cateServices->cateIdByPid($cateId);
  233. $cateIds = array_merge($cateId, $catePids);
  234. } else {
  235. $cateIds = $cateId;
  236. }
  237. }
  238. $productCouponList = $this->dao->productIdsByCoupon($productId, $uid, (string)$maxPrice);
  239. $cateCouponList = $this->dao->cateIdsByCoupon($cateIds, $uid, (string)$maxPrice);
  240. $list = array_merge($productCouponList, $cateCouponList);
  241. $couponIds = Arr::getUniqueKey($list, 'id');
  242. $sumCartPrice = array_sum($cartPrice);
  243. $list1 = $this->dao->getUserCoupon($couponIds, $uid, (string)$sumCartPrice);
  244. $list = array_merge($list, $list1);
  245. foreach ($list as &$item) {
  246. $item['add_time'] = date('Y/m/d', $item['add_time']);
  247. $item['end_time'] = date('Y/m/d', $item['end_time']);
  248. $item['title'] = $item['coupon_title'];
  249. $item['type'] = $item['applicable_type'] ?? 0;
  250. }
  251. return $list;
  252. }
  253. /**
  254. * 用户领取优惠券
  255. * @param $uid
  256. * @param $issueCouponInfo
  257. * @param string $type
  258. * @return mixed
  259. */
  260. public function addUserCoupon($uid, $issueCouponInfo, $type = 'get')
  261. {
  262. $data = [];
  263. $data['cid'] = $issueCouponInfo['id'];
  264. $data['uid'] = $uid;
  265. $data['coupon_title'] = $issueCouponInfo['title'];
  266. $data['coupon_price'] = $issueCouponInfo['coupon_price'];
  267. $data['use_min_price'] = $issueCouponInfo['use_min_price'];
  268. $data['add_time'] = time();
  269. if ($issueCouponInfo['coupon_time']) {
  270. $data['start_time'] = $data['add_time'];
  271. $data['end_time'] = $data['add_time'] + $issueCouponInfo['coupon_time'] * 86400;
  272. } else {
  273. $data['start_time'] = $issueCouponInfo['start_use_time'];
  274. $data['end_time'] = $issueCouponInfo['end_use_time'];
  275. }
  276. $data['type'] = $type;
  277. return $this->dao->save($data);
  278. }
  279. /**会员领取优惠券
  280. * @param $uid
  281. * @param $issueCouponInfo
  282. * @param string $type
  283. * @return mixed
  284. */
  285. public function addMemberUserCoupon($uid, $issueCouponInfo, $type = 'get')
  286. {
  287. $data = [];
  288. $data['cid'] = $issueCouponInfo['id'];
  289. $data['uid'] = $uid;
  290. $data['coupon_title'] = $issueCouponInfo['title'];
  291. $data['coupon_price'] = $issueCouponInfo['coupon_price'];
  292. $data['use_min_price'] = $issueCouponInfo['use_min_price'];
  293. $data['add_time'] = time();
  294. $data['start_time'] = strtotime(date('Y-m-d 00:00:00', time()));
  295. $data['end_time'] = strtotime(date('Y-m-d 23:59:59', strtotime('+30 day')));
  296. $data['type'] = $type;
  297. return $this->dao->save($data);
  298. }
  299. /**
  300. * 获取用户已领取的优惠卷
  301. * @param int $uid
  302. * @param $type
  303. * @return array
  304. * @throws \think\db\exception\DataNotFoundException
  305. * @throws \think\db\exception\DbException
  306. * @throws \think\db\exception\ModelNotFoundException
  307. */
  308. public function getUserCounpon(int $uid, $type)
  309. {
  310. $where = [];
  311. $where['uid'] = $uid;
  312. switch ($type) {
  313. case 0:
  314. case '':
  315. break;
  316. case 1:
  317. $where['status'] = 0;
  318. break;
  319. case 2:
  320. $where['status'] = 1;
  321. break;
  322. default:
  323. $where['status'] = 1;
  324. break;
  325. }
  326. [$page, $limit] = $this->getPageValue();
  327. $list = $this->dao->getCouponListByOrder($where, 'status ASC,add_time DESC', $page, $limit);
  328. return $list ? $this->tidyCouponList($list) : [];
  329. }
  330. /**
  331. * 格式化优惠券
  332. * @param $couponList
  333. * @return mixed
  334. */
  335. public function tidyCouponList($couponList)
  336. {
  337. $time = time();
  338. foreach ($couponList as &$coupon) {
  339. if ($coupon['status'] == '已使用') {
  340. $coupon['_type'] = 0;
  341. $coupon['_msg'] = '已使用';
  342. $coupon['pc_type'] = 0;
  343. $coupon['pc_msg'] = '已使用';
  344. } else if ($coupon['status'] == '已过期') {
  345. $coupon['is_fail'] = 1;
  346. $coupon['_type'] = 0;
  347. $coupon['_msg'] = '已过期';
  348. $coupon['pc_type'] = 0;
  349. $coupon['pc_msg'] = '已过期';
  350. } else if ($coupon['end_time'] < $time) {
  351. $coupon['is_fail'] = 1;
  352. $coupon['_type'] = 0;
  353. $coupon['_msg'] = '已过期';
  354. $coupon['pc_type'] = 0;
  355. $coupon['pc_msg'] = '已过期';
  356. } else if ($coupon['start_time'] > $time) {
  357. $coupon['_type'] = 0;
  358. $coupon['_msg'] = '未开始';
  359. $coupon['pc_type'] = 1;
  360. $coupon['pc_msg'] = '未开始';
  361. } else {
  362. if ($coupon['start_time'] + 3600 * 24 > $time) {
  363. $coupon['_type'] = 2;
  364. $coupon['_msg'] = '立即使用';
  365. $coupon['pc_type'] = 1;
  366. $coupon['pc_msg'] = '可使用';
  367. } else {
  368. $coupon['_type'] = 1;
  369. $coupon['_msg'] = '立即使用';
  370. $coupon['pc_type'] = 1;
  371. $coupon['pc_msg'] = '可使用';
  372. }
  373. }
  374. $coupon['add_time'] = $coupon['_add_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
  375. $coupon['end_time'] = $coupon['_end_time'] = date('Y/m/d', $coupon['end_time']);
  376. $coupon['use_min_price'] = floatval($coupon['use_min_price']);
  377. $coupon['coupon_price'] = floatval($coupon['coupon_price']);
  378. }
  379. return $couponList;
  380. }
  381. /** 获取会员优惠券列表
  382. * @param array $where
  383. * @return array
  384. * @throws \think\db\exception\DataNotFoundException
  385. * @throws \think\db\exception\DbException
  386. * @throws \think\db\exception\ModelNotFoundException
  387. */
  388. public function getMemberCoupon($uid)
  389. {
  390. if (!$uid) return [];
  391. /** @var StoreCouponIssueServices $couponIssueService */
  392. $couponIssueService = app()->make(StoreCouponIssueServices::class);
  393. $couponWhere['receive_type'] = 4;
  394. $couponInfo = $couponIssueService->getMemberCouponIssueList($couponWhere);
  395. $couponList = [];
  396. if ($couponInfo) {
  397. $couponIds = array_column($couponInfo, 'id');
  398. $couponType = array_column($couponInfo, 'type', 'id');
  399. $couponList = $this->dao->getCouponListByOrder(['uid' => $uid, 'coupon_ids' => $couponIds], 'add_time desc');
  400. if ($couponList) {
  401. foreach ($couponList as $k => $v) {
  402. $couponList[$k]['coupon_type'] = $couponIssueService->_couponType[$couponType[$v['cid']]];
  403. }
  404. }
  405. }
  406. return $couponList ? $this->tidyCouponList($couponList) : [];
  407. }
  408. /**根据月分组看会员发放优惠券情况
  409. * @param array $where
  410. * @return array
  411. * @throws \think\db\exception\DataNotFoundException
  412. * @throws \think\db\exception\DbException
  413. * @throws \think\db\exception\ModelNotFoundException
  414. */
  415. public function memberCouponUserGroupBymonth(array $where)
  416. {
  417. return $this->dao->memberCouponUserGroupBymonth($where);
  418. }
  419. /**会员券失效
  420. * @param $coupon_user_id
  421. * @return bool|mixed
  422. */
  423. public function memberCouponIsFail($coupon_user)
  424. {
  425. if (!$coupon_user) return false;
  426. if ($coupon_user['use_time'] == 0) {
  427. return $this->dao->update($coupon_user['id'], ['is_fail' => 1, 'status' => 2]);
  428. }
  429. }
  430. /**根据id查询会员优惠劵
  431. * @param $id
  432. * @return array|bool|\think\Model|null
  433. * @throws \think\db\exception\DataNotFoundException
  434. * @throws \think\db\exception\DbException
  435. * @throws \think\db\exception\ModelNotFoundException
  436. */
  437. public function getCouponUserOne($id)
  438. {
  439. if (!$id) return false;
  440. return $this->dao->getOne(['id' => $id]);
  441. }
  442. /**
  443. * 根据时间查询用户优惠券
  444. * @param array $where
  445. * @return array|bool|\think\Model|null
  446. * @throws \think\db\exception\DataNotFoundException
  447. * @throws \think\db\exception\DbException
  448. * @throws \think\db\exception\ModelNotFoundException
  449. */
  450. public function getUserCounponByMonth(array $where, string $field = '*')
  451. {
  452. if (!$where) return [];
  453. return $this->dao->getUserCounponByMonth($where, $field);
  454. }
  455. /**
  456. * 检查付费会员是否领取了会员券
  457. * @param $uid
  458. * @param $vipCouponIds
  459. * @return array
  460. * @throws \think\db\exception\DataNotFoundException
  461. * @throws \think\db\exception\DbException
  462. * @throws \think\db\exception\ModelNotFoundException
  463. */
  464. public function checkHave($uid, $vipCouponIds)
  465. {
  466. $list = $this->dao->getVipCouponList($uid);
  467. $have = [];
  468. foreach ($list as $item) {
  469. if ($vipCouponIds && in_array($item['cid'], $vipCouponIds)) {
  470. $have[$item['cid']] = true;
  471. } else {
  472. $have[$item['cid']] = false;
  473. }
  474. }
  475. return $have;
  476. }
  477. }