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